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:
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
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:
73: public class JTable extends JComponent
74: implements TableModelListener, Scrollable, TableColumnModelListener,
75: ListSelectionListener, CellEditorListener, Accessible
76: {
77:
78:
81: private class BooleanCellRenderer
82: extends DefaultTableCellRenderer
83: {
84:
85:
88: private JCheckBox checkBox = new JCheckBox();
89:
90:
102: public Component getTableCellRendererComponent(JTable table, Object value,
103: boolean isSelected,
104: boolean hasFocus, int row,
105: int column)
106: {
107: Boolean boolValue = (Boolean) value;
108: checkBox.setSelected(boolValue.booleanValue());
109: return checkBox;
110: }
111: }
112:
113:
116: private class DateCellRenderer
117: extends DefaultTableCellRenderer
118: {
119:
131: public Component getTableCellRendererComponent(JTable table, Object value,
132: boolean isSelected,
133: boolean hasFocus, int row,
134: int column)
135: {
136: super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
137: row, column);
138: if (value instanceof Date)
139: {
140: Date dateValue = (Date) value;
141: DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
142: setText(df.format(dateValue));
143: }
144: return this;
145: }
146: }
147:
148:
151: private class DoubleCellRenderer
152: extends DefaultTableCellRenderer
153: {
154:
157: public DoubleCellRenderer()
158: {
159: setHorizontalAlignment(JLabel.RIGHT);
160: }
161:
162:
174: public Component getTableCellRendererComponent(JTable table, Object value,
175: boolean isSelected,
176: boolean hasFocus, int row,
177: int column)
178: {
179: super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
180: row, column);
181: if (value instanceof Double)
182: {
183: Double doubleValue = (Double) value;
184: NumberFormat nf = NumberFormat.getInstance();
185: setText(nf.format(doubleValue.doubleValue()));
186: }
187: return this;
188: }
189: }
190:
191:
194: private class FloatCellRenderer
195: extends DefaultTableCellRenderer
196: {
197:
200: public FloatCellRenderer()
201: {
202: setHorizontalAlignment(JLabel.RIGHT);
203: }
204:
205:
217: public Component getTableCellRendererComponent(JTable table, Object value,
218: boolean isSelected,
219: boolean hasFocus, int row,
220: int column)
221: {
222: super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
223: row, column);
224: if (value instanceof Float)
225: {
226: Float floatValue = (Float) value;
227: NumberFormat nf = NumberFormat.getInstance();
228: setText(nf.format(floatValue.floatValue()));
229: }
230: return this;
231: }
232: }
233:
234:
237: private class NumberCellRenderer
238: extends DefaultTableCellRenderer
239: {
240:
243: public NumberCellRenderer()
244: {
245: setHorizontalAlignment(JLabel.RIGHT);
246: }
247: }
248:
249:
252: private class IconCellRenderer
253: extends DefaultTableCellRenderer
254: {
255:
267: public Component getTableCellRendererComponent(JTable table, Object value,
268: boolean isSelected,
269: boolean hasFocus, int row,
270: int column)
271: {
272: super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
273: row, column);
274: if (value instanceof Icon)
275: {
276: Icon iconValue = (Icon) value;
277: setIcon(iconValue);
278: }
279: return this;
280: }
281: }
282:
283: private static final long serialVersionUID = 3876025080382781659L;
284:
285:
286:
291: public static final int AUTO_RESIZE_OFF = 0;
292:
293:
298: public static final int AUTO_RESIZE_NEXT_COLUMN = 1;
299:
300:
305: public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS = 2;
306:
307:
313: public static final int AUTO_RESIZE_ALL_COLUMNS = 4;
314:
315:
320: public static final int AUTO_RESIZE_LAST_COLUMN = 3;
321:
322:
323:
328: protected Hashtable defaultEditorsByColumnClass;
329:
330:
335: protected Hashtable defaultRenderersByColumnClass;
336:
337:
340: protected int editingColumn;
341:
342:
345: protected int editingRow;
346:
347:
352: protected transient Component editorComp;
353:
354:
365: protected boolean autoCreateColumnsFromModel;
366:
367:
377: protected int autoResizeMode;
378:
379:
390: protected int rowHeight;
391:
392:
402: protected int rowMargin;
403:
404:
417: protected boolean rowSelectionAllowed;
418:
419:
424: protected boolean cellSelectionEnabled;
425:
426:
435: protected TableModel dataModel;
436:
437:
459: protected TableColumnModel columnModel;
460:
461:
473: protected ListSelectionModel selectionModel;
474:
475:
478: protected AccessibleContext accessibleContext;
479:
480:
483: protected TableCellEditor cellEditor;
484:
485:
491: private boolean dragEnabled;
492:
493:
500: protected Color gridColor;
501:
502:
509: protected Dimension preferredViewportSize;
510:
511:
519: protected Color selectionBackground;
520:
521:
525: private static final String SELECTION_BACKGROUND_CHANGED_PROPERTY = "selectionBackground";
526:
527:
535: protected Color selectionForeground;
536:
537:
541: private static final String SELECTION_FOREGROUND_CHANGED_PROPERTY = "selectionForeground";
542:
543:
546: protected boolean showHorizontalLines;
547:
548:
551: protected boolean showVerticalLines;
552:
553:
556: protected JTableHeader tableHeader;
557:
558:
559:
562: public JTable ()
563: {
564: this(null, null, null);
565: }
566:
567:
573: public JTable (int numRows, int numColumns)
574: {
575: this(new DefaultTableModel(numRows, numColumns));
576: }
577:
578:
584: public JTable(Object[][] data, Object[] columnNames)
585: {
586: this(new DefaultTableModel(data, columnNames));
587: }
588:
589:
594: public JTable (TableModel dm)
595: {
596: this(dm, null, null);
597: }
598:
599:
605: public JTable (TableModel dm, TableColumnModel cm)
606: {
607: this(dm, cm, null);
608: }
609:
610:
617: public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm)
618: {
619: setModel(dm == null ? createDefaultDataModel() : dm);
620: setSelectionModel(sm == null ? createDefaultSelectionModel() : sm);
621:
622: this.columnModel = cm;
623: initializeLocalVars();
624: updateUI();
625: }
626:
627: protected void initializeLocalVars()
628: {
629: setTableHeader(createDefaultTableHeader());
630: this.autoCreateColumnsFromModel = false;
631: if (columnModel == null)
632: {
633: this.autoCreateColumnsFromModel = true;
634: createColumnsFromModel();
635: }
636: this.columnModel.addColumnModelListener(this);
637:
638: this.defaultRenderersByColumnClass = new Hashtable();
639: createDefaultRenderers();
640:
641: this.defaultEditorsByColumnClass = new Hashtable();
642: createDefaultEditors();
643:
644: this.autoResizeMode = AUTO_RESIZE_ALL_COLUMNS;
645: this.rowHeight = 16;
646: this.rowMargin = 1;
647: this.rowSelectionAllowed = true;
648:
649: this.cellEditor = null;
650:
651: this.dragEnabled = true;
652: this.preferredViewportSize = new Dimension(450,400);
653: this.showHorizontalLines = true;
654: this.showVerticalLines = true;
655: this.editingColumn = -1;
656: this.editingRow = -1;
657: setIntercellSpacing(new Dimension(1,1));
658: }
659:
660:
666: public JTable(Vector data, Vector columnNames)
667: {
668: this(new DefaultTableModel(data, columnNames));
669: }
670:
671: public void addColumn(TableColumn column)
672: {
673: if (column.getHeaderValue() == null)
674: {
675: String name = dataModel.getColumnName(column.getModelIndex());
676: column.setHeaderValue(name);
677: }
678:
679: columnModel.addColumn(column);
680: }
681:
682: protected void createDefaultEditors()
683: {
684:
685: }
686:
687: protected void createDefaultRenderers()
688: {
689: setDefaultRenderer(Boolean.class, new BooleanCellRenderer());
690: setDefaultRenderer(Number.class, new NumberCellRenderer());
691: setDefaultRenderer(Double.class, new DoubleCellRenderer());
692: setDefaultRenderer(Double.class, new FloatCellRenderer());
693: setDefaultRenderer(Date.class, new DateCellRenderer());
694: setDefaultRenderer(Icon.class, new IconCellRenderer());
695: }
696:
697:
700: public static JScrollPane createScrollPaneForTable(JTable table)
701: {
702: return new JScrollPane(table);
703: }
704:
705: protected TableColumnModel createDefaultColumnModel()
706: {
707: return new DefaultTableColumnModel();
708: }
709:
710: protected TableModel createDefaultDataModel()
711: {
712: return new DefaultTableModel();
713: }
714:
715: protected ListSelectionModel createDefaultSelectionModel()
716: {
717: return new DefaultListSelectionModel();
718: }
719:
720: protected JTableHeader createDefaultTableHeader()
721: {
722: return new JTableHeader(columnModel);
723: }
724:
725: private void createColumnsFromModel()
726: {
727: if (dataModel == null)
728: return;
729:
730: TableColumnModel cm = createDefaultColumnModel();
731:
732: for (int i = 0; i < dataModel.getColumnCount(); ++i)
733: {
734: cm.addColumn(new TableColumn(i));
735: }
736: this.setColumnModel(cm);
737: }
738:
739:
740:
741: public void columnAdded (TableColumnModelEvent event)
742: {
743: revalidate();
744: repaint();
745: }
746:
747: public void columnMarginChanged (ChangeEvent event)
748: {
749: revalidate();
750: repaint();
751: }
752:
753: public void columnMoved (TableColumnModelEvent event)
754: {
755: revalidate();
756: repaint();
757: }
758:
759: public void columnRemoved (TableColumnModelEvent event)
760: {
761: revalidate();
762: repaint();
763: }
764:
765: public void columnSelectionChanged (ListSelectionEvent event)
766: {
767: repaint();
768: }
769:
770: public void editingCanceled (ChangeEvent event)
771: {
772: repaint();
773: }
774:
775: public void editingStopped (ChangeEvent event)
776: {
777: repaint();
778: }
779:
780: public void tableChanged (TableModelEvent event)
781: {
782:
783:
784: if ((event.getFirstRow() ==TableModelEvent.HEADER_ROW)
785: && autoCreateColumnsFromModel)
786:
787: createColumnsFromModel();
788:
789: repaint();
790: }
791:
792: public void valueChanged (ListSelectionEvent event)
793: {
794: repaint();
795: }
796:
797:
805: public int columnAtPoint(Point point)
806: {
807: int x0 = getLocation().x;
808: int ncols = getColumnCount();
809: Dimension gap = getIntercellSpacing();
810: TableColumnModel cols = getColumnModel();
811: int x = point.x;
812:
813: for (int i = 0; i < ncols; ++i)
814: {
815: int width = cols.getColumn(i).getWidth() + (gap == null ? 0 : gap.width);
816: if (0 <= x && x < width)
817: return i;
818: x -= width;
819: }
820:
821: return -1;
822: }
823:
824:
832: public int rowAtPoint(Point point)
833: {
834: int y0 = getLocation().y;
835: int nrows = getRowCount();
836: Dimension gap = getIntercellSpacing();
837: int height = getRowHeight() + (gap == null ? 0 : gap.height);
838: int y = point.y;
839:
840: for (int i = 0; i < nrows; ++i)
841: {
842: if (0 <= y && y < height)
843: return i;
844: y -= height;
845: }
846:
847: return -1;
848: }
849:
850:
866: public Rectangle getCellRect(int row,
867: int column,
868: boolean includeSpacing)
869: {
870: int height = getHeight();
871: int width = columnModel.getColumn(column).getWidth();
872: int x_gap = columnModel.getColumnMargin();
873: int y_gap = rowMargin;
874:
875: column = Math.max(0, Math.min(column, getColumnCount() - 1));
876: row = Math.max(0, Math.min(row, getRowCount() - 1));
877:
878: int x = 0;
879: int y = (height + y_gap) * row;
880:
881: for (int i = 0; i < column; ++i)
882: {
883: x += columnModel.getColumn(i).getWidth();
884: x += x_gap;
885: }
886:
887: if (includeSpacing)
888: return new Rectangle(x, y, width, height);
889: else
890: return new Rectangle(x, y, width - x_gap, height - y_gap);
891: }
892:
893: public void clearSelection()
894: {
895: selectionModel.clearSelection();
896: getColumnModel().getSelectionModel().clearSelection();
897: }
898:
899:
906: public int getSelectedRow ()
907: {
908: return selectionModel.getMinSelectionIndex();
909: }
910:
911:
916: public ListSelectionModel getSelectionModel()
917: {
918:
919: return selectionModel;
920: }
921:
922: public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
923: {
924: if (orientation == SwingConstants.VERTICAL)
925: return visibleRect.height * direction;
926: else
927: return visibleRect.width * direction;
928: }
929:
930:
935: public boolean getScrollableTracksViewportHeight()
936: {
937: return false;
938: }
939:
940:
946: public boolean getScrollableTracksViewportWidth()
947: {
948: if (autoResizeMode == AUTO_RESIZE_OFF)
949: return false;
950: else
951: return true;
952: }
953:
954: public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
955: {
956:
957:
958:
959:
960: if (orientation == SwingConstants.VERTICAL)
961: return rowHeight;
962: else
963: {
964: int sum = 0;
965: for (int i = 0; i < getColumnCount(); ++i)
966: sum += columnModel.getColumn(0).getWidth();
967: return getColumnCount() == 0 ? 10 : sum / getColumnCount();
968: }
969: }
970:
971:
972: public TableCellEditor getCellEditor(int row, int column)
973: {
974: TableCellEditor editor = columnModel.getColumn(column).getCellEditor();
975:
976: if (editor == null)
977: editor = getDefaultEditor(dataModel.getColumnClass(column));
978:
979: return editor;
980: }
981:
982: public TableCellEditor getDefaultEditor(Class columnClass)
983: {
984: if (defaultEditorsByColumnClass.containsKey(columnClass))
985: return (TableCellEditor) defaultEditorsByColumnClass.get(columnClass);
986: else
987: {
988:
989: TableCellEditor r = new DefaultCellEditor(new JTextField());
990: defaultEditorsByColumnClass.put(columnClass, r);
991: return r;
992: }
993: }
994:
995:
996:
997: public TableCellRenderer getCellRenderer(int row, int column)
998: {
999: TableCellRenderer renderer =
1000: columnModel.getColumn(column).getCellRenderer();
1001:
1002: if (renderer == null)
1003: renderer = getDefaultRenderer(dataModel.getColumnClass(column));
1004:
1005: return renderer;
1006: }
1007:
1008: public void setDefaultRenderer(Class columnClass, TableCellRenderer rend)
1009: {
1010: defaultRenderersByColumnClass.put(columnClass, rend);
1011: }
1012:
1013: public TableCellRenderer getDefaultRenderer(Class columnClass)
1014: {
1015: if (defaultRenderersByColumnClass.containsKey(columnClass))
1016: return (TableCellRenderer) defaultRenderersByColumnClass.get(columnClass);
1017: else
1018: {
1019: TableCellRenderer r = new DefaultTableCellRenderer();
1020: defaultRenderersByColumnClass.put(columnClass, r);
1021: return r;
1022: }
1023: }
1024:
1025: public int convertColumnIndexToModel(int vc)
1026: {
1027: if (vc < 0)
1028: return vc;
1029: else
1030: return columnModel.getColumn(vc).getModelIndex();
1031: }
1032:
1033: public int convertColumnIndexToView(int mc)
1034: {
1035: if (mc < 0)
1036: return mc;
1037: int ncols = getColumnCount();
1038: for (int vc = 0; vc < ncols; ++vc)
1039: {
1040: if (columnModel.getColumn(vc).getModelIndex() == mc)
1041: return vc;
1042: }
1043: return -1;
1044: }
1045:
1046: public Component prepareRenderer(TableCellRenderer renderer,
1047: int row,
1048: int column)
1049: {
1050: boolean rsa = getRowSelectionAllowed();
1051: boolean csa = getColumnSelectionAllowed();
1052: boolean rs = rsa ? getSelectionModel().isSelectedIndex(row) : false;
1053: boolean cs = csa ? columnModel.getSelectionModel().isSelectedIndex(column) : false;
1054: boolean isSelected = ((rsa && csa && rs && cs)
1055: || (rsa && !csa && rs)
1056: || (!rsa && csa && cs));
1057:
1058: return renderer.getTableCellRendererComponent(this,
1059: dataModel.getValueAt(row,
1060: convertColumnIndexToModel(column)),
1061: isSelected,
1062: false,
1063: row, column);
1064: }
1065:
1066:
1067:
1072: public boolean getAutoCreateColumnsFromModel()
1073: {
1074: return autoCreateColumnsFromModel;
1075: }
1076:
1077:
1082: public int getAutoResizeMode()
1083: {
1084: return autoResizeMode;
1085: }
1086:
1087:
1092: public int getRowHeight()
1093: {
1094: return rowHeight;
1095: }
1096:
1097:
1102: public int getRowMargin()
1103: {
1104: return rowMargin;
1105: }
1106:
1107:
1112: public boolean getRowSelectionAllowed()
1113: {
1114: return rowSelectionAllowed;
1115: }
1116:
1117:
1122: public boolean getCellSelectionEnabled()
1123: {
1124: return getColumnSelectionAllowed() && getRowSelectionAllowed();
1125: }
1126:
1127:
1132: public TableModel getModel()
1133: {
1134: return dataModel;
1135: }
1136:
1137:
1143: public int getColumnCount()
1144: {
1145: return columnModel.getColumnCount();
1146: }
1147:
1148:
1154: public int getRowCount()
1155: {
1156: return dataModel.getRowCount();
1157: }
1158:
1159:
1164: public TableColumnModel getColumnModel()
1165: {
1166: return columnModel;
1167: }
1168:
1169:
1175: public int getSelectedColumn()
1176: {
1177: return columnModel.getSelectionModel().getMinSelectionIndex();
1178: }
1179:
1180: private static int countSelections(ListSelectionModel lsm)
1181: {
1182: int lo = lsm.getMinSelectionIndex();
1183: int hi = lsm.getMaxSelectionIndex();
1184: int sum = 0;
1185: if (lo != -1 && hi != -1)
1186: {
1187: switch (lsm.getSelectionMode())
1188: {
1189: case ListSelectionModel.SINGLE_SELECTION:
1190: sum = 1;
1191: break;
1192:
1193: case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
1194: sum = hi - lo + 1;
1195: break;
1196:
1197: case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
1198: for (int i = lo; i <= hi; ++i)
1199: if (lsm.isSelectedIndex(i))
1200: ++sum;
1201: break;
1202: }
1203: }
1204: return sum;
1205: }
1206:
1207: private static int[] getSelections(ListSelectionModel lsm)
1208: {
1209: int sz = countSelections(lsm);
1210: int [] ret = new int[sz];
1211:
1212: int lo = lsm.getMinSelectionIndex();
1213: int hi = lsm.getMaxSelectionIndex();
1214: int j = 0;
1215: java.util.ArrayList ls = new java.util.ArrayList();
1216: if (lo != -1 && hi != -1)
1217: {
1218: switch (lsm.getSelectionMode())
1219: {
1220: case ListSelectionModel.SINGLE_SELECTION:
1221: ret[0] = lo;
1222: break;
1223:
1224: case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
1225: for (int i = lo; i <= hi; ++i)
1226: ret[j++] = i;
1227: break;
1228:
1229: case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
1230: for (int i = lo; i <= hi; ++i)
1231: if (lsm.isSelectedIndex(i))
1232: ret[j++] = i;
1233: break;
1234: }
1235: }
1236: return ret;
1237: }
1238:
1239:
1245: public int getSelectedColumnCount()
1246: {
1247: return countSelections(columnModel.getSelectionModel());
1248: }
1249:
1250:
1256: public int[] getSelectedColumns()
1257: {
1258: return getSelections(columnModel.getSelectionModel());
1259: }
1260:
1261:
1266: public boolean getColumnSelectionAllowed()
1267: {
1268: return getColumnModel().getColumnSelectionAllowed();
1269: }
1270:
1271:
1277: public int getSelectedRowCount()
1278: {
1279: return countSelections(selectionModel);
1280: }
1281:
1282:
1288: public int[] getSelectedRows()
1289: {
1290: return getSelections(selectionModel);
1291: }
1292:
1293:
1298: public AccessibleContext getAccessibleContext()
1299: {
1300: return accessibleContext;
1301: }
1302:
1303:
1308: public TableCellEditor getCellEditor()
1309: {
1310: return cellEditor;
1311: }
1312:
1313:
1318: public boolean getDragEnabled()
1319: {
1320: return dragEnabled;
1321: }
1322:
1323:
1328: public Color getGridColor()
1329: {
1330: return gridColor;
1331: }
1332:
1333:
1338: public Dimension getIntercellSpacing()
1339: {
1340: return new Dimension(columnModel.getColumnMargin(), rowMargin);
1341: }
1342:
1343:
1348: public Dimension getPreferredScrollableViewportSize()
1349: {
1350: return preferredViewportSize;
1351: }
1352:
1353:
1358: public Color getSelectionBackground()
1359: {
1360: return selectionBackground;
1361: }
1362:
1363:
1368: public Color getSelectionForeground()
1369: {
1370: return selectionForeground;
1371: }
1372:
1373:
1378: public boolean getShowHorizontalLines()
1379: {
1380: return showHorizontalLines;
1381: }
1382:
1383:
1388: public boolean getShowVerticalLines()
1389: {
1390: return showVerticalLines;
1391: }
1392:
1393:
1398: public JTableHeader getTableHeader()
1399: {
1400: return tableHeader;
1401: }
1402:
1403:
1408: public void removeColumn(TableColumn column)
1409: {
1410: columnModel.removeColumn(column);
1411: }
1412:
1413:
1419: public void moveColumn(int column,int targetColumn)
1420: {
1421: columnModel.moveColumn(column, targetColumn);
1422: }
1423:
1424:
1431: public void setAutoCreateColumnsFromModel(boolean autoCreate)
1432: {
1433: if (autoCreateColumnsFromModel != autoCreate)
1434: {
1435: autoCreateColumnsFromModel = autoCreate;
1436: if (autoCreate)
1437: createDefaultColumnsFromModel();
1438: }
1439: }
1440:
1441:
1446: public void setAutoResizeMode(int a)
1447: {
1448: autoResizeMode = a;
1449: revalidate();
1450: repaint();
1451: }
1452:
1453:
1458: public void setRowHeight(int r)
1459: {
1460: if (rowHeight < 1)
1461: throw new IllegalArgumentException();
1462:
1463: rowHeight = r;
1464: revalidate();
1465: repaint();
1466: }
1467:
1468:
1473: public void setRowMargin(int r)
1474: {
1475: rowMargin = r;
1476: revalidate();
1477: repaint();
1478: }
1479:
1480:
1485: public void setRowSelectionAllowed(boolean r)
1486: {
1487: rowSelectionAllowed = r;
1488: repaint();
1489: }
1490:
1491:
1496: public void setCellSelectionEnabled(boolean c)
1497: {
1498: setColumnSelectionAllowed(c);
1499: setRowSelectionAllowed(c);
1500:
1501: cellSelectionEnabled = true;
1502: }
1503:
1504:
1513: public void setModel(TableModel m)
1514: {
1515:
1516: if (m == null)
1517: throw new IllegalArgumentException();
1518:
1519:
1520: if (dataModel == m)
1521: return;
1522:
1523:
1524: if (dataModel != null)
1525: dataModel.removeTableModelListener(this);
1526:
1527: if (m != null)
1528: {
1529:
1530: dataModel = m;
1531:
1532:
1533: dataModel.addTableModelListener(this);
1534:
1535:
1536: if (autoCreateColumnsFromModel)
1537: createColumnsFromModel();
1538: }
1539:
1540:
1541: revalidate();
1542: repaint();
1543: }
1544:
1545:
1554: public void setColumnModel(TableColumnModel c)
1555: {
1556: if (c == null)
1557: throw new IllegalArgumentException();
1558: TableColumnModel tmp = columnModel;
1559: if (tmp != null)
1560: tmp.removeColumnModelListener(this);
1561: if (c != null)
1562: c.addColumnModelListener(this);
1563: columnModel = c;
1564: if (dataModel != null && columnModel != null)
1565: {
1566: int ncols = getColumnCount();
1567: for (int i = 0; i < ncols; ++i)
1568: columnModel.getColumn(i).setHeaderValue(dataModel.getColumnName(i));
1569: }
1570:
1571:
1572:
1573: if (tableHeader != null)
1574: tableHeader.setColumnModel(c);
1575:
1576: revalidate();
1577: repaint();
1578: }
1579:
1580:
1585: public void setColumnSelectionAllowed(boolean c)
1586: {
1587: getColumnModel().setColumnSelectionAllowed(c);
1588: repaint();
1589: }
1590:
1591:
1600: public void setSelectionModel(ListSelectionModel s)
1601: {
1602: if (s == null)
1603: throw new IllegalArgumentException();
1604: ListSelectionModel tmp = selectionModel;
1605: if (tmp != null)
1606: tmp.removeListSelectionListener(this);
1607: if (s != null)
1608: s.addListSelectionListener(this);
1609: selectionModel = s;
1610: }
1611:
1612:
1619: public void setSelectionMode(int s)
1620: {
1621: selectionModel.setSelectionMode(s);
1622: columnModel.getSelectionModel().setSelectionMode(s);
1623:
1624: repaint();
1625: }
1626:
1627:
1636: public void setCellEditor(TableCellEditor c)
1637: {
1638: TableCellEditor tmp = cellEditor;
1639: if (tmp != null)
1640: tmp.removeCellEditorListener(this);
1641: if (c != null)
1642: c.addCellEditorListener(this);
1643: cellEditor = c;
1644: }
1645:
1646:
1651: public void setDragEnabled(boolean d)
1652: {
1653: dragEnabled = d;
1654: }
1655:
1656:
1661: public void setGridColor(Color g)
1662: {
1663: gridColor = g;
1664: repaint();
1665: }
1666:
1667:
1672: public void setIntercellSpacing(Dimension i)
1673: {
1674: rowMargin = i.height;
1675: columnModel.setColumnMargin(i.width);
1676: repaint();
1677: }
1678:
1679:
1684: public void setPreferredScrollableViewportSize(Dimension p)
1685: {
1686: preferredViewportSize = p;
1687: revalidate();
1688: repaint();
1689: }
1690:
1691:
1700: public void setSelectionBackground(Color s)
1701: {
1702: Color tmp = selectionBackground;
1703: selectionBackground = s;
1704: if (((tmp == null && s != null)
1705: || (s == null && tmp != null)
1706: || (tmp != null && s != null && !tmp.equals(s))))
1707: firePropertyChange(SELECTION_BACKGROUND_CHANGED_PROPERTY, tmp, s);
1708: repaint();
1709: }
1710:
1711:
1720: public void setSelectionForeground(Color s)
1721: {
1722: Color tmp = selectionForeground;
1723: selectionForeground = s;
1724: if (((tmp == null && s != null)
1725: || (s == null && tmp != null)
1726: || (tmp != null && s != null && !tmp.equals(s))))
1727: firePropertyChange(SELECTION_FOREGROUND_CHANGED_PROPERTY, tmp, s);
1728: repaint();
1729: }
1730:
1731:
1736: public void setShowGrid(boolean s)
1737: {
1738: setShowVerticalLines(s);
1739: setShowHorizontalLines(s);
1740: }
1741:
1742:
1747: public void setShowHorizontalLines(boolean s)
1748: {
1749: showHorizontalLines = s;
1750: repaint();
1751: }
1752:
1753:
1758: public void setShowVerticalLines(boolean s)
1759: {
1760: showVerticalLines = s;
1761: repaint();
1762: }
1763:
1764:
1769: public void setTableHeader(JTableHeader t)
1770: {
1771: if (tableHeader != null)
1772: tableHeader.setTable(null);
1773: tableHeader = t;
1774: if (tableHeader != null)
1775: tableHeader.setTable(this);
1776: revalidate();
1777: repaint();
1778: }
1779:
1780: protected void configureEnclosingScrollPane()
1781: {
1782: JScrollPane jsp = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this);
1783: if (jsp != null && tableHeader != null)
1784: {
1785: jsp.setColumnHeaderView(tableHeader);
1786: }
1787: }
1788:
1789: protected void unconfigureEnclosingScrollPane()
1790: {
1791: JScrollPane jsp = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this);
1792: if (jsp != null)
1793: {
1794: jsp.setColumnHeaderView(null);
1795: }
1796: }
1797:
1798:
1799: public void addNotify()
1800: {
1801: super.addNotify();
1802: configureEnclosingScrollPane();
1803: }
1804:
1805: public void removeNotify()
1806: {
1807: super.addNotify();
1808: unconfigureEnclosingScrollPane();
1809: }
1810:
1811:
1812:
1820: private void distributeSpill(TableColumn[] cols, int spill)
1821: {
1822: int average = spill / cols.length;
1823: for (int i = 0; i < cols.length; i++)
1824: {
1825: cols[i].setWidth(cols[i].getWidth() + average);
1826: }
1827: }
1828:
1829: public void doLayout()
1830: {
1831: TableColumn resizingColumn = null;
1832:
1833: int ncols = getColumnCount();
1834: if (ncols < 1)
1835: return;
1836:
1837: int[] pref = new int[ncols];
1838: int prefSum = 0;
1839: int rCol = -1;
1840:
1841: if (tableHeader != null)
1842: resizingColumn = tableHeader.getResizingColumn();
1843:
1844: for (int i = 0; i < ncols; ++i)
1845: {
1846: TableColumn col = columnModel.getColumn(i);
1847: int p = col.getWidth();
1848: pref[i] = p;
1849: prefSum += p;
1850: if (resizingColumn == col)
1851: rCol = i;
1852: }
1853:
1854: int spill = getWidth() - prefSum;
1855:
1856: if (resizingColumn != null)
1857: {
1858: TableColumn col;
1859: TableColumn [] cols;
1860:
1861: switch (getAutoResizeMode())
1862: {
1863: case AUTO_RESIZE_LAST_COLUMN:
1864: col = columnModel.getColumn(ncols-1);
1865: col.setWidth(col.getPreferredWidth() + spill);
1866: break;
1867:
1868: case AUTO_RESIZE_NEXT_COLUMN:
1869: col = columnModel.getColumn(ncols-1);
1870: col.setWidth(col.getPreferredWidth() + spill);
1871: break;
1872:
1873: case AUTO_RESIZE_ALL_COLUMNS:
1874: cols = new TableColumn[ncols];
1875: for (int i = 0; i < ncols; ++i)
1876: cols[i] = columnModel.getColumn(i);
1877: distributeSpill(cols, spill);
1878: break;
1879:
1880: case AUTO_RESIZE_SUBSEQUENT_COLUMNS:
1881: cols = new TableColumn[ncols];
1882: for (int i = rCol; i < ncols; ++i)
1883: cols[i] = columnModel.getColumn(i);
1884: distributeSpill(cols, spill);
1885: break;
1886:
1887: case AUTO_RESIZE_OFF:
1888: default:
1889: }
1890: }
1891: else
1892: {
1893: TableColumn [] cols = new TableColumn[ncols];
1894: for (int i = 0; i < ncols; ++i)
1895: cols[i] = columnModel.getColumn(i);
1896: distributeSpill(cols, spill);
1897: }
1898: }
1899:
1900:
1903: public void sizeColumnsToFit(boolean lastColumnOnly)
1904: {
1905: doLayout();
1906: }
1907:
1908:
1911: public void sizeColumnsToFit(int resizingColumn)
1912: {
1913: doLayout();
1914: }
1915:
1916: public String getUIClassID()
1917: {
1918: return "TableUI";
1919: }
1920:
1921:
1926: public TableUI getUI()
1927: {
1928: return (TableUI) ui;
1929: }
1930:
1931:
1936: public void setUI(TableUI ui)
1937: {
1938: super.setUI(ui);
1939: }
1940:
1941: public void updateUI()
1942: {
1943: setUI((TableUI) UIManager.getUI(this));
1944: revalidate();
1945: repaint();
1946: }
1947:
1948: public Class getColumnClass(int column)
1949: {
1950: return dataModel.getColumnClass(column);
1951: }
1952:
1953: public String getColumnName(int column)
1954: {
1955: int modelColumn = columnModel.getColumn(column).getModelIndex();
1956: return dataModel.getColumnName(modelColumn);
1957: }
1958:
1959: public int getEditingColumn()
1960: {
1961: return editingColumn;
1962: }
1963:
1964: public void setEditingColumn(int column)
1965: {
1966: editingColumn = column;
1967: }
1968:
1969: public int getEditingRow()
1970: {
1971: return editingRow;
1972: }
1973:
1974: public void setEditingRow(int column)
1975: {
1976: editingRow = column;
1977: }
1978:
1979: public Component getEditorComponent()
1980: {
1981: return editorComp;
1982: }
1983:
1984: public boolean isEditing()
1985: {
1986: return editorComp != null;
1987: }
1988:
1989: public void setDefaultEditor(Class columnClass, TableCellEditor editor)
1990: {
1991: if (editor != null)
1992: defaultEditorsByColumnClass.put(columnClass, editor);
1993: else
1994: defaultEditorsByColumnClass.remove(columnClass);
1995: }
1996:
1997: public void addColumnSelectionInterval(int index0, int index1)
1998: {
1999: if ((index0 < 0 || index0 > (getColumnCount()-1)
2000: || index1 < 0 || index1 > (getColumnCount()-1)))
2001: throw new IllegalArgumentException("Column index out of range.");
2002:
2003: getColumnModel().getSelectionModel().addSelectionInterval(index0, index1);
2004: }
2005:
2006: public void addRowSelectionInterval(int index0, int index1)
2007: {
2008: if ((index0 < 0 || index0 > (getRowCount()-1)
2009: || index1 < 0 || index1 > (getRowCount()-1)))
2010: throw new IllegalArgumentException("Row index out of range.");
2011:
2012: getSelectionModel().addSelectionInterval(index0, index1);
2013: }
2014:
2015: public void setColumnSelectionInterval(int index0, int index1)
2016: {
2017: if ((index0 < 0 || index0 > (getColumnCount()-1)
2018: || index1 < 0 || index1 > (getColumnCount()-1)))
2019: throw new IllegalArgumentException("Column index out of range.");
2020:
2021: getColumnModel().getSelectionModel().setSelectionInterval(index0, index1);
2022: }
2023:
2024: public void setRowSelectionInterval(int index0, int index1)
2025: {
2026: if ((index0 < 0 || index0 > (getRowCount()-1)
2027: || index1 < 0 || index1 > (getRowCount()-1)))
2028: throw new IllegalArgumentException("Row index out of range.");
2029:
2030: getSelectionModel().setSelectionInterval(index0, index1);
2031: }
2032:
2033: public void removeColumnSelectionInterval(int index0, int index1)
2034: {
2035: if ((index0 < 0 || index0 > (getColumnCount()-1)
2036: || index1 < 0 || index1 > (getColumnCount()-1)))
2037: throw new IllegalArgumentException("Column index out of range.");
2038:
2039: getColumnModel().getSelectionModel().removeSelectionInterval(index0, index1);
2040: }
2041:
2042: public void removeRowSelectionInterval(int index0, int index1)
2043: {
2044: if ((index0 < 0 || index0 > (getRowCount()-1)
2045: || index1 < 0 || index1 > (getRowCount()-1)))
2046: throw new IllegalArgumentException("Row index out of range.");
2047:
2048: getSelectionModel().removeSelectionInterval(index0, index1);
2049: }
2050:
2051: public boolean isColumnSelected(int column)
2052: {
2053: return getColumnModel().getSelectionModel().isSelectedIndex(column);
2054: }
2055:
2056: public boolean isRowSelected(int row)
2057: {
2058: return getSelectionModel().isSelectedIndex(row);
2059: }
2060:
2061: public boolean isCellSelected(int row, int column)
2062: {
2063: return isRowSelected(row) && isColumnSelected(column);
2064: }
2065:
2066: public void selectAll()
2067: {
2068: setColumnSelectionInterval(0, getColumnCount() - 1);
2069: setRowSelectionInterval(0, getRowCount() - 1);
2070: }
2071:
2072: public Object getValueAt(int row, int column)
2073: {
2074: return dataModel.getValueAt(row, convertColumnIndexToModel(column));
2075: }
2076:
2077: public void setValueAt(Object value, int row, int column)
2078: {
2079: dataModel.setValueAt(value, row, convertColumnIndexToModel(column));
2080: }
2081:
2082: public TableColumn getColumn(Object identifier)
2083: {
2084: return columnModel.getColumn(columnModel.getColumnIndex(identifier));
2085: }
2086:
2087:
2096: public boolean isCellEditable(int row, int column)
2097: {
2098: return dataModel.isCellEditable(row, convertColumnIndexToModel(column));
2099: }
2100:
2101:
2108: public void createDefaultColumnsFromModel()
2109: {
2110:
2111: int columnIndex = columnModel.getColumnCount() - 1;
2112: while (columnIndex >= 0)
2113: {
2114: columnModel.removeColumn(columnModel.getColumn(columnIndex));
2115: columnIndex--;
2116: }
2117:
2118:
2119: int columnCount = dataModel.getColumnCount();
2120: for (int c = 0; c < columnCount; c++)
2121: {
2122: TableColumn column = new TableColumn(c);
2123: column.setIdentifier(dataModel.getColumnName(c));
2124: columnModel.addColumn(column);
2125: }
2126: }
2127:
2128: public void changeSelection (int rowIndex, int columnIndex, boolean toggle, boolean extend)
2129: {
2130: if (toggle && extend)
2131: {
2132:
2133:
2134: selectionModel.setAnchorSelectionIndex(rowIndex);
2135: getColumnModel().getSelectionModel().setAnchorSelectionIndex(columnIndex);
2136: }
2137: else if (toggle)
2138: {
2139:
2140: if (isCellSelected(rowIndex,columnIndex))
2141: {
2142: selectionModel.removeSelectionInterval(rowIndex,rowIndex);
2143: getColumnModel().getSelectionModel().removeSelectionInterval(columnIndex,columnIndex);
2144: }
2145: else
2146: {
2147: selectionModel.addSelectionInterval(rowIndex,rowIndex);
2148: getColumnModel().getSelectionModel().addSelectionInterval(columnIndex,columnIndex);
2149: }
2150: }
2151: else if (extend)
2152: {
2153:
2154:
2155: selectionModel.setLeadSelectionIndex(rowIndex);
2156: getColumnModel().getSelectionModel().setLeadSelectionIndex(columnIndex);
2157: }
2158: else
2159: {
2160:
2161:
2162: selectionModel.clearSelection();
2163: selectionModel.setSelectionInterval(rowIndex,rowIndex);
2164: getColumnModel().getSelectionModel().clearSelection();
2165: getColumnModel().getSelectionModel().setSelectionInterval(columnIndex, columnIndex);
2166:
2167:
2168: }
2169: }
2170: }