OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
132 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
132 ilm 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 org.openconcerto.utils.io.JSONConverter;
17
 
156 ilm 18
import java.io.IOException;
19
import java.io.ObjectInput;
20
import java.io.ObjectOutput;
132 ilm 21
import java.io.PrintStream;
22
import java.util.ArrayList;
23
import java.util.List;
24
 
25
import net.minidev.json.JSONArray;
26
import net.minidev.json.JSONObject;
27
 
142 ilm 28
public class LightUIContainer extends LightUIElement {
144 ilm 29
    private List<LightUIElement> children = new ArrayList<LightUIElement>(1);
132 ilm 30
 
156 ilm 31
    public LightUIContainer() {
32
        // Serialization
33
    }
34
 
132 ilm 35
    public LightUIContainer(final String id) {
36
        super(id);
37
    }
38
 
39
    public LightUIContainer(final JSONObject json) {
40
        super(json);
41
    }
42
 
43
    public LightUIContainer(final LightUIContainer container) {
44
        super(container);
45
    }
46
 
47
    public void addChild(final LightUIElement child) {
48
        if (child == null) {
144 ilm 49
            throw new IllegalArgumentException("Attempt to put a null child in container, id:" + this.getId());
132 ilm 50
        }
51
        child.setReadOnly(this.isReadOnly());
52
        child.setParent(this);
53
        this.children.add(child);
54
    }
144 ilm 55
 
142 ilm 56
    public void addChildren(final List<LightUIElement> children) {
57
        if (children == null) {
58
            throw new IllegalArgumentException("List null, id:" + this.getId());
59
        }
144 ilm 60
        for (final LightUIElement child : children) {
142 ilm 61
            this.addChild(child);
62
        }
63
    }
132 ilm 64
 
65
    public void insertChild(final int index, final LightUIElement child) {
66
        if (child == null) {
67
            throw new IllegalArgumentException("Attempt to put null child in container, id:" + this.getId());
68
        }
69
        child.setReadOnly(this.isReadOnly());
70
        child.setParent(this);
71
        this.children.add(index, child);
72
    }
73
 
74
    public void removeChild(final LightUIElement child) {
144 ilm 75
        this.children.remove(child);
132 ilm 76
    }
77
 
78
    public void removeChild(final int index) {
144 ilm 79
        this.children.remove(index);
132 ilm 80
    }
81
 
82
    public LightUIElement getChild(final int index) {
83
        return this.getChild(index, LightUIElement.class);
84
    }
85
 
86
    public <T extends LightUIElement> T getChild(final int index, final Class<T> expectedClass) {
144 ilm 87
        return expectedClass.cast(this.children.get(index));
132 ilm 88
    }
89
 
90
    public <T extends LightUIElement> T getFirstChild(final Class<T> expectedClass) {
91
        if (this.getChildrenCount() > 0)
142 ilm 92
            return expectedClass.cast(this.children.get(0));
132 ilm 93
        else
94
            return null;
95
    }
96
 
97
    public int getChildrenCount() {
144 ilm 98
        return this.children.size();
132 ilm 99
    }
100
 
156 ilm 101
    /**
102
     * Remove all the children from this container
103
     *
104
     * Warning : it doesn't destroy the children
105
     */
132 ilm 106
    public void clear() {
144 ilm 107
        this.children.clear();
132 ilm 108
    }
109
 
110
    public boolean replaceChild(final LightUIElement pChild) {
111
        final int childCount = this.getChildrenCount();
112
        pChild.setReadOnly(this.isReadOnly());
113
        for (int i = 0; i < childCount; i++) {
114
            final LightUIElement child = this.children.get(i);
115
            if (child.getId().equals(pChild.getId())) {
116
                this.children.set(i, pChild);
142 ilm 117
                pChild.setParent(this);
132 ilm 118
                return true;
119
            }
120
            if (child instanceof LightUIContainer) {
121
                if (((LightUIContainer) child).replaceChild(pChild)) {
122
                    return true;
123
                }
124
            }
125
            if (child instanceof LightUITable) {
126
                if (((LightUITable) child).replaceChild(pChild)) {
127
                    return true;
128
                }
129
            }
130
        }
131
        return false;
132
    }
133
 
144 ilm 134
    public <T extends LightUIElement> T findChildByID(final String id, final Class<T> expectedClass) {
132 ilm 135
        final int childCount = this.getChildrenCount();
136
        LightUIElement result = null;
137
        for (int i = 0; i < childCount; i++) {
138
            final LightUIElement child = this.getChild(i);
144 ilm 139
 
140
            if (child.getId().equals(id)) {
141
                result = child;
142
                break;
132 ilm 143
            } else {
144 ilm 144
 
145
                if (child instanceof LightUIContainer) {
146
                    result = ((LightUIContainer) child).findChildByID(id, expectedClass);
147
                    if (result != null) {
148
                        break;
149
                    }
132 ilm 150
                }
144 ilm 151
 
152
                if (child instanceof LightUITable) {
153
                    result = ((LightUITable) child).findElementByID(id, expectedClass);
154
                    if (result != null) {
155
                        break;
156
                    }
157
                }
132 ilm 158
            }
144 ilm 159
        }
132 ilm 160
 
144 ilm 161
        if (result != null) {
162
            if (expectedClass.isAssignableFrom(result.getClass())) {
163
                return expectedClass.cast(result);
164
            } else {
165
                throw new InvalidClassException(expectedClass.getName(), result.getClass().getName(), result.getId());
166
            }
167
        }
168
        return null;
169
    }
170
 
171
    public <T extends LightUIElement> T findChildByUUID(final String uuid, final Class<T> expectedClass) {
172
        final int childCount = this.getChildrenCount();
173
        LightUIElement result = null;
174
        for (int i = 0; i < childCount; i++) {
175
            final LightUIElement child = this.getChild(i);
176
            if (child.getUUID().equals(uuid)) {
177
                result = child;
178
                break;
179
            }
180
 
132 ilm 181
            if (child instanceof LightUIContainer) {
144 ilm 182
                result = ((LightUIContainer) child).findChildByUUID(uuid, expectedClass);
132 ilm 183
                if (result != null) {
184
                    break;
185
                }
186
            }
187
 
188
            if (child instanceof LightUITable) {
144 ilm 189
                result = ((LightUITable) child).findElementByUUID(uuid, expectedClass);
132 ilm 190
                if (result != null) {
191
                    break;
192
                }
193
            }
194
        }
195
 
196
        if (result != null) {
142 ilm 197
            if (expectedClass.isAssignableFrom(result.getClass())) {
198
                return expectedClass.cast(result);
132 ilm 199
            } else {
142 ilm 200
                throw new InvalidClassException(expectedClass.getName(), result.getClass().getName(), result.getId());
132 ilm 201
            }
202
        }
203
        return null;
204
    }
205
 
142 ilm 206
    public <T extends LightUIElement> List<T> findChildren(final Class<T> expectedClass, final boolean recursively) {
207
        final List<T> result = new ArrayList<T>();
208
        final int childCount = this.getChildrenCount();
209
        for (int i = 0; i < childCount; i++) {
210
            final LightUIElement child = this.getChild(i);
211
 
212
            if (recursively) {
213
                if (child instanceof LightUIContainer) {
214
                    result.addAll(((LightUIContainer) child).findChildren(expectedClass, true));
215
                }
216
 
217
                if (child instanceof LightUITable) {
218
                    result.addAll(((LightUITable) child).findChildren(expectedClass, true));
219
                }
220
            }
221
 
222
            if (expectedClass.isAssignableFrom(child.getClass())) {
223
                result.add(expectedClass.cast(child));
224
            }
225
        }
226
 
227
        return result;
228
    }
229
 
132 ilm 230
    @Override
231
    protected void copy(LightUIElement element) {
232
        super.copy(element);
233
        if (!(element instanceof LightUIContainer)) {
234
            throw new InvalidClassException(LightUIContainer.class.getName(), element.getClassName(), element.getId());
235
        }
236
 
237
        this.clear();
238
        final LightUIContainer container = (LightUIContainer) element;
144 ilm 239
        System.err.println("LightUIContainer.copy() warning children not copied");
240
        this.children.addAll(container.children);
132 ilm 241
 
242
        for (final LightUIElement child : this.children) {
243
            child.setParent(this);
244
        }
245
    }
246
 
247
    @Override
248
    public void dump(final PrintStream out, final int depth) {
249
        super.dump(out, depth);
250
        for (final LightUIElement child : this.children) {
251
            child.dump(out, depth + 1);
252
        }
253
    }
254
 
255
    @Override
256
    public void setReadOnly(final boolean readOnly) {
257
        super.setReadOnly(readOnly);
258
 
259
        final int childCount = this.getChildrenCount();
260
        for (int i = 0; i < childCount; i++) {
261
            final LightUIElement child = this.getChild(i);
262
            child.setReadOnly(readOnly);
263
        }
264
    }
265
 
266
    @Override
267
    public JSONObject toJSON() {
268
        final JSONObject result = super.toJSON();
144 ilm 269
        if (!this.children.isEmpty()) {
132 ilm 270
            result.put("childs", JSONConverter.getJSON(this.children));
271
        }
272
        return result;
273
    }
274
 
275
    @Override
276
    public void fromJSON(final JSONObject json) {
277
        super.fromJSON(json);
278
        this.clear();
279
 
144 ilm 280
        final JSONArray jsonElements = JSONConverter.getParameterFromJSON(json, "childs", JSONArray.class);
132 ilm 281
        if (jsonElements != null) {
282
            for (final Object o : jsonElements) {
144 ilm 283
                final JSONObject jsonElement = JSONConverter.getObjectFromJSON(o, JSONObject.class);
132 ilm 284
                if (jsonElement == null) {
285
                    throw new IllegalArgumentException("null element in json parameter");
286
                }
287
                final LightUIElement lightElement = JSONToLightUIConvertorManager.getInstance().createUIElementFromJSON(jsonElement);
156 ilm 288
                this.addChild(lightElement);
132 ilm 289
            }
290
        }
291
    }
142 ilm 292
 
293
    @Override
156 ilm 294
    public void writeExternal(ObjectOutput out) throws IOException {
295
        super.writeExternal(out);
296
        out.writeInt(this.children.size());
297
        for (LightUIElement e : this.children) {
298
            out.writeObject(e);
299
        }
300
    }
301
 
302
    @Override
303
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
304
        super.readExternal(in);
305
        int n = in.readInt();
306
        this.children.clear();
307
        for (int i = 0; i < n; i++) {
308
            LightUIElement e = (LightUIElement) in.readObject();
309
            this.children.add(e);
310
        }
311
 
312
    }
313
 
314
    @Override
142 ilm 315
    public void destroy() {
316
        super.destroy();
317
        for (final LightUIElement child : this.children) {
318
            child.destroy();
319
        }
320
    }
149 ilm 321
 
132 ilm 322
}