1 package fr.ifremer.quadrige2.ui.swing.common.table.action;
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 fr.ifremer.quadrige2.ui.swing.common.table.AbstractTableModel;
27 import fr.ifremer.quadrige2.ui.swing.common.table.AbstractTableUIHandler;
28 import fr.ifremer.quadrige2.ui.swing.common.table.CheckTableColumn;
29 import org.jdesktop.swingx.JXTable;
30
31 import javax.swing.AbstractAction;
32 import javax.swing.table.TableCellEditor;
33 import java.awt.Component;
34
35
36
37
38
39
40 public abstract class AbstractCellSelectionAction extends AbstractAction {
41
42 private final AbstractTableUIHandler handler;
43
44
45
46
47
48
49
50 public AbstractCellSelectionAction(String name, AbstractTableUIHandler handler) {
51 super(name);
52 this.handler = handler;
53 }
54
55 private AbstractTableModel getTableModel() {
56 return handler.getTableModel();
57 }
58
59 private JXTable getTable() {
60 return handler.getTable();
61 }
62
63
64
65
66
67
68 protected boolean isTableEditing() {
69 return getTable().isEditing();
70 }
71
72
73
74
75
76
77 protected boolean isCreateNewRow() {
78 return getTableModel().isCreateNewRow();
79 }
80
81
82
83
84 protected void addNewRow() {
85
86 if (stopActiveEdition()) {
87
88 getTableModel().addNewRow();
89 }
90 }
91
92
93
94
95
96
97 protected int getRowCount() {
98 return getTable().getRowCount();
99 }
100
101
102
103
104
105
106 protected int getColumnCount() {
107 return getTable().getColumnCount();
108 }
109
110
111
112
113
114
115 protected int getSelectedRow() {
116 return getTable().getSelectedRow();
117 }
118
119
120
121
122
123
124 protected int getSelectedColumn() {
125 return getTable().getSelectedColumn();
126 }
127
128
129
130
131
132
133
134 protected int getNextColumn(int column) {
135 int nextColumn = column;
136 do {
137 nextColumn++;
138 } while (nextColumn < getColumnCount() && getTable().getColumn(nextColumn) instanceof CheckTableColumn);
139 return nextColumn;
140 }
141
142
143
144
145
146
147
148 protected int getPreviousColumn(int column) {
149 int nextColumn = column;
150 do {
151 nextColumn--;
152 } while (nextColumn >= 0 && getTable().getColumn(nextColumn) instanceof CheckTableColumn);
153 return nextColumn;
154 }
155
156
157
158
159
160
161
162
163 protected boolean isCellValid(int row, int column) {
164 return row > -1 && column > -1 && row < getRowCount() && column < getColumnCount();
165 }
166
167
168
169
170
171
172
173
174 protected boolean isCellEditable(int row, int column) {
175 return isCellValid(row, column) && getTable().isCellEditable(row, column) && !(getTable().getColumn(column) instanceof CheckTableColumn);
176 }
177
178
179
180
181
182
183
184
185 protected boolean selectCell(int row, int column) {
186
187 if (!isCellValid(row, column)) return false;
188
189
190 if (!stopActiveEdition()) {
191 return false;
192 }
193
194 getTable().setColumnSelectionInterval(column, column);
195 getTable().setRowSelectionInterval(row, row);
196 getTable().scrollCellToVisible(row, column);
197 return true;
198 }
199
200
201
202
203
204
205
206 protected void editCell(int row, int column) {
207 if (selectCell(row, column)) getTable().editCellAt(row, column);
208 }
209
210
211
212
213
214
215 protected boolean stopActiveEdition() {
216 TableCellEditor editor = getTable().getCellEditor();
217 return editor == null || editor.stopCellEditing();
218 }
219
220
221
222
223 protected void selectNextComponent() {
224 Component next = handler.getNextComponentToFocus();
225 if (next != null) {
226 next.requestFocus();
227 }
228 }
229
230
231
232
233 protected void selectPreviousComponent() {
234 Component previous = handler.getPreviousComponentToFocus();
235 if (previous != null) {
236 previous.requestFocus();
237 }
238 }
239
240 public enum Direction {
241 NEXT_CELL, PREVIOUS_CELL, NEXT_ROW, PREVIOUS_ROW
242 }
243 }
244