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