OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
80 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui.light;
15
 
16
import java.io.Externalizable;
17
import java.io.IOException;
18
import java.io.ObjectInput;
19
import java.io.ObjectOutput;
94 ilm 20
import java.util.ArrayList;
21
import java.util.List;
80 ilm 22
 
142 ilm 23
import org.openconcerto.utils.CollectionUtils;
94 ilm 24
import org.openconcerto.utils.io.JSONAble;
25
import org.openconcerto.utils.io.JSONConverter;
26
import net.minidev.json.JSONArray;
27
import net.minidev.json.JSONObject;
28
 
29
public class RowSelectionSpec implements Externalizable, JSONAble {
80 ilm 30
    private String tableId;
31
 
142 ilm 32
    private List<Number> ids;
94 ilm 33
 
80 ilm 34
    /**
35
     * Define selected ids of a table. ids are ids from selected lines
94 ilm 36
     */
80 ilm 37
    public RowSelectionSpec() {
38
        // Serialization
39
    }
40
 
94 ilm 41
    public RowSelectionSpec(final JSONObject json) {
42
        this.fromJSON(json);
43
    }
156 ilm 44
 
94 ilm 45
    public RowSelectionSpec(String tableId) {
156 ilm 46
        this(tableId, new ArrayList<Number>(0));
94 ilm 47
    }
132 ilm 48
 
142 ilm 49
    public RowSelectionSpec(final String tableId, final List<Number> selectedIds) {
80 ilm 50
        this.tableId = tableId;
156 ilm 51
        this.ids = new ArrayList<>(selectedIds);
52
        this.ids.addAll(selectedIds);
80 ilm 53
    }
54
 
142 ilm 55
    public final List<Number> getIds() {
94 ilm 56
        return this.ids;
80 ilm 57
    }
156 ilm 58
 
142 ilm 59
    public final void setIds(List<Number> ids) {
156 ilm 60
        this.ids.clear();
61
        this.ids.addAll(ids);
142 ilm 62
    }
80 ilm 63
 
156 ilm 64
    public void clear() {
65
        this.ids.clear();
66
    }
67
 
80 ilm 68
    public String getTableId() {
94 ilm 69
        return this.tableId;
80 ilm 70
    }
156 ilm 71
 
72
    public boolean hasSelectedId() {
73
        return !this.getIds().isEmpty();
142 ilm 74
    }
80 ilm 75
 
76
    @Override
77
    public String toString() {
94 ilm 78
        final StringBuilder r = new StringBuilder("RowSelectionSpec: ").append(this.tableId).append(" : ");
79
        final int idsSize = this.ids.size();
80
        for (int i = 0; i < idsSize; i++) {
81
            if (i < idsSize - 1) {
82
                r.append(this.ids.get(i)).append(", ");
80 ilm 83
            } else {
94 ilm 84
                r.append(this.ids.get(i));
80 ilm 85
            }
86
        }
94 ilm 87
        return r.toString();
80 ilm 88
    }
89
 
90
    @Override
91
    public void writeExternal(ObjectOutput out) throws IOException {
92
        try {
94 ilm 93
            out.writeUTF(this.tableId);
94
            out.writeObject(this.ids);
80 ilm 95
 
96
        } catch (Exception e) {
97
            e.printStackTrace();
98
            throw new IOException(e);
99
        }
100
    }
101
 
102
    @Override
103
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
94 ilm 104
        this.tableId = in.readUTF();
142 ilm 105
        this.ids = CollectionUtils.castList((List<?>) in.readObject(), Number.class);
80 ilm 106
 
107
    }
108
 
94 ilm 109
    @Override
110
    public JSONObject toJSON() {
111
        final JSONObject json = new JSONObject();
112
        json.put("class", "RowSelectionSpec");
113
        json.put("table-id", this.tableId);
114
        json.put("ids", this.ids);
115
        return json;
116
    }
117
 
118
    @Override
119
    public void fromJSON(JSONObject json) {
142 ilm 120
        this.tableId = JSONConverter.getParameterFromJSON(json, "table-id", String.class);
121
        final JSONArray jsonIds = JSONConverter.getParameterFromJSON(json, "ids", JSONArray.class);
156 ilm 122
        this.ids = new ArrayList<>(jsonIds.size());
94 ilm 123
        for (final Object jsonId : jsonIds) {
142 ilm 124
            this.ids.add(JSONConverter.getObjectFromJSON(jsonId, Number.class));
94 ilm 125
        }
126
    }
127
 
132 ilm 128
    public void setTableId(String id) {
129
        this.tableId = id;
130
    }
131
 
80 ilm 132
}