OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
94 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;
132 ilm 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;
94 ilm 26
import net.minidev.json.JSONObject;
27
 
132 ilm 28
public class LightUIFrame extends LightUIContainer {
144 ilm 29
    private static final String KEY_ACTIVE = "active";
30
    private static final String KEY_TITLE_PANEL = "title-panel";
31
    private static final String KEY_FOOTER_PANEL = "footer-panel";
32
    private static final String KEY_CHILDREN_FRAME = "children-frame";
94 ilm 33
 
132 ilm 34
    private Boolean active = false;
142 ilm 35
    private LightUIPanel titlePanel = null;
149 ilm 36
    private LightUIPanel contentPanel;
142 ilm 37
    private LightUIPanel footerPanel = new LightUIPanel(this.getId() + ".footer.panel");
132 ilm 38
 
39
    private List<LightUIFrame> childrenFrame;
177 ilm 40
    private List<LightUIElement> asynchronousChildren = new ArrayList<>();
132 ilm 41
 
156 ilm 42
    public LightUIFrame() {
43
        // Serialization
44
    }
45
 
94 ilm 46
    // Init from json constructor
47
    public LightUIFrame(final JSONObject json) {
132 ilm 48
        super(json);
94 ilm 49
    }
50
 
51
    // Clone constructor
52
    public LightUIFrame(final LightUIFrame frame) {
53
        super(frame);
54
        this.active = frame.active;
142 ilm 55
        this.titlePanel = frame.titlePanel;
132 ilm 56
        this.childrenFrame = frame.childrenFrame;
149 ilm 57
        this.contentPanel = frame.contentPanel;
142 ilm 58
        this.setFooterPanel(frame.footerPanel);
94 ilm 59
    }
60
 
156 ilm 61
    @Override
62
    public void destroy() {
63
        super.destroy();
64
        if (this.titlePanel != null) {
65
            this.titlePanel.destroy();
174 ilm 66
            this.titlePanel = null;
156 ilm 67
        }
68
        if (this.footerPanel != null) {
69
            this.footerPanel.destroy();
174 ilm 70
            this.footerPanel = null;
156 ilm 71
        }
174 ilm 72
        if (this.contentPanel != null) {
73
            this.contentPanel.destroy();
74
            this.contentPanel = null;
75
        }
156 ilm 76
        if (this.childrenFrame != null) {
77
            for (LightUIFrame frame : childrenFrame) {
78
                frame.destroy();
79
            }
80
        }
81
    }
82
 
132 ilm 83
    /**
84
     * Creation of an instance of a frame, this one is initialized with an empty main panel
85
     *
86
     * @param id Id of the frame
87
     */
94 ilm 88
    public LightUIFrame(final String id) {
132 ilm 89
        super(id);
94 ilm 90
        this.setType(TYPE_FRAME);
149 ilm 91
        this.childrenFrame = new ArrayList<>();
92
        this.contentPanel = new LightUIPanel(this.getId() + ".main.panel");
93
        this.addChild(contentPanel);
132 ilm 94
        this.footerPanel.setParent(this);
142 ilm 95
        this.footerPanel.setFillHeight(false);
96
        this.footerPanel.setHeight(50);
97
        this.createTitlePanel();
94 ilm 98
    }
99
 
149 ilm 100
    public LightUIPanel getContentPanel() {
101
        return this.contentPanel;
102
    }
103
 
104
    public void setContentPanel(LightUIPanel contentPanel) {
105
        this.contentPanel = contentPanel;
106
        this.addChild(contentPanel);
107
    }
108
 
142 ilm 109
    public LightUIPanel createTitlePanel(final String title) {
110
        this.createTitlePanel();
111
        final LightUILabel titleLabel = new LightUILabel(this.titlePanel.getId() + ".label", title, true);
112
        titleLabel.setVerticalAlignement(VALIGN_CENTER);
113
        this.titlePanel.getLastLine().addChild(titleLabel);
114
        return this.titlePanel;
94 ilm 115
    }
132 ilm 116
 
142 ilm 117
    public LightUIPanel createTitlePanel() {
118
        this.titlePanel = new LightUIPanel(this.getId() + ".title.panel");
119
        this.titlePanel.setFillHeight(false);
120
        this.titlePanel.setHeight(50);
121
        return this.titlePanel;
94 ilm 122
    }
132 ilm 123
 
142 ilm 124
    public LightUIPanel getTitlePanel() {
125
        return this.titlePanel;
126
    }
127
 
149 ilm 128
    public void setTitlePanel(LightUIPanel titlePanel) {
129
        titlePanel.setId(this.getId() + ".title.panel");
130
        this.titlePanel = titlePanel;
131
    }
132
 
132 ilm 133
    public LightUIPanel getFooterPanel() {
134
        return this.footerPanel;
135
    }
136
 
142 ilm 137
    /**
138
     * Set the footer panel of the frame, be careful the ID is automatically replace by
139
     * <code>this.getId() + ".footer.panel"</code>.
140
     *
141
     * @param footerPanel - The new footer panel of this frame.
142
     */
143
    public void setFooterPanel(final LightUIPanel footerPanel) {
144
        footerPanel.setId(this.getId() + ".footer.panel");
145
        this.footerPanel = footerPanel;
146
        this.footerPanel.setFillHeight(false);
147
        this.footerPanel.setParent(this);
148
        this.footerPanel.setHeight(50);
149
    }
150
 
132 ilm 151
    public void updateFooterPanel(final LightUIPanel footerPanel) {
152
        if (footerPanel != null) {
153
            this.footerPanel.copy(footerPanel);
154
        } else {
155
            this.footerPanel.clear();
156
        }
157
 
158
    }
159
 
94 ilm 160
    public boolean isActive() {
161
        return this.active;
162
    }
132 ilm 163
 
94 ilm 164
    public void setActive(final boolean active) {
165
        this.active = active;
166
    }
132 ilm 167
 
168
    public String getPanelId() {
169
        return this.getId() + ".main.panel";
94 ilm 170
    }
171
 
132 ilm 172
    public void removeChildFrame(final LightUIFrame childFrame) {
173
        this.childrenFrame.remove(childFrame);
174
    }
175
 
176
    public void removeChildFrame(final int index) {
177
        this.childrenFrame.remove(index);
178
    }
179
 
180
    public void clearChildrenFrame() {
181
        this.childrenFrame.clear();
182
    }
183
 
94 ilm 184
    @Override
132 ilm 185
    /**
186
     * Only one panel is accepted into a frame. And it's Id is always : frame.getId() +
187
     * ".main.panel"
188
     *
189
     * @param parent The parent frame of this one.
190
     * @throws InvalidClassException
191
     */
192
    public void setParent(final LightUIElement parent) {
193
        if (!(parent instanceof LightUIFrame)) {
194
            throw new InvalidClassException(LightUIFrame.class.getName(), parent.getClassName(), parent.getId());
195
        }
196
        super.setParent(parent);
197
 
198
        ((LightUIFrame) parent).childrenFrame.add(this);
199
    }
200
 
201
    @Override
144 ilm 202
    public <T extends LightUIElement> T findChildByID(String searchParam, Class<T> childClass) {
203
        final T result = super.findChildByID(searchParam, childClass);
132 ilm 204
        if (result != null) {
205
            return result;
206
        } else {
144 ilm 207
            return this.footerPanel.findChildByID(searchParam, childClass);
132 ilm 208
        }
209
    }
210
 
211
    @Override
144 ilm 212
    public <T extends LightUIElement> T findChildByUUID(String searchParam, Class<T> childClass) {
213
        final T result = super.findChildByUUID(searchParam, childClass);
214
        if (result != null) {
215
            return result;
216
        } else {
217
            return this.footerPanel.findChildByUUID(searchParam, childClass);
218
        }
219
    }
220
 
221
    @Override
132 ilm 222
    public void setReadOnly(boolean readOnly) {
223
        super.setReadOnly(readOnly);
224
        this.footerPanel.setReadOnly(readOnly);
225
    }
226
 
227
    @Override
228
    /**
229
     * Only one panel is accepted into a frame. And it's Id is always : frame.getId() +
230
     * ".main.panel"
231
     *
232
     * @param child The panel which will replace the main panel
233
     */
144 ilm 234
    public void addChild(final LightUIElement child) {
132 ilm 235
        if (!(child instanceof LightUIPanel)) {
236
            throw new InvalidClassException(LightUIPanel.class.getName(), child.getClassName(), child.getId());
237
        }
238
        child.setId(this.getPanelId());
239
        this.clear();
240
        super.addChild(child);
241
    }
242
 
243
    @Override
244
    /**
245
     * Only one panel is accepted into a frame. And it's Id is always : frame.getId() +
246
     * ".main.panel"
247
     *
248
     * @param index No importance
249
     * @param child The panel which will replace the main panel
250
     */
144 ilm 251
    public void insertChild(int index, LightUIElement child) {
132 ilm 252
        if (!(child instanceof LightUIPanel)) {
253
            throw new InvalidClassException(LightUIPanel.class.getName(), child.getClassName(), child.getId());
254
        }
255
        child.setId(this.getPanelId());
256
        this.clear();
257
        super.insertChild(index, child);
258
    }
259
 
260
    @Override
261
    public void dump(final PrintStream out, final int depth) {
262
        out.println("------------- LightUIFrame -------------");
263
        super.dump(out, 0);
264
 
265
        out.println("footer-panel: ");
266
        if (this.footerPanel != null) {
267
            this.footerPanel.dump(out, 0);
268
        } else {
269
            out.println("null");
270
        }
271
 
272
        out.println("--------------------------");
273
    }
274
 
275
    @Override
94 ilm 276
    public JSONObject toJSON() {
277
        final JSONObject json = super.toJSON();
132 ilm 278
        if (this.active) {
144 ilm 279
            json.put(KEY_ACTIVE, true);
94 ilm 280
        }
142 ilm 281
        if (this.titlePanel != null) {
144 ilm 282
            json.put(KEY_TITLE_PANEL, this.titlePanel.toJSON());
94 ilm 283
        }
144 ilm 284
        json.put(KEY_FOOTER_PANEL, this.footerPanel.toJSON());
94 ilm 285
        return json;
286
    }
287
 
288
    @Override
289
    public void fromJSON(final JSONObject json) {
290
        super.fromJSON(json);
144 ilm 291
        this.active = JSONConverter.getParameterFromJSON(json, KEY_ACTIVE, Boolean.class, false);
156 ilm 292
        this.createTitlePanel();
144 ilm 293
        final JSONObject jsonTitlePanel = JSONConverter.getParameterFromJSON(json, KEY_TITLE_PANEL, JSONObject.class);
142 ilm 294
        if (jsonTitlePanel != null) {
295
            this.titlePanel.fromJSON(jsonTitlePanel);
296
        }
297
 
144 ilm 298
        final JSONObject jsonFooterPanel = JSONConverter.getParameterFromJSON(json, KEY_FOOTER_PANEL, JSONObject.class, null);
132 ilm 299
        if (jsonFooterPanel != null) {
300
            this.footerPanel.fromJSON(jsonFooterPanel);
94 ilm 301
        }
132 ilm 302
 
144 ilm 303
        final JSONArray jsonChildrenFrame = JSONConverter.getParameterFromJSON(json, KEY_CHILDREN_FRAME, JSONArray.class, null);
132 ilm 304
        this.childrenFrame = new ArrayList<LightUIFrame>();
305
        if (jsonChildrenFrame != null) {
306
            for (final Object objJsonFrame : jsonChildrenFrame) {
307
                final JSONObject jsonFrame = JSONConverter.getObjectFromJSON(objJsonFrame, JSONObject.class);
308
                final LightUIFrame childFrame = new LightUIFrame(jsonFrame);
309
                this.childrenFrame.add(childFrame);
310
            }
311
        }
94 ilm 312
    }
156 ilm 313
 
314
    @Override
315
    public void writeExternal(ObjectOutput out) throws IOException {
316
        super.writeExternal(out);
317
        out.writeBoolean(this.active);
318
        // Title panel
319
        if (this.titlePanel != null) {
320
            out.writeBoolean(true);
321
            out.writeObject(this.titlePanel);
322
        } else {
323
            out.writeBoolean(false);
324
        }
325
        // Footer panel
326
        if (this.footerPanel != null) {
327
            out.writeBoolean(true);
328
            out.writeObject(this.footerPanel);
329
        } else {
330
            out.writeBoolean(false);
331
        }
332
    }
333
 
334
    @Override
335
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
336
        super.readExternal(in);
337
        this.active = in.readBoolean();
338
        if (in.readBoolean()) {
339
            this.titlePanel = (LightUIPanel) in.readObject();
340
        }
341
        if (in.readBoolean()) {
342
            this.footerPanel = (LightUIPanel) in.readObject();
343
        }
344
    }
345
 
177 ilm 346
    public List<LightUIElement> getAsynchronousChildren() {
347
        return this.asynchronousChildren;
348
    }
94 ilm 349
}