1:
37:
38:
39: package ;
40:
41: import ;
42:
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: import ;
57: import ;
58: import ;
59:
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: import ;
83: import ;
84:
85: public class BasicTableUI extends TableUI
86: {
87: public static ComponentUI createUI(JComponent comp)
88: {
89: return new BasicTableUI();
90: }
91:
92: protected FocusListener focusListener;
93: protected KeyListener keyListener;
94: protected MouseInputListener mouseInputListener;
95: protected CellRendererPane rendererPane;
96: protected JTable table;
97:
98:
99: Border cellBorder;
100:
101:
102: TableAction action;
103:
104:
107: private PropertyChangeListener propertyChangeListener;
108:
109:
116: public class KeyHandler implements KeyListener
117: {
118:
119:
126: public void keyTyped(KeyEvent event)
127: {
128:
129:
130:
131:
132:
133: if (!table.isEditing() && table.isEnabled())
134: {
135: int r = table.getSelectedRow();
136: int c = table.getSelectedColumn();
137: if (table.isCellEditable(r, c))
138: table.editCellAt(r, c);
139: }
140: }
141:
142:
147: public void keyPressed(KeyEvent event)
148: {
149:
150:
151: }
152:
153:
158: public void keyReleased(KeyEvent event)
159: {
160:
161:
162: }
163: }
164:
165: public class FocusHandler implements FocusListener
166: {
167: public void focusGained(FocusEvent e)
168: {
169:
170: }
171:
172: public void focusLost(FocusEvent e)
173: {
174:
175: }
176: }
177:
178: public class MouseInputHandler implements MouseInputListener
179: {
180: Point begin, curr;
181:
182: private void updateSelection(boolean controlPressed)
183: {
184:
185: int lo_row = table.rowAtPoint(begin);
186: int hi_row = table.rowAtPoint(curr);
187: ListSelectionModel rowModel = table.getSelectionModel();
188: if (lo_row != -1 && hi_row != -1)
189: {
190: if (controlPressed && rowModel.getSelectionMode()
191: != ListSelectionModel.SINGLE_SELECTION)
192: rowModel.addSelectionInterval(lo_row, hi_row);
193: else
194: rowModel.setSelectionInterval(lo_row, hi_row);
195: }
196:
197:
198: int lo_col = table.columnAtPoint(begin);
199: int hi_col = table.columnAtPoint(curr);
200: ListSelectionModel colModel = table.getColumnModel().
201: getSelectionModel();
202: if (lo_col != -1 && hi_col != -1)
203: {
204: if (controlPressed && colModel.getSelectionMode() !=
205: ListSelectionModel.SINGLE_SELECTION)
206: colModel.addSelectionInterval(lo_col, hi_col);
207: else
208: colModel.setSelectionInterval(lo_col, hi_col);
209: }
210: }
211:
212:
215: public void mouseClicked(MouseEvent e)
216: {
217: Point p = e.getPoint();
218: int row = table.rowAtPoint(p);
219: int col = table.columnAtPoint(p);
220: if (table.isCellEditable(row, col))
221: {
222:
223:
224:
225: TableCellEditor editor = table.getCellEditor(row, col);
226: if (editor instanceof DefaultCellEditor)
227: {
228: DefaultCellEditor ce = (DefaultCellEditor) editor;
229: if (e.getClickCount() < ce.getClickCountToStart())
230: return;
231: }
232: table.editCellAt(row, col);
233: }
234: }
235:
236: public void mouseDragged(MouseEvent e)
237: {
238: if (table.isEnabled())
239: {
240: curr = new Point(e.getX(), e.getY());
241: updateSelection(e.isControlDown());
242: }
243: }
244:
245: public void mouseEntered(MouseEvent e)
246: {
247:
248: }
249:
250: public void mouseExited(MouseEvent e)
251: {
252:
253: }
254:
255: public void mouseMoved(MouseEvent e)
256: {
257:
258: }
259:
260: public void mousePressed(MouseEvent e)
261: {
262: if (table.isEnabled())
263: {
264: ListSelectionModel rowModel = table.getSelectionModel();
265: ListSelectionModel colModel = table.getColumnModel().getSelectionModel();
266: int rowLead = rowModel.getLeadSelectionIndex();
267: int colLead = colModel.getLeadSelectionIndex();
268:
269: begin = new Point(e.getX(), e.getY());
270: curr = new Point(e.getX(), e.getY());
271:
272: if (e.isControlDown() && table.isCellSelected(
273: table.rowAtPoint(begin), table.columnAtPoint(begin)))
274: {
275: table.getSelectionModel().
276: removeSelectionInterval(table.rowAtPoint(begin),
277: table.rowAtPoint(begin));
278: table.getColumnModel().getSelectionModel().
279: removeSelectionInterval(table.columnAtPoint(begin),
280: table.columnAtPoint(begin));
281: }
282: else
283: updateSelection(e.isControlDown());
284:
285:
286: if (rowLead != rowModel.getLeadSelectionIndex() ||
287: colLead != colModel.getLeadSelectionIndex())
288: if (table.isEditing())
289: table.editingStopped(new ChangeEvent(e));
290: }
291: }
292:
293: public void mouseReleased(MouseEvent e)
294: {
295: if (table.isEnabled())
296: {
297: begin = null;
298: curr = null;
299: }
300: }
301: }
302:
303:
309: private class PropertyChangeHandler implements PropertyChangeListener
310: {
311:
316: public void propertyChange(PropertyChangeEvent ev)
317: {
318: String propName = ev.getPropertyName();
319: if (propName.equals("model"))
320: {
321: ListSelectionModel rowSel = table.getSelectionModel();
322: rowSel.clearSelection();
323: ListSelectionModel colSel = table.getColumnModel().getSelectionModel();
324: colSel.clearSelection();
325: TableModel model = table.getModel();
326:
327:
328:
329: if (model.getRowCount() > 0)
330: {
331: rowSel.setAnchorSelectionIndex(0);
332: rowSel.setLeadSelectionIndex(0);
333: }
334: else
335: {
336: rowSel.setAnchorSelectionIndex(-1);
337: rowSel.setLeadSelectionIndex(-1);
338: }
339: if (model.getColumnCount() > 0)
340: {
341: colSel.setAnchorSelectionIndex(0);
342: colSel.setLeadSelectionIndex(0);
343: }
344: else
345: {
346: colSel.setAnchorSelectionIndex(-1);
347: colSel.setLeadSelectionIndex(-1);
348: }
349: }
350: }
351: }
352:
353: protected FocusListener createFocusListener()
354: {
355: return new FocusHandler();
356: }
357:
358: protected MouseInputListener createMouseInputListener()
359: {
360: return new MouseInputHandler();
361: }
362:
363:
364:
369: protected KeyListener createKeyListener()
370: {
371: return new KeyHandler();
372: }
373:
374:
384: public Dimension getMaximumSize(JComponent comp)
385: {
386: int maxTotalColumnWidth = 0;
387: for (int i = 0; i < table.getColumnCount(); i++)
388: maxTotalColumnWidth += table.getColumnModel().getColumn(i).getMaxWidth();
389:
390: return new Dimension(maxTotalColumnWidth, getHeight());
391: }
392:
393:
403: public Dimension getMinimumSize(JComponent comp)
404: {
405: int minTotalColumnWidth = 0;
406: for (int i = 0; i < table.getColumnCount(); i++)
407: minTotalColumnWidth += table.getColumnModel().getColumn(i).getMinWidth();
408:
409: return new Dimension(minTotalColumnWidth, getHeight());
410: }
411:
412:
419: public Dimension getPreferredSize(JComponent comp)
420: {
421: int prefTotalColumnWidth = 0;
422: for (int i = 0; i < table.getColumnCount(); i++)
423: {
424: TableColumn col = table.getColumnModel().getColumn(i);
425: prefTotalColumnWidth += col.getPreferredWidth();
426: }
427: return new Dimension(prefTotalColumnWidth, getHeight());
428: }
429:
430:
437: private int getHeight()
438: {
439: int height = 0;
440: int rowCount = table.getRowCount();
441: if (rowCount > 0 && table.getColumnCount() > 0)
442: {
443: Rectangle r = table.getCellRect(rowCount - 1, 0, true);
444: height = r.y + r.height;
445: }
446: return height;
447: }
448:
449: protected void installDefaults()
450: {
451: LookAndFeel.installColorsAndFont(table, "Table.background",
452: "Table.foreground", "Table.font");
453: table.setGridColor(UIManager.getColor("Table.gridColor"));
454: table.setSelectionForeground(UIManager.getColor("Table.selectionForeground"));
455: table.setSelectionBackground(UIManager.getColor("Table.selectionBackground"));
456: table.setOpaque(true);
457: }
458:
459: protected void installKeyboardActions()
460: {
461: InputMap ancestorMap = (InputMap) UIManager.get("Table.ancestorInputMap");
462: InputMapUIResource parentInputMap = new InputMapUIResource();
463:
464: ActionMap parentActionMap = new ActionMapUIResource();
465: action = new TableAction();
466: Object keys[] = ancestorMap.allKeys();
467:
468: for (int i = 0; i < keys.length; i++)
469: {
470: KeyStroke stroke = (KeyStroke) keys[i];
471: String actionString = (String) ancestorMap.get(stroke);
472:
473: parentInputMap.put(KeyStroke.getKeyStroke(stroke.getKeyCode(),
474: stroke.getModifiers()),
475: actionString);
476:
477: parentActionMap.put(actionString,
478: new ActionListenerProxy(action, actionString));
479:
480: }
481:
482:
483: parentInputMap.setParent(table.getInputMap(
484: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).getParent());
485: parentActionMap.setParent(table.getActionMap().getParent());
486: table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
487: setParent(parentInputMap);
488: table.getActionMap().setParent(parentActionMap);
489: }
490:
491:
498: private static class ActionListenerProxy
499: extends AbstractAction
500: {
501: ActionListener target;
502: String bindingCommandName;
503:
504: public ActionListenerProxy(ActionListener li,
505: String cmd)
506: {
507: target = li;
508: bindingCommandName = cmd;
509: }
510:
511: public void actionPerformed(ActionEvent e)
512: {
513: ActionEvent derivedEvent = new ActionEvent(e.getSource(),
514: e.getID(),
515: bindingCommandName,
516: e.getModifiers());
517: target.actionPerformed(derivedEvent);
518: }
519: }
520:
521:
527: class TableAction extends AbstractAction
528: {
529:
534: public void actionPerformed(ActionEvent e)
535: {
536: DefaultListSelectionModel rowModel
537: = (DefaultListSelectionModel) table.getSelectionModel();
538: DefaultListSelectionModel colModel
539: = (DefaultListSelectionModel) table.getColumnModel().getSelectionModel();
540:
541: int rowLead = rowModel.getLeadSelectionIndex();
542: int rowMax = table.getModel().getRowCount() - 1;
543:
544: int colLead = colModel.getLeadSelectionIndex();
545: int colMax = table.getModel().getColumnCount() - 1;
546:
547: String command = e.getActionCommand();
548:
549: if (command.equals("selectPreviousRowExtendSelection"))
550: {
551: rowModel.setLeadSelectionIndex(Math.max(rowLead - 1, 0));
552: }
553: else if (command.equals("selectLastColumn"))
554: {
555: colModel.setSelectionInterval(colMax, colMax);
556: }
557: else if (command.equals("startEditing"))
558: {
559: if (table.isCellEditable(rowLead, colLead))
560: table.editCellAt(rowLead, colLead);
561: }
562: else if (command.equals("selectFirstRowExtendSelection"))
563: {
564: rowModel.setLeadSelectionIndex(0);
565: }
566: else if (command.equals("selectFirstColumn"))
567: {
568: colModel.setSelectionInterval(0, 0);
569: }
570: else if (command.equals("selectFirstColumnExtendSelection"))
571: {
572: colModel.setLeadSelectionIndex(0);
573: }
574: else if (command.equals("selectLastRow"))
575: {
576: rowModel.setSelectionInterval(rowMax, rowMax);
577: }
578: else if (command.equals("selectNextRowExtendSelection"))
579: {
580: rowModel.setLeadSelectionIndex(Math.min(rowLead + 1, rowMax));
581: }
582: else if (command.equals("selectFirstRow"))
583: {
584: rowModel.setSelectionInterval(0, 0);
585: }
586: else if (command.equals("selectNextColumnExtendSelection"))
587: {
588: colModel.setLeadSelectionIndex(Math.min(colLead + 1, colMax));
589: }
590: else if (command.equals("selectLastColumnExtendSelection"))
591: {
592: colModel.setLeadSelectionIndex(colMax);
593: }
594: else if (command.equals("selectPreviousColumnExtendSelection"))
595: {
596: colModel.setLeadSelectionIndex(Math.max(colLead - 1, 0));
597: }
598: else if (command.equals("selectNextRow"))
599: {
600: rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),
601: Math.min(rowLead + 1, rowMax));
602: }
603: else if (command.equals("scrollUpExtendSelection"))
604: {
605: int target;
606: if (rowLead == getFirstVisibleRowIndex())
607: target = Math.max(0, rowLead - (getLastVisibleRowIndex()
608: - getFirstVisibleRowIndex() + 1));
609: else
610: target = getFirstVisibleRowIndex();
611:
612: rowModel.setLeadSelectionIndex(target);
613: colModel.setLeadSelectionIndex(colLead);
614: }
615: else if (command.equals("selectPreviousRow"))
616: {
617: rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),
618: Math.max(rowLead - 1, 0));
619: }
620: else if (command.equals("scrollRightChangeSelection"))
621: {
622: int target;
623: if (colLead == getLastVisibleColumnIndex())
624: target = Math.min(colMax, colLead + (getLastVisibleColumnIndex()
625: - getFirstVisibleColumnIndex() + 1));
626: else
627: target = getLastVisibleColumnIndex();
628:
629: colModel.setSelectionInterval(target, target);
630: rowModel.setSelectionInterval(rowLead, rowLead);
631: }
632: else if (command.equals("selectPreviousColumn"))
633: {
634: colModel.setSelectionInterval(Math.max(colLead - 1, 0),
635: Math.max(colLead - 1, 0));
636: }
637: else if (command.equals("scrollLeftChangeSelection"))
638: {
639: int target;
640: if (colLead == getFirstVisibleColumnIndex())
641: target = Math.max(0, colLead - (getLastVisibleColumnIndex()
642: - getFirstVisibleColumnIndex() + 1));
643: else
644: target = getFirstVisibleColumnIndex();
645:
646: colModel.setSelectionInterval(target, target);
647: rowModel.setSelectionInterval(rowLead, rowLead);
648: }
649: else if (command.equals("clearSelection"))
650: {
651: table.clearSelection();
652: }
653: else if (command.equals("cancel"))
654: {
655:
656:
657:
658: if (table.isEditing())
659: table.editingCanceled(new ChangeEvent("cancel"));
660: }
661: else if (command.equals("selectNextRowCell")
662: || command.equals("selectPreviousRowCell")
663: || command.equals("selectNextColumnCell")
664: || command.equals("selectPreviousColumnCell"))
665: {
666:
667: if (table.getSelectedRowCount() == 0 &&
668: table.getSelectedColumnCount() == 0)
669: {
670: rowModel.setSelectionInterval(0, 0);
671: colModel.setSelectionInterval(0, 0);
672: return;
673: }
674:
675:
676:
677:
678: if (!table.isCellSelected(rowLead, colLead))
679: {
680: rowModel.addSelectionInterval(rowModel.getMinSelectionIndex(),
681: rowModel.getMinSelectionIndex());
682: colModel.addSelectionInterval(colModel.getMinSelectionIndex(),
683: colModel.getMinSelectionIndex());
684: return;
685: }
686:
687:
688:
689: boolean multRowsSelected, multColsSelected;
690: multRowsSelected = table.getSelectedRowCount() > 1 &&
691: table.getRowSelectionAllowed();
692:
693: multColsSelected = table.getSelectedColumnCount() > 1 &&
694: table.getColumnSelectionAllowed();
695:
696:
697:
698: if (!multColsSelected && !multRowsSelected)
699: {
700: if (command.indexOf("Column") != -1)
701: advanceSingleSelection(colModel, colMax, rowModel, rowMax,
702: command.equals("selectPreviousColumnCell"));
703: else
704: advanceSingleSelection(rowModel, rowMax, colModel, colMax,
705: command.equals("selectPreviousRowCell"));
706: return;
707: }
708:
709:
710:
711:
712:
713: int rowMaxSelected = table.getRowSelectionAllowed() ?
714: rowModel.getMaxSelectionIndex() : table.getModel().getRowCount() - 1;
715: int rowMinSelected = table.getRowSelectionAllowed() ?
716: rowModel.getMinSelectionIndex() : 0;
717: int colMaxSelected = table.getColumnSelectionAllowed() ?
718: colModel.getMaxSelectionIndex() :
719: table.getModel().getColumnCount() - 1;
720: int colMinSelected = table.getColumnSelectionAllowed() ?
721: colModel.getMinSelectionIndex() : 0;
722:
723:
724:
725: if (command.indexOf("Column") != -1)
726: advanceMultipleSelection(colModel, colMinSelected, colMaxSelected,
727: rowModel, rowMinSelected, rowMaxSelected,
728: command.equals("selectPreviousColumnCell"), true);
729:
730: else
731: advanceMultipleSelection(rowModel, rowMinSelected, rowMaxSelected,
732: colModel, colMinSelected, colMaxSelected,
733: command.equals("selectPreviousRowCell"), false);
734: }
735: else if (command.equals("selectNextColumn"))
736: {
737: colModel.setSelectionInterval(Math.min(colLead + 1, colMax),
738: Math.min(colLead + 1, colMax));
739: }
740: else if (command.equals("scrollLeftExtendSelection"))
741: {
742: int target;
743: if (colLead == getFirstVisibleColumnIndex())
744: target = Math.max(0, colLead - (getLastVisibleColumnIndex()
745: - getFirstVisibleColumnIndex() + 1));
746: else
747: target = getFirstVisibleColumnIndex();
748:
749: colModel.setLeadSelectionIndex(target);
750: rowModel.setLeadSelectionIndex(rowLead);
751: }
752: else if (command.equals("scrollDownChangeSelection"))
753: {
754: int target;
755: if (rowLead == getLastVisibleRowIndex())
756: target = Math.min(rowMax, rowLead + (getLastVisibleRowIndex()
757: - getFirstVisibleRowIndex() + 1));
758: else
759: target = getLastVisibleRowIndex();
760:
761: rowModel.setSelectionInterval(target, target);
762: colModel.setSelectionInterval(colLead, colLead);
763: }
764: else if (command.equals("scrollRightExtendSelection"))
765: {
766: int target;
767: if (colLead == getLastVisibleColumnIndex())
768: target = Math.min(colMax, colLead + (getLastVisibleColumnIndex()
769: - getFirstVisibleColumnIndex() + 1));
770: else
771: target = getLastVisibleColumnIndex();
772:
773: colModel.setLeadSelectionIndex(target);
774: rowModel.setLeadSelectionIndex(rowLead);
775: }
776: else if (command.equals("selectAll"))
777: {
778: table.selectAll();
779: }
780: else if (command.equals("selectLastRowExtendSelection"))
781: {
782: rowModel.setLeadSelectionIndex(rowMax);
783: colModel.setLeadSelectionIndex(colLead);
784: }
785: else if (command.equals("scrollDownExtendSelection"))
786: {
787: int target;
788: if (rowLead == getLastVisibleRowIndex())
789: target = Math.min(rowMax, rowLead + (getLastVisibleRowIndex()
790: - getFirstVisibleRowIndex() + 1));
791: else
792: target = getLastVisibleRowIndex();
793:
794: rowModel.setLeadSelectionIndex(target);
795: colModel.setLeadSelectionIndex(colLead);
796: }
797: else if (command.equals("scrollUpChangeSelection"))
798: {
799: int target;
800: if (rowLead == getFirstVisibleRowIndex())
801: target = Math.max(0, rowLead - (getLastVisibleRowIndex()
802: - getFirstVisibleRowIndex() + 1));
803: else
804: target = getFirstVisibleRowIndex();
805:
806: rowModel.setSelectionInterval(target, target);
807: colModel.setSelectionInterval(colLead, colLead);
808: }
809: else if (command.equals("selectNextRowChangeLead"))
810: {
811: if (rowModel.getSelectionMode()
812: != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
813: {
814:
815: rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),
816: Math.min(rowLead + 1, rowMax));
817: colModel.setSelectionInterval(colLead, colLead);
818: }
819: else
820: rowModel.moveLeadSelectionIndex(Math.min(rowLead + 1, rowMax));
821: }
822: else if (command.equals("selectPreviousRowChangeLead"))
823: {
824: if (rowModel.getSelectionMode()
825: != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
826: {
827:
828: rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),
829: Math.min(rowLead - 1, 0));
830: colModel.setSelectionInterval(colLead, colLead);
831: }
832: else
833: rowModel.moveLeadSelectionIndex(Math.max(rowLead - 1, 0));
834: }
835: else if (command.equals("selectNextColumnChangeLead"))
836: {
837: if (colModel.getSelectionMode()
838: != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
839: {
840:
841: rowModel.setSelectionInterval(rowLead, rowLead);
842: colModel.setSelectionInterval(Math.min(colLead + 1, colMax),
843: Math.min(colLead + 1, colMax));
844: }
845: else
846: colModel.moveLeadSelectionIndex(Math.min(colLead + 1, colMax));
847: }
848: else if (command.equals("selectPreviousColumnChangeLead"))
849: {
850: if (colModel.getSelectionMode()
851: != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
852: {
853:
854: rowModel.setSelectionInterval(rowLead, rowLead);
855: colModel.setSelectionInterval(Math.max(colLead - 1, 0),
856: Math.max(colLead - 1, 0));
857:
858: }
859: else
860: colModel.moveLeadSelectionIndex(Math.max(colLead - 1, 0));
861: }
862: else if (command.equals("addToSelection"))
863: {
864: if (!table.isEditing())
865: {
866: int oldRowAnchor = rowModel.getAnchorSelectionIndex();
867: int oldColAnchor = colModel.getAnchorSelectionIndex();
868: rowModel.addSelectionInterval(rowLead, rowLead);
869: colModel.addSelectionInterval(colLead, colLead);
870: rowModel.setAnchorSelectionIndex(oldRowAnchor);
871: colModel.setAnchorSelectionIndex(oldColAnchor);
872: }
873: }
874: else if (command.equals("extendTo"))
875: {
876: rowModel.setSelectionInterval(rowModel.getAnchorSelectionIndex(),
877: rowLead);
878: colModel.setSelectionInterval(colModel.getAnchorSelectionIndex(),
879: colLead);
880: }
881: else if (command.equals("toggleAndAnchor"))
882: {
883: if (rowModel.isSelectedIndex(rowLead))
884: rowModel.removeSelectionInterval(rowLead, rowLead);
885: else
886: rowModel.addSelectionInterval(rowLead, rowLead);
887:
888: if (colModel.isSelectedIndex(colLead))
889: colModel.removeSelectionInterval(colLead, colLead);
890: else
891: colModel.addSelectionInterval(colLead, colLead);
892:
893: rowModel.setAnchorSelectionIndex(rowLead);
894: colModel.setAnchorSelectionIndex(colLead);
895: }
896: else if (command.equals("stopEditing"))
897: {
898: table.editingStopped(new ChangeEvent(command));
899: }
900: else
901: {
902:
903:
904:
905:
906:
907:
908:
909:
910: }
911:
912:
913:
914:
915:
916: if (table.isEditing() && command != "startEditing"
917: && command != "addToSelection")
918: table.editingStopped(new ChangeEvent("update"));
919:
920: table.scrollRectToVisible(table.getCellRect(
921: rowModel.getLeadSelectionIndex(), colModel.getLeadSelectionIndex(),
922: false));
923: }
924:
925:
929: int getFirstVisibleColumnIndex()
930: {
931: ComponentOrientation or = table.getComponentOrientation();
932: Rectangle r = table.getVisibleRect();
933: if (!or.isLeftToRight())
934: r.translate((int) r.getWidth() - 1, 0);
935: return table.columnAtPoint(r.getLocation());
936: }
937:
938:
942: int getLastVisibleColumnIndex()
943: {
944: ComponentOrientation or = table.getComponentOrientation();
945: Rectangle r = table.getVisibleRect();
946: if (or.isLeftToRight())
947: r.translate((int) r.getWidth() - 1, 0);
948: return table.columnAtPoint(r.getLocation());
949: }
950:
951:
955: int getFirstVisibleRowIndex()
956: {
957: ComponentOrientation or = table.getComponentOrientation();
958: Rectangle r = table.getVisibleRect();
959: if (!or.isLeftToRight())
960: r.translate((int) r.getWidth() - 1, 0);
961: return table.rowAtPoint(r.getLocation());
962: }
963:
964:
968: int getLastVisibleRowIndex()
969: {
970: ComponentOrientation or = table.getComponentOrientation();
971: Rectangle r = table.getVisibleRect();
972: r.translate(0, (int) r.getHeight() - 1);
973: if (or.isLeftToRight())
974: r.translate((int) r.getWidth() - 1, 0);
975:
976:
977:
978: if (table.rowAtPoint(r.getLocation()) == -1)
979: {
980: if (getFirstVisibleRowIndex() == -1)
981: return -1;
982: else
983: return table.getModel().getRowCount() - 1;
984: }
985: return table.rowAtPoint(r.getLocation());
986: }
987:
988:
1006: void advanceMultipleSelection(ListSelectionModel firstModel, int firstMin,
1007: int firstMax, ListSelectionModel secondModel,
1008: int secondMin, int secondMax, boolean reverse,
1009: boolean eventIsTab)
1010: {
1011:
1012:
1013: int firstLead = firstModel.getLeadSelectionIndex();
1014: int secondLead = secondModel.getLeadSelectionIndex();
1015: int numFirsts = eventIsTab ?
1016: table.getModel().getColumnCount() : table.getModel().getRowCount();
1017: int numSeconds = eventIsTab ?
1018: table.getModel().getRowCount() : table.getModel().getColumnCount();
1019:
1020:
1021: if ((firstLead == firstMax && !reverse) ||
1022: (reverse && firstLead == firstMin))
1023: {
1024: firstModel.addSelectionInterval(reverse ? firstMax : firstMin,
1025: reverse ? firstMax : firstMin);
1026:
1027:
1028: if ((secondLead == secondMax && !reverse) ||
1029: (reverse && secondLead == secondMin))
1030: secondModel.addSelectionInterval(reverse ? secondMax : secondMin,
1031: reverse ? secondMax : secondMin);
1032:
1033:
1034:
1035:
1036: else
1037: {
1038: int[] secondsSelected;
1039: if (eventIsTab && table.getRowSelectionAllowed() ||
1040: !eventIsTab && table.getColumnSelectionAllowed())
1041: secondsSelected = eventIsTab ?
1042: table.getSelectedRows() : table.getSelectedColumns();
1043: else
1044: {
1045:
1046:
1047: secondsSelected = new int[numSeconds];
1048: for (int i = 0; i < numSeconds; i++)
1049: secondsSelected[i] = i;
1050: }
1051:
1052:
1053: int secondIndex = reverse ? secondsSelected.length - 1 : 0;
1054: if (!reverse)
1055: while (secondsSelected[secondIndex] <= secondLead)
1056: secondIndex++;
1057: else
1058: while (secondsSelected[secondIndex] >= secondLead)
1059: secondIndex--;
1060:
1061:
1062: secondModel.addSelectionInterval(secondsSelected[secondIndex],
1063: secondsSelected[secondIndex]);
1064: }
1065: }
1066:
1067:
1068: else
1069: {
1070: int[] firstsSelected;
1071: if (eventIsTab && table.getColumnSelectionAllowed() ||
1072: !eventIsTab && table.getRowSelectionAllowed())
1073: firstsSelected = eventIsTab ?
1074: table.getSelectedColumns() : table.getSelectedRows();
1075: else
1076: {
1077:
1078: firstsSelected = new int[numFirsts];
1079: for (int i = 0; i < numFirsts; i++)
1080: firstsSelected[i] = i;
1081: }
1082: int firstIndex = reverse ? firstsSelected.length - 1 : 0;
1083: if (!reverse)
1084: while (firstsSelected[firstIndex] <= firstLead)
1085: firstIndex++;
1086: else
1087: while (firstsSelected[firstIndex] >= firstLead)
1088: firstIndex--;
1089: firstModel.addSelectionInterval(firstsSelected[firstIndex],
1090: firstsSelected[firstIndex]);
1091: secondModel.addSelectionInterval(secondLead, secondLead);
1092: }
1093: }
1094:
1095:
1110:
1111: void advanceSingleSelection(ListSelectionModel firstModel, int firstMax,
1112: ListSelectionModel secondModel, int secondMax,
1113: boolean reverse)
1114: {
1115:
1116:
1117: int firstLead = firstModel.getLeadSelectionIndex();
1118: int secondLead = secondModel.getLeadSelectionIndex();
1119:
1120:
1121:
1122: if (reverse && (firstLead == 0))
1123: {
1124:
1125: if (secondLead == 0)
1126: secondLead += secondMax + 1;
1127: secondLead -= 2;
1128: }
1129:
1130:
1131: if (reverse && (firstLead == 0) || !reverse && (firstLead == firstMax))
1132: secondModel.setSelectionInterval((secondLead + 1) % (secondMax + 1),
1133: (secondLead + 1) % (secondMax + 1));
1134:
1135: else
1136: secondModel.setSelectionInterval(secondLead, secondLead);
1137:
1138:
1139:
1140: if (reverse)
1141: {
1142:
1143: if (firstLead == 0)
1144: firstLead += firstMax + 1;
1145: firstLead -= 2;
1146: }
1147:
1148: firstModel.setSelectionInterval((firstLead + 1) % (firstMax + 1),
1149: (firstLead + 1) % (firstMax + 1));
1150: }
1151: }
1152:
1153: protected void installListeners()
1154: {
1155: if (focusListener == null)
1156: focusListener = createFocusListener();
1157: table.addFocusListener(focusListener);
1158: if (keyListener == null)
1159: keyListener = createKeyListener();
1160: table.addKeyListener(keyListener);
1161: if (mouseInputListener == null)
1162: mouseInputListener = createMouseInputListener();
1163: table.addMouseListener(mouseInputListener);
1164: table.addMouseMotionListener(mouseInputListener);
1165: if (propertyChangeListener == null)
1166: propertyChangeListener = new PropertyChangeHandler();
1167: table.addPropertyChangeListener(propertyChangeListener);
1168: }
1169:
1170: protected void uninstallDefaults()
1171: {
1172:
1173:
1174:
1175:
1176:
1177:
1178:
1179:
1180:
1181:
1182:
1183:
1184:
1185:
1186:
1187:
1188: }
1189:
1190: protected void uninstallKeyboardActions()
1191: throws NotImplementedException
1192: {
1193:
1194: }
1195:
1196: protected void uninstallListeners()
1197: {
1198: table.removeFocusListener(focusListener);
1199: table.removeKeyListener(keyListener);
1200: table.removeMouseListener(mouseInputListener);
1201: table.removeMouseMotionListener(mouseInputListener);
1202: table.removePropertyChangeListener(propertyChangeListener);
1203: propertyChangeListener = null;
1204: }
1205:
1206: public void installUI(JComponent comp)
1207: {
1208: table = (JTable) comp;
1209: rendererPane = new CellRendererPane();
1210: table.add(rendererPane);
1211:
1212: installDefaults();
1213: installKeyboardActions();
1214: installListeners();
1215: }
1216:
1217: public void uninstallUI(JComponent c)
1218: {
1219: uninstallListeners();
1220: uninstallKeyboardActions();
1221: uninstallDefaults();
1222:
1223: table.remove(rendererPane);
1224: rendererPane = null;
1225: table = null;
1226: }
1227:
1228:
1239: void paintCell(Graphics g, int row, int col, Rectangle bounds,
1240: TableCellRenderer rend)
1241: {
1242: Component comp = table.prepareRenderer(rend, row, col);
1243: rendererPane.paintComponent(g, comp, table, bounds);
1244: }
1245:
1246:
1249: public void paint(Graphics gfx, JComponent ignored)
1250: {
1251: int ncols = table.getColumnCount();
1252: int nrows = table.getRowCount();
1253: if (nrows == 0 || ncols == 0)
1254: return;
1255:
1256: Rectangle clip = gfx.getClipBounds();
1257:
1258:
1259: Point p1 = new Point(clip.x, clip.y);
1260: int c0 = table.columnAtPoint(p1);
1261: if (c0 == -1)
1262: c0 = 0;
1263: int r0 = table.rowAtPoint(p1);
1264: if (r0 == -1)
1265: r0 = 0;
1266: Point p2 = new Point(clip.x + clip.width, clip.y + clip.height);
1267: int cn = table.columnAtPoint(p2);
1268: if (cn == -1)
1269: cn = table.getColumnCount() - 1;
1270: int rn = table.rowAtPoint(p2);
1271: if (rn == -1)
1272: rn = table.getRowCount() - 1;
1273:
1274: int columnMargin = table.getColumnModel().getColumnMargin();
1275: int rowMargin = table.getRowMargin();
1276:
1277: TableColumnModel cmodel = table.getColumnModel();
1278: int[] widths = new int[cn + 1];
1279: for (int i = c0; i <= cn; i++)
1280: {
1281: widths[i] = cmodel.getColumn(i).getWidth() - columnMargin;
1282: }
1283:
1284: Rectangle bounds = table.getCellRect(r0, c0, false);
1285:
1286: int left = bounds.x;
1287:
1288:
1289: int top = bounds.y;
1290:
1291:
1292: int bottom;
1293:
1294:
1295: Color grid = table.getGridColor();
1296: for (int r = r0; r <= rn; ++r)
1297: {
1298: for (int c = c0; c <= cn; ++c)
1299: {
1300: bounds.width = widths[c];
1301: paintCell(gfx, r, c, bounds, table.getCellRenderer(r, c));
1302: bounds.x += widths[c] + columnMargin;
1303: }
1304: bounds.x = left;
1305: bounds.y += table.getRowHeight(r);
1306:
1307: bounds.height = table.getRowHeight(r + 1) - rowMargin;
1308: }
1309:
1310: bottom = bounds.y - rowMargin;
1311:
1312:
1313: if (grid != null && table.getShowVerticalLines())
1314: {
1315: Color save = gfx.getColor();
1316: gfx.setColor(grid);
1317: int x = left - columnMargin;
1318: for (int c = c0; c <= cn; ++c)
1319: {
1320:
1321:
1322: x += widths[c] + columnMargin;
1323: gfx.drawLine(x, top, x, bottom);
1324: }
1325: gfx.setColor(save);
1326: }
1327:
1328:
1329: if (grid != null && table.getShowHorizontalLines())
1330: {
1331: Color save = gfx.getColor();
1332: gfx.setColor(grid);
1333: int y = top - rowMargin;
1334: for (int r = r0; r <= rn; ++r)
1335: {
1336:
1337:
1338: y += table.getRowHeight(r);
1339: gfx.drawLine(left, y, p2.x, y);
1340: }
1341: gfx.setColor(save);
1342: }
1343: }
1344: }