46 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
47 |
import java.awt.event.FocusEvent; |
import java.awt.event.FocusEvent; |
48 |
import java.awt.event.FocusListener; |
import java.awt.event.FocusListener; |
49 |
|
import java.awt.event.InputEvent; |
50 |
import java.awt.event.KeyEvent; |
import java.awt.event.KeyEvent; |
51 |
import java.awt.event.KeyListener; |
import java.awt.event.KeyListener; |
52 |
import java.awt.event.MouseEvent; |
import java.awt.event.MouseEvent; |
267 |
{ |
{ |
268 |
ListSelectionModel rowModel = table.getSelectionModel(); |
ListSelectionModel rowModel = table.getSelectionModel(); |
269 |
ListSelectionModel colModel = table.getColumnModel().getSelectionModel(); |
ListSelectionModel colModel = table.getColumnModel().getSelectionModel(); |
270 |
|
|
271 |
int rowLead = rowModel.getLeadSelectionIndex(); |
int rowLead = rowModel.getLeadSelectionIndex(); |
272 |
int rowMax = table.getModel().getRowCount() - 1; |
int rowMax = table.getModel().getRowCount() - 1; |
273 |
|
|
274 |
int colLead = colModel.getLeadSelectionIndex(); |
int colLead = colModel.getLeadSelectionIndex(); |
275 |
int colMax = table.getModel().getColumnCount() - 1; |
int colMax = table.getModel().getColumnCount() - 1; |
276 |
|
|
277 |
if ((evt.getKeyCode() == KeyEvent.VK_DOWN) |
if ((evt.getKeyCode() == KeyEvent.VK_DOWN) |
278 |
|| (evt.getKeyCode() == KeyEvent.VK_KP_DOWN)) |
|| (evt.getKeyCode() == KeyEvent.VK_KP_DOWN)) |
279 |
{ |
{ |
280 |
if (!evt.isShiftDown()) |
if (evt.getModifiers() == 0) |
281 |
{ |
{ |
282 |
|
|
283 |
table.clearSelection(); |
table.clearSelection(); |
285 |
Math.min(rowLead + 1, rowMax)); |
Math.min(rowLead + 1, rowMax)); |
286 |
colModel.setSelectionInterval(colLead,colLead); |
colModel.setSelectionInterval(colLead,colLead); |
287 |
} |
} |
288 |
else |
else if (evt.getModifiers() == InputEvent.SHIFT_MASK) |
289 |
{ |
{ |
290 |
rowModel.setLeadSelectionIndex(Math.min(rowLead + 1, rowMax)); |
rowModel.setLeadSelectionIndex(Math.min(rowLead + 1, rowMax)); |
291 |
colModel.setLeadSelectionIndex(colLead); |
colModel.setLeadSelectionIndex(colLead); |
294 |
else if ((evt.getKeyCode() == KeyEvent.VK_UP) |
else if ((evt.getKeyCode() == KeyEvent.VK_UP) |
295 |
|| (evt.getKeyCode() == KeyEvent.VK_KP_UP)) |
|| (evt.getKeyCode() == KeyEvent.VK_KP_UP)) |
296 |
{ |
{ |
297 |
if (!evt.isShiftDown()) |
if (evt.getModifiers() == 0) |
298 |
{ |
{ |
299 |
table.clearSelection(); |
table.clearSelection(); |
300 |
rowModel.setSelectionInterval(Math.max(rowLead - 1, 0), |
rowModel.setSelectionInterval(Math.max(rowLead - 1, 0), |
301 |
Math.max(rowLead - 1, 0)); |
Math.max(rowLead - 1, 0)); |
302 |
colModel.setSelectionInterval(colLead,colLead); |
colModel.setSelectionInterval(colLead,colLead); |
303 |
} |
} |
304 |
else |
else if (evt.getModifiers() == InputEvent.SHIFT_MASK) |
305 |
{ |
{ |
306 |
rowModel.setLeadSelectionIndex(Math.max(rowLead - 1, 0)); |
rowModel.setLeadSelectionIndex(Math.max(rowLead - 1, 0)); |
307 |
colModel.setLeadSelectionIndex(colLead); |
colModel.setLeadSelectionIndex(colLead); |
310 |
else if ((evt.getKeyCode() == KeyEvent.VK_LEFT) |
else if ((evt.getKeyCode() == KeyEvent.VK_LEFT) |
311 |
|| (evt.getKeyCode() == KeyEvent.VK_KP_LEFT)) |
|| (evt.getKeyCode() == KeyEvent.VK_KP_LEFT)) |
312 |
{ |
{ |
313 |
if (evt.isShiftDown()) |
if (evt.getModifiers() == InputEvent.SHIFT_MASK) |
314 |
{ |
{ |
315 |
colModel.setLeadSelectionIndex(Math.max(colLead - 1, 0)); |
colModel.setLeadSelectionIndex(Math.max(colLead - 1, 0)); |
316 |
rowModel.setLeadSelectionIndex(rowLead); |
rowModel.setLeadSelectionIndex(rowLead); |
317 |
} |
} |
318 |
else |
else if (evt.getModifiers() == 0) |
319 |
{ |
{ |
320 |
table.clearSelection(); |
table.clearSelection(); |
321 |
rowModel.setSelectionInterval(rowLead,rowLead); |
rowModel.setSelectionInterval(rowLead,rowLead); |
326 |
else if ((evt.getKeyCode() == KeyEvent.VK_RIGHT) |
else if ((evt.getKeyCode() == KeyEvent.VK_RIGHT) |
327 |
|| (evt.getKeyCode() == KeyEvent.VK_KP_RIGHT)) |
|| (evt.getKeyCode() == KeyEvent.VK_KP_RIGHT)) |
328 |
{ |
{ |
329 |
if (evt.isShiftDown()) |
if (evt.getModifiers() == InputEvent.SHIFT_MASK) |
330 |
{ |
{ |
331 |
colModel.setLeadSelectionIndex(Math.min(colLead + 1, colMax)); |
colModel.setLeadSelectionIndex(Math.min(colLead + 1, colMax)); |
332 |
rowModel.setLeadSelectionIndex(rowLead); |
rowModel.setLeadSelectionIndex(rowLead); |
333 |
} |
} |
334 |
else |
else if (evt.getModifiers() == 0) |
335 |
{ |
{ |
336 |
table.clearSelection(); |
table.clearSelection(); |
337 |
rowModel.setSelectionInterval(rowLead,rowLead); |
rowModel.setSelectionInterval(rowLead,rowLead); |
341 |
} |
} |
342 |
else if (evt.getKeyCode() == KeyEvent.VK_END) |
else if (evt.getKeyCode() == KeyEvent.VK_END) |
343 |
{ |
{ |
344 |
if (evt.isControlDown() && evt.isShiftDown()) |
if (evt.getModifiers() == (InputEvent.SHIFT_MASK | InputEvent.CTRL_MASK)) |
345 |
{ |
{ |
346 |
rowModel.setLeadSelectionIndex(rowMax); |
rowModel.setLeadSelectionIndex(rowMax); |
347 |
colModel.setLeadSelectionIndex(colLead); |
colModel.setLeadSelectionIndex(colLead); |
348 |
} |
} |
349 |
else if (evt.isControlDown()) |
else if (evt.getModifiers() == InputEvent.CTRL_MASK) |
350 |
{ |
{ |
351 |
table.clearSelection(); |
table.clearSelection(); |
352 |
rowModel.setSelectionInterval(rowMax,rowMax); |
rowModel.setSelectionInterval(rowMax,rowMax); |
353 |
colModel.setSelectionInterval(colLead, colLead); |
colModel.setSelectionInterval(colLead, colLead); |
354 |
} |
} |
355 |
else if (evt.isShiftDown()) |
else if (evt.getModifiers() == InputEvent.SHIFT_MASK) |
356 |
{ |
{ |
357 |
colModel.setLeadSelectionIndex(colMax); |
colModel.setLeadSelectionIndex(colMax); |
358 |
rowModel.setLeadSelectionIndex(rowLead); |
rowModel.setLeadSelectionIndex(rowLead); |
359 |
} |
} |
360 |
else |
else if (evt.getModifiers() == 0) |
361 |
{ |
{ |
362 |
table.clearSelection(); |
table.clearSelection(); |
363 |
rowModel.setSelectionInterval(rowLead, rowLead); |
rowModel.setSelectionInterval(rowLead, rowLead); |
364 |
colModel.setSelectionInterval(colMax, colMax); |
colModel.setSelectionInterval(colMax, colMax); |
365 |
} |
} |
366 |
} |
} |
367 |
else if (evt.getKeyCode() == KeyEvent.VK_HOME) |
else if (evt.getKeyCode() == KeyEvent.VK_HOME) |
368 |
{ |
{ |
369 |
if (evt.isControlDown() && evt.isShiftDown()) |
if (evt.getModifiers() == (InputEvent.SHIFT_MASK | InputEvent.CTRL_MASK)) |
370 |
{ |
{ |
371 |
rowModel.setLeadSelectionIndex(0); |
rowModel.setLeadSelectionIndex(0); |
372 |
colModel.setLeadSelectionIndex(colLead); |
colModel.setLeadSelectionIndex(colLead); |
373 |
} |
} |
374 |
else if (evt.isControlDown()) |
else if (evt.getModifiers() == InputEvent.CTRL_MASK) |
375 |
{ |
{ |
376 |
table.clearSelection(); |
table.clearSelection(); |
377 |
rowModel.setSelectionInterval(0,0); |
rowModel.setSelectionInterval(0,0); |
378 |
colModel.setSelectionInterval(colLead, colLead); |
colModel.setSelectionInterval(colLead, colLead); |
379 |
} |
} |
380 |
else if (evt.isShiftDown()) |
else if (evt.getModifiers() == InputEvent.SHIFT_MASK) |
381 |
{ |
{ |
382 |
colModel.setLeadSelectionIndex(0); |
colModel.setLeadSelectionIndex(0); |
383 |
rowModel.setLeadSelectionIndex(rowLead); |
rowModel.setLeadSelectionIndex(rowLead); |
384 |
} |
} |
385 |
else |
else if (evt.getModifiers() == 0) |
386 |
{ |
{ |
387 |
table.clearSelection(); |
table.clearSelection(); |
388 |
rowModel.setSelectionInterval(rowLead, rowLead); |
rowModel.setSelectionInterval(rowLead, rowLead); |
404 |
else if (evt.getKeyCode() == KeyEvent.VK_TAB |
else if (evt.getKeyCode() == KeyEvent.VK_TAB |
405 |
|| evt.getKeyCode() == KeyEvent.VK_ENTER) |
|| evt.getKeyCode() == KeyEvent.VK_ENTER) |
406 |
{ |
{ |
407 |
|
// If modifers other than SHIFT are pressed, do nothing |
408 |
|
if (evt.getModifiers() != 0 && evt.getModifiers() != |
409 |
|
InputEvent.SHIFT_MASK) |
410 |
|
return; |
411 |
|
|
412 |
// If nothing is selected, select the first cell in the table |
// If nothing is selected, select the first cell in the table |
413 |
if (table.getSelectedRowCount() == 0 && |
if (table.getSelectedRowCount() == 0 && |
414 |
table.getSelectedColumnCount() == 0) |
table.getSelectedColumnCount() == 0) |
429 |
colModel.getMinSelectionIndex()); |
colModel.getMinSelectionIndex()); |
430 |
return; |
return; |
431 |
} |
} |
432 |
|
|
433 |
// multRowsSelected and multColsSelected tell us if multiple rows or |
// multRowsSelected and multColsSelected tell us if multiple rows or |
434 |
// columns are selected, respectively |
// columns are selected, respectively |
435 |
boolean multRowsSelected, multColsSelected; |
boolean multRowsSelected, multColsSelected; |
439 |
multColsSelected = (table.getSelectedColumnCount() > 1) || |
multColsSelected = (table.getSelectedColumnCount() > 1) || |
440 |
(!table.getColumnSelectionAllowed() && |
(!table.getColumnSelectionAllowed() && |
441 |
table.getSelectedRowCount() > 0); |
table.getSelectedRowCount() > 0); |
442 |
|
|
443 |
// If there is just one selection, select the next cell, and wrap |
// If there is just one selection, select the next cell, and wrap |
444 |
// when you get to the edges of the table. |
// when you get to the edges of the table. |
445 |
if (!multColsSelected || !multRowsSelected) |
if (!multColsSelected || !multRowsSelected) |
446 |
{ |
{ |
447 |
if (evt.getKeyCode() == KeyEvent.VK_TAB) |
if (evt.getKeyCode() == KeyEvent.VK_TAB) |
448 |
advanceSingleSelection(colModel, colMax, rowModel, rowMax, evt.isShiftDown()); |
advanceSingleSelection(colModel, colMax, rowModel, rowMax, |
449 |
|
(evt.getModifiers() == |
450 |
|
InputEvent.SHIFT_MASK)); |
451 |
else |
else |
452 |
advanceSingleSelection(rowModel, rowMax, colModel, colMax, evt.isShiftDown()); |
advanceSingleSelection(rowModel, rowMax, colModel, colMax, |
453 |
|
(evt.getModifiers() == |
454 |
|
InputEvent.SHIFT_MASK)); |
455 |
return; |
return; |
456 |
} |
} |
457 |
|
|
458 |
|
|
459 |
// rowMinSelected and rowMaxSelected are the minimum and maximum |
// rowMinSelected and rowMaxSelected are the minimum and maximum |
460 |
// values respectively of selected cells in the row selection model |
// values respectively of selected cells in the row selection model |
461 |
// Similarly for colMinSelected and colMaxSelected. |
// Similarly for colMinSelected and colMaxSelected. |
468 |
table.getModel().getColumnCount() - 1; |
table.getModel().getColumnCount() - 1; |
469 |
int colMinSelected = table.getColumnSelectionAllowed() ? |
int colMinSelected = table.getColumnSelectionAllowed() ? |
470 |
colModel.getMinSelectionIndex() : 0; |
colModel.getMinSelectionIndex() : 0; |
471 |
|
|
472 |
// If there are multiple rows and columns selected, select the next |
// If there are multiple rows and columns selected, select the next |
473 |
// cell and wrap at the edges of the selection. |
// cell and wrap at the edges of the selection. |
474 |
if (evt.getKeyCode() == KeyEvent.VK_TAB) |
if (evt.getKeyCode() == KeyEvent.VK_TAB) |
475 |
advanceMultipleSelection(colModel, colMinSelected, colMaxSelected, rowModel, rowMinSelected, rowMaxSelected, evt.isShiftDown(), true); |
advanceMultipleSelection(colModel, colMinSelected, colMaxSelected, |
476 |
|
rowModel, rowMinSelected, rowMaxSelected, |
477 |
|
(evt.getModifiers() == |
478 |
|
InputEvent.SHIFT_MASK), true); |
479 |
else |
else |
480 |
advanceMultipleSelection(rowModel, rowMinSelected, rowMaxSelected, colModel, colMinSelected, colMaxSelected, evt.isShiftDown(), false); |
advanceMultipleSelection(rowModel, rowMinSelected, rowMaxSelected, |
481 |
|
colModel, colMinSelected, colMaxSelected, |
482 |
|
(evt.getModifiers() == |
483 |
|
InputEvent.SHIFT_MASK), false); |
484 |
|
|
485 |
table.repaint(); |
table.repaint(); |
486 |
} |
} |
487 |
else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) |
else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) |
489 |
// FIXME: implement "cancel" |
// FIXME: implement "cancel" |
490 |
} |
} |
491 |
else if ((evt.getKeyCode() == KeyEvent.VK_A || evt.getKeyCode() |
else if ((evt.getKeyCode() == KeyEvent.VK_A || evt.getKeyCode() |
492 |
== KeyEvent.VK_SLASH) && evt.isControlDown()) |
== KeyEvent.VK_SLASH) && (evt.getModifiers() == |
493 |
|
InputEvent.CTRL_MASK)) |
494 |
{ |
{ |
495 |
table.selectAll(); |
table.selectAll(); |
496 |
} |
} |
497 |
else if (evt.getKeyCode() == KeyEvent.VK_BACK_SLASH |
else if (evt.getKeyCode() == KeyEvent.VK_BACK_SLASH |
498 |
&& evt.isControlDown()) |
&& (evt.getModifiers() == InputEvent.CTRL_MASK)) |
499 |
{ |
{ |
500 |
table.clearSelection(); |
table.clearSelection(); |
501 |
} |
} |
502 |
else if (evt.getKeyCode() == KeyEvent.VK_SPACE |
else if (evt.getKeyCode() == KeyEvent.VK_SPACE |
503 |
&& evt.isControlDown()) |
&& (evt.getModifiers() == InputEvent.CTRL_MASK)) |
504 |
{ |
{ |
505 |
table.changeSelection(rowLead, colLead, true, false); |
table.changeSelection(rowLead, colLead, true, false); |
506 |
} |
} |