C:\Users\akluge\Documents\vizit\presentations\FixedJtable\code\src\com\vizitsolutions\identitytable\TabKeyActionHandler.java |
1
2 package com.vizitsolutions.identitytable;
3
4 import javax.swing.AbstractAction;
5 import java.awt.event.ActionEvent;
6 import javax.swing.JTable;
7 import javax.swing.ListSelectionModel;
8 import java.awt.Rectangle;
9
10
11
12 <p></p>
13
14
15
16
17
18
19 @author
20 @version
21
22 public class TabKeyActionHandler extends AbstractAction
23 {
24
25
26
27
28 int delta;
29
30
31
32
33
34
35
36 @see
37
38 ListSelectionModel mainColumnSelectionModel;
39
40
41
42
43
44 JTable mainTable;
45
46
47
48
49
50
51
52 @see
53
54 ListSelectionModel rowHeaderColumnSelectionModel;
55
56
57
58
59 JTable rowHeaderTable;
60
61
62
63
64
65
66 @see
67
68 ListSelectionModel rowSelectionModel;
69
70
71
72
73
74
75 boolean wrap;
76
77
78
79
80
81
82 @param
83
84 @param
85 @param
86 @param
87 @param
88 @param
89 @param
90
91 public TabKeyActionHandler(JTable rowHeaderTable, JTable mainTable,
92 ListSelectionModel rowSelectionModel, ListSelectionModel headerColumnSelectionModel,
93 ListSelectionModel mainColumnSelectionModel, int delta,
94 boolean wrap)
95 {
96 this.rowHeaderTable = rowHeaderTable;
97 this.mainTable = mainTable;
98 this.rowSelectionModel = rowSelectionModel;
99 this.rowHeaderColumnSelectionModel = headerColumnSelectionModel;
100 this.mainColumnSelectionModel = mainColumnSelectionModel;
101 this.delta = delta;
102 this.wrap = wrap;
103 }
104
105
106
107
108
109 @param
110
111 public void actionPerformed(ActionEvent event)
112 {
113 JTable currentTable = (JTable)event.getSource();
114
115
116 if (currentTable.isEditing() && !currentTable.getCellEditor().stopCellEditing())
117 {
118 return;
119 }
120
121 adjustColumn(currentTable);
122 }
123
124
125
126
127 @param
128
129 public void adjustColumn(JTable currentTable)
130 {
131
132
133 int rowIndex = rowSelectionModel.getLeadSelectionIndex();
134 if (rowIndex < 0)
135 {
136 rowIndex = 0;
137 }
138
139 int columnIndex;
140
141
142
143
144 if (currentTable == rowHeaderTable)
145 {
146
147 columnIndex = rowHeaderColumnSelectionModel.getLeadSelectionIndex();
148
149 if (columnIndex < 0)
150 {
151
152 columnIndex = 0;
153 }
154
155
156
157 columnIndex += delta;
158
159
160 if (columnIndex > rowHeaderTable.getColumnCount()-1)
161 {
162
163 Rectangle currentCell = mainTable.getCellRect(rowIndex, 0, true);
164
165 mainTable.scrollRectToVisible(currentCell);
166
167 rowHeaderColumnSelectionModel.clearSelection();
168
169 mainTable.requestFocusInWindow();
170
171 mainColumnSelectionModel.setSelectionInterval(0, 0);
172
173 rowSelectionModel.setSelectionInterval(rowIndex, rowIndex);
174 return;
175 }
176 else if (columnIndex < 0)
177 {
178
179
180
181 if (!wrap)
182 {
183 return;
184 }
185
186 rowIndex -=1;
187 columnIndex = mainTable.getColumnCount()-1;
188
189
190 if (rowIndex < 0)
191 {
192
193 rowIndex = mainTable.getRowCount()-1;
194 }
195
196
197
198 Rectangle currentCell = mainTable.getCellRect(rowIndex, columnIndex, true);
199 mainTable.scrollRectToVisible(currentCell);
200 rowHeaderColumnSelectionModel.clearSelection();
201 mainTable.requestFocusInWindow();
202 mainColumnSelectionModel.setSelectionInterval(columnIndex, columnIndex);
203 rowSelectionModel.setSelectionInterval(rowIndex, rowIndex);
204 return;
205 }
206 else
207 {
208
209 Rectangle currentCell = rowHeaderTable.getCellRect(rowIndex, columnIndex, true);
210 rowHeaderTable.scrollRectToVisible(currentCell);
211 rowHeaderColumnSelectionModel.setSelectionInterval(columnIndex, columnIndex);
212 rowSelectionModel.setSelectionInterval(rowIndex, rowIndex);
213 return;
214 }
215 }
216 else
217 {
218
219 columnIndex = mainColumnSelectionModel.getLeadSelectionIndex();
220 if (columnIndex < 0)
221 {
222 columnIndex = 0;
223 }
224
225 columnIndex += delta;
226
227
228 if (columnIndex > mainTable.getColumnCount()-1)
229 {
230
231 if (!wrap)
232 {
233 return;
234 }
235
236
237 rowIndex += 1;
238 columnIndex = 0;
239
240
241 if (rowIndex > mainTable.getRowCount()-1)
242 {
243
244 rowIndex = 0;
245 }
246
247
248
249
250 Rectangle currentCell = rowHeaderTable.getCellRect(rowIndex, columnIndex, true);
251 rowHeaderTable.scrollRectToVisible(currentCell);
252 currentCell = mainTable.getCellRect(0, 0, true);
253 mainTable.scrollRectToVisible(currentCell);
254 mainColumnSelectionModel.clearSelection();
255 rowHeaderTable.requestFocusInWindow();
256 rowHeaderColumnSelectionModel.setSelectionInterval(columnIndex, columnIndex);
257 rowSelectionModel.setSelectionInterval(rowIndex, rowIndex);
258 return;
259 }
260 else if (columnIndex < 0)
261 {
262
263
264 Rectangle currentCell = rowHeaderTable.getCellRect(rowIndex, columnIndex, true);
265 rowHeaderTable.scrollRectToVisible(currentCell);
266 mainColumnSelectionModel.clearSelection();
267 rowHeaderTable.requestFocusInWindow();
268
269 columnIndex = rowHeaderTable.getColumnCount()-1;
270 rowHeaderColumnSelectionModel.setSelectionInterval(columnIndex, columnIndex);
271 rowSelectionModel.setSelectionInterval(rowIndex, rowIndex);
272 return;
273 }
274 else
275 {
276
277 Rectangle currentCell = mainTable.getCellRect(rowIndex, columnIndex, true);
278 mainTable.scrollRectToVisible(currentCell);
279 mainColumnSelectionModel.setSelectionInterval(columnIndex, columnIndex);
280 rowSelectionModel.setSelectionInterval(rowIndex, rowIndex);
281 return;
282 }
283 }
284 }
285 }
286
287