OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Rev 180 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 174
Line 16... Line 16...
16
import org.openconcerto.utils.NumberUtils;
16
import org.openconcerto.utils.NumberUtils;
17
import org.openconcerto.utils.io.JSONConverter;
17
import org.openconcerto.utils.io.JSONConverter;
18
 
18
 
19
import java.awt.event.ActionEvent;
19
import java.awt.event.ActionEvent;
20
import java.awt.event.ActionListener;
20
import java.awt.event.ActionListener;
21
import java.io.Externalizable;
-
 
22
import java.io.IOException;
-
 
23
import java.io.ObjectInput;
-
 
24
import java.io.ObjectOutput;
-
 
25
import java.util.ArrayList;
21
import java.util.ArrayList;
26
import java.util.List;
22
import java.util.List;
27
 
23
 
28
import javax.xml.parsers.ParserConfigurationException;
24
import javax.xml.parsers.ParserConfigurationException;
29
 
25
 
Line 31... Line 27...
31
import org.jdom2.Element;
27
import org.jdom2.Element;
32
 
28
 
33
import net.minidev.json.JSONArray;
29
import net.minidev.json.JSONArray;
34
import net.minidev.json.JSONObject;
30
import net.minidev.json.JSONObject;
35
 
31
 
36
public class LightUITable extends LightUserControlContainer implements Externalizable {
32
public class LightUITable extends LightUserControlContainer {
37
 
33
 
38
    public static final int DEFAULT_LINE_HEIGHT = 40;
34
    public static final int DEFAULT_LINE_HEIGHT = 40;
39
 
35
 
40
    private static final String LINE_PER_ROW = "line-per-row";
36
    private static final String LINE_PER_ROW = "line-per-row";
41
    private static final String TABLE_SPEC = "table-spec";
37
    private static final String TABLE_SPEC = "table-spec";
Line 47... Line 43...
47
    private Boolean allowSelection = false;
43
    private Boolean allowSelection = false;
48
    private Boolean allowMultiSelection = false;
44
    private Boolean allowMultiSelection = false;
49
    private Boolean autoSelectFirstLine = true;
45
    private Boolean autoSelectFirstLine = true;
50
    private TableSpec tableSpec = null;
46
    private TableSpec tableSpec = null;
51
 
47
 
52
    private transient List<ActionListener> selectionListeners = new ArrayList<ActionListener>();
48
    private transient List<ActionListener> selectionListeners = new ArrayList<>();
53
 
49
 
54
    // Nombre de ligne à afficher par Row
50
    // Nombre de ligne à afficher par Row
55
    private int linePerRow = 1;
51
    private int linePerRow = 1;
56
 
52
 
57
    private int lineHeight = DEFAULT_LINE_HEIGHT;
53
    private int lineHeight = DEFAULT_LINE_HEIGHT;
Line 79... Line 75...
79
        this.setWeightX(1);
75
        this.setWeightX(1);
80
        this.setFillWidth(true);
76
        this.setFillWidth(true);
81
 
77
 
82
        final RowSelectionSpec selection = new RowSelectionSpec(this.getId());
78
        final RowSelectionSpec selection = new RowSelectionSpec(this.getId());
83
        final ColumnsSpec columnsSpec = new ColumnsSpec(this.getId(), new ArrayList<ColumnSpec>(), new ArrayList<String>(), new ArrayList<String>());
79
        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);
80
        this.tableSpec = new TableSpec(this.getId(), selection, columnsSpec);
85
        tableSpec.setContent(new TableContent(this.getId()));
81
        this.tableSpec.setContent(new TableContent(this.getId()));
86
 
-
 
87
        this.setTableSpec(tableSpec);
-
 
88
    }
82
    }
89
 
83
 
90
    @Override
84
    @Override
91
    public void setId(final String id) {
85
    public void setId(final String id) {
92
        super.setId(id);
86
        super.setId(id);
Line 241... Line 235...
241
            this.fireSelectionChange();
235
            this.fireSelectionChange();
242
        }
236
        }
243
    }
237
    }
244
 
238
 
245
    public final List<Row> getSelectedRows() {
239
    public final List<Row> getSelectedRows() {
246
        final List<Row> selectedRows = new ArrayList<Row>();
240
        final List<Row> selectedRows = new ArrayList<>();
247
 
241
 
248
        if (this.getTableSpec().getSelection() != null) {
242
        if (this.getTableSpec().getSelection() != null) {
249
            final List<Number> selectedIds = this.getSelectedIds();
243
            final List<Number> selectedIds = this.getSelectedIds();
250
            for (final Number selectedId : selectedIds) {
244
            for (final Number selectedId : selectedIds) {
251
                final Row selectedRow = this.getRowById(selectedId);
245
                final Row selectedRow = this.getRowById(selectedId);
Line 372... Line 366...
372
        }
366
        }
373
        return null;
367
        return null;
374
    }
368
    }
375
 
369
 
376
    public <T extends LightUIElement> List<T> findChildren(final Class<T> expectedClass, final boolean recursively) {
370
    public <T extends LightUIElement> List<T> findChildren(final Class<T> expectedClass, final boolean recursively) {
377
        final List<T> result = new ArrayList<T>();
371
        final List<T> result = new ArrayList<>();
378
 
372
 
379
        if (this.hasRow()) {
373
        if (this.hasRow()) {
380
            final int size = this.getRowsCount();
374
            final int size = this.getRowsCount();
381
            for (int i = 0; i < size; i++) {
375
            for (int i = 0; i < size; i++) {
382
                final Row row = this.getRow(i);
376
                final Row row = this.getRow(i);
383
                final List<Object> rowValues = row.getValues();
377
                final List<Object> rowValues = row.getValues();
384
                for (final Object value : rowValues) {
378
                for (final Object value : rowValues) {
-
 
379
                    if (value != null) {
385
                    if (recursively) {
380
                        if (recursively) {
386
                        if (value instanceof LightUIContainer) {
381
                            if (value instanceof LightUIContainer) {
387
                            result.addAll(((LightUIContainer) value).findChildren(expectedClass, recursively));
382
                                result.addAll(((LightUIContainer) value).findChildren(expectedClass, recursively));
388
                        } else if (value instanceof LightUITable) {
383
                            } else if (value instanceof LightUITable) {
389
                            result.addAll(((LightUITable) value).findChildren(expectedClass, recursively));
384
                                result.addAll(((LightUITable) value).findChildren(expectedClass, recursively));
Line 392... Line 387...
392
                    if (expectedClass.isAssignableFrom(value.getClass())) {
387
                        if (expectedClass.isAssignableFrom(value.getClass())) {
393
                        result.add(expectedClass.cast(value));
388
                            result.add(expectedClass.cast(value));
394
                    }
389
                        }
395
                }
390
                    }
396
            }
391
                }
-
 
392
            }
397
        } else {
393
        } else {
398
            System.out.println("LightUITable.getElementById() - No rows for table: " + this.getId());
394
            System.out.println("LightUITable.getElementById() - No rows for table: " + this.getId());
399
        }
395
        }
400
 
396
 
401
        return result;
397
        return result;
Line 425... Line 421...
425
 
421
 
426
        final Element rootElement = new Element("list");
422
        final Element rootElement = new Element("list");
427
        final Document xmlConf = new Document();
423
        final Document xmlConf = new Document();
428
 
424
 
429
        final int columnSpecCount = columnsSpec.getColumnCount();
425
        final int columnSpecCount = columnsSpec.getColumnCount();
430
        final List<String> visibleIds = new ArrayList<String>();
426
        final List<String> visibleIds = new ArrayList<>();
431
        for (int i = 0; i < columnSpecCount; i++) {
427
        for (int i = 0; i < columnSpecCount; i++) {
432
            final ColumnSpec columnSpec = columnsSpec.getColumn(i);
428
            final ColumnSpec columnSpec = columnsSpec.getColumn(i);
433
            final Element xmlColumn = this.createXmlColumn(columnSpec.getId(), columnSpec.getMaxWidth(), columnSpec.getMinWidth(), columnSpec.getWidth());
429
            final Element xmlColumn = this.createXmlColumn(columnSpec.getId(), columnSpec.getMaxWidth(), columnSpec.getMinWidth(), columnSpec.getWidth());
434
            rootElement.addContent(xmlColumn);
430
            rootElement.addContent(xmlColumn);
435
            visibleIds.add(columnSpec.getId());
431
            visibleIds.add(columnSpec.getId());
Line 492... Line 488...
492
            final int size = this.getRowsCount();
488
            final int size = this.getRowsCount();
493
            for (int i = 0; i < size; i++) {
489
            for (int i = 0; i < size; i++) {
494
                final Row row = this.getRow(i);
490
                final Row row = this.getRow(i);
495
                final List<Object> values = row.getValues();
491
                final List<Object> values = row.getValues();
496
                for (final Object value : values) {
492
                for (final Object value : values) {
497
                    if (value != null && value instanceof LightUIElement) {
493
                    if (value instanceof LightUIElement) {
498
                        ((LightUIElement) value).setReadOnly(readOnly);
494
                        ((LightUIElement) value).setReadOnly(readOnly);
499
                    }
495
                    }
500
                }
496
                }
501
            }
497
            }
502
        }
498
        }
Line 524... Line 520...
524
                    System.err.println("LightUITable.setValueFromContext() - Incorrect line count in JSON");
520
                    System.err.println("LightUITable.setValueFromContext() - Incorrect line count in JSON");
525
                } else {
521
                } else {
526
 
522
 
527
                    for (int i = 0; i < size; i++) {
523
                    for (int i = 0; i < size; i++) {
528
                        final Row row = this.getRow(i);
524
                        final Row row = this.getRow(i);
529
                        final JSONObject jsonLineContext = (JSONObject) JSONConverter.getObjectFromJSON(jsonContext.get(i), JSONObject.class);
525
                        final JSONObject jsonLineContext = JSONConverter.getObjectFromJSON(jsonContext.get(i), JSONObject.class);
530
                        final Number rowId = JSONConverter.getParameterFromJSON(jsonLineContext, "row.id", Number.class);
526
                        final Number rowId = JSONConverter.getParameterFromJSON(jsonLineContext, "row.id", Number.class);
531
                        final String rowExtendId = (String) JSONConverter.getParameterFromJSON(jsonLineContext, "row.extend.id", String.class);
527
                        final String rowExtendId = 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())))) {
528
                        if (NumberUtils.areNumericallyEqual(rowId, row.getId()) && (row.getExtendId() == null || (row.getExtendId() != null && rowExtendId.equals(row.getExtendId())))) {
533
                            if (row.isFillWidth()) {
529
                            if (row.isFillWidth()) {
534
                                if (!row.getValues().isEmpty() && row.getValues().get(0) instanceof LightUserControl) {
530
                                if (!row.getValues().isEmpty() && row.getValues().get(0) instanceof LightUserControl) {
535
                                    final LightUIElement element = (LightUIElement) row.getValues().get(0);
531
                                    final LightUIElement element = (LightUIElement) row.getValues().get(0);
536
                                    if (element instanceof LightUserControl) {
532
                                    if (element instanceof LightUserControl) {
Line 557... Line 553...
557
                                                "Impossible to find value for editor: " + editor.getId() + " for row: " + rowId.toString() + " at position: " + String.valueOf(k));
553
                                                "Impossible to find value for editor: " + editor.getId() + " for row: " + rowId.toString() + " at position: " + String.valueOf(k));
558
                                    }
554
                                    }
559
                                }
555
                                }
560
                            }
556
                            }
561
                        } else {
557
                        } else {
562
                            final List<Number> ids = new ArrayList<Number>(size);
558
                            final List<Number> ids = new ArrayList<>(size);
563
                            for (int j = 0; j < size; j++) {
559
                            for (int j = 0; j < size; j++) {
564
                                ids.add(this.getRow(j).getId());
560
                                ids.add(this.getRow(j).getId());
565
                            }
561
                            }
566
                            throw new IllegalArgumentException("Impossible to find row: " + rowId.toString() + " known table row ids :" + ids);
562
                            throw new IllegalArgumentException("Impossible to find row: " + rowId.toString() + " known table row ids :" + ids);
567
                        }
563
                        }
Line 609... Line 605...
609
            this.tableSpec = new TableSpec(jsonRawContent);
605
            this.tableSpec = new TableSpec(jsonRawContent);
610
        }
606
        }
611
    }
607
    }
612
 
608
 
613
    @Override
609
    @Override
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
-
 
637
    public void destroy() {
610
    public void destroy() {
638
        super.destroy();
611
        super.destroy();
639
        this.selectionListeners.clear();
612
        this.selectionListeners.clear();
640
    }
613
    }
641
}
614
}