1 package fr.ifremer.quadrige2.ui.swing.common.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
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.Lists;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.jdesktop.swingx.table.TableColumnModelExt;
31 import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
32
33 import java.util.Collections;
34 import java.util.List;
35
36
37
38
39
40
41
42 public abstract class AbstractTableModel<R extends AbstractRowUIModel<?, ?>> extends AbstractApplicationTableModel<R> {
43
44 public static final String DEFAULT_STATE_CONTEXT = "DEFAULT_STATE_CONTEXT";
45 private AbstractTableUIModel tableUIModel;
46
47 private boolean hasCheckTableColumn;
48
49
50
51
52
53
54
55
56 public AbstractTableModel(TableColumnModelExt columnModel, boolean createNewRow, boolean createEmptyRowIsEmpty) {
57 super(columnModel, createNewRow, createEmptyRowIsEmpty);
58 }
59
60
61
62
63
64
65 public abstract ColumnIdentifier<R> getFirstColumnEditing();
66
67
68 @Override
69 public boolean isCellEditable(int rowIndex, int columnIndex, org.nuiton.jaxx.application.swing.table.ColumnIdentifier<R> propertyName) {
70
71
72 if (AbstractRowUIModel.PROPERTY_SELECTED.equals(propertyName.getPropertyName())) {
73 return true;
74 }
75
76 R row = getEntry(rowIndex);
77
78
79 if (!row.isEditable()) {
80 return false;
81 }
82
83
84 try {
85 return super.isCellEditable(rowIndex, columnIndex, propertyName);
86 } catch (NullPointerException e) {
87
88 return true;
89 }
90 }
91
92
93 @Override
94 public Class<?> getColumnClass(int columnIndex) {
95 org.nuiton.jaxx.application.swing.table.ColumnIdentifier<R> colId = getIdentifier(columnIndex);
96 if (colId instanceof ColumnIdentifier) {
97 return ((ColumnIdentifier) colId).getPropertyType();
98 }
99 return super.getColumnClass(columnIndex);
100 }
101
102
103
104
105
106
107 public List<org.nuiton.jaxx.application.swing.table.ColumnIdentifier<R>> getIdentifiers() {
108 return identifiers;
109 }
110
111
112
113
114
115
116 public List<ColumnIdentifier<R>> getMandatoryIdentifiers() {
117 List<ColumnIdentifier<R>> mandatoryIdentifiers = Lists.newArrayList();
118 if (CollectionUtils.isNotEmpty(getIdentifiers())) {
119 for (org.nuiton.jaxx.application.swing.table.ColumnIdentifier<R> identifier : getIdentifiers()) {
120 if ((identifier instanceof ColumnIdentifier) && ((ColumnIdentifier<R>) identifier).isMandatory()) {
121 mandatoryIdentifiers.add((ColumnIdentifier<R>) identifier);
122 }
123 }
124 }
125 return mandatoryIdentifiers;
126 }
127
128
129
130
131
132
133 public AbstractTableUIModel getTableUIModel() {
134 return tableUIModel;
135 }
136
137
138
139
140
141
142 public void setTableUIModel(AbstractTableUIModel tableUIModel) {
143 this.tableUIModel = tableUIModel;
144 }
145
146
147 @Override
148 protected void onRowAdded(int rowIndex, R row) {
149 super.onRowAdded(rowIndex, row);
150
151
152 if (getTableUIModel() != null) {
153 getTableUIModel().firePropertyChanged(AbstractTableUIModel.PROPERTY_ROWS_ADDED, null, Collections.singletonList(row));
154 }
155
156 }
157
158
159
160
161
162
163 public void addNonEditableColumn(ColumnIdentifier identifier) {
164 if (noneEditableCols == null) {
165 setNoneEditableCols(identifier);
166 } else {
167 noneEditableCols.add(identifier);
168 }
169 }
170
171
172
173
174
175
176 public void addCheckTableColumnIdentifier(ColumnIdentifier<R> identifier) {
177 Preconditions.checkArgument(identifier instanceof CheckTableColumnIdentifier);
178 identifiers.add(identifier);
179 hasCheckTableColumn = true;
180 }
181
182
183
184
185
186
187 public boolean hasCheckTableColumn() {
188 return hasCheckTableColumn;
189 }
190
191
192
193
194
195
196 public String getStateContext() {
197 return DEFAULT_STATE_CONTEXT;
198 }
199 }