OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 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
 
132 ilm 16
import org.openconcerto.utils.io.JSONConverter;
17
import org.openconcerto.utils.io.Transferable;
18
 
19
import java.awt.Color;
156 ilm 20
import java.io.IOException;
21
import java.io.ObjectInput;
22
import java.io.ObjectOutput;
94 ilm 23
import java.io.PrintStream;
24
import java.util.ArrayList;
25
import java.util.List;
26
 
27
import net.minidev.json.JSONArray;
28
import net.minidev.json.JSONObject;
29
 
142 ilm 30
public class LightUIPanel extends LightUserControlContainer implements Transferable {
156 ilm 31
 
132 ilm 32
    private static final long serialVersionUID = -3399395824294128572L;
94 ilm 33
 
132 ilm 34
    private String title;
35
    private Color titleColor;
36
    private Color titleBackgroundColor;
156 ilm 37
    private List<LightController> controlers = new ArrayList<>();
94 ilm 38
 
156 ilm 39
    public LightUIPanel() {
40
        // Serialization
41
    }
94 ilm 42
 
43
    public LightUIPanel(final JSONObject json) {
132 ilm 44
        super(json);
94 ilm 45
    }
46
 
47
    // Clone constructor
48
    public LightUIPanel(final LightUIPanel panelElement) {
49
        super(panelElement);
50
    }
51
 
132 ilm 52
    public LightUIPanel(final String id) {
53
        super(id);
94 ilm 54
        this.setType(TYPE_PANEL);
132 ilm 55
        this.setWeightX(1);
56
        this.setFillWidth(true);
142 ilm 57
        this.setFillHeight(true);
94 ilm 58
    }
59
 
156 ilm 60
    @Override
61
    public void destroy() {
62
        super.destroy();
63
        this.controlers = null;
64
    }
65
 
132 ilm 66
    public final LightUILine getLastLine() {
67
        final int childCount = this.getChildrenCount();
68
        if (childCount == 0) {
69
            final LightUILine l = new LightUILine();
70
            this.addChild(l);
94 ilm 71
 
72
            return l;
73
        }
132 ilm 74
        return this.getChild(childCount - 1, LightUILine.class);
94 ilm 75
    }
76
 
132 ilm 77
    public String getTitle() {
78
        return this.title;
94 ilm 79
    }
80
 
132 ilm 81
    public void setTitle(final String title) {
82
        this.title = title;
94 ilm 83
    }
84
 
132 ilm 85
    public void setTitleColor(final Color c) {
86
        this.titleColor = c;
94 ilm 87
    }
88
 
132 ilm 89
    public void setTitleBackgoundColor(final Color c) {
90
        this.titleBackgroundColor = c;
94 ilm 91
    }
92
 
156 ilm 93
    public void addControler(final LightController controler) {
94 ilm 94
        this.controlers.add(controler);
95
    }
96
 
156 ilm 97
    public List<LightController> getControlers() {
94 ilm 98
        return this.controlers;
99
    }
100
 
132 ilm 101
    public void dumpControllers(final PrintStream out) {
94 ilm 102
        dumpControllers(out, 0);
103
    }
104
 
132 ilm 105
    public void dumpControllers(final PrintStream out, final int depth) {
94 ilm 106
        addSpacer(out, depth);
107
        out.println("Contollers for id:" + this.getId() + " title: " + this.title);
156 ilm 108
        for (LightController controler : this.controlers) {
94 ilm 109
            addSpacer(out, depth);
110
            out.println(controler);
111
        }
132 ilm 112
 
113
        final int lineCount = this.getChildrenCount();
94 ilm 114
        addSpacer(out, depth);
115
        out.println(getId() + " : " + this.title);
116
        addSpacer(out, depth);
132 ilm 117
        out.println("LightUIPanel " + lineCount + " lines ");
118
        for (int i = 0; i < lineCount; i++) {
119
            final LightUILine line = this.getChild(i, LightUILine.class);
120
            for (int j = 0; j < line.getChildrenCount(); j++) {
121
                final LightUIElement e = line.getChild(j);
94 ilm 122
                if (e instanceof LightUIPanel) {
123
                    ((LightUIPanel) e).dumpControllers(out, depth + 1);
124
                }
125
            }
126
        }
127
    }
128
 
132 ilm 129
    @Override
130
    public void copy(final LightUIElement element) {
131
        super.copy(element);
132
        if (!(element instanceof LightUIPanel)) {
133
            throw new InvalidClassException(LightUIPanel.class.getName(), element.getClassName(), element.getId());
94 ilm 134
        }
132 ilm 135
        final LightUIPanel panelElement = (LightUIPanel) element;
136
        this.title = panelElement.title;
137
        this.titleColor = panelElement.titleColor;
138
        this.titleBackgroundColor = panelElement.titleBackgroundColor;
139
        this.controlers.addAll(panelElement.controlers);
140
    }
94 ilm 141
 
132 ilm 142
    @Override
143
    public void clear() {
144
        super.clear();
145
        this.controlers.clear();
94 ilm 146
    }
147
 
132 ilm 148
    @Override
144 ilm 149
    public void addChild(final LightUIElement e) {
150
        LightUILine line;
151
        if (e instanceof LightUILine) {
152
            line = (LightUILine) e;
153
        } else {
154
            line = new LightUILine();
155
            line.addChild(e);
94 ilm 156
        }
144 ilm 157
        addLine(line);
158
    }
159
 
160
    public void addLine(final LightUILine line) {
132 ilm 161
        // Ensure uniqueness of line id
162
        line.setId(LightUILine.createId(this));
163
        super.addChild(line);
94 ilm 164
    }
165
 
132 ilm 166
    @Override
167
    public void setReadOnly(final boolean readOnly) {
168
        super.setReadOnly(readOnly);
169
        final int lineCount = this.getChildrenCount();
170
        for (int i = 0; i < lineCount; i++) {
171
            final LightUILine line = this.getChild(i, LightUILine.class);
172
            line.setReadOnly(readOnly);
173
        }
174
    }
175
 
176
    @Override
177
    public String getClassName() {
178
        return this.getClass().getName();
179
    }
180
 
181
    @Override
182
    public void dump(final PrintStream out, final int depth) {
183
        this.addSpacer(out, depth);
184
        out.println("------LightUIPanel-----");
185
        this.addSpacer(out, depth);
186
        out.println("Title : " + this.title);
187
        this.addSpacer(out, depth);
144 ilm 188
        if (this.titleColor != null) {
189
            out.print(" title-color: " + this.titleColor.toString());
190
        }
191
        if (this.titleBackgroundColor != null) {
192
            out.print(" bg-title-color: " + this.titleBackgroundColor.toString());
193
        }
194
        out.println("v-scroll : " + this.isVerticallyScrollable());
132 ilm 195
        super.dump(out, depth);
196
        this.addSpacer(out, depth);
197
        out.println("------------------------");
198
    }
199
 
200
    @Override
142 ilm 201
    public void _setValueFromContext(final Object value) {
144 ilm 202
        final JSONObject jsonContext = JSONConverter.getObjectFromJSON(value, JSONObject.class);
142 ilm 203
 
204
        if (jsonContext == null) {
205
            System.err.println("LightUIPanel.setValueFromContext() - json is null for this panel: " + this.getId());
206
        } else {
207
            final int childCount = this.getChildrenCount();
208
            for (int i = 0; i < childCount; i++) {
209
                final LightUILine line = this.getChild(i, LightUILine.class);
210
                final int lineChildCount = line.getChildrenCount();
211
                for (int j = 0; j < lineChildCount; j++) {
212
                    final LightUIElement lineChild = line.getChild(j);
213
                    if (lineChild instanceof LightUserControlContainer) {
214
                        if (!jsonContext.containsKey(lineChild.getUUID())) {
215
                            System.err.println("LightUIPanel.setValueFromContext() - Impossible to find key " + lineChild.getUUID() + " in context, LightUIElement id: " + lineChild.getId());
216
                        }
217
                        ((LightUserControlContainer) lineChild).setValueFromContext(jsonContext.get(lineChild.getUUID()));
218
                    } else if (lineChild instanceof LightUserControl) {
219
                        if (!jsonContext.containsKey(lineChild.getUUID())) {
220
                            System.err.println("LightUIPanel.setValueFromContext() - Impossible to find key " + lineChild.getUUID() + " in context, LightUIElement id: " + lineChild.getId());
221
                        }
222
                        ((LightUserControl) lineChild).setValueFromContext(jsonContext.get(lineChild.getUUID()));
132 ilm 223
                    }
94 ilm 224
                }
225
            }
226
        }
227
    }
228
 
229
    @Override
230
    public JSONObject toJSON() {
231
        final JSONObject result = super.toJSON();
232
        if (this.title != null) {
233
            result.put("title", this.title);
234
        }
132 ilm 235
        if (this.titleColor != null) {
236
            result.put("title-color", JSONConverter.getJSON(this.titleColor));
94 ilm 237
        }
132 ilm 238
        if (this.titleBackgroundColor != null) {
239
            result.put("title-bgcolor", JSONConverter.getJSON(this.titleBackgroundColor));
240
        }
94 ilm 241
        if (!this.controlers.isEmpty()) {
242
            result.put("controlers", JSONConverter.getJSON(this.controlers));
243
        }
244
        return result;
245
    }
246
 
247
    @Override
248
    public void fromJSON(final JSONObject json) {
249
        super.fromJSON(json);
144 ilm 250
        this.title = JSONConverter.getParameterFromJSON(json, "title", String.class, null);
251
        this.titleColor = JSONConverter.getParameterFromJSON(json, "title-color", Color.class, null);
252
        this.titleBackgroundColor = JSONConverter.getParameterFromJSON(json, "title-bgcolor", Color.class, null);
94 ilm 253
 
144 ilm 254
        final JSONArray jsonControlers = JSONConverter.getParameterFromJSON(json, "controlers", JSONArray.class);
94 ilm 255
        if (jsonControlers != null) {
256
            final int controlersSize = jsonControlers.size();
257
            for (int i = 0; i < controlersSize; i++) {
144 ilm 258
                final JSONObject jsonControler = JSONConverter.getObjectFromJSON(jsonControlers.get(i), JSONObject.class);
156 ilm 259
                this.controlers.add(new LightController((JSONObject) jsonControler));
94 ilm 260
            }
261
        }
262
    }
132 ilm 263
 
264
    @Override
156 ilm 265
    public void writeExternal(ObjectOutput out) throws IOException {
266
        super.writeExternal(out);
267
        writeIfNotNull(out, this.title);
268
        writeIfNotNull(out, this.titleColor);
269
        writeIfNotNull(out, this.titleBackgroundColor);
270
        out.writeByte(this.controlers.size());
271
        for (LightController lightControler : controlers) {
272
            lightControler.writeExternal(out);
273
        }
274
    }
275
 
276
    @Override
277
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
278
        super.readExternal(in);
279
        if (in.readBoolean()) {
280
            this.title = in.readUTF();
281
        }
282
        if (in.readBoolean()) {
283
            this.titleColor = new Color(in.readInt());
284
        }
285
        if (in.readBoolean()) {
286
            this.titleBackgroundColor = new Color(in.readInt());
287
        }
288
        this.controlers.clear();
289
        final int size = in.readByte();
290
        for (int i = 0; i < size; i++) {
291
            final LightController lightControler = new LightController();
292
            lightControler.readExternal(in);
293
            this.controlers.add(lightControler);
294
        }
295
    }
296
 
297
    @Override
132 ilm 298
    public void setFoldable(boolean foldable) {
299
        super.setFoldable(foldable);
300
        if (foldable && this.title == null) {
301
            this.title = "missing title on panel " + this.getId();
302
        }
303
    }
94 ilm 304
}