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
73 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
 
94 ilm 16
import org.openconcerto.utils.io.JSONConverter;
17
import org.openconcerto.utils.io.Transferable;
132 ilm 18
 
156 ilm 19
import java.io.Externalizable;
20
import java.io.IOException;
21
import java.io.ObjectInput;
22
import java.io.ObjectOutput;
23
 
94 ilm 24
import net.minidev.json.JSONObject;
25
 
156 ilm 26
public class LightUILine extends LightUIContainer implements Transferable, Externalizable {
132 ilm 27
    private static final long serialVersionUID = 4132718509484530435L;
73 ilm 28
 
29
    public static final int ALIGN_GRID = 0;
30
    public static final int ALIGN_LEFT = 1;
31
    public static final int ALIGN_RIGHT = 2;
32
 
94 ilm 33
    private int gridAlignment = ALIGN_GRID;
73 ilm 34
 
156 ilm 35
    private int elementPadding = 0;
36
    private int elementMargin = 0;
94 ilm 37
 
38
    public LightUILine() {
132 ilm 39
        // Id will be set when this line will be added into a panel, or set manually.
40
        super("");
41
        this.setType(TYPE_PANEL_LINE);
73 ilm 42
    }
43
 
94 ilm 44
    public LightUILine(final JSONObject json) {
132 ilm 45
        super(json);
73 ilm 46
    }
94 ilm 47
 
132 ilm 48
    public LightUILine(final LightUILine line) {
49
        super(line);
94 ilm 50
    }
51
 
132 ilm 52
    public void setElementPadding(final Integer elementPadding) {
53
        this.elementPadding = elementPadding;
94 ilm 54
    }
55
 
132 ilm 56
    public Integer getElementPadding() {
57
        return this.elementPadding;
94 ilm 58
    }
59
 
132 ilm 60
    public void setElementMargin(final Integer elementMargin) {
61
        this.elementMargin = elementMargin;
94 ilm 62
    }
63
 
132 ilm 64
    public Integer getElementMargin() {
65
        return this.elementMargin;
93 ilm 66
    }
94 ilm 67
 
68
    public int getGridAlignment() {
69
        return this.gridAlignment;
73 ilm 70
    }
71
 
94 ilm 72
    public void setGridAlignment(final int gridAlignment) {
73
        this.gridAlignment = gridAlignment;
73 ilm 74
    }
75
 
142 ilm 76
    public Integer getTotalHeight() {
132 ilm 77
        Integer h = 0;
78
        final int size = this.getChildrenCount();
79
        for (int i = 0; i < size; i++) {
80
            h += this.getChild(i).getHeight();
81
        }
82
        return h;
94 ilm 83
    }
84
 
142 ilm 85
    public Integer getTotalGridWidth() {
132 ilm 86
        Integer w = 0;
87
        final int size = this.getChildrenCount();
94 ilm 88
        for (int i = 0; i < size; i++) {
132 ilm 89
            w += this.getChild(i).getGridWidth();
94 ilm 90
        }
91
        return w;
73 ilm 92
    }
93
 
132 ilm 94
    public static String createId(final LightUIPanel parent) {
95
        return parent.getId() + ".line." + parent.getChildrenCount();
73 ilm 96
    }
94 ilm 97
 
132 ilm 98
    @Override
99
    protected void copy(LightUIElement element) {
100
        super.copy(element);
101
 
102
        if (!(element instanceof LightUILine)) {
103
            throw new InvalidClassException(this.getClassName(), element.getClassName(), element.getId());
94 ilm 104
        }
132 ilm 105
 
106
        final LightUILine line = (LightUILine) element;
107
        this.elementMargin = line.elementMargin;
108
        this.elementPadding = line.elementPadding;
109
        this.gridAlignment = line.gridAlignment;
94 ilm 110
    }
111
 
93 ilm 112
    @Override
94 ilm 113
    public JSONObject toJSON() {
132 ilm 114
        final JSONObject result = super.toJSON();
94 ilm 115
        result.put("class", "LightUILine");
156 ilm 116
        if (this.elementPadding != 0) {
132 ilm 117
            result.put("element-padding", this.elementPadding);
118
        }
156 ilm 119
        if (this.elementMargin != 0) {
94 ilm 120
            result.put("element-margin", this.elementMargin);
121
        }
122
        if (this.gridAlignment != ALIGN_GRID) {
123
            result.put("grid-alignment", this.gridAlignment);
124
        }
125
        return result;
93 ilm 126
    }
94 ilm 127
 
128
    @Override
129
    public void fromJSON(final JSONObject json) {
132 ilm 130
        super.fromJSON(json);
156 ilm 131
        this.elementPadding = JSONConverter.getParameterFromJSON(json, "element-padding", Integer.class, 0);
132
        this.elementMargin = JSONConverter.getParameterFromJSON(json, "element-margin", Integer.class, 0);
144 ilm 133
        this.gridAlignment = JSONConverter.getParameterFromJSON(json, "grid-alignment", Integer.class, ALIGN_GRID);
94 ilm 134
    }
156 ilm 135
 
136
    @Override
137
    public void writeExternal(ObjectOutput out) throws IOException {
138
        super.writeExternal(out);
139
        out.writeInt(this.elementMargin);
140
        out.writeInt(this.elementPadding);
141
        out.writeByte(this.gridAlignment);
142
    }
143
 
144
    @Override
145
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
146
        super.readExternal(in);
147
        this.elementMargin = in.readInt();
148
        this.elementPadding = in.readInt();
149
        this.gridAlignment = in.readByte();
150
    }
151
 
152
    @Override
153
    public void addChild(LightUIElement child) {
154
        if (child instanceof LightUILine) {
155
            throw new IllegalArgumentException("child is a LightUILine");
156
        }
157
        super.addChild(child);
158
    }
73 ilm 159
}