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