OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Go to most recent revision | 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
    }
142 ilm 44
 
94 ilm 45
    public RowSelectionSpec(String tableId) {
142 ilm 46
        this(tableId, new ArrayList<Number>());
94 ilm 47
    }
132 ilm 48
 
142 ilm 49
    public RowSelectionSpec(final String tableId, final List<Number> selectedIds) {
80 ilm 50
        this.tableId = tableId;
142 ilm 51
        this.ids = selectedIds;
80 ilm 52
    }
53
 
142 ilm 54
    public final List<Number> getIds() {
94 ilm 55
        return this.ids;
80 ilm 56
    }
142 ilm 57
 
58
    public final void setIds(List<Number> ids) {
59
        this.ids = ids;
60
    }
80 ilm 61
 
62
    public String getTableId() {
94 ilm 63
        return this.tableId;
80 ilm 64
    }
142 ilm 65
 
66
    public boolean hasSelectedId(){
67
        return this.getIds() != null && !this.getIds().isEmpty();
68
    }
80 ilm 69
 
70
    @Override
71
    public String toString() {
94 ilm 72
        final StringBuilder r = new StringBuilder("RowSelectionSpec: ").append(this.tableId).append(" : ");
73
        final int idsSize = this.ids.size();
74
        for (int i = 0; i < idsSize; i++) {
75
            if (i < idsSize - 1) {
76
                r.append(this.ids.get(i)).append(", ");
80 ilm 77
            } else {
94 ilm 78
                r.append(this.ids.get(i));
80 ilm 79
            }
80
        }
94 ilm 81
        return r.toString();
80 ilm 82
    }
83
 
84
    @Override
85
    public void writeExternal(ObjectOutput out) throws IOException {
86
        try {
94 ilm 87
            out.writeUTF(this.tableId);
88
            out.writeObject(this.ids);
80 ilm 89
 
90
        } catch (Exception e) {
91
            e.printStackTrace();
92
            throw new IOException(e);
93
        }
94
    }
95
 
96
    @Override
97
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
94 ilm 98
        this.tableId = in.readUTF();
142 ilm 99
        this.ids = CollectionUtils.castList((List<?>) in.readObject(), Number.class);
80 ilm 100
 
101
    }
102
 
94 ilm 103
    @Override
104
    public JSONObject toJSON() {
105
        final JSONObject json = new JSONObject();
106
        json.put("class", "RowSelectionSpec");
107
        json.put("table-id", this.tableId);
108
        json.put("ids", this.ids);
109
        return json;
110
    }
111
 
112
    @Override
113
    public void fromJSON(JSONObject json) {
142 ilm 114
        this.tableId = JSONConverter.getParameterFromJSON(json, "table-id", String.class);
115
        final JSONArray jsonIds = JSONConverter.getParameterFromJSON(json, "ids", JSONArray.class);
116
        this.ids = new ArrayList<Number>();
94 ilm 117
        for (final Object jsonId : jsonIds) {
142 ilm 118
            this.ids.add(JSONConverter.getObjectFromJSON(jsonId, Number.class));
94 ilm 119
        }
120
    }
121
 
132 ilm 122
    public void setTableId(String id) {
123
        this.tableId = id;
124
    }
125
 
80 ilm 126
}