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.i18n.TranslationManager;
|
|
|
17 |
import org.openconcerto.utils.io.JSONConverter;
|
|
|
18 |
|
156 |
ilm |
19 |
import java.io.IOException;
|
|
|
20 |
import java.io.ObjectInput;
|
|
|
21 |
import java.io.ObjectOutput;
|
132 |
ilm |
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 LightUIComboBox extends LightUserControl {
|
144 |
ilm |
29 |
private static final String KEY_HAS_NOT_SPECIFIED_LINE = "has-not-specified-line";
|
|
|
30 |
private static final String KEY_VALUES = "values";
|
|
|
31 |
private static final String KEY_SELECTED_VALUE = "selected-value";
|
|
|
32 |
private static final String KEY_ALREADY_FILLED = "already-filled";
|
132 |
ilm |
33 |
|
142 |
ilm |
34 |
private boolean alreadyFilled = false;
|
|
|
35 |
private boolean hasNotSpecifedLine = false;
|
132 |
ilm |
36 |
|
142 |
ilm |
37 |
private LightUIComboBoxElement selectedValue = null;
|
132 |
ilm |
38 |
|
156 |
ilm |
39 |
private List<LightUIComboBoxElement> values = new ArrayList<>();
|
132 |
ilm |
40 |
|
156 |
ilm |
41 |
public LightUIComboBox() {
|
|
|
42 |
// Serialization
|
|
|
43 |
}
|
|
|
44 |
|
132 |
ilm |
45 |
// Init from json constructor
|
|
|
46 |
public LightUIComboBox(final JSONObject json) {
|
|
|
47 |
super(json);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
// Clone constructor
|
|
|
51 |
public LightUIComboBox(final LightUIComboBox combo) {
|
|
|
52 |
super(combo);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public LightUIComboBox(final String id) {
|
|
|
56 |
super(id);
|
|
|
57 |
this.setType(TYPE_COMBOBOX);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public void addValue(final LightUIComboBoxElement values) {
|
|
|
61 |
this.values.add(values);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public void addValues(final List<LightUIComboBoxElement> values) {
|
|
|
65 |
this.values.addAll(values);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public static LightUIComboBoxElement getDefaultValue() {
|
|
|
69 |
final String defaultLabelKey = "not.specified.label";
|
|
|
70 |
final String defaultLabel = TranslationManager.getInstance().getTranslationForItem(defaultLabelKey);
|
|
|
71 |
|
|
|
72 |
final LightUIComboBoxElement defaultElement = new LightUIComboBoxElement(0);
|
|
|
73 |
if (defaultLabel != null) {
|
|
|
74 |
defaultElement.setValue1(defaultLabel);
|
|
|
75 |
} else {
|
|
|
76 |
defaultElement.setValue1(defaultLabelKey);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return defaultElement;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public List<LightUIComboBoxElement> getValues() {
|
|
|
83 |
return this.values;
|
|
|
84 |
}
|
|
|
85 |
|
142 |
ilm |
86 |
// if id=0 means this is the not specifed line
|
132 |
ilm |
87 |
public boolean hasSelectedValue() {
|
142 |
ilm |
88 |
return this.selectedValue != null && ((this.hasNotSpecifedLine && this.selectedValue.getId() != 0) || !this.hasNotSpecifedLine);
|
132 |
ilm |
89 |
}
|
|
|
90 |
|
|
|
91 |
public LightUIComboBoxElement getSelectedValue() {
|
|
|
92 |
return this.selectedValue;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public void setSelectedValue(final LightUIComboBoxElement selectedValue) {
|
|
|
96 |
this.selectedValue = selectedValue;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public void clearValues() {
|
|
|
100 |
this.selectedValue = null;
|
|
|
101 |
this.values.clear();
|
|
|
102 |
}
|
|
|
103 |
|
142 |
ilm |
104 |
public void setAlreadyFilled(final boolean alreadyFilled) {
|
|
|
105 |
this.alreadyFilled = alreadyFilled;
|
132 |
ilm |
106 |
}
|
|
|
107 |
|
142 |
ilm |
108 |
public boolean isAlreadyFilled() {
|
|
|
109 |
return this.alreadyFilled;
|
132 |
ilm |
110 |
}
|
|
|
111 |
|
142 |
ilm |
112 |
public void setHasNotSpecifedLine(final boolean hasNotSpecifedLine) {
|
|
|
113 |
this.hasNotSpecifedLine = hasNotSpecifedLine;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
public boolean hasNotSpecifedLine() {
|
|
|
117 |
return this.hasNotSpecifedLine;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
public void setSelectedId(final Integer id) {
|
|
|
121 |
if (id == null) {
|
|
|
122 |
this.setSelectedValue(null);
|
|
|
123 |
} else {
|
|
|
124 |
for (final LightUIComboBoxElement value : this.values) {
|
|
|
125 |
if (value.getId() == id) {
|
|
|
126 |
this.setSelectedValue(value);
|
|
|
127 |
break;
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
132 |
ilm |
133 |
@Override
|
|
|
134 |
protected void copy(LightUIElement element) {
|
|
|
135 |
super.copy(element);
|
|
|
136 |
|
|
|
137 |
if (!(element instanceof LightUIComboBox)) {
|
|
|
138 |
throw new InvalidClassException(this.getClassName(), element.getClassName(), element.getId());
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
final LightUIComboBox combo = (LightUIComboBox) element;
|
142 |
ilm |
142 |
this.alreadyFilled = combo.alreadyFilled;
|
132 |
ilm |
143 |
this.values = combo.values;
|
|
|
144 |
this.selectedValue = combo.selectedValue;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
@Override
|
|
|
148 |
public JSONObject toJSON() {
|
|
|
149 |
final JSONObject json = super.toJSON();
|
|
|
150 |
|
144 |
ilm |
151 |
if (this.values != null && !this.values.isEmpty()) {
|
132 |
ilm |
152 |
final JSONArray jsonValues = new JSONArray();
|
|
|
153 |
for (final LightUIComboBoxElement value : this.values) {
|
|
|
154 |
jsonValues.add(value.toJSON());
|
|
|
155 |
}
|
144 |
ilm |
156 |
json.put(KEY_VALUES, jsonValues);
|
132 |
ilm |
157 |
}
|
|
|
158 |
|
142 |
ilm |
159 |
if (this.alreadyFilled) {
|
144 |
ilm |
160 |
json.put(KEY_ALREADY_FILLED, true);
|
132 |
ilm |
161 |
}
|
142 |
ilm |
162 |
if (this.hasNotSpecifedLine) {
|
144 |
ilm |
163 |
json.put(KEY_HAS_NOT_SPECIFIED_LINE, true);
|
132 |
ilm |
164 |
}
|
|
|
165 |
if (this.selectedValue != null) {
|
144 |
ilm |
166 |
json.put(KEY_SELECTED_VALUE, this.selectedValue.toJSON());
|
132 |
ilm |
167 |
}
|
|
|
168 |
|
|
|
169 |
return json;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
@Override
|
|
|
173 |
public void fromJSON(final JSONObject json) {
|
|
|
174 |
super.fromJSON(json);
|
|
|
175 |
|
144 |
ilm |
176 |
this.alreadyFilled = JSONConverter.getParameterFromJSON(json, KEY_ALREADY_FILLED, Boolean.class, false);
|
|
|
177 |
this.hasNotSpecifedLine = JSONConverter.getParameterFromJSON(json, KEY_HAS_NOT_SPECIFIED_LINE, Boolean.class, false);
|
142 |
ilm |
178 |
|
|
|
179 |
final JSONObject jsonSelectedValue = JSONConverter.getParameterFromJSON(json, "", JSONObject.class);
|
132 |
ilm |
180 |
if (jsonSelectedValue != null) {
|
|
|
181 |
this.selectedValue = new LightUIComboBoxElement(jsonSelectedValue);
|
|
|
182 |
}
|
|
|
183 |
|
144 |
ilm |
184 |
final JSONArray jsonValues = JSONConverter.getParameterFromJSON(json, KEY_VALUES, JSONArray.class);
|
132 |
ilm |
185 |
this.values = new ArrayList<LightUIComboBoxElement>();
|
|
|
186 |
if (jsonValues != null) {
|
|
|
187 |
for (final Object jsonValue : jsonValues) {
|
142 |
ilm |
188 |
this.values.add(new LightUIComboBoxElement(JSONConverter.getObjectFromJSON(jsonValue, JSONObject.class)));
|
132 |
ilm |
189 |
}
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
@Override
|
142 |
ilm |
194 |
public Object getValueForContext() {
|
132 |
ilm |
195 |
if (this.hasSelectedValue()) {
|
|
|
196 |
return this.getSelectedValue();
|
|
|
197 |
} else {
|
|
|
198 |
return null;
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
@Override
|
142 |
ilm |
203 |
public void _setValueFromContext(Object value) {
|
132 |
ilm |
204 |
if (value != null) {
|
142 |
ilm |
205 |
final JSONObject jsonSelectedValue = JSONConverter.getObjectFromJSON(value, JSONObject.class);
|
132 |
ilm |
206 |
this.selectedValue = new LightUIComboBoxElement(jsonSelectedValue);
|
|
|
207 |
} else {
|
|
|
208 |
this.selectedValue = null;
|
|
|
209 |
}
|
|
|
210 |
}
|
142 |
ilm |
211 |
|
|
|
212 |
@Override
|
|
|
213 |
public void destroy() {
|
|
|
214 |
super.destroy();
|
156 |
ilm |
215 |
this.selectedValue = null;
|
|
|
216 |
this.values = null;
|
142 |
ilm |
217 |
}
|
156 |
ilm |
218 |
|
|
|
219 |
@Override
|
|
|
220 |
public void writeExternal(ObjectOutput out) throws IOException {
|
|
|
221 |
super.writeExternal(out);
|
|
|
222 |
out.writeBoolean(this.alreadyFilled);
|
|
|
223 |
out.writeBoolean(this.hasNotSpecifedLine);
|
|
|
224 |
|
|
|
225 |
if (this.selectedValue != null) {
|
|
|
226 |
out.writeBoolean(true);
|
|
|
227 |
this.selectedValue.writeExternal(out);
|
|
|
228 |
} else {
|
|
|
229 |
out.writeBoolean(false);
|
|
|
230 |
}
|
|
|
231 |
out.writeInt(this.values.size());
|
|
|
232 |
for (LightUIComboBoxElement e : this.values) {
|
|
|
233 |
e.writeExternal(out);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
@Override
|
|
|
239 |
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
|
|
240 |
super.readExternal(in);
|
|
|
241 |
this.alreadyFilled = in.readBoolean();
|
|
|
242 |
this.hasNotSpecifedLine = in.readBoolean();
|
|
|
243 |
if (in.readBoolean()) {
|
|
|
244 |
this.selectedValue = new LightUIComboBoxElement();
|
|
|
245 |
this.selectedValue.readExternal(in);
|
|
|
246 |
}
|
|
|
247 |
int size = in.readInt();
|
|
|
248 |
this.values.clear();
|
|
|
249 |
for (int i = 0; i < size; i++) {
|
|
|
250 |
LightUIComboBoxElement e = new LightUIComboBoxElement();
|
|
|
251 |
e.readExternal(in);
|
|
|
252 |
this.values.add(e);
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
}
|
132 |
ilm |
256 |
}
|