OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Details | Compare with Previous | Last modification | View Log | RSS feed

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