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