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 |
|
132 |
ilm |
16 |
import org.openconcerto.utils.io.JSONConverter;
|
|
|
17 |
|
|
|
18 |
import java.awt.event.ActionEvent;
|
|
|
19 |
import java.awt.event.ActionListener;
|
|
|
20 |
import java.util.ArrayList;
|
94 |
ilm |
21 |
import java.util.List;
|
|
|
22 |
|
132 |
ilm |
23 |
import net.minidev.json.JSONArray;
|
94 |
ilm |
24 |
import net.minidev.json.JSONObject;
|
|
|
25 |
|
132 |
ilm |
26 |
public class LightUITable extends LightUIElement implements IUserControlContainer {
|
|
|
27 |
private Boolean dynamicLoad = false;
|
94 |
ilm |
28 |
private Boolean verticallyScrollable = false;
|
132 |
ilm |
29 |
private Boolean allowSelection = false;
|
94 |
ilm |
30 |
private TableSpec tableSpec = null;
|
132 |
ilm |
31 |
private String elementCode = null;
|
94 |
ilm |
32 |
|
132 |
ilm |
33 |
private List<ActionListener> selectionListeners = new ArrayList<ActionListener>();
|
|
|
34 |
|
|
|
35 |
// Nombre de ligne à afficher par Row
|
|
|
36 |
private int linePerRow = 1;
|
|
|
37 |
|
94 |
ilm |
38 |
// Init from json constructor
|
|
|
39 |
public LightUITable(final JSONObject json) {
|
132 |
ilm |
40 |
super(json);
|
94 |
ilm |
41 |
}
|
132 |
ilm |
42 |
|
94 |
ilm |
43 |
// Clone constructor
|
|
|
44 |
public LightUITable(final LightUITable tableElement) {
|
|
|
45 |
super(tableElement);
|
|
|
46 |
this.verticallyScrollable = tableElement.verticallyScrollable;
|
|
|
47 |
this.tableSpec = tableElement.tableSpec;
|
132 |
ilm |
48 |
this.elementCode = tableElement.elementCode;
|
|
|
49 |
this.allowSelection = tableElement.allowSelection;
|
94 |
ilm |
50 |
}
|
132 |
ilm |
51 |
|
94 |
ilm |
52 |
public LightUITable(final String id) {
|
132 |
ilm |
53 |
super(id);
|
94 |
ilm |
54 |
this.setType(LightUIElement.TYPE_TABLE);
|
132 |
ilm |
55 |
|
|
|
56 |
this.setWeightX(1);
|
94 |
ilm |
57 |
this.setFillWidth(true);
|
|
|
58 |
}
|
|
|
59 |
|
132 |
ilm |
60 |
public int getIndexfromRowID(int rowID) {
|
|
|
61 |
final TableContent content = this.getTableSpec().getContent();
|
|
|
62 |
|
|
|
63 |
for (int i = 0; i < content.getRows().size(); i++) {
|
|
|
64 |
Row r = content.getRows().get(i);
|
|
|
65 |
if (r.getId() == rowID) {
|
|
|
66 |
return i;
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
return -1;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
@Override
|
|
|
73 |
public void setId(final String id) {
|
|
|
74 |
super.setId(id);
|
|
|
75 |
this.tableSpec.setId(id);
|
|
|
76 |
this.tableSpec.getSelection().setTableId(id);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public String getElementCode() {
|
|
|
80 |
return this.elementCode;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public void setElementCode(final String elementCode) {
|
|
|
84 |
this.elementCode = elementCode;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public void setLinePerRow(int linePerRow) {
|
|
|
88 |
this.linePerRow = linePerRow;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public int getLinePerRow() {
|
|
|
92 |
return this.linePerRow;
|
|
|
93 |
}
|
|
|
94 |
|
94 |
ilm |
95 |
public TableSpec getTableSpec() {
|
|
|
96 |
return this.tableSpec;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public void setTableSpec(final TableSpec tableSpec) {
|
|
|
100 |
this.tableSpec = tableSpec;
|
|
|
101 |
}
|
132 |
ilm |
102 |
|
|
|
103 |
public Boolean isAllowSelection() {
|
|
|
104 |
return this.allowSelection;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public void setAllowSelection(final boolean allowSelection) {
|
|
|
108 |
this.allowSelection = allowSelection;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public Boolean isDynamicLoad() {
|
|
|
112 |
return this.dynamicLoad;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public void setDynamicLoad(final boolean dynamicLoad) {
|
|
|
116 |
this.dynamicLoad = dynamicLoad;
|
|
|
117 |
}
|
|
|
118 |
|
94 |
ilm |
119 |
public Boolean isVerticallyScrollable() {
|
|
|
120 |
return this.verticallyScrollable;
|
|
|
121 |
}
|
132 |
ilm |
122 |
|
94 |
ilm |
123 |
public void setVerticallyScrollable(final Boolean verticallyScrollable) {
|
|
|
124 |
this.verticallyScrollable = verticallyScrollable;
|
|
|
125 |
}
|
132 |
ilm |
126 |
|
|
|
127 |
public boolean replaceChild(final LightUIElement pChild) {
|
|
|
128 |
final List<Row> tableRows = this.getTableSpec().getContent().getRows();
|
|
|
129 |
final int tableRowsCount = tableRows.size();
|
|
|
130 |
pChild.setReadOnly(this.isReadOnly());
|
|
|
131 |
|
|
|
132 |
for (int i = 0; i < tableRowsCount; i++) {
|
|
|
133 |
final Row tableRow = tableRows.get(i);
|
|
|
134 |
final List<Object> tableRowValues = tableRow.getValues();
|
|
|
135 |
final int tableRowValuesCount = tableRowValues.size();
|
|
|
136 |
|
|
|
137 |
for (int j = 0; j < tableRowValuesCount; j++) {
|
|
|
138 |
final Object tableRowValue = tableRowValues.get(j);
|
|
|
139 |
if (tableRowValue instanceof LightUIElement) {
|
|
|
140 |
final LightUIElement child = (LightUIElement) tableRowValue;
|
|
|
141 |
|
|
|
142 |
if (child.getId().equals(pChild.getId())) {
|
|
|
143 |
tableRowValues.set(i, pChild);
|
|
|
144 |
child.setParent(this);
|
|
|
145 |
return true;
|
|
|
146 |
}
|
|
|
147 |
if (child instanceof LightUIContainer) {
|
|
|
148 |
if (((LightUIContainer) child).replaceChild(pChild)) {
|
|
|
149 |
return true;
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
if (child instanceof LightUITable) {
|
|
|
153 |
if (((LightUITable) child).replaceChild(pChild)) {
|
|
|
154 |
return true;
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
return false;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
public LightUIElement findElement(final String searchParam, final boolean byUUID) {
|
|
|
164 |
return this.findElement(searchParam, byUUID, LightUIElement.class);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
public <T extends LightUIElement> T findElement(final String searchParam, final boolean byUUID, final Class<T> objectClass) {
|
|
|
168 |
if (this.tableSpec != null) {
|
94 |
ilm |
169 |
final TableContent content = this.tableSpec.getContent();
|
132 |
ilm |
170 |
if (content != null) {
|
94 |
ilm |
171 |
final List<Row> listRows = content.getRows();
|
132 |
ilm |
172 |
if (listRows != null) {
|
|
|
173 |
for (final Row row : listRows) {
|
94 |
ilm |
174 |
final List<Object> rowValues = row.getValues();
|
132 |
ilm |
175 |
for (final Object value : rowValues) {
|
|
|
176 |
if (value instanceof LightUIContainer) {
|
|
|
177 |
final LightUIContainer panel = (LightUIContainer) value;
|
|
|
178 |
final T element = panel.findChild(searchParam, byUUID, objectClass);
|
|
|
179 |
if (element != null) {
|
94 |
ilm |
180 |
return element;
|
|
|
181 |
}
|
132 |
ilm |
182 |
} else if (value instanceof LightUIElement) {
|
|
|
183 |
final LightUIElement element = (LightUIElement) value;
|
|
|
184 |
if (byUUID) {
|
|
|
185 |
if (element.getUUID().equals(searchParam)) {
|
|
|
186 |
if (objectClass.isAssignableFrom(element.getClass())) {
|
|
|
187 |
return (T) element;
|
|
|
188 |
} else {
|
|
|
189 |
throw new IllegalArgumentException("Element found at is not an instance of " + objectClass.getName() + ", element class: " + element.getClass().getName()
|
|
|
190 |
+ " element ID: " + element.getId());
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
} else {
|
|
|
194 |
if (element.getId().equals(searchParam)) {
|
|
|
195 |
if (objectClass.isAssignableFrom(element.getClass())) {
|
|
|
196 |
return (T) element;
|
|
|
197 |
} else {
|
|
|
198 |
throw new IllegalArgumentException("Element found at is not an instance of " + objectClass.getName() + ", element class: " + element.getClass().getName()
|
|
|
199 |
+ " element ID: " + element.getId());
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
if (element instanceof LightUITable) {
|
|
|
205 |
final T resultElement = ((LightUITable) element).findElement(searchParam, byUUID, objectClass);
|
|
|
206 |
if (resultElement != null) {
|
|
|
207 |
return resultElement;
|
|
|
208 |
}
|
|
|
209 |
}
|
94 |
ilm |
210 |
}
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
} else {
|
|
|
214 |
System.out.println("LightUITable.getElementById() - No rows for table: " + this.getId());
|
|
|
215 |
}
|
|
|
216 |
} else {
|
|
|
217 |
System.out.println("LightUITable.getElementById() - Null TableContent for table: " + this.getId());
|
|
|
218 |
}
|
|
|
219 |
} else {
|
|
|
220 |
System.out.println("LightUITable.getElementById() - Null TableSpec for table: " + this.getId());
|
|
|
221 |
}
|
|
|
222 |
return null;
|
|
|
223 |
}
|
|
|
224 |
|
132 |
ilm |
225 |
public void addSelectionListener(final ActionListener selectionListener) {
|
|
|
226 |
this.selectionListeners.add(selectionListener);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
public void removeSelectionListeners() {
|
|
|
230 |
this.selectionListeners.clear();
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
public void fireSelectionChange() {
|
|
|
234 |
for (final ActionListener listener : this.selectionListeners) {
|
|
|
235 |
listener.actionPerformed(new ActionEvent(this, 1, "selection"));
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
@Override
|
|
|
240 |
public void setReadOnly(final boolean readOnly) {
|
|
|
241 |
super.setReadOnly(readOnly);
|
|
|
242 |
final List<Row> rows = this.tableSpec.getContent().getRows();
|
|
|
243 |
if (rows != null) {
|
|
|
244 |
final int rowCount = rows.size();
|
|
|
245 |
for (int i = 0; i < rowCount; i++) {
|
|
|
246 |
final Row row = rows.get(i);
|
|
|
247 |
final List<Object> values = row.getValues();
|
|
|
248 |
for (final Object value : values) {
|
|
|
249 |
if (value != null && value instanceof LightUIElement) {
|
|
|
250 |
((LightUIElement) value).setReadOnly(readOnly);
|
|
|
251 |
}
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
@Override
|
|
|
258 |
public JSONToLightUIConvertor getConvertor() {
|
|
|
259 |
return new JSONToLightUIConvertor() {
|
|
|
260 |
@Override
|
|
|
261 |
public LightUIElement convert(final JSONObject json) {
|
|
|
262 |
return new LightUITable(json);
|
|
|
263 |
}
|
|
|
264 |
};
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
@Override
|
|
|
268 |
public void setValueFromContext(final Object value) {
|
|
|
269 |
if (value != null) {
|
|
|
270 |
final JSONArray jsonContext = (JSONArray) JSONConverter.getObjectFromJSON(value, JSONArray.class);
|
|
|
271 |
final ColumnsSpec columnsSpec = this.getTableSpec().getColumns();
|
|
|
272 |
final int columnsCount = columnsSpec.getColumnCount();
|
|
|
273 |
|
|
|
274 |
final List<Integer> editorsIndex = new ArrayList<Integer>();
|
|
|
275 |
|
|
|
276 |
for (int i = 0; i < columnsCount; i++) {
|
|
|
277 |
final ColumnSpec columnSpec = columnsSpec.getColumn(i);
|
|
|
278 |
if (columnSpec.getEditor() != null) {
|
|
|
279 |
editorsIndex.add(i);
|
|
|
280 |
}
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
final TableContent tableContent = this.getTableSpec().getContent();
|
|
|
284 |
if (tableContent != null) {
|
|
|
285 |
final List<Row> rows = tableContent.getRows();
|
|
|
286 |
for (int i = 0; i < rows.size(); i++) {
|
|
|
287 |
final Row row = rows.get(i);
|
|
|
288 |
final JSONObject jsonLineContext = (JSONObject) JSONConverter.getObjectFromJSON(jsonContext.get(i), JSONObject.class);
|
|
|
289 |
final Long rowId = (Long) JSONConverter.getParameterFromJSON(jsonLineContext, "row.id", Long.class);
|
|
|
290 |
final String rowExtendId = (String) JSONConverter.getParameterFromJSON(jsonLineContext, "row.extend.id", String.class);
|
|
|
291 |
if (rowId == row.getId() && (row.getExtendId() == null || (row.getExtendId() != null && rowExtendId.equals(row.getExtendId())))) {
|
|
|
292 |
if (row.isFillWidth()) {
|
|
|
293 |
if (!row.getValues().isEmpty() && row.getValues().get(0) instanceof IUserControl) {
|
|
|
294 |
final LightUIElement element = (LightUIElement) row.getValues().get(0);
|
|
|
295 |
if (element instanceof IUserControl) {
|
|
|
296 |
if (jsonLineContext.containsKey(element.getUUID())) {
|
|
|
297 |
((IUserControl) element).setValueFromContext(jsonLineContext.get(element.getUUID()));
|
|
|
298 |
} else {
|
|
|
299 |
System.out.println("LightUITable.setValueFromContext() - Unable to find element : id - " + element.getId() + " uuid - " + element.getUUID());
|
|
|
300 |
System.out.println("LightUITable.setValueFromContext() - In JSON : " + jsonLineContext.toJSONString());
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
} else {
|
|
|
305 |
for (int k = 0; k < editorsIndex.size(); k++) {
|
|
|
306 |
final Object objEditor = row.getValues().get(editorsIndex.get(k));
|
|
|
307 |
if (!(objEditor instanceof IUserControl)) {
|
|
|
308 |
throw new IllegalArgumentException("Impossible to find editor for row: " + rowId.toString() + " at position: " + String.valueOf(k));
|
|
|
309 |
}
|
|
|
310 |
final LightUIElement editor = (LightUIElement) objEditor;
|
|
|
311 |
|
|
|
312 |
if (editor instanceof IUserControl && jsonLineContext.containsKey(editor.getUUID())) {
|
|
|
313 |
((IUserControl) editor).setValueFromContext(jsonLineContext.get(editor.getUUID()));
|
|
|
314 |
} else {
|
|
|
315 |
throw new IllegalArgumentException(
|
|
|
316 |
"Impossible to find value for editor: " + editor.getId() + " for row: " + rowId.toString() + " at position: " + String.valueOf(k));
|
|
|
317 |
}
|
|
|
318 |
}
|
|
|
319 |
}
|
|
|
320 |
} else {
|
|
|
321 |
throw new IllegalArgumentException("Impossible to find row: " + rowId.toString());
|
|
|
322 |
}
|
|
|
323 |
}
|
|
|
324 |
}
|
|
|
325 |
}
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
@Override
|
94 |
ilm |
329 |
public LightUIElement clone() {
|
|
|
330 |
return new LightUITable(this);
|
|
|
331 |
}
|
132 |
ilm |
332 |
|
94 |
ilm |
333 |
@Override
|
|
|
334 |
public JSONObject toJSON() {
|
|
|
335 |
final JSONObject json = super.toJSON();
|
132 |
ilm |
336 |
if (this.allowSelection) {
|
|
|
337 |
json.put("allow-selection", true);
|
|
|
338 |
}
|
|
|
339 |
if (this.dynamicLoad) {
|
|
|
340 |
json.put("dynamic-load", true);
|
|
|
341 |
}
|
|
|
342 |
if (this.verticallyScrollable) {
|
94 |
ilm |
343 |
json.put("vertically-scrollable", true);
|
|
|
344 |
}
|
132 |
ilm |
345 |
if (this.tableSpec != null) {
|
94 |
ilm |
346 |
json.put("table-spec", this.tableSpec.toJSON());
|
|
|
347 |
}
|
132 |
ilm |
348 |
if (this.elementCode != null) {
|
|
|
349 |
json.put("element-code", this.elementCode);
|
|
|
350 |
}
|
|
|
351 |
json.put("line-per-row", this.linePerRow);
|
94 |
ilm |
352 |
return json;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
@Override
|
|
|
356 |
public void fromJSON(final JSONObject json) {
|
|
|
357 |
super.fromJSON(json);
|
132 |
ilm |
358 |
this.allowSelection = JSONConverter.getParameterFromJSON(json, "allow-selection", Boolean.class, false);
|
|
|
359 |
this.dynamicLoad = JSONConverter.getParameterFromJSON(json, "dynamic-load", Boolean.class, false);
|
|
|
360 |
this.verticallyScrollable = JSONConverter.getParameterFromJSON(json, "vertically-scrollable", Boolean.class, false);
|
|
|
361 |
this.elementCode = JSONConverter.getParameterFromJSON(json, "element-code", String.class);
|
|
|
362 |
this.linePerRow = JSONConverter.getParameterFromJSON(json, "line-per-row", Integer.class);
|
|
|
363 |
|
94 |
ilm |
364 |
final JSONObject jsonRawContent = (JSONObject) JSONConverter.getParameterFromJSON(json, "table-spec", JSONObject.class);
|
132 |
ilm |
365 |
|
94 |
ilm |
366 |
if (jsonRawContent != null) {
|
|
|
367 |
this.tableSpec = new TableSpec(jsonRawContent);
|
|
|
368 |
}
|
|
|
369 |
}
|
|
|
370 |
}
|