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 |
|
142 |
ilm |
16 |
import org.openconcerto.utils.NumberUtils;
|
132 |
ilm |
17 |
import org.openconcerto.utils.io.JSONConverter;
|
|
|
18 |
|
|
|
19 |
import java.awt.event.ActionEvent;
|
|
|
20 |
import java.awt.event.ActionListener;
|
156 |
ilm |
21 |
import java.io.Externalizable;
|
|
|
22 |
import java.io.IOException;
|
|
|
23 |
import java.io.ObjectInput;
|
|
|
24 |
import java.io.ObjectOutput;
|
132 |
ilm |
25 |
import java.util.ArrayList;
|
94 |
ilm |
26 |
import java.util.List;
|
|
|
27 |
|
142 |
ilm |
28 |
import javax.xml.parsers.ParserConfigurationException;
|
|
|
29 |
|
|
|
30 |
import org.jdom2.Document;
|
|
|
31 |
import org.jdom2.Element;
|
|
|
32 |
|
132 |
ilm |
33 |
import net.minidev.json.JSONArray;
|
94 |
ilm |
34 |
import net.minidev.json.JSONObject;
|
|
|
35 |
|
156 |
ilm |
36 |
public class LightUITable extends LightUserControlContainer implements Externalizable {
|
142 |
ilm |
37 |
|
|
|
38 |
public static final int DEFAULT_LINE_HEIGHT = 40;
|
|
|
39 |
|
|
|
40 |
private static final String LINE_PER_ROW = "line-per-row";
|
|
|
41 |
private static final String TABLE_SPEC = "table-spec";
|
|
|
42 |
private static final String ALLOW_SELECTION = "allow-selection";
|
|
|
43 |
private static final String ALLOW_MULTI_SELECTION = "allow-multi-selection";
|
|
|
44 |
private static final String DYNAMIC_LOAD = "dynamic-load";
|
|
|
45 |
private static final String AUTO_SELECT_FIRST_LINE = "auto-select-first-line";
|
132 |
ilm |
46 |
private Boolean dynamicLoad = false;
|
|
|
47 |
private Boolean allowSelection = false;
|
142 |
ilm |
48 |
private Boolean allowMultiSelection = false;
|
|
|
49 |
private Boolean autoSelectFirstLine = true;
|
94 |
ilm |
50 |
private TableSpec tableSpec = null;
|
|
|
51 |
|
144 |
ilm |
52 |
private transient List<ActionListener> selectionListeners = new ArrayList<ActionListener>();
|
132 |
ilm |
53 |
|
|
|
54 |
// Nombre de ligne à afficher par Row
|
|
|
55 |
private int linePerRow = 1;
|
|
|
56 |
|
142 |
ilm |
57 |
private int lineHeight = DEFAULT_LINE_HEIGHT;
|
|
|
58 |
|
156 |
ilm |
59 |
public LightUITable() {
|
|
|
60 |
// Serialization
|
|
|
61 |
}
|
|
|
62 |
|
94 |
ilm |
63 |
// Init from json constructor
|
|
|
64 |
public LightUITable(final JSONObject json) {
|
132 |
ilm |
65 |
super(json);
|
94 |
ilm |
66 |
}
|
132 |
ilm |
67 |
|
94 |
ilm |
68 |
// Clone constructor
|
|
|
69 |
public LightUITable(final LightUITable tableElement) {
|
|
|
70 |
super(tableElement);
|
|
|
71 |
this.tableSpec = tableElement.tableSpec;
|
132 |
ilm |
72 |
this.allowSelection = tableElement.allowSelection;
|
94 |
ilm |
73 |
}
|
132 |
ilm |
74 |
|
94 |
ilm |
75 |
public LightUITable(final String id) {
|
132 |
ilm |
76 |
super(id);
|
94 |
ilm |
77 |
this.setType(LightUIElement.TYPE_TABLE);
|
132 |
ilm |
78 |
|
|
|
79 |
this.setWeightX(1);
|
94 |
ilm |
80 |
this.setFillWidth(true);
|
|
|
81 |
|
142 |
ilm |
82 |
final RowSelectionSpec selection = new RowSelectionSpec(this.getId());
|
|
|
83 |
final ColumnsSpec columnsSpec = new ColumnsSpec(this.getId(), new ArrayList<ColumnSpec>(), new ArrayList<String>(), new ArrayList<String>());
|
|
|
84 |
final TableSpec tableSpec = new TableSpec(this.getId(), selection, columnsSpec);
|
|
|
85 |
tableSpec.setContent(new TableContent(this.getId()));
|
132 |
ilm |
86 |
|
142 |
ilm |
87 |
this.setTableSpec(tableSpec);
|
132 |
ilm |
88 |
}
|
|
|
89 |
|
|
|
90 |
@Override
|
|
|
91 |
public void setId(final String id) {
|
|
|
92 |
super.setId(id);
|
|
|
93 |
|
142 |
ilm |
94 |
if (this.tableSpec != null) {
|
|
|
95 |
this.tableSpec.setId(id);
|
132 |
ilm |
96 |
|
142 |
ilm |
97 |
if (this.tableSpec.getSelection() != null) {
|
|
|
98 |
this.tableSpec.getSelection().setTableId(id);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
if (this.tableSpec.getContent() != null) {
|
|
|
102 |
this.tableSpec.getContent().setTableId(id);
|
|
|
103 |
}
|
|
|
104 |
}
|
132 |
ilm |
105 |
}
|
|
|
106 |
|
142 |
ilm |
107 |
public final void setLinePerRow(int linePerRow) {
|
132 |
ilm |
108 |
this.linePerRow = linePerRow;
|
|
|
109 |
}
|
|
|
110 |
|
142 |
ilm |
111 |
public final int getLinePerRow() {
|
132 |
ilm |
112 |
return this.linePerRow;
|
|
|
113 |
}
|
|
|
114 |
|
142 |
ilm |
115 |
public final void setLineHeight(int lineHeight) {
|
|
|
116 |
this.lineHeight = lineHeight;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public final int getLineHeight() {
|
|
|
120 |
return this.lineHeight;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public final TableSpec getTableSpec() {
|
94 |
ilm |
124 |
return this.tableSpec;
|
|
|
125 |
}
|
|
|
126 |
|
142 |
ilm |
127 |
public final void setTableSpec(final TableSpec tableSpec) {
|
94 |
ilm |
128 |
this.tableSpec = tableSpec;
|
|
|
129 |
}
|
132 |
ilm |
130 |
|
142 |
ilm |
131 |
public final Boolean isAllowSelection() {
|
132 |
ilm |
132 |
return this.allowSelection;
|
|
|
133 |
}
|
|
|
134 |
|
142 |
ilm |
135 |
public final void setAllowSelection(final boolean allowSelection) {
|
132 |
ilm |
136 |
this.allowSelection = allowSelection;
|
|
|
137 |
}
|
|
|
138 |
|
142 |
ilm |
139 |
public final Boolean isAllowMultiSelection() {
|
|
|
140 |
return this.allowMultiSelection;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
public final void setAllowMultiSelection(final boolean allowMultiSelection) {
|
|
|
144 |
this.allowMultiSelection = allowMultiSelection;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
public final Boolean isDynamicLoad() {
|
132 |
ilm |
148 |
return this.dynamicLoad;
|
|
|
149 |
}
|
|
|
150 |
|
142 |
ilm |
151 |
public final void setDynamicLoad(final boolean dynamicLoad) {
|
132 |
ilm |
152 |
this.dynamicLoad = dynamicLoad;
|
|
|
153 |
}
|
|
|
154 |
|
142 |
ilm |
155 |
public final Boolean isAutoSelectFirstLine() {
|
|
|
156 |
return this.autoSelectFirstLine;
|
94 |
ilm |
157 |
}
|
132 |
ilm |
158 |
|
142 |
ilm |
159 |
public final void setAutoSelectFirstLine(final boolean autoSelectFirstLine) {
|
|
|
160 |
this.autoSelectFirstLine = autoSelectFirstLine;
|
94 |
ilm |
161 |
}
|
132 |
ilm |
162 |
|
142 |
ilm |
163 |
public final Row removeRow(final int index) {
|
|
|
164 |
return this.tableSpec.getContent().removeRow(index);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
public final boolean removeRow(final Row row) {
|
|
|
168 |
final TableContent content = this.getTableSpec().getContent();
|
|
|
169 |
return content.removeRow(row);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
public final boolean hasRow() {
|
|
|
173 |
return (this.tableSpec != null && this.tableSpec.getContent() != null && this.tableSpec.getContent().getRowsCount() > 0);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
public final Row getRow(final int index) {
|
|
|
177 |
return this.getTableSpec().getContent().getRow(index);
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
public Row getRowById(final Number rowId) {
|
|
|
181 |
final int size = this.getTableSpec().getContent().getRowsCount();
|
|
|
182 |
for (int i = 0; i < size; i++) {
|
|
|
183 |
final Row row = this.getRow(i);
|
|
|
184 |
if (NumberUtils.areNumericallyEqual(row.getId(), rowId)) {
|
|
|
185 |
return row;
|
|
|
186 |
} else {
|
|
|
187 |
System.err.println("LightUITable.getSelectedRows() - Null selectedRow");
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
return null;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
public final Row setRow(final int index, final Row row) {
|
|
|
194 |
return this.getTableSpec().getContent().setRow(index, row);
|
|
|
195 |
}
|
|
|
196 |
|
156 |
ilm |
197 |
public final void insertRow(final int index, final Row row) {
|
|
|
198 |
this.getTableSpec().getContent().insertRow(index, row);
|
|
|
199 |
}
|
|
|
200 |
|
142 |
ilm |
201 |
public final boolean addRow(final Row row) {
|
|
|
202 |
return this.getTableSpec().getContent().addRow(row);
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
public final int getRowsCount() {
|
|
|
206 |
return this.getTableSpec().getContent().getRowsCount();
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
public final void clearRows() {
|
|
|
210 |
this.getTableSpec().getContent().clearRows();
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Get Ids of SQLRowAccessor store in selected rows
|
|
|
215 |
*
|
|
|
216 |
* @return The list of selected DB Ids
|
|
|
217 |
*/
|
|
|
218 |
public final List<Number> getSelectedIds() {
|
|
|
219 |
return this.getTableSpec().getSelection().getIds();
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
public final Number getFirstSelectedId() {
|
|
|
223 |
final List<Number> selectedIds = this.getTableSpec().getSelection().getIds();
|
|
|
224 |
if (selectedIds.isEmpty()) {
|
|
|
225 |
return null;
|
|
|
226 |
} else {
|
|
|
227 |
return selectedIds.get(0);
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
public final void setSelectedIds(final List<Number> selectedIds, final boolean fire) {
|
|
|
232 |
this.getTableSpec().getSelection().setIds(selectedIds);
|
|
|
233 |
if (fire) {
|
|
|
234 |
this.fireSelectionChange();
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
public final void clearSelection(final boolean fire) {
|
156 |
ilm |
239 |
this.getTableSpec().getSelection().clear();
|
142 |
ilm |
240 |
if (fire) {
|
|
|
241 |
this.fireSelectionChange();
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
public final List<Row> getSelectedRows() {
|
|
|
246 |
final List<Row> selectedRows = new ArrayList<Row>();
|
|
|
247 |
|
|
|
248 |
if (this.getTableSpec().getSelection() != null) {
|
|
|
249 |
final List<Number> selectedIds = this.getSelectedIds();
|
|
|
250 |
for (final Number selectedId : selectedIds) {
|
|
|
251 |
final Row selectedRow = this.getRowById(selectedId);
|
|
|
252 |
if (selectedRow != null) {
|
|
|
253 |
selectedRows.add(selectedRow);
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
return selectedRows;
|
|
|
259 |
}
|
|
|
260 |
|
144 |
ilm |
261 |
@Override
|
142 |
ilm |
262 |
public final boolean replaceChild(final LightUIElement pChild) {
|
132 |
ilm |
263 |
pChild.setReadOnly(this.isReadOnly());
|
|
|
264 |
|
142 |
ilm |
265 |
for (int i = 0; i < this.getRowsCount(); i++) {
|
|
|
266 |
final Row tableRow = this.getTableSpec().getContent().getRow(i);
|
132 |
ilm |
267 |
final List<Object> tableRowValues = tableRow.getValues();
|
|
|
268 |
final int tableRowValuesCount = tableRowValues.size();
|
|
|
269 |
|
|
|
270 |
for (int j = 0; j < tableRowValuesCount; j++) {
|
|
|
271 |
final Object tableRowValue = tableRowValues.get(j);
|
|
|
272 |
if (tableRowValue instanceof LightUIElement) {
|
|
|
273 |
final LightUIElement child = (LightUIElement) tableRowValue;
|
|
|
274 |
|
|
|
275 |
if (child.getId().equals(pChild.getId())) {
|
|
|
276 |
tableRowValues.set(i, pChild);
|
|
|
277 |
child.setParent(this);
|
|
|
278 |
return true;
|
|
|
279 |
}
|
|
|
280 |
if (child instanceof LightUIContainer) {
|
|
|
281 |
if (((LightUIContainer) child).replaceChild(pChild)) {
|
|
|
282 |
return true;
|
|
|
283 |
}
|
|
|
284 |
}
|
|
|
285 |
if (child instanceof LightUITable) {
|
|
|
286 |
if (((LightUITable) child).replaceChild(pChild)) {
|
|
|
287 |
return true;
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
}
|
|
|
291 |
}
|
|
|
292 |
}
|
|
|
293 |
return false;
|
|
|
294 |
}
|
|
|
295 |
|
144 |
ilm |
296 |
public final <T extends LightUIElement> T findElementByID(final String searchParam, final Class<T> objectClass) {
|
|
|
297 |
if (this.hasRow()) {
|
|
|
298 |
|
|
|
299 |
for (int i = 0; i < this.getRowsCount(); i++) {
|
|
|
300 |
final Row row = this.getRow(i);
|
|
|
301 |
final List<Object> rowValues = row.getValues();
|
|
|
302 |
for (final Object value : rowValues) {
|
|
|
303 |
if (value instanceof LightUIContainer) {
|
|
|
304 |
final LightUIContainer panel = (LightUIContainer) value;
|
|
|
305 |
final T element = panel.findChildByID(searchParam, objectClass);
|
|
|
306 |
if (element != null) {
|
|
|
307 |
return element;
|
|
|
308 |
}
|
|
|
309 |
} else if (value instanceof LightUIElement) {
|
|
|
310 |
final LightUIElement element = (LightUIElement) value;
|
|
|
311 |
|
|
|
312 |
if (element.getId().equals(searchParam)) {
|
|
|
313 |
if (objectClass.isAssignableFrom(element.getClass())) {
|
|
|
314 |
return objectClass.cast(element);
|
|
|
315 |
} else {
|
|
|
316 |
throw new IllegalArgumentException(
|
|
|
317 |
"Element found at is not an instance of " + objectClass.getName() + ", element class: " + element.getClass().getName() + " element ID: " + element.getId());
|
|
|
318 |
}
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
if (element instanceof LightUITable) {
|
|
|
322 |
final T resultElement = ((LightUITable) element).findElementByID(searchParam, objectClass);
|
|
|
323 |
if (resultElement != null) {
|
|
|
324 |
return resultElement;
|
|
|
325 |
}
|
|
|
326 |
}
|
|
|
327 |
}
|
|
|
328 |
}
|
|
|
329 |
}
|
|
|
330 |
} else {
|
|
|
331 |
System.out.println("LightUITable.findElementByID() - No rows for table: " + this.getId());
|
|
|
332 |
}
|
|
|
333 |
return null;
|
132 |
ilm |
334 |
}
|
|
|
335 |
|
144 |
ilm |
336 |
public final <T extends LightUIElement> T findElementByUUID(final String searchParam, final Class<T> objectClass) {
|
142 |
ilm |
337 |
if (this.hasRow()) {
|
|
|
338 |
|
|
|
339 |
for (int i = 0; i < this.getRowsCount(); i++) {
|
|
|
340 |
final Row row = this.getRow(i);
|
|
|
341 |
final List<Object> rowValues = row.getValues();
|
|
|
342 |
for (final Object value : rowValues) {
|
|
|
343 |
if (value instanceof LightUIContainer) {
|
|
|
344 |
final LightUIContainer panel = (LightUIContainer) value;
|
144 |
ilm |
345 |
final T element = panel.findChildByUUID(searchParam, objectClass);
|
142 |
ilm |
346 |
if (element != null) {
|
|
|
347 |
return element;
|
|
|
348 |
}
|
|
|
349 |
} else if (value instanceof LightUIElement) {
|
|
|
350 |
final LightUIElement element = (LightUIElement) value;
|
144 |
ilm |
351 |
|
|
|
352 |
if (element.getUUID().equals(searchParam)) {
|
|
|
353 |
if (objectClass.isAssignableFrom(element.getClass())) {
|
|
|
354 |
return objectClass.cast(element);
|
|
|
355 |
} else {
|
|
|
356 |
throw new IllegalArgumentException(
|
|
|
357 |
"Element found at is not an instance of " + objectClass.getName() + ", element class: " + element.getClass().getName() + " element ID: " + element.getId());
|
142 |
ilm |
358 |
}
|
|
|
359 |
}
|
132 |
ilm |
360 |
|
142 |
ilm |
361 |
if (element instanceof LightUITable) {
|
144 |
ilm |
362 |
final T resultElement = ((LightUITable) element).findElementByUUID(searchParam, objectClass);
|
142 |
ilm |
363 |
if (resultElement != null) {
|
|
|
364 |
return resultElement;
|
94 |
ilm |
365 |
}
|
|
|
366 |
}
|
|
|
367 |
}
|
|
|
368 |
}
|
|
|
369 |
}
|
|
|
370 |
} else {
|
144 |
ilm |
371 |
System.out.println("LightUITable.findElementByUUID() - No rows for table: " + this.getId());
|
94 |
ilm |
372 |
}
|
|
|
373 |
return null;
|
|
|
374 |
}
|
|
|
375 |
|
142 |
ilm |
376 |
public <T extends LightUIElement> List<T> findChildren(final Class<T> expectedClass, final boolean recursively) {
|
|
|
377 |
final List<T> result = new ArrayList<T>();
|
|
|
378 |
|
|
|
379 |
if (this.hasRow()) {
|
|
|
380 |
final int size = this.getRowsCount();
|
|
|
381 |
for (int i = 0; i < size; i++) {
|
|
|
382 |
final Row row = this.getRow(i);
|
|
|
383 |
final List<Object> rowValues = row.getValues();
|
|
|
384 |
for (final Object value : rowValues) {
|
|
|
385 |
if (recursively) {
|
|
|
386 |
if (value instanceof LightUIContainer) {
|
|
|
387 |
result.addAll(((LightUIContainer) value).findChildren(expectedClass, recursively));
|
|
|
388 |
} else if (value instanceof LightUITable) {
|
|
|
389 |
result.addAll(((LightUITable) value).findChildren(expectedClass, recursively));
|
|
|
390 |
}
|
|
|
391 |
}
|
|
|
392 |
if (expectedClass.isAssignableFrom(value.getClass())) {
|
|
|
393 |
result.add(expectedClass.cast(value));
|
|
|
394 |
}
|
|
|
395 |
}
|
|
|
396 |
}
|
|
|
397 |
} else {
|
|
|
398 |
System.out.println("LightUITable.getElementById() - No rows for table: " + this.getId());
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
return result;
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
public final void addSelectionListener(final ActionListener selectionListener) {
|
132 |
ilm |
405 |
this.selectionListeners.add(selectionListener);
|
|
|
406 |
}
|
|
|
407 |
|
142 |
ilm |
408 |
public final void removeSelectionListeners() {
|
132 |
ilm |
409 |
this.selectionListeners.clear();
|
|
|
410 |
}
|
|
|
411 |
|
142 |
ilm |
412 |
public final void fireSelectionChange() {
|
132 |
ilm |
413 |
for (final ActionListener listener : this.selectionListeners) {
|
|
|
414 |
listener.actionPerformed(new ActionEvent(this, 1, "selection"));
|
|
|
415 |
}
|
|
|
416 |
}
|
|
|
417 |
|
142 |
ilm |
418 |
// TODO: garder l'ordre des colonnes invisibles
|
|
|
419 |
/**
|
|
|
420 |
* Create columns preferences with the current ColumnsSpec
|
|
|
421 |
*
|
|
|
422 |
* @return XML document with columns preferences
|
|
|
423 |
*/
|
|
|
424 |
public final Document createXmlPreferences(final Document userPrefs, final ColumnsSpec columnsSpec) throws ParserConfigurationException {
|
|
|
425 |
|
|
|
426 |
final Element rootElement = new Element("list");
|
|
|
427 |
final Document xmlConf = new Document();
|
|
|
428 |
|
|
|
429 |
final int columnSpecCount = columnsSpec.getColumnCount();
|
|
|
430 |
final List<String> visibleIds = new ArrayList<String>();
|
|
|
431 |
for (int i = 0; i < columnSpecCount; i++) {
|
|
|
432 |
final ColumnSpec columnSpec = columnsSpec.getColumn(i);
|
|
|
433 |
final Element xmlColumn = this.createXmlColumn(columnSpec.getId(), columnSpec.getMaxWidth(), columnSpec.getMinWidth(), columnSpec.getWidth());
|
|
|
434 |
rootElement.addContent(xmlColumn);
|
|
|
435 |
visibleIds.add(columnSpec.getId());
|
|
|
436 |
}
|
|
|
437 |
|
|
|
438 |
final Element rootUserPrefs = userPrefs.getRootElement();
|
|
|
439 |
final List<Element> xmlColumns = rootUserPrefs.getChildren();
|
|
|
440 |
final int columnsSize = xmlColumns.size();
|
|
|
441 |
for (int i = 0; i < columnsSize; i++) {
|
|
|
442 |
final Element xmlColumn = xmlColumns.get(i);
|
|
|
443 |
final String columnId = xmlColumn.getAttribute("id").getValue();
|
|
|
444 |
if (!visibleIds.contains(columnId)) {
|
|
|
445 |
final int maxWidth = Integer.parseInt(xmlColumn.getAttribute("max-width").getValue());
|
|
|
446 |
final int minWidth = Integer.parseInt(xmlColumn.getAttribute("min-width").getValue());
|
|
|
447 |
final int width = Integer.parseInt(xmlColumn.getAttribute("width").getValue());
|
|
|
448 |
final Element newXmlColumn = this.createXmlColumn(columnId, maxWidth, minWidth, width);
|
|
|
449 |
rootElement.addContent(newXmlColumn);
|
|
|
450 |
}
|
|
|
451 |
}
|
|
|
452 |
xmlConf.setRootElement(rootElement);
|
|
|
453 |
return xmlConf;
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
/**
|
|
|
457 |
* Create default columns preferences from the SQLTableModelLinesSourceOnline
|
|
|
458 |
*
|
|
|
459 |
* @return XML document with columns preferences
|
|
|
460 |
*/
|
|
|
461 |
public Document createDefaultXmlPreferences() {
|
|
|
462 |
final Element rootElement = new Element("list");
|
|
|
463 |
|
|
|
464 |
if (this.getTableSpec() != null && this.getTableSpec().getColumns() != null) {
|
|
|
465 |
final int sqlColumnsCount = this.getTableSpec().getColumns().getColumnCount();
|
|
|
466 |
for (int i = 0; i < sqlColumnsCount; i++) {
|
|
|
467 |
final ColumnSpec column = this.getTableSpec().getColumns().getColumn(i);
|
|
|
468 |
final String columnId = column.getId();
|
|
|
469 |
final Element columnElement = this.createXmlColumn(columnId, column.getMaxWidth(), column.getMinWidth(), column.getWidth());
|
|
|
470 |
rootElement.addContent(columnElement);
|
|
|
471 |
}
|
|
|
472 |
}
|
|
|
473 |
final Document xmlConf = new Document(rootElement);
|
|
|
474 |
|
|
|
475 |
return xmlConf;
|
|
|
476 |
}
|
|
|
477 |
|
|
|
478 |
protected final Element createXmlColumn(final String columnId, final double maxWidth, final double minWidth, final double width) {
|
|
|
479 |
final Element columnElement = new Element("column");
|
|
|
480 |
columnElement.setAttribute("id", columnId);
|
|
|
481 |
columnElement.setAttribute("max-width", String.valueOf(maxWidth));
|
|
|
482 |
columnElement.setAttribute("min-width", String.valueOf(minWidth));
|
|
|
483 |
columnElement.setAttribute("width", String.valueOf(width));
|
|
|
484 |
return columnElement;
|
|
|
485 |
}
|
|
|
486 |
|
132 |
ilm |
487 |
@Override
|
|
|
488 |
public void setReadOnly(final boolean readOnly) {
|
|
|
489 |
super.setReadOnly(readOnly);
|
142 |
ilm |
490 |
|
|
|
491 |
if (this.hasRow()) {
|
|
|
492 |
final int size = this.getRowsCount();
|
|
|
493 |
for (int i = 0; i < size; i++) {
|
|
|
494 |
final Row row = this.getRow(i);
|
132 |
ilm |
495 |
final List<Object> values = row.getValues();
|
|
|
496 |
for (final Object value : values) {
|
|
|
497 |
if (value != null && value instanceof LightUIElement) {
|
|
|
498 |
((LightUIElement) value).setReadOnly(readOnly);
|
|
|
499 |
}
|
|
|
500 |
}
|
|
|
501 |
}
|
|
|
502 |
}
|
|
|
503 |
}
|
|
|
504 |
|
|
|
505 |
@Override
|
142 |
ilm |
506 |
public void _setValueFromContext(final Object value) {
|
132 |
ilm |
507 |
if (value != null) {
|
156 |
ilm |
508 |
final JSONArray jsonContext = JSONConverter.getObjectFromJSON(value, JSONArray.class);
|
132 |
ilm |
509 |
final ColumnsSpec columnsSpec = this.getTableSpec().getColumns();
|
|
|
510 |
final int columnsCount = columnsSpec.getColumnCount();
|
|
|
511 |
|
156 |
ilm |
512 |
final List<Integer> editorsIndex = new ArrayList<>();
|
132 |
ilm |
513 |
|
|
|
514 |
for (int i = 0; i < columnsCount; i++) {
|
|
|
515 |
final ColumnSpec columnSpec = columnsSpec.getColumn(i);
|
|
|
516 |
if (columnSpec.getEditor() != null) {
|
|
|
517 |
editorsIndex.add(i);
|
|
|
518 |
}
|
|
|
519 |
}
|
|
|
520 |
|
142 |
ilm |
521 |
if (this.hasRow()) {
|
|
|
522 |
final int size = this.getRowsCount();
|
|
|
523 |
if (jsonContext.size() != size) {
|
|
|
524 |
System.err.println("LightUITable.setValueFromContext() - Incorrect line count in JSON");
|
|
|
525 |
} else {
|
|
|
526 |
|
|
|
527 |
for (int i = 0; i < size; i++) {
|
|
|
528 |
final Row row = this.getRow(i);
|
|
|
529 |
final JSONObject jsonLineContext = (JSONObject) JSONConverter.getObjectFromJSON(jsonContext.get(i), JSONObject.class);
|
|
|
530 |
final Number rowId = JSONConverter.getParameterFromJSON(jsonLineContext, "row.id", Number.class);
|
|
|
531 |
final String rowExtendId = (String) JSONConverter.getParameterFromJSON(jsonLineContext, "row.extend.id", String.class);
|
|
|
532 |
if (NumberUtils.areNumericallyEqual(rowId, row.getId()) && (row.getExtendId() == null || (row.getExtendId() != null && rowExtendId.equals(row.getExtendId())))) {
|
|
|
533 |
if (row.isFillWidth()) {
|
|
|
534 |
if (!row.getValues().isEmpty() && row.getValues().get(0) instanceof LightUserControl) {
|
|
|
535 |
final LightUIElement element = (LightUIElement) row.getValues().get(0);
|
|
|
536 |
if (element instanceof LightUserControl) {
|
|
|
537 |
if (jsonLineContext.containsKey(element.getUUID())) {
|
|
|
538 |
((LightUserControl) element)._setValueFromContext(jsonLineContext.get(element.getUUID()));
|
|
|
539 |
} else {
|
|
|
540 |
System.out.println("LightUITable.setValueFromContext() - Unable to find element : id - " + element.getId() + " uuid - " + element.getUUID());
|
|
|
541 |
System.out.println("LightUITable.setValueFromContext() - In JSON : " + jsonLineContext.toJSONString());
|
|
|
542 |
}
|
|
|
543 |
}
|
|
|
544 |
}
|
|
|
545 |
} else {
|
|
|
546 |
for (int k = 0; k < editorsIndex.size(); k++) {
|
|
|
547 |
final Object objEditor = row.getValues().get(editorsIndex.get(k));
|
|
|
548 |
if (!(objEditor instanceof LightUserControl)) {
|
|
|
549 |
throw new IllegalArgumentException("Impossible to find editor for row: " + rowId.toString() + " at position: " + String.valueOf(k));
|
|
|
550 |
}
|
|
|
551 |
final LightUIElement editor = (LightUIElement) objEditor;
|
|
|
552 |
|
|
|
553 |
if (editor instanceof LightUserControl && jsonLineContext.containsKey(editor.getUUID())) {
|
|
|
554 |
((LightUserControl) editor)._setValueFromContext(jsonLineContext.get(editor.getUUID()));
|
132 |
ilm |
555 |
} else {
|
142 |
ilm |
556 |
throw new IllegalArgumentException(
|
|
|
557 |
"Impossible to find value for editor: " + editor.getId() + " for row: " + rowId.toString() + " at position: " + String.valueOf(k));
|
132 |
ilm |
558 |
}
|
|
|
559 |
}
|
|
|
560 |
}
|
|
|
561 |
} else {
|
144 |
ilm |
562 |
final List<Number> ids = new ArrayList<Number>(size);
|
|
|
563 |
for (int j = 0; j < size; j++) {
|
|
|
564 |
ids.add(this.getRow(j).getId());
|
|
|
565 |
}
|
|
|
566 |
throw new IllegalArgumentException("Impossible to find row: " + rowId.toString() + " known table row ids :" + ids);
|
132 |
ilm |
567 |
}
|
|
|
568 |
}
|
|
|
569 |
}
|
|
|
570 |
}
|
|
|
571 |
}
|
|
|
572 |
}
|
|
|
573 |
|
|
|
574 |
@Override
|
94 |
ilm |
575 |
public JSONObject toJSON() {
|
|
|
576 |
final JSONObject json = super.toJSON();
|
132 |
ilm |
577 |
if (this.allowSelection) {
|
142 |
ilm |
578 |
json.put(ALLOW_SELECTION, true);
|
132 |
ilm |
579 |
}
|
142 |
ilm |
580 |
if (this.allowMultiSelection) {
|
|
|
581 |
json.put(ALLOW_MULTI_SELECTION, true);
|
|
|
582 |
}
|
132 |
ilm |
583 |
if (this.dynamicLoad) {
|
142 |
ilm |
584 |
json.put(DYNAMIC_LOAD, true);
|
132 |
ilm |
585 |
}
|
142 |
ilm |
586 |
if (!this.autoSelectFirstLine) {
|
|
|
587 |
json.put(AUTO_SELECT_FIRST_LINE, false);
|
94 |
ilm |
588 |
}
|
132 |
ilm |
589 |
if (this.tableSpec != null) {
|
142 |
ilm |
590 |
json.put(TABLE_SPEC, this.tableSpec.toJSON());
|
94 |
ilm |
591 |
}
|
142 |
ilm |
592 |
|
|
|
593 |
json.put(LINE_PER_ROW, this.linePerRow);
|
94 |
ilm |
594 |
return json;
|
|
|
595 |
}
|
|
|
596 |
|
|
|
597 |
@Override
|
|
|
598 |
public void fromJSON(final JSONObject json) {
|
|
|
599 |
super.fromJSON(json);
|
142 |
ilm |
600 |
this.allowSelection = JSONConverter.getParameterFromJSON(json, ALLOW_SELECTION, Boolean.class, false);
|
|
|
601 |
this.allowSelection = JSONConverter.getParameterFromJSON(json, ALLOW_MULTI_SELECTION, Boolean.class, false);
|
|
|
602 |
this.dynamicLoad = JSONConverter.getParameterFromJSON(json, DYNAMIC_LOAD, Boolean.class, false);
|
|
|
603 |
this.autoSelectFirstLine = JSONConverter.getParameterFromJSON(json, AUTO_SELECT_FIRST_LINE, Boolean.class, true);
|
|
|
604 |
this.linePerRow = JSONConverter.getParameterFromJSON(json, LINE_PER_ROW, Integer.class);
|
132 |
ilm |
605 |
|
144 |
ilm |
606 |
final JSONObject jsonRawContent = JSONConverter.getParameterFromJSON(json, TABLE_SPEC, JSONObject.class);
|
132 |
ilm |
607 |
|
94 |
ilm |
608 |
if (jsonRawContent != null) {
|
|
|
609 |
this.tableSpec = new TableSpec(jsonRawContent);
|
|
|
610 |
}
|
|
|
611 |
}
|
142 |
ilm |
612 |
|
|
|
613 |
@Override
|
156 |
ilm |
614 |
public void writeExternal(ObjectOutput out) throws IOException {
|
|
|
615 |
super.writeExternal(out);
|
|
|
616 |
|
|
|
617 |
out.writeBoolean(dynamicLoad);
|
|
|
618 |
out.writeBoolean(allowSelection);
|
|
|
619 |
out.writeBoolean(allowMultiSelection);
|
|
|
620 |
out.writeBoolean(autoSelectFirstLine);
|
|
|
621 |
tableSpec.writeExternal(out);
|
|
|
622 |
|
|
|
623 |
}
|
|
|
624 |
|
|
|
625 |
@Override
|
|
|
626 |
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
|
|
627 |
super.readExternal(in);
|
|
|
628 |
this.dynamicLoad = in.readBoolean();
|
|
|
629 |
this.allowSelection = in.readBoolean();
|
|
|
630 |
this.allowMultiSelection = in.readBoolean();
|
|
|
631 |
this.autoSelectFirstLine = in.readBoolean();
|
|
|
632 |
tableSpec = new TableSpec();
|
|
|
633 |
tableSpec.readExternal(in);
|
|
|
634 |
}
|
|
|
635 |
|
|
|
636 |
@Override
|
142 |
ilm |
637 |
public void destroy() {
|
|
|
638 |
super.destroy();
|
|
|
639 |
this.selectionListeners.clear();
|
|
|
640 |
}
|
94 |
ilm |
641 |
}
|