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 |
|
132 |
ilm |
16 |
import org.openconcerto.utils.io.JSONConverter;
|
|
|
17 |
import org.openconcerto.utils.io.Transferable;
|
|
|
18 |
|
94 |
ilm |
19 |
import java.io.Externalizable;
|
|
|
20 |
import java.io.IOException;
|
|
|
21 |
import java.io.ObjectInput;
|
|
|
22 |
import java.io.ObjectOutput;
|
|
|
23 |
|
132 |
ilm |
24 |
import org.jdom2.Element;
|
|
|
25 |
|
94 |
ilm |
26 |
import net.minidev.json.JSONObject;
|
73 |
ilm |
27 |
|
94 |
ilm |
28 |
public class ColumnSpec implements Externalizable, Transferable {
|
142 |
ilm |
29 |
private static final String DEFAULT_VALUE = "default-value";
|
|
|
30 |
private static final String EDITORS = "editors";
|
|
|
31 |
private static final String VALUE_CLASS = "value-class";
|
|
|
32 |
private static final String ID = "id";
|
|
|
33 |
private static final String COLUMN_NAME = "column-name";
|
|
|
34 |
private static final String WIDTH = "width";
|
|
|
35 |
private static final String MAX_WIDTH = "max-width";
|
|
|
36 |
private static final String MIN_WIDTH = "min-width";
|
|
|
37 |
private static final String EDITABLE = "editable";
|
|
|
38 |
private static final String HORIZONTAL_ALIGNMENT = "h-align";
|
73 |
ilm |
39 |
// Must stay immutable
|
142 |
ilm |
40 |
private String id;
|
|
|
41 |
private String columnName;
|
73 |
ilm |
42 |
|
93 |
ilm |
43 |
private Class<?> valueClass;
|
142 |
ilm |
44 |
|
73 |
ilm |
45 |
// Default value (to add a new line)
|
|
|
46 |
private Object defaultValue;
|
142 |
ilm |
47 |
|
156 |
ilm |
48 |
private Integer horizontalAlignment = LightUIElement.HALIGN_LEFT;
|
142 |
ilm |
49 |
|
|
|
50 |
private double width;
|
|
|
51 |
private double maxWidth;
|
|
|
52 |
private double minWidth;
|
|
|
53 |
|
73 |
ilm |
54 |
private boolean editable;
|
142 |
ilm |
55 |
|
73 |
ilm |
56 |
private LightUIElement editors;
|
|
|
57 |
|
94 |
ilm |
58 |
public ColumnSpec() {
|
|
|
59 |
// Serialization
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public ColumnSpec(final JSONObject json) {
|
|
|
63 |
this.fromJSON(json);
|
|
|
64 |
}
|
|
|
65 |
|
142 |
ilm |
66 |
public ColumnSpec(final String id, final Class<?> valueClass, final String columnName, final Object defaultValue, final double width, final boolean editable, final LightUIElement editors) {
|
94 |
ilm |
67 |
this.init(id, valueClass, columnName, defaultValue, editable, editors);
|
|
|
68 |
this.width = width;
|
|
|
69 |
|
142 |
ilm |
70 |
final double minWidth = width - 200;
|
|
|
71 |
final double maxWidth = width + 200;
|
94 |
ilm |
72 |
|
132 |
ilm |
73 |
this.minWidth = (minWidth < 10) ? 10 : minWidth;
|
94 |
ilm |
74 |
this.maxWidth = maxWidth;
|
|
|
75 |
}
|
|
|
76 |
|
144 |
ilm |
77 |
public ColumnSpec(final String id, final Class<?> valueClass, final String columnName, final Object defaultValue) {
|
|
|
78 |
this.init(id, valueClass, columnName, defaultValue, false, null);
|
|
|
79 |
this.setDefaultPrefs();
|
|
|
80 |
}
|
|
|
81 |
|
94 |
ilm |
82 |
public ColumnSpec(final String id, final Class<?> valueClass, final String columnName, final Object defaultValue, final boolean editable, final LightUIElement editors) {
|
|
|
83 |
this.init(id, valueClass, columnName, defaultValue, editable, editors);
|
|
|
84 |
this.setDefaultPrefs();
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
private void init(final String id, final Class<?> valueClass, final String columnName, final Object defaultValue, final boolean editable, final LightUIElement editors) {
|
73 |
ilm |
88 |
this.id = id;
|
|
|
89 |
this.valueClass = valueClass;
|
|
|
90 |
this.columnName = columnName;
|
|
|
91 |
this.defaultValue = defaultValue;
|
|
|
92 |
this.editable = editable;
|
|
|
93 |
this.editors = editors;
|
|
|
94 |
}
|
|
|
95 |
|
142 |
ilm |
96 |
public void setPrefs(final double width, final double maxWidth, final double minWidth) {
|
94 |
ilm |
97 |
this.width = width;
|
|
|
98 |
this.maxWidth = maxWidth;
|
|
|
99 |
this.minWidth = minWidth;
|
|
|
100 |
}
|
|
|
101 |
|
73 |
ilm |
102 |
public String getId() {
|
93 |
ilm |
103 |
return this.id;
|
73 |
ilm |
104 |
}
|
|
|
105 |
|
|
|
106 |
public void setId(String id) {
|
|
|
107 |
this.id = id;
|
|
|
108 |
}
|
|
|
109 |
|
93 |
ilm |
110 |
public Class<?> getValueClass() {
|
|
|
111 |
return this.valueClass;
|
73 |
ilm |
112 |
}
|
|
|
113 |
|
93 |
ilm |
114 |
public void setValueClass(Class<?> valueClass) {
|
73 |
ilm |
115 |
this.valueClass = valueClass;
|
|
|
116 |
}
|
|
|
117 |
|
142 |
ilm |
118 |
public int getHorizontalAlignment() {
|
|
|
119 |
return this.horizontalAlignment;
|
|
|
120 |
}
|
|
|
121 |
|
156 |
ilm |
122 |
/**
|
|
|
123 |
*
|
|
|
124 |
* @param horizontalAlignment LightUIElement.HALIGN_LEFT, LightUIElement.HALIGN_RIGHT or
|
|
|
125 |
* LightUIElement.HALIGN_CENTER
|
|
|
126 |
*/
|
142 |
ilm |
127 |
public void setHorizontalAlignment(final int horizontalAlignment) {
|
|
|
128 |
this.horizontalAlignment = horizontalAlignment;
|
|
|
129 |
}
|
|
|
130 |
|
73 |
ilm |
131 |
public String getColumnName() {
|
93 |
ilm |
132 |
return this.columnName;
|
73 |
ilm |
133 |
}
|
|
|
134 |
|
|
|
135 |
public void setColumnName(String columnName) {
|
|
|
136 |
this.columnName = columnName;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
public Object getDefaultValue() {
|
93 |
ilm |
140 |
return this.defaultValue;
|
73 |
ilm |
141 |
}
|
|
|
142 |
|
|
|
143 |
public void setDefaultValue(Object defaultValue) {
|
|
|
144 |
this.defaultValue = defaultValue;
|
|
|
145 |
}
|
|
|
146 |
|
142 |
ilm |
147 |
public double getMaxWidth() {
|
94 |
ilm |
148 |
return this.maxWidth;
|
|
|
149 |
}
|
132 |
ilm |
150 |
|
142 |
ilm |
151 |
public void setMaxWidth(final double maxWidth) {
|
94 |
ilm |
152 |
this.maxWidth = maxWidth;
|
|
|
153 |
}
|
|
|
154 |
|
142 |
ilm |
155 |
public double getMinWidth() {
|
94 |
ilm |
156 |
return this.minWidth;
|
|
|
157 |
}
|
132 |
ilm |
158 |
|
142 |
ilm |
159 |
public void setMinWidth(final double minWidth) {
|
94 |
ilm |
160 |
this.minWidth = minWidth;
|
|
|
161 |
}
|
|
|
162 |
|
142 |
ilm |
163 |
public double getWidth() {
|
93 |
ilm |
164 |
return this.width;
|
73 |
ilm |
165 |
}
|
|
|
166 |
|
142 |
ilm |
167 |
public void setWidth(double width) {
|
73 |
ilm |
168 |
this.width = width;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
public boolean isEditable() {
|
93 |
ilm |
172 |
return this.editable;
|
73 |
ilm |
173 |
}
|
|
|
174 |
|
|
|
175 |
public void setEditable(boolean editable) {
|
|
|
176 |
this.editable = editable;
|
|
|
177 |
}
|
|
|
178 |
|
80 |
ilm |
179 |
public LightUIElement getEditor() {
|
93 |
ilm |
180 |
return this.editors;
|
73 |
ilm |
181 |
}
|
|
|
182 |
|
|
|
183 |
public void setEditors(LightUIElement editors) {
|
|
|
184 |
this.editors = editors;
|
|
|
185 |
}
|
|
|
186 |
|
94 |
ilm |
187 |
private void setDefaultPrefs() {
|
|
|
188 |
// TODO : Faire varier en fonction du type;
|
|
|
189 |
this.width = 200;
|
|
|
190 |
this.maxWidth = 500;
|
|
|
191 |
this.minWidth = 50;
|
156 |
ilm |
192 |
this.horizontalAlignment = LightUIElement.HALIGN_LEFT;
|
94 |
ilm |
193 |
}
|
|
|
194 |
|
132 |
ilm |
195 |
public Element createXmlColumnPref() {
|
|
|
196 |
final Element columnElement = new Element("column");
|
142 |
ilm |
197 |
columnElement.setAttribute(ID, this.getId());
|
|
|
198 |
columnElement.setAttribute(MAX_WIDTH, String.valueOf(this.getMaxWidth()));
|
|
|
199 |
columnElement.setAttribute(MIN_WIDTH, String.valueOf(this.getMinWidth()));
|
|
|
200 |
columnElement.setAttribute(WIDTH, String.valueOf(this.getWidth()));
|
132 |
ilm |
201 |
return columnElement;
|
|
|
202 |
}
|
|
|
203 |
|
93 |
ilm |
204 |
@Override
|
94 |
ilm |
205 |
public void writeExternal(ObjectOutput out) throws IOException {
|
|
|
206 |
out.writeUTF(this.id);
|
|
|
207 |
out.writeUTF(this.columnName);
|
142 |
ilm |
208 |
out.writeDouble(this.width);
|
|
|
209 |
out.writeDouble(this.maxWidth);
|
|
|
210 |
out.writeDouble(this.minWidth);
|
94 |
ilm |
211 |
out.writeObject(this.defaultValue);
|
|
|
212 |
out.writeBoolean(this.editable);
|
|
|
213 |
out.writeObject(this.editors);
|
|
|
214 |
out.writeObject(this.valueClass);
|
142 |
ilm |
215 |
out.writeInt(this.horizontalAlignment);
|
93 |
ilm |
216 |
}
|
|
|
217 |
|
94 |
ilm |
218 |
@Override
|
|
|
219 |
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
|
|
220 |
this.id = in.readUTF();
|
|
|
221 |
this.columnName = in.readUTF();
|
142 |
ilm |
222 |
this.width = in.readDouble();
|
|
|
223 |
this.maxWidth = in.readDouble();
|
|
|
224 |
this.minWidth = in.readDouble();
|
94 |
ilm |
225 |
this.defaultValue = in.readObject();
|
|
|
226 |
this.editable = in.readBoolean();
|
|
|
227 |
this.editors = (LightUIElement) in.readObject();
|
|
|
228 |
this.valueClass = (Class<?>) in.readObject();
|
142 |
ilm |
229 |
this.horizontalAlignment = in.readInt();
|
94 |
ilm |
230 |
}
|
|
|
231 |
|
|
|
232 |
@Override
|
|
|
233 |
public JSONObject toJSON() {
|
|
|
234 |
final JSONObject result = new JSONObject();
|
|
|
235 |
result.put("class", "ColumnSpec");
|
142 |
ilm |
236 |
result.put(ID, this.id);
|
|
|
237 |
result.put(COLUMN_NAME, this.columnName);
|
|
|
238 |
result.put(WIDTH, this.width);
|
|
|
239 |
result.put(MAX_WIDTH, this.maxWidth);
|
|
|
240 |
result.put(MIN_WIDTH, this.minWidth);
|
132 |
ilm |
241 |
if (this.defaultValue != null) {
|
142 |
ilm |
242 |
result.put(DEFAULT_VALUE, JSONConverter.getJSON(this.defaultValue));
|
94 |
ilm |
243 |
}
|
|
|
244 |
if (this.editable) {
|
142 |
ilm |
245 |
result.put(EDITABLE, true);
|
94 |
ilm |
246 |
}
|
132 |
ilm |
247 |
if (this.editors != null) {
|
142 |
ilm |
248 |
result.put(EDITORS, JSONConverter.getJSON(this.editors));
|
94 |
ilm |
249 |
}
|
142 |
ilm |
250 |
result.put(VALUE_CLASS, JSONConverter.getJSON(this.valueClass));
|
|
|
251 |
|
156 |
ilm |
252 |
if (this.horizontalAlignment != LightUIElement.HALIGN_LEFT) {
|
142 |
ilm |
253 |
result.put(HORIZONTAL_ALIGNMENT, this.horizontalAlignment);
|
|
|
254 |
}
|
94 |
ilm |
255 |
return result;
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
@Override
|
|
|
259 |
public void fromJSON(final JSONObject json) {
|
142 |
ilm |
260 |
this.id = JSONConverter.getParameterFromJSON(json, ID, String.class);
|
|
|
261 |
this.columnName = JSONConverter.getParameterFromJSON(json, COLUMN_NAME, String.class);
|
94 |
ilm |
262 |
|
142 |
ilm |
263 |
/** JavaScript convert Double to Long, this will fix that. **/
|
|
|
264 |
final Number numWidth = JSONConverter.getParameterFromJSON(json, WIDTH, Number.class);
|
|
|
265 |
final Number numMaxWidth = JSONConverter.getParameterFromJSON(json, MAX_WIDTH, Number.class);
|
|
|
266 |
final Number numMinWidth = JSONConverter.getParameterFromJSON(json, MIN_WIDTH, Number.class);
|
|
|
267 |
if (numWidth != null) {
|
|
|
268 |
this.width = numWidth.doubleValue();
|
94 |
ilm |
269 |
}
|
142 |
ilm |
270 |
if (numMaxWidth != null) {
|
|
|
271 |
this.maxWidth = numMaxWidth.doubleValue();
|
|
|
272 |
}
|
|
|
273 |
if (numMinWidth != null) {
|
|
|
274 |
this.minWidth = numMinWidth.doubleValue();
|
|
|
275 |
}
|
|
|
276 |
/************************************************************/
|
|
|
277 |
|
|
|
278 |
this.editable = JSONConverter.getParameterFromJSON(json, EDITABLE, Boolean.class, Boolean.FALSE);
|
|
|
279 |
this.horizontalAlignment = JSONConverter.getParameterFromJSON(json, HORIZONTAL_ALIGNMENT, Integer.class);
|
156 |
ilm |
280 |
if (this.horizontalAlignment == null) {
|
|
|
281 |
this.horizontalAlignment = LightUIElement.HALIGN_LEFT;
|
|
|
282 |
}
|
142 |
ilm |
283 |
final JSONObject jsonDefaultValue = JSONConverter.getParameterFromJSON(json, DEFAULT_VALUE, JSONObject.class);
|
|
|
284 |
// TODO: implement default value
|
|
|
285 |
|
|
|
286 |
final JSONObject jsonEditors = JSONConverter.getParameterFromJSON(json, EDITORS, JSONObject.class);
|
94 |
ilm |
287 |
if (jsonEditors != null) {
|
132 |
ilm |
288 |
this.editors = JSONToLightUIConvertorManager.getInstance().createUIElementFromJSON(jsonEditors);
|
94 |
ilm |
289 |
}
|
142 |
ilm |
290 |
|
|
|
291 |
final String sValueClass = JSONConverter.getParameterFromJSON(json, VALUE_CLASS, String.class);
|
94 |
ilm |
292 |
if (sValueClass != null) {
|
|
|
293 |
try {
|
|
|
294 |
this.valueClass = Class.forName(sValueClass);
|
|
|
295 |
} catch (Exception ex) {
|
|
|
296 |
throw new IllegalArgumentException("invalid value for 'value-class', " + ex.getMessage());
|
|
|
297 |
}
|
|
|
298 |
}
|
|
|
299 |
}
|
73 |
ilm |
300 |
}
|