1 package fr.ifremer.quadrige3.ui.swing.table;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import org.jdesktop.swingx.table.DefaultTableColumnModelExt;
27 import org.jdesktop.swingx.table.TableColumnExt;
28
29 import javax.swing.table.TableColumn;
30 import java.util.HashSet;
31 import java.util.Objects;
32 import java.util.Set;
33 import java.util.function.Predicate;
34
35
36
37
38
39
40
41
42 public class SwingTableColumnModel extends DefaultTableColumnModelExt {
43
44
45
46
47
48 private Set<TableColumn> fixedColumns = new HashSet<>();
49
50 @Override
51 public TableColumnExt getColumnExt(Object identifier) {
52
53 TableColumnExt column = super.getColumnExt(identifier);
54
55 if (column == null && identifier instanceof ColumnIdentifier) {
56 ColumnIdentifier<?> columnIdentifier = (ColumnIdentifier<?>) identifier;
57
58
59 column = (TableColumnExt) getColumns(true).stream()
60 .filter(tableColumn -> tableColumn.getIdentifier() instanceof ColumnIdentifier)
61 .filter(tableColumn -> Objects.equals(((ColumnIdentifier<?>) tableColumn.getIdentifier()).getPropertyName(), columnIdentifier.getPropertyName()))
62 .filter(tableColumn -> {
63 if (identifier instanceof PmfmColumnIdentifier && tableColumn.getIdentifier() instanceof PmfmColumnIdentifier) {
64 return ((PmfmColumnIdentifier) identifier).getPmfmId() == ((PmfmColumnIdentifier) tableColumn.getIdentifier()).getPmfmId();
65 }
66 return false;
67 })
68 .findFirst()
69 .orElse(null);
70 }
71
72 return column;
73 }
74
75
76
77
78 @Override
79 public void moveColumn(int columnIndex, int newIndex) {
80
81
82 if (fixedColumns.contains(getColumn(columnIndex)) || fixedColumns.contains(getColumn(newIndex))) {
83 return;
84 }
85
86 super.moveColumn(columnIndex, newIndex);
87 }
88
89
90
91
92 @Override
93 public void addColumn(TableColumn column) {
94 super.addColumn(column);
95
96 if (column instanceof FixedColumn) {
97 Integer fixedPosition = ((FixedColumn) column).getFixedPosition();
98 if (fixedPosition != null) {
99 setFixedColumn(column, fixedPosition);
100 }
101 }
102 }
103
104 @Override
105 public void removeColumn(TableColumn column) {
106 super.removeColumn(column);
107
108 fixedColumns.remove(column);
109 }
110
111 private void setFixedColumn(TableColumn column, int fixedViewIndex) {
112
113
114 fixedViewIndex = Math.min(fixedViewIndex, fixedColumns.size());
115
116 fixedColumns.add(column);
117
118
119 super.moveColumn(getColumnIndex(column.getIdentifier()), fixedViewIndex);
120 }
121
122 public boolean isFixedColumn(TableColumn column) {
123 return column != null && fixedColumns.contains(column);
124 }
125
126 public boolean hasFixedColumn(Predicate<TableColumn> predicate) {
127 return fixedColumns.stream().anyMatch(predicate);
128 }
129 }