1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82:
83: public class BasicTableUI extends TableUI
84: {
85: public static ComponentUI createUI(JComponent comp)
86: {
87: return new BasicTableUI();
88: }
89:
90: protected FocusListener focusListener;
91: protected KeyListener keyListener;
92: protected MouseInputListener mouseInputListener;
93: protected CellRendererPane rendererPane;
94: protected JTable table;
95:
96:
97: Border cellBorder;
98:
99:
100: TableAction action;
101:
102:
105: private PropertyChangeListener propertyChangeListener;
106:
107:
114: public class KeyHandler implements KeyListener
115: {
116:
117:
124: public void keyTyped(KeyEvent event)
125: {
126:
127:
128:
129:
130:
131: if (!table.isEditing() && table.isEnabled())
132: {
133: int r = table.getSelectedRow();
134: int c = table.getSelectedColumn();
135: if (table.isCellEditable(r, c))
136: table.editCellAt(r, c);
137: }
138: }
139:
140:
145: public void keyPressed(KeyEvent event)
146: {
147:
148:
149: }
150:
151:
156: public void keyReleased(KeyEvent event)
157: {
158:
159:
160: }
161: }
162:
163: public class FocusHandler implements FocusListener
164: {
165: public void focusGained(FocusEvent e)
166: {
167:
168:
169: repaintLeadCell();
170: }
171:
172: public void focusLost(FocusEvent e)
173: {
174:
175:
176: repaintLeadCell();
177: }
178:
179:
183: private void repaintLeadCell()
184: {
185: int rowCount = table.getRowCount();
186: int columnCount = table.getColumnCount();
187: int rowLead = table.getSelectionModel().getLeadSelectionIndex();
188: int columnLead = table.getColumnModel().getSelectionModel().
189: getLeadSelectionIndex();
190: if (rowLead >= 0 && rowLead < rowCount && columnLead >= 0
191: && columnLead < columnCount)
192: {
193: Rectangle dirtyRect = table.getCellRect(rowLead, columnLead, false);
194: table.repaint(dirtyRect);
195: }
196: }
197: }
198:
199: public class MouseInputHandler implements MouseInputListener
200: {
201: Point begin, curr;
202:
203: private void updateSelection(boolean controlPressed)
204: {
205:
206: int lo_row = table.rowAtPoint(begin);
207: int hi_row = table.rowAtPoint(curr);
208: ListSelectionModel rowModel = table.getSelectionModel();
209: if (lo_row != -1 && hi_row != -1)
210: {
211: if (controlPressed && rowModel.getSelectionMode()
212: != ListSelectionModel.SINGLE_SELECTION)
213: rowModel.addSelectionInterval(lo_row, hi_row);
214: else
215: rowModel.setSelectionInterval(lo_row, hi_row);
216: }
217:
218:
219: int lo_col = table.columnAtPoint(begin);
220: int hi_col = table.columnAtPoint(curr);
221: ListSelectionModel colModel = table.getColumnModel().
222: getSelectionModel();
223: if (lo_col != -1 && hi_col != -1)
224: {
225: if (controlPressed && colModel.getSelectionMode() !=
226: ListSelectionModel.SINGLE_SELECTION)
227: colModel.addSelectionInterval(lo_col, hi_col);
228: else
229: colModel.setSelectionInterval(lo_col, hi_col);
230: }
231: }
232:
233:
236: public void mouseClicked(MouseEvent e)
237: {
238: Point p = e.getPoint();
239: int row = table.rowAtPoint(p);
240: int col = table.columnAtPoint(p);
241: if (table.isCellEditable(row, col))
242: {
243:
244:
245:
246: TableCellEditor editor = table.getCellEditor(row, col);
247: if (editor instanceof DefaultCellEditor)
248: {
249: DefaultCellEditor ce = (DefaultCellEditor) editor;
250: if (e.getClickCount() < ce.getClickCountToStart())
251: return;
252: }
253: table.editCellAt(row, col);
254: }
255: }
256:
257: public void mouseDragged(MouseEvent e)
258: {
259: if (table.isEnabled())
260: {
261: curr = new Point(e.getX(), e.getY());
262: updateSelection(e.isControlDown());
263: }
264: }
265:
266: public void mouseEntered(MouseEvent e)
267: {
268:
269: }
270:
271: public void mouseExited(MouseEvent e)
272: {
273:
274: }
275:
276: public void mouseMoved(MouseEvent e)
277: {
278:
279: }
280:
281: public void mousePressed(MouseEvent e)
282: {
283: if (table.isEnabled())
284: {
285: ListSelectionModel rowModel = table.getSelectionModel();
286: ListSelectionModel colModel = table.getColumnModel().getSelectionModel();
287: int rowLead = rowModel.getLeadSelectionIndex();
288: int colLead = colModel.getLeadSelectionIndex();
289:
290: begin = new Point(e.getX(), e.getY());
291: curr = new Point(e.getX(), e.getY());
292:
293: if (e.isControlDown() && table.isCellSelected(
294: table.rowAtPoint(begin), table.columnAtPoint(begin)))
295: {
296: table.getSelectionModel().
297: removeSelectionInterval(table.rowAtPoint(begin),
298: table.rowAtPoint(begin));
299: table.getColumnModel().getSelectionModel().
300: removeSelectionInterval(table.columnAtPoint(begin),
301: table.columnAtPoint(begin));
302: }
303: else
304: updateSelection(e.isControlDown());
305:
306:
307: if (rowLead != rowModel.getLeadSelectionIndex() ||
308: colLead != colModel.getLeadSelectionIndex())
309: if (table.isEditing())
310: table.editingStopped(new ChangeEvent(e));
311:
312:
313: table.requestFocusInWindow();
314: }
315: }
316:
317: public void mouseReleased(MouseEvent e)
318: {
319: if (table.isEnabled())
320: {
321: begin = null;
322: curr = null;
323: }
324: }
325: }
326:
327:
333: private class PropertyChangeHandler implements PropertyChangeListener
334: {
335:
340: public void propertyChange(PropertyChangeEvent ev)
341: {
342: String propName = ev.getPropertyName();
343: if (propName.equals("model"))
344: {
345: ListSelectionModel rowSel = table.getSelectionModel();
346: rowSel.clearSelection();
347: ListSelectionModel colSel = table.getColumnModel().getSelectionModel();
348: colSel.clearSelection();
349: TableModel model = table.getModel();
350:
351:
352:
353: if (model.getRowCount() > 0)
354: {
355: rowSel.setAnchorSelectionIndex(0);
356: rowSel.setLeadSelectionIndex(0);
357: }
358: else
359: {
360: rowSel.setAnchorSelectionIndex(-1);
361: rowSel.setLeadSelectionIndex(-1);
362: }
363: if (model.getColumnCount() > 0)
364: {
365: colSel.setAnchorSelectionIndex(0);
366: colSel.setLeadSelectionIndex(0);
367: }
368: else
369: {
370: colSel.setAnchorSelectionIndex(-1);
371: colSel.setLeadSelectionIndex(-1);
372: }
373: }
374: }
375: }
376:
377: protected FocusListener createFocusListener()
378: {
379: return new FocusHandler();
380: }
381:
382: protected MouseInputListener createMouseInputListener()
383: {
384: return new MouseInputHandler();
385: }
386:
387:
388:
393: protected KeyListener createKeyListener()
394: {
395: return new KeyHandler();
396: }
397:
398:
408: public Dimension getMaximumSize(JComponent comp)
409: {
410: int maxTotalColumnWidth = 0;
411: for (int i = 0; i < table.getColumnCount(); i++)
412: maxTotalColumnWidth += table.getColumnModel().getColumn(i).getMaxWidth();
413:
414: return new Dimension(maxTotalColumnWidth, getHeight());
415: }
416:
417:
427: public Dimension getMinimumSize(JComponent comp)
428: {
429: int minTotalColumnWidth = 0;
430: for (int i = 0; i < table.getColumnCount(); i++)
431: minTotalColumnWidth += table.getColumnModel().getColumn(i).getMinWidth();
432:
433: return new Dimension(minTotalColumnWidth, getHeight());
434: }
435:
436:
443: public Dimension getPreferredSize(JComponent comp)
444: {
445: int prefTotalColumnWidth = 0;
446: for (int i = 0; i < table.getColumnCount(); i++)
447: {
448: TableColumn col = table.getColumnModel().getColumn(i);
449: prefTotalColumnWidth += col.getPreferredWidth();
450: }
451: return new Dimension(prefTotalColumnWidth, getHeight());
452: }
453:
454:
461: private int getHeight()
462: {
463: int height = 0;
464: int rowCount = table.getRowCount();
465: if (rowCount > 0 && table.getColumnCount() > 0)
466: {
467: Rectangle r = table.getCellRect(rowCount - 1, 0, true);
468: height = r.y + r.height;
469: }
470: return height;
471: }
472:
473: protected void installDefaults()
474: {
475: LookAndFeel.installColorsAndFont(table, "Table.background",
476: "Table.foreground", "Table.font");
477: table.setGridColor(UIManager.getColor("Table.gridColor"));
478: table.setSelectionForeground(UIManager.getColor("Table.selectionForeground"));
479: table.setSelectionBackground(UIManager.getColor("Table.selectionBackground"));
480: table.setOpaque(true);
481: }
482:
483:
486: protected void installKeyboardActions()
487: {
488:
489: InputMap inputMap =
490: (InputMap) SharedUIDefaults.get("Table.ancestorInputMap");
491: SwingUtilities.replaceUIInputMap(table,
492: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
493: inputMap);
494:
495:
496: SwingUtilities.replaceUIActionMap(table, getActionMap());
497:
498: }
499:
500:
506: private ActionMap getActionMap()
507: {
508: ActionMap am = (ActionMap) UIManager.get("Table.actionMap");
509: if (am == null)
510: {
511: am = createDefaultActions();
512: UIManager.getLookAndFeelDefaults().put("Table.actionMap", am);
513: }
514: return am;
515: }
516:
517: private ActionMap createDefaultActions()
518: {
519: ActionMapUIResource am = new ActionMapUIResource();
520: Action action = new TableAction();
521:
522: am.put("cut", TransferHandler.getCutAction());
523: am.put("copy", TransferHandler.getCopyAction());
524: am.put("paste", TransferHandler.getPasteAction());
525:
526: am.put("cancel", action);
527: am.put("selectAll", action);
528: am.put("clearSelection", action);
529: am.put("startEditing", action);
530:
531: am.put("selectNextRow", action);
532: am.put("selectNextRowCell", action);
533: am.put("selectNextRowExtendSelection", action);
534: am.put("selectNextRowChangeLead", action);
535:
536: am.put("selectPreviousRow", action);
537: am.put("selectPreviousRowCell", action);
538: am.put("selectPreviousRowExtendSelection", action);
539: am.put("selectPreviousRowChangeLead", action);
540:
541: am.put("selectNextColumn", action);
542: am.put("selectNextColumnCell", action);
543: am.put("selectNextColumnExtendSelection", action);
544: am.put("selectNextColumnChangeLead", action);
545:
546: am.put("selectPreviousColumn", action);
547: am.put("selectPreviousColumnCell", action);
548: am.put("selectPreviousColumnExtendSelection", action);
549: am.put("selectPreviousColumnChangeLead", action);
550:
551: am.put("scrollLeftChangeSelection", action);
552: am.put("scrollLeftExtendSelection", action);
553: am.put("scrollRightChangeSelection", action);
554: am.put("scrollRightExtendSelection", action);
555:
556: am.put("scrollUpChangeSelection", action);
557: am.put("scrollUpExtendSelection", action);
558: am.put("scrollDownChangeSelection", action);
559: am.put("scrolldownExtendSelection", action);
560:
561: am.put("selectFirstColumn", action);
562: am.put("selectFirstColumnExtendSelection", action);
563: am.put("selectLastColumn", action);
564: am.put("selectLastColumnExtendSelection", action);
565:
566: am.put("selectFirstRow", action);
567: am.put("selectFirstRowExtendSelection", action);
568: am.put("selectLastRow", action);
569: am.put("selectLastRowExtendSelection", action);
570:
571: am.put("addToSelection", action);
572: am.put("toggleAndAnchor", action);
573: am.put("extendTo", action);
574: am.put("moveSelectionTo", action);
575:
576: return am;
577: }
578:
579:
585: private static class TableAction
586: extends AbstractAction
587: {
588:
593: public void actionPerformed(ActionEvent e)
594: {
595: JTable table = (JTable) e.getSource();
596:
597: DefaultListSelectionModel rowModel
598: = (DefaultListSelectionModel) table.getSelectionModel();
599: DefaultListSelectionModel colModel
600: = (DefaultListSelectionModel) table.getColumnModel().getSelectionModel();
601:
602: int rowLead = rowModel.getLeadSelectionIndex();
603: int rowMax = table.getModel().getRowCount() - 1;
604:
605: int colLead = colModel.getLeadSelectionIndex();
606: int colMax = table.getModel().getColumnCount() - 1;
607:
608:
609:
610:
611: String command = (String) getValue("__command__");
612: if (command.equals("selectPreviousRowExtendSelection"))
613: {
614: rowModel.setLeadSelectionIndex(Math.max(rowLead - 1, 0));
615: }
616: else if (command.equals("selectLastColumn"))
617: {
618: colModel.setSelectionInterval(colMax, colMax);
619: }
620: else if (command.equals("startEditing"))
621: {
622: if (table.isCellEditable(rowLead, colLead))
623: table.editCellAt(rowLead, colLead);
624: }
625: else if (command.equals("selectFirstRowExtendSelection"))
626: {
627: rowModel.setLeadSelectionIndex(0);
628: }
629: else if (command.equals("selectFirstColumn"))
630: {
631: colModel.setSelectionInterval(0, 0);
632: }
633: else if (command.equals("selectFirstColumnExtendSelection"))
634: {
635: colModel.setLeadSelectionIndex(0);
636: }
637: else if (command.equals("selectLastRow"))
638: {
639: rowModel.setSelectionInterval(rowMax, rowMax);
640: }
641: else if (command.equals("selectNextRowExtendSelection"))
642: {
643: rowModel.setLeadSelectionIndex(Math.min(rowLead + 1, rowMax));
644: }
645: else if (command.equals("selectFirstRow"))
646: {
647: rowModel.setSelectionInterval(0, 0);
648: }
649: else if (command.equals("selectNextColumnExtendSelection"))
650: {
651: colModel.setLeadSelectionIndex(Math.min(colLead + 1, colMax));
652: }
653: else if (command.equals("selectLastColumnExtendSelection"))
654: {
655: colModel.setLeadSelectionIndex(colMax);
656: }
657: else if (command.equals("selectPreviousColumnExtendSelection"))
658: {
659: colModel.setLeadSelectionIndex(Math.max(colLead - 1, 0));
660: }
661: else if (command.equals("selectNextRow"))
662: {
663: rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),
664: Math.min(rowLead + 1, rowMax));
665: }
666: else if (command.equals("scrollUpExtendSelection"))
667: {
668: int target;
669: if (rowLead == getFirstVisibleRowIndex(table))
670: target = Math.max(0, rowLead - (getLastVisibleRowIndex(table)
671: - getFirstVisibleRowIndex(table) + 1));
672: else
673: target = getFirstVisibleRowIndex(table);
674:
675: rowModel.setLeadSelectionIndex(target);
676: colModel.setLeadSelectionIndex(colLead);
677: }
678: else if (command.equals("selectPreviousRow"))
679: {
680: rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),
681: Math.max(rowLead - 1, 0));
682: }
683: else if (command.equals("scrollRightChangeSelection"))
684: {
685: int target;
686: if (colLead == getLastVisibleColumnIndex(table))
687: target = Math.min(colMax, colLead
688: + (getLastVisibleColumnIndex(table)
689: - getFirstVisibleColumnIndex(table) + 1));
690: else
691: target = getLastVisibleColumnIndex(table);
692:
693: colModel.setSelectionInterval(target, target);
694: rowModel.setSelectionInterval(rowLead, rowLead);
695: }
696: else if (command.equals("selectPreviousColumn"))
697: {
698: colModel.setSelectionInterval(Math.max(colLead - 1, 0),
699: Math.max(colLead - 1, 0));
700: }
701: else if (command.equals("scrollLeftChangeSelection"))
702: {
703: int target;
704: if (colLead == getFirstVisibleColumnIndex(table))
705: target = Math.max(0, colLead - (getLastVisibleColumnIndex(table)
706: - getFirstVisibleColumnIndex(table) + 1));
707: else
708: target = getFirstVisibleColumnIndex(table);
709:
710: colModel.setSelectionInterval(target, target);
711: rowModel.setSelectionInterval(rowLead, rowLead);
712: }
713: else if (command.equals("clearSelection"))
714: {
715: table.clearSelection();
716: }
717: else if (command.equals("cancel"))
718: {
719:
720:
721:
722: if (table.isEditing())
723: table.editingCanceled(new ChangeEvent("cancel"));
724: }
725: else if (command.equals("selectNextRowCell")
726: || command.equals("selectPreviousRowCell")
727: || command.equals("selectNextColumnCell")
728: || command.equals("selectPreviousColumnCell"))
729: {
730:
731: if (table.getSelectedRowCount() == 0 &&
732: table.getSelectedColumnCount() == 0)
733: {
734: rowModel.setSelectionInterval(0, 0);
735: colModel.setSelectionInterval(0, 0);
736: return;
737: }
738:
739:
740:
741:
742: if (!table.isCellSelected(rowLead, colLead))
743: {
744: rowModel.addSelectionInterval(rowModel.getMinSelectionIndex(),
745: rowModel.getMinSelectionIndex());
746: colModel.addSelectionInterval(colModel.getMinSelectionIndex(),
747: colModel.getMinSelectionIndex());
748: return;
749: }
750:
751:
752:
753: boolean multRowsSelected, multColsSelected;
754: multRowsSelected = table.getSelectedRowCount() > 1 &&
755: table.getRowSelectionAllowed();
756:
757: multColsSelected = table.getSelectedColumnCount() > 1 &&
758: table.getColumnSelectionAllowed();
759:
760:
761:
762: if (!multColsSelected && !multRowsSelected)
763: {
764: if (command.indexOf("Column") != -1)
765: advanceSingleSelection(colModel, colMax, rowModel, rowMax,
766: command.equals("selectPreviousColumnCell"));
767: else
768: advanceSingleSelection(rowModel, rowMax, colModel, colMax,
769: command.equals("selectPreviousRowCell"));
770: return;
771: }
772:
773:
774:
775:
776:
777: int rowMaxSelected = table.getRowSelectionAllowed() ?
778: rowModel.getMaxSelectionIndex() : table.getModel().getRowCount() - 1;
779: int rowMinSelected = table.getRowSelectionAllowed() ?
780: rowModel.getMinSelectionIndex() : 0;
781: int colMaxSelected = table.getColumnSelectionAllowed() ?
782: colModel.getMaxSelectionIndex() :
783: table.getModel().getColumnCount() - 1;
784: int colMinSelected = table.getColumnSelectionAllowed() ?
785: colModel.getMinSelectionIndex() : 0;
786:
787:
788:
789: if (command.indexOf("Column") != -1)
790: advanceMultipleSelection(table, colModel, colMinSelected,
791: colMaxSelected, rowModel, rowMinSelected,
792: rowMaxSelected,
793: command.equals("selectPreviousColumnCell"),
794: true);
795:
796: else
797: advanceMultipleSelection(table, rowModel, rowMinSelected,
798: rowMaxSelected, colModel, colMinSelected,
799: colMaxSelected,
800: command.equals("selectPreviousRowCell"),
801: false);
802: }
803: else if (command.equals("selectNextColumn"))
804: {
805: colModel.setSelectionInterval(Math.min(colLead + 1, colMax),
806: Math.min(colLead + 1, colMax));
807: }
808: else if (command.equals("scrollLeftExtendSelection"))
809: {
810: int target;
811: if (colLead == getFirstVisibleColumnIndex(table))
812: target = Math.max(0, colLead - (getLastVisibleColumnIndex(table)
813: - getFirstVisibleColumnIndex(table) + 1));
814: else
815: target = getFirstVisibleColumnIndex(table);
816:
817: colModel.setLeadSelectionIndex(target);
818: rowModel.setLeadSelectionIndex(rowLead);
819: }
820: else if (command.equals("scrollDownChangeSelection"))
821: {
822: int target;
823: if (rowLead == getLastVisibleRowIndex(table))
824: target = Math.min(rowMax, rowLead + (getLastVisibleRowIndex(table)
825: - getFirstVisibleRowIndex(table) + 1));
826: else
827: target = getLastVisibleRowIndex(table);
828:
829: rowModel.setSelectionInterval(target, target);
830: colModel.setSelectionInterval(colLead, colLead);
831: }
832: else if (command.equals("scrollRightExtendSelection"))
833: {
834: int target;
835: if (colLead == getLastVisibleColumnIndex(table))
836: target = Math.min(colMax, colLead + (getLastVisibleColumnIndex(table)
837: - getFirstVisibleColumnIndex(table) + 1));
838: else
839: target = getLastVisibleColumnIndex(table);
840:
841: colModel.setLeadSelectionIndex(target);
842: rowModel.setLeadSelectionIndex(rowLead);
843: }
844: else if (command.equals("selectAll"))
845: {
846: table.selectAll();
847: }
848: else if (command.equals("selectLastRowExtendSelection"))
849: {
850: rowModel.setLeadSelectionIndex(rowMax);
851: colModel.setLeadSelectionIndex(colLead);
852: }
853: else if (command.equals("scrollDownExtendSelection"))
854: {
855: int target;
856: if (rowLead == getLastVisibleRowIndex(table))
857: target = Math.min(rowMax, rowLead + (getLastVisibleRowIndex(table)
858: - getFirstVisibleRowIndex(table) + 1));
859: else
860: target = getLastVisibleRowIndex(table);
861:
862: rowModel.setLeadSelectionIndex(target);
863: colModel.setLeadSelectionIndex(colLead);
864: }
865: else if (command.equals("scrollUpChangeSelection"))
866: {
867: int target;
868: if (rowLead == getFirstVisibleRowIndex(table))
869: target = Math.max(0, rowLead - (getLastVisibleRowIndex(table)
870: - getFirstVisibleRowIndex(table) + 1));
871: else
872: target = getFirstVisibleRowIndex(table);
873:
874: rowModel.setSelectionInterval(target, target);
875: colModel.setSelectionInterval(colLead, colLead);
876: }
877: else if (command.equals("selectNextRowChangeLead"))
878: {
879: if (rowModel.getSelectionMode()
880: != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
881: {
882:
883: rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),
884: Math.min(rowLead + 1, rowMax));
885: colModel.setSelectionInterval(colLead, colLead);
886: }
887: else
888: rowModel.moveLeadSelectionIndex(Math.min(rowLead + 1, rowMax));
889: }
890: else if (command.equals("selectPreviousRowChangeLead"))
891: {
892: if (rowModel.getSelectionMode()
893: != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
894: {
895:
896: rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),
897: Math.min(rowLead - 1, 0));
898: colModel.setSelectionInterval(colLead, colLead);
899: }
900: else
901: rowModel.moveLeadSelectionIndex(Math.max(rowLead - 1, 0));
902: }
903: else if (command.equals("selectNextColumnChangeLead"))
904: {
905: if (colModel.getSelectionMode()
906: != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
907: {
908:
909: rowModel.setSelectionInterval(rowLead, rowLead);
910: colModel.setSelectionInterval(Math.min(colLead + 1, colMax),
911: Math.min(colLead + 1, colMax));
912: }
913: else
914: colModel.moveLeadSelectionIndex(Math.min(colLead + 1, colMax));
915: }
916: else if (command.equals("selectPreviousColumnChangeLead"))
917: {
918: if (colModel.getSelectionMode()
919: != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
920: {
921:
922: rowModel.setSelectionInterval(rowLead, rowLead);
923: colModel.setSelectionInterval(Math.max(colLead - 1, 0),
924: Math.max(colLead - 1, 0));
925:
926: }
927: else
928: colModel.moveLeadSelectionIndex(Math.max(colLead - 1, 0));
929: }
930: else if (command.equals("addToSelection"))
931: {
932: if (!table.isEditing())
933: {
934: int oldRowAnchor = rowModel.getAnchorSelectionIndex();
935: int oldColAnchor = colModel.getAnchorSelectionIndex();
936: rowModel.addSelectionInterval(rowLead, rowLead);
937: colModel.addSelectionInterval(colLead, colLead);
938: rowModel.setAnchorSelectionIndex(oldRowAnchor);
939: colModel.setAnchorSelectionIndex(oldColAnchor);
940: }
941: }
942: else if (command.equals("extendTo"))
943: {
944: rowModel.setSelectionInterval(rowModel.getAnchorSelectionIndex(),
945: rowLead);
946: colModel.setSelectionInterval(colModel.getAnchorSelectionIndex(),
947: colLead);
948: }
949: else if (command.equals("toggleAndAnchor"))
950: {
951: if (rowModel.isSelectedIndex(rowLead))
952: rowModel.removeSelectionInterval(rowLead, rowLead);
953: else
954: rowModel.addSelectionInterval(rowLead, rowLead);
955:
956: if (colModel.isSelectedIndex(colLead))
957: colModel.removeSelectionInterval(colLead, colLead);
958: else
959: colModel.addSelectionInterval(colLead, colLead);
960:
961: rowModel.setAnchorSelectionIndex(rowLead);
962: colModel.setAnchorSelectionIndex(colLead);
963: }
964: else if (command.equals("stopEditing"))
965: {
966: table.editingStopped(new ChangeEvent(command));
967: }
968: else
969: {
970:
971:
972:
973:
974:
975:
976:
977:
978: }
979:
980:
981:
982:
983:
984: if (table.isEditing() && command != "startEditing"
985: && command != "addToSelection")
986: table.editingStopped(new ChangeEvent("update"));
987:
988: table.scrollRectToVisible(table.getCellRect(
989: rowModel.getLeadSelectionIndex(), colModel.getLeadSelectionIndex(),
990: false));
991: }
992:
993:
997: int getFirstVisibleColumnIndex(JTable table)
998: {
999: ComponentOrientation or = table.getComponentOrientation();
1000: Rectangle r = table.getVisibleRect();
1001: if (!or.isLeftToRight())
1002: r.translate((int) r.getWidth() - 1, 0);
1003: return table.columnAtPoint(r.getLocation());
1004: }
1005:
1006:
1010: int getLastVisibleColumnIndex(JTable table)
1011: {
1012: ComponentOrientation or = table.getComponentOrientation();
1013: Rectangle r = table.getVisibleRect();
1014: if (or.isLeftToRight())
1015: r.translate((int) r.getWidth() - 1, 0);
1016: return table.columnAtPoint(r.getLocation());
1017: }
1018:
1019:
1023: int getFirstVisibleRowIndex(JTable table)
1024: {
1025: ComponentOrientation or = table.getComponentOrientation();
1026: Rectangle r = table.getVisibleRect();
1027: if (!or.isLeftToRight())
1028: r.translate((int) r.getWidth() - 1, 0);
1029: return table.rowAtPoint(r.getLocation());
1030: }
1031:
1032:
1036: int getLastVisibleRowIndex(JTable table)
1037: {
1038: ComponentOrientation or = table.getComponentOrientation();
1039: Rectangle r = table.getVisibleRect();
1040: r.translate(0, (int) r.getHeight() - 1);
1041: if (or.isLeftToRight())
1042: r.translate((int) r.getWidth() - 1, 0);
1043:
1044:
1045:
1046: if (table.rowAtPoint(r.getLocation()) == -1)
1047: {
1048: if (getFirstVisibleRowIndex(table) == -1)
1049: return -1;
1050: else
1051: return table.getModel().getRowCount() - 1;
1052: }
1053: return table.rowAtPoint(r.getLocation());
1054: }
1055:
1056:
1074: void advanceMultipleSelection(JTable table, ListSelectionModel firstModel,
1075: int firstMin,
1076: int firstMax, ListSelectionModel secondModel,
1077: int secondMin, int secondMax, boolean reverse,
1078: boolean eventIsTab)
1079: {
1080:
1081:
1082: int firstLead = firstModel.getLeadSelectionIndex();
1083: int secondLead = secondModel.getLeadSelectionIndex();
1084: int numFirsts = eventIsTab ?
1085: table.getModel().getColumnCount() : table.getModel().getRowCount();
1086: int numSeconds = eventIsTab ?
1087: table.getModel().getRowCount() : table.getModel().getColumnCount();
1088:
1089:
1090: if ((firstLead == firstMax && !reverse) ||
1091: (reverse && firstLead == firstMin))
1092: {
1093: firstModel.addSelectionInterval(reverse ? firstMax : firstMin,
1094: reverse ? firstMax : firstMin);
1095:
1096:
1097: if ((secondLead == secondMax && !reverse) ||
1098: (reverse && secondLead == secondMin))
1099: secondModel.addSelectionInterval(reverse ? secondMax : secondMin,
1100: reverse ? secondMax : secondMin);
1101:
1102:
1103:
1104:
1105: else
1106: {
1107: int[] secondsSelected;
1108: if (eventIsTab && table.getRowSelectionAllowed() ||
1109: !eventIsTab && table.getColumnSelectionAllowed())
1110: secondsSelected = eventIsTab ?
1111: table.getSelectedRows() : table.getSelectedColumns();
1112: else
1113: {
1114:
1115:
1116: secondsSelected = new int[numSeconds];
1117: for (int i = 0; i < numSeconds; i++)
1118: secondsSelected[i] = i;
1119: }
1120:
1121:
1122: int secondIndex = reverse ? secondsSelected.length - 1 : 0;
1123: if (!reverse)
1124: while (secondsSelected[secondIndex] <= secondLead)
1125: secondIndex++;
1126: else
1127: while (secondsSelected[secondIndex] >= secondLead)
1128: secondIndex--;
1129:
1130:
1131: secondModel.addSelectionInterval(secondsSelected[secondIndex],
1132: secondsSelected[secondIndex]);
1133: }
1134: }
1135:
1136:
1137: else
1138: {
1139: int[] firstsSelected;
1140: if (eventIsTab && table.getColumnSelectionAllowed() ||
1141: !eventIsTab && table.getRowSelectionAllowed())
1142: firstsSelected = eventIsTab ?
1143: table.getSelectedColumns() : table.getSelectedRows();
1144: else
1145: {
1146:
1147: firstsSelected = new int[numFirsts];
1148: for (int i = 0; i < numFirsts; i++)
1149: firstsSelected[i] = i;
1150: }
1151: int firstIndex = reverse ? firstsSelected.length - 1 : 0;
1152: if (!reverse)
1153: while (firstsSelected[firstIndex] <= firstLead)
1154: firstIndex++;
1155: else
1156: while (firstsSelected[firstIndex] >= firstLead)
1157: firstIndex--;
1158: firstModel.addSelectionInterval(firstsSelected[firstIndex],
1159: firstsSelected[firstIndex]);
1160: secondModel.addSelectionInterval(secondLead, secondLead);
1161: }
1162: }
1163:
1164:
1179:
1180: void advanceSingleSelection(ListSelectionModel firstModel, int firstMax,
1181: ListSelectionModel secondModel, int secondMax,
1182: boolean reverse)
1183: {
1184:
1185:
1186: int firstLead = firstModel.getLeadSelectionIndex();
1187: int secondLead = secondModel.getLeadSelectionIndex();
1188:
1189:
1190:
1191: if (reverse && (firstLead == 0))
1192: {
1193:
1194: if (secondLead == 0)
1195: secondLead += secondMax + 1;
1196: secondLead -= 2;
1197: }
1198:
1199:
1200: if (reverse && (firstLead == 0) || !reverse && (firstLead == firstMax))
1201: secondModel.setSelectionInterval((secondLead + 1) % (secondMax + 1),
1202: (secondLead + 1) % (secondMax + 1));
1203:
1204: else
1205: secondModel.setSelectionInterval(secondLead, secondLead);
1206:
1207:
1208:
1209: if (reverse)
1210: {
1211:
1212: if (firstLead == 0)
1213: firstLead += firstMax + 1;
1214: firstLead -= 2;
1215: }
1216:
1217: firstModel.setSelectionInterval((firstLead + 1) % (firstMax + 1),
1218: (firstLead + 1) % (firstMax + 1));
1219: }
1220: }
1221:
1222: protected void installListeners()
1223: {
1224: if (focusListener == null)
1225: focusListener = createFocusListener();
1226: table.addFocusListener(focusListener);
1227: if (keyListener == null)
1228: keyListener = createKeyListener();
1229: table.addKeyListener(keyListener);
1230: if (mouseInputListener == null)
1231: mouseInputListener = createMouseInputListener();
1232: table.addMouseListener(mouseInputListener);
1233: table.addMouseMotionListener(mouseInputListener);
1234: if (propertyChangeListener == null)
1235: propertyChangeListener = new PropertyChangeHandler();
1236: table.addPropertyChangeListener(propertyChangeListener);
1237: }
1238:
1239:
1243: protected void uninstallDefaults()
1244: {
1245:
1246: }
1247:
1248:
1252: protected void uninstallKeyboardActions()
1253: {
1254: SwingUtilities.replaceUIInputMap(table, JComponent.
1255: WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
1256: SwingUtilities.replaceUIActionMap(table, null);
1257: }
1258:
1259: protected void uninstallListeners()
1260: {
1261: table.removeFocusListener(focusListener);
1262: table.removeKeyListener(keyListener);
1263: table.removeMouseListener(mouseInputListener);
1264: table.removeMouseMotionListener(mouseInputListener);
1265: table.removePropertyChangeListener(propertyChangeListener);
1266: propertyChangeListener = null;
1267: }
1268:
1269: public void installUI(JComponent comp)
1270: {
1271: table = (JTable) comp;
1272: rendererPane = new CellRendererPane();
1273: table.add(rendererPane);
1274:
1275: installDefaults();
1276: installKeyboardActions();
1277: installListeners();
1278: }
1279:
1280: public void uninstallUI(JComponent c)
1281: {
1282: uninstallListeners();
1283: uninstallKeyboardActions();
1284: uninstallDefaults();
1285:
1286: table.remove(rendererPane);
1287: rendererPane = null;
1288: table = null;
1289: }
1290:
1291:
1302: void paintCell(Graphics g, int row, int col, Rectangle bounds,
1303: TableCellRenderer rend)
1304: {
1305: Component comp = table.prepareRenderer(rend, row, col);
1306: rendererPane.paintComponent(g, comp, table, bounds);
1307: }
1308:
1309:
1312: public void paint(Graphics gfx, JComponent ignored)
1313: {
1314: int ncols = table.getColumnCount();
1315: int nrows = table.getRowCount();
1316: if (nrows == 0 || ncols == 0)
1317: return;
1318:
1319: Rectangle clip = gfx.getClipBounds();
1320:
1321:
1322: Point p1 = new Point(clip.x, clip.y);
1323: int c0 = table.columnAtPoint(p1);
1324: if (c0 == -1)
1325: c0 = 0;
1326: int r0 = table.rowAtPoint(p1);
1327: if (r0 == -1)
1328: r0 = 0;
1329: Point p2 = new Point(clip.x + clip.width, clip.y + clip.height);
1330: int cn = table.columnAtPoint(p2);
1331: if (cn == -1)
1332: cn = table.getColumnCount() - 1;
1333: int rn = table.rowAtPoint(p2);
1334: if (rn == -1)
1335: rn = table.getRowCount() - 1;
1336:
1337: int columnMargin = table.getColumnModel().getColumnMargin();
1338: int rowMargin = table.getRowMargin();
1339:
1340: TableColumnModel cmodel = table.getColumnModel();
1341: int[] widths = new int[cn + 1];
1342: for (int i = c0; i <= cn; i++)
1343: {
1344: widths[i] = cmodel.getColumn(i).getWidth() - columnMargin;
1345: }
1346:
1347: Rectangle bounds = table.getCellRect(r0, c0, false);
1348:
1349: int left = bounds.x;
1350:
1351:
1352: int top = bounds.y;
1353:
1354:
1355: int bottom;
1356:
1357:
1358: Color grid = table.getGridColor();
1359: for (int r = r0; r <= rn; ++r)
1360: {
1361: for (int c = c0; c <= cn; ++c)
1362: {
1363: bounds.width = widths[c];
1364: paintCell(gfx, r, c, bounds, table.getCellRenderer(r, c));
1365: bounds.x += widths[c] + columnMargin;
1366: }
1367: bounds.x = left;
1368: bounds.y += table.getRowHeight(r);
1369:
1370: bounds.height = table.getRowHeight(r + 1) - rowMargin;
1371: }
1372:
1373: bottom = bounds.y - rowMargin;
1374:
1375:
1376: if (grid != null && table.getShowVerticalLines())
1377: {
1378: Color save = gfx.getColor();
1379: gfx.setColor(grid);
1380: int x = left - columnMargin;
1381: for (int c = c0; c <= cn; ++c)
1382: {
1383:
1384:
1385: x += widths[c] + columnMargin;
1386: gfx.drawLine(x, top, x, bottom);
1387: }
1388: gfx.setColor(save);
1389: }
1390:
1391:
1392: if (grid != null && table.getShowHorizontalLines())
1393: {
1394: Color save = gfx.getColor();
1395: gfx.setColor(grid);
1396: int y = top - rowMargin;
1397: for (int r = r0; r <= rn; ++r)
1398: {
1399:
1400:
1401: y += table.getRowHeight(r);
1402: gfx.drawLine(left, y, p2.x, y);
1403: }
1404: gfx.setColor(save);
1405: }
1406: }
1407: }