OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | 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
 
142 ilm 16
import org.openconcerto.utils.CollectionUtils;
132 ilm 17
import org.openconcerto.utils.io.JSONAble;
18
import org.openconcerto.utils.io.JSONConverter;
19
 
80 ilm 20
import java.io.Externalizable;
21
import java.io.IOException;
22
import java.io.ObjectInput;
23
import java.io.ObjectOutput;
24
import java.util.ArrayList;
25
import java.util.List;
26
 
94 ilm 27
import net.minidev.json.JSONArray;
28
import net.minidev.json.JSONObject;
29
 
93 ilm 30
public class Row implements Externalizable, JSONAble {
80 ilm 31
 
142 ilm 32
    private Number id;
156 ilm 33
    private String extendId = "";// writeExternal crash if not set
80 ilm 34
    private List<Object> values;
142 ilm 35
 
94 ilm 36
    private Boolean fillWidth = false;
37
    private Boolean toggleable = false;
38
    private Boolean visible = true;
80 ilm 39
 
40
    public Row() {
41
        // Serialization
42
    }
43
 
94 ilm 44
    public Row(final JSONObject json) {
45
        this.fromJSON(json);
46
    }
47
 
142 ilm 48
    public Row(final Number id, int valueCount) {
80 ilm 49
        this.id = id;
156 ilm 50
        this.values = new ArrayList<>(valueCount);
132 ilm 51
        if (valueCount > 0) {
142 ilm 52
            for (int i = 0; i < valueCount; i++) {
132 ilm 53
                this.values.add(null);
54
            }
55
        }
80 ilm 56
    }
57
 
142 ilm 58
    public Row(final Number id) {
80 ilm 59
        this.id = id;
156 ilm 60
        this.values = new ArrayList<>();
80 ilm 61
    }
62
 
142 ilm 63
    public final void setValues(List<Object> values) {
80 ilm 64
        this.values = values;
65
    }
66
 
142 ilm 67
    public final List<Object> getValues() {
93 ilm 68
        return this.values;
80 ilm 69
    }
70
 
142 ilm 71
    public final Number getId() {
93 ilm 72
        return this.id;
80 ilm 73
    }
142 ilm 74
 
75
    public final String getExtendId() {
132 ilm 76
        return this.extendId;
77
    }
142 ilm 78
 
79
    public final void setExtendId(final String extendId) {
156 ilm 80
        if (extendId == null) {
81
            throw new IllegalArgumentException("extendId cannot be null");
82
        }
132 ilm 83
        this.extendId = extendId;
84
    }
142 ilm 85
 
86
    public final void addValue(Object v) {
93 ilm 87
        this.values.add(v);
80 ilm 88
    }
89
 
142 ilm 90
    public final void setValue(int index, Object v) {
93 ilm 91
        this.values.set(index, v);
80 ilm 92
    }
142 ilm 93
 
94
    public final Boolean isFillWidth() {
94 ilm 95
        return this.fillWidth;
96
    }
80 ilm 97
 
142 ilm 98
    public final void setFillWidth(final Boolean fillWidth) {
94 ilm 99
        this.fillWidth = fillWidth;
100
    }
142 ilm 101
 
102
    public final Boolean isToggleable() {
94 ilm 103
        return this.toggleable;
104
    }
105
 
142 ilm 106
    public final void setToggleable(final Boolean toggleable) {
94 ilm 107
        this.toggleable = toggleable;
108
    }
142 ilm 109
 
110
    public final Boolean isVisible() {
94 ilm 111
        return this.visible;
112
    }
113
 
142 ilm 114
    public final void setVisible(final Boolean visible) {
94 ilm 115
        this.visible = visible;
116
    }
117
 
80 ilm 118
    @Override
119
    public void writeExternal(ObjectOutput out) throws IOException {
142 ilm 120
        out.writeObject(this.id);
132 ilm 121
        out.writeUTF(this.extendId);
93 ilm 122
        out.writeObject(this.values);
94 ilm 123
        out.writeBoolean(this.fillWidth);
124
        out.writeBoolean(this.toggleable);
125
        out.writeBoolean(this.visible);
80 ilm 126
    }
127
 
128
    @Override
129
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
142 ilm 130
        this.id = (Number) in.readObject();
132 ilm 131
        this.extendId = in.readUTF();
142 ilm 132
        this.values = CollectionUtils.castList((List<?>) in.readObject(), Object.class);
94 ilm 133
        this.fillWidth = in.readBoolean();
134
        this.toggleable = in.readBoolean();
135
        this.visible = in.readBoolean();
93 ilm 136
    }
80 ilm 137
 
93 ilm 138
    @Override
94 ilm 139
    public JSONObject toJSON() {
140
        final JSONObject result = new JSONObject();
141
 
142
        result.put("class", "Row");
143
        result.put("id", this.id);
142 ilm 144
        if (this.extendId != null) {
132 ilm 145
            result.put("extend-id", this.extendId);
146
        }
142 ilm 147
        if (!this.values.isEmpty()) {
94 ilm 148
            result.put("values", JSONConverter.getJSON(this.values));
149
        }
142 ilm 150
        if (this.fillWidth) {
94 ilm 151
            result.put("fill-width", true);
152
        }
142 ilm 153
        if (this.toggleable) {
94 ilm 154
            result.put("toggleable", true);
155
        }
142 ilm 156
        if (!this.visible) {
94 ilm 157
            result.put("visible", false);
158
        }
142 ilm 159
 
94 ilm 160
        return result;
80 ilm 161
    }
162
 
94 ilm 163
    @Override
164
    public void fromJSON(final JSONObject json) {
142 ilm 165
        this.id = JSONConverter.getParameterFromJSON(json, "id", Number.class);
166
        this.extendId = JSONConverter.getParameterFromJSON(json, "extend-id", String.class);
167
        this.fillWidth = JSONConverter.getParameterFromJSON(json, "fill-width", Boolean.class, false);
168
        this.toggleable = JSONConverter.getParameterFromJSON(json, "toggleable", Boolean.class, false);
169
        this.visible = JSONConverter.getParameterFromJSON(json, "visible", Boolean.class, true);
94 ilm 170
 
171
        final JSONArray jsonValues = (JSONArray) JSONConverter.getParameterFromJSON(json, "values", JSONArray.class);
172
        if (jsonValues != null) {
173
            final int valuesSize = jsonValues.size();
174
            this.values = new ArrayList<Object>(valuesSize);
175
            for (int i = 0; i < valuesSize; i++) {
176
                Object objValue = jsonValues.get(i);
177
                if (objValue instanceof JSONObject) {
178
                    final JSONObject jsonValue = (JSONObject) objValue;
142 ilm 179
                    objValue = JSONToLightUIConvertorManager.getInstance().createUIElementFromJSON(jsonValue);
94 ilm 180
                } else {
142 ilm 181
                    if (objValue instanceof String) {
94 ilm 182
                        objValue = JSONConverter.getObjectFromJSON(objValue, String.class);
142 ilm 183
                    } else if (objValue instanceof Integer) {
94 ilm 184
                        objValue = JSONConverter.getObjectFromJSON(objValue, Integer.class);
142 ilm 185
                    } else if (objValue instanceof Long) {
94 ilm 186
                        objValue = JSONConverter.getObjectFromJSON(objValue, Long.class);
142 ilm 187
                    } else if (objValue instanceof Boolean) {
94 ilm 188
                        objValue = JSONConverter.getObjectFromJSON(objValue, Boolean.class);
156 ilm 189
                    } else if (objValue instanceof Double) {
190
                        objValue = JSONConverter.getObjectFromJSON(objValue, Double.class);
142 ilm 191
                    } else if (objValue != null) {
94 ilm 192
                        throw new IllegalArgumentException("unknow type: " + objValue.getClass().getName());
193
                    }
194
                }
195
                this.values.add(objValue);
196
            }
197
        }
198
    }
142 ilm 199
 
94 ilm 200
    @Override
201
    public String toString() {
144 ilm 202
        return "Row id: " + this.id + " values: " + this.values;
94 ilm 203
    }
80 ilm 204
}