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: 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: import ;
85: import ;
86: import ;
87: import ;
88:
89:
98: public class JTable
99: extends JComponent
100: implements TableModelListener, Scrollable, TableColumnModelListener,
101: ListSelectionListener, CellEditorListener, Accessible
102: {
103:
108: protected class AccessibleJTable
109: extends AccessibleJComponent
110: implements AccessibleSelection, ListSelectionListener, TableModelListener,
111: TableColumnModelListener, CellEditorListener, PropertyChangeListener,
112: AccessibleExtendedTable
113: {
114:
115:
120: protected class AccessibleJTableCell
121: extends AccessibleContext
122: implements Accessible, AccessibleComponent
123: {
124:
125:
128: private JTable table;
129:
130:
133: private int row;
134:
135:
138: private int column;
139:
140:
143: private int index;
144:
145:
153: public AccessibleJTableCell(JTable t, int r, int c, int i)
154: {
155: table = t;
156: row = r;
157: column = c;
158: index = i;
159: }
160:
161:
166: public AccessibleRole getAccessibleRole()
167: {
168:
169:
170:
171: return AccessibleRole.UNKNOWN;
172: }
173:
174:
179: public AccessibleStateSet getAccessibleStateSet()
180: {
181: AccessibleStateSet state = new AccessibleStateSet();
182:
183:
184: Rectangle visibleRect = getVisibleRect();
185: Rectangle cellRect = getCellRect(row, column, false);
186: if (visibleRect.intersects(cellRect))
187: state.add(AccessibleState.SHOWING);
188:
189:
190: if (isCellSelected(row, column))
191: state.add(AccessibleState.SELECTED);
192:
193:
194: if (row == getSelectedRow() && column == getSelectedColumn())
195: state.add(AccessibleState.ACTIVE);
196:
197:
198: state.add(AccessibleState.TRANSIENT);
199:
200:
201: return state;
202: }
203:
204:
209: public int getAccessibleIndexInParent()
210: {
211: return index;
212: }
213:
214:
220: public int getAccessibleChildrenCount()
221: {
222: return 0;
223: }
224:
225:
231: public Accessible getAccessibleChild(int i)
232: {
233: return null;
234: }
235:
236:
241: public Locale getLocale()
242: {
243:
244:
245: return Locale.ENGLISH;
246: }
247:
248:
255: public AccessibleContext getAccessibleContext()
256: {
257: return this;
258: }
259:
260:
265: public Color getBackground()
266: {
267: return table.getBackground();
268: }
269:
270:
277: public void setBackground(Color color)
278: {
279:
280: }
281:
282:
287: public Color getForeground()
288: {
289: return table.getForeground();
290: }
291:
292:
299: public void setForeground(Color color)
300: {
301:
302: }
303:
304:
309: public Cursor getCursor()
310: {
311: return table.getCursor();
312: }
313:
314:
321: public void setCursor(Cursor cursor)
322: {
323:
324: }
325:
326:
331: public Font getFont()
332: {
333: return table.getFont();
334: }
335:
336:
343: public void setFont(Font font)
344: {
345:
346: }
347:
348:
355: public FontMetrics getFontMetrics(Font font)
356: {
357: return table.getFontMetrics(font);
358: }
359:
360:
367: public boolean isEnabled()
368: {
369: return table.isEnabled();
370: }
371:
372:
378: public void setEnabled(boolean b)
379: {
380:
381: }
382:
383:
390: public boolean isVisible()
391: {
392: return table.isVisible();
393: }
394:
395:
401: public void setVisible(boolean b)
402: {
403:
404: }
405:
406:
413: public boolean isShowing()
414: {
415: return table.isShowing();
416: }
417:
418:
427: public boolean contains(Point point)
428: {
429: Rectangle cellRect = table.getCellRect(row, column, true);
430: cellRect.x = 0;
431: cellRect.y = 0;
432: return cellRect.contains(point);
433: }
434:
435:
440: public Point getLocationOnScreen()
441: {
442: Point tableLoc = table.getLocationOnScreen();
443: Rectangle cellRect = table.getCellRect(row, column, true);
444: tableLoc.x += cellRect.x;
445: tableLoc.y += cellRect.y;
446: return tableLoc;
447: }
448:
449:
454: public Point getLocation()
455: {
456: Rectangle cellRect = table.getCellRect(row, column, true);
457: return new Point(cellRect.x, cellRect.y);
458: }
459:
460:
466: public void setLocation(Point point)
467: {
468:
469: }
470:
471:
476: public Rectangle getBounds()
477: {
478: return table.getCellRect(row, column, true);
479: }
480:
481:
487: public void setBounds(Rectangle rectangle)
488: {
489:
490: }
491:
492:
497: public Dimension getSize()
498: {
499: Rectangle cellRect = table.getCellRect(row, column, true);
500: return new Dimension(cellRect.width, cellRect.height);
501: }
502:
503:
509: public void setSize(Dimension dimension)
510: {
511:
512: }
513:
514:
519: public Accessible getAccessibleAt(Point point)
520: {
521: return null;
522: }
523:
524:
531: public boolean isFocusTraversable()
532: {
533: return table.isFocusable();
534: }
535:
536:
539: public void requestFocus()
540: {
541:
542: table.getColumnModel().getSelectionModel()
543: .setLeadSelectionIndex(column);
544: table.getSelectionModel().setLeadSelectionIndex(row);
545:
546: table.requestFocus();
547: }
548:
549:
556: public void addFocusListener(FocusListener listener)
557: {
558: table.addFocusListener(listener);
559: }
560:
561:
567: public void removeFocusListener(FocusListener listener)
568: {
569: table.removeFocusListener(listener);
570: }
571:
572: }
573:
574: protected class AccessibleJTableModelChange
575: implements AccessibleTableModelChange
576: {
577: protected int type;
578: protected int firstRow;
579: protected int lastRow;
580: protected int firstColumn;
581: protected int lastColumn;
582:
583: protected AccessibleJTableModelChange(int type, int firstRow,
584: int lastRow, int firstColumn,
585: int lastColumn)
586: {
587: this.type = type;
588: this.firstRow = firstRow;
589: this.lastRow = lastRow;
590: this.firstColumn = firstColumn;
591: this.lastColumn = lastColumn;
592: }
593:
594: public int getType()
595: {
596: return type;
597: }
598:
599: public int getFirstRow()
600: {
601: return firstRow;
602: }
603:
604: public int getLastRow()
605: {
606: return lastRow;
607: }
608:
609: public int getFirstColumn()
610: {
611: return firstColumn;
612: }
613:
614: public int getLastColumn()
615: {
616: return lastColumn;
617: }
618: }
619:
620:
625: private class AccessibleTableHeader
626: implements AccessibleTable
627: {
628:
629:
632: private JTableHeader header;
633:
634:
639: private AccessibleTableHeader(JTableHeader h)
640: {
641: header = h;
642: }
643:
644:
649: public Accessible getAccessibleCaption()
650: {
651:
652: return null;
653: }
654:
655:
660: public void setAccessibleCaption(Accessible caption)
661: {
662:
663: }
664:
665:
670: public Accessible getAccessibleSummary()
671: {
672:
673: return null;
674: }
675:
676:
681: public void setAccessibleSummary(Accessible summary)
682: {
683:
684: }
685:
686:
691: public int getAccessibleRowCount()
692: {
693: return 1;
694: }
695:
696:
701: public int getAccessibleColumnCount()
702: {
703: return header.getColumnModel().getColumnCount();
704: }
705:
706:
717: public Accessible getAccessibleAt(int r, int c)
718: {
719: TableColumn column = header.getColumnModel().getColumn(c);
720: TableCellRenderer rend = column.getHeaderRenderer();
721: if (rend == null)
722: rend = header.getDefaultRenderer();
723: Component comp =
724: rend.getTableCellRendererComponent(header.getTable(),
725: column.getHeaderValue(), false,
726: false, -1, c);
727: return new AccessibleJTableHeaderCell(header, comp, r, c);
728: }
729:
730: public int getAccessibleRowExtentAt(int r, int c)
731: {
732:
733: return 0;
734: }
735:
736: public int getAccessibleColumnExtentAt(int r, int c)
737: {
738:
739: return 0;
740: }
741:
742: public AccessibleTable getAccessibleRowHeader()
743: {
744:
745: return null;
746: }
747:
748: public void setAccessibleRowHeader(AccessibleTable header)
749: {
750:
751:
752: }
753:
754: public AccessibleTable getAccessibleColumnHeader()
755: {
756:
757: return null;
758: }
759:
760: public void setAccessibleColumnHeader(AccessibleTable header)
761: {
762:
763:
764: }
765:
766: public Accessible getAccessibleRowDescription(int r)
767: {
768:
769: return null;
770: }
771:
772: public void setAccessibleRowDescription(int r, Accessible description)
773: {
774:
775:
776: }
777:
778: public Accessible getAccessibleColumnDescription(int c)
779: {
780:
781: return null;
782: }
783:
784: public void setAccessibleColumnDescription(int c, Accessible description)
785: {
786:
787:
788: }
789:
790: public boolean isAccessibleSelected(int r, int c)
791: {
792:
793: return false;
794: }
795:
796: public boolean isAccessibleRowSelected(int r)
797: {
798:
799: return false;
800: }
801:
802: public boolean isAccessibleColumnSelected(int c)
803: {
804:
805: return false;
806: }
807:
808: public int[] getSelectedAccessibleRows()
809: {
810:
811: return null;
812: }
813:
814: public int[] getSelectedAccessibleColumns()
815: {
816:
817: return null;
818: }
819:
820: }
821:
822:
827: private class AccessibleJTableHeaderCell
828: extends AccessibleContext
829: implements Accessible, AccessibleComponent
830: {
831:
832: JTableHeader header;
833:
834: int columnIndex;
835:
836:
843: private AccessibleJTableHeaderCell(JTableHeader h, Component comp, int r,
844: int c)
845: {
846: header = h;
847: columnIndex = c;
848: }
849:
850:
855: Component getColumnHeaderRenderer()
856: {
857: TableColumn tc = header.getColumnModel().getColumn(columnIndex);
858: TableCellRenderer r = tc.getHeaderRenderer();
859: if (r == null)
860: r = header.getDefaultRenderer();
861: return r.getTableCellRendererComponent(header.getTable(),
862: tc.getHeaderValue(), false, false, -1, columnIndex);
863: }
864:
865:
870: public AccessibleRole getAccessibleRole()
871: {
872: Component renderer = getColumnHeaderRenderer();
873: if (renderer instanceof Accessible)
874: {
875: Accessible ac = (Accessible) renderer;
876: return ac.getAccessibleContext().getAccessibleRole();
877: }
878: return null;
879: }
880:
881: public AccessibleStateSet getAccessibleStateSet()
882: {
883:
884: return null;
885: }
886:
887: public int getAccessibleIndexInParent()
888: {
889:
890: return 0;
891: }
892:
893: public int getAccessibleChildrenCount()
894: {
895:
896: return 0;
897: }
898:
899: public Accessible getAccessibleChild(int i)
900: {
901:
902: return null;
903: }
904:
905: public Locale getLocale()
906: {
907:
908: return null;
909: }
910:
911:
916: public AccessibleContext getAccessibleContext()
917: {
918: return this;
919: }
920:
921: public Color getBackground()
922: {
923:
924: return null;
925: }
926:
927: public void setBackground(Color color)
928: {
929:
930:
931: }
932:
933: public Color getForeground()
934: {
935:
936: return null;
937: }
938:
939: public void setForeground(Color color)
940: {
941:
942:
943: }
944:
945: public Cursor getCursor()
946: {
947:
948: return null;
949: }
950:
951: public void setCursor(Cursor cursor)
952: {
953:
954:
955: }
956:
957: public Font getFont()
958: {
959:
960: return null;
961: }
962:
963: public void setFont(Font font)
964: {
965:
966:
967: }
968:
969: public FontMetrics getFontMetrics(Font font)
970: {
971:
972: return null;
973: }
974:
975: public boolean isEnabled()
976: {
977:
978: return false;
979: }
980:
981: public void setEnabled(boolean b)
982: {
983:
984:
985: }
986:
987: public boolean isVisible()
988: {
989:
990: return false;
991: }
992:
993: public void setVisible(boolean b)
994: {
995:
996:
997: }
998:
999: public boolean isShowing()
1000: {
1001:
1002: return false;
1003: }
1004:
1005: public boolean contains(Point point)
1006: {
1007:
1008: return false;
1009: }
1010:
1011: public Point getLocationOnScreen()
1012: {
1013:
1014: return null;
1015: }
1016:
1017: public Point getLocation()
1018: {
1019:
1020: return null;
1021: }
1022:
1023: public void setLocation(Point point)
1024: {
1025:
1026:
1027: }
1028:
1029: public Rectangle getBounds()
1030: {
1031:
1032: return null;
1033: }
1034:
1035: public void setBounds(Rectangle rectangle)
1036: {
1037:
1038:
1039: }
1040:
1041: public Dimension getSize()
1042: {
1043:
1044: return null;
1045: }
1046:
1047: public void setSize(Dimension dimension)
1048: {
1049:
1050:
1051: }
1052:
1053: public Accessible getAccessibleAt(Point point)
1054: {
1055:
1056: return null;
1057: }
1058:
1059: public boolean isFocusTraversable()
1060: {
1061:
1062: return false;
1063: }
1064:
1065: public void requestFocus()
1066: {
1067:
1068:
1069: }
1070:
1071: public void addFocusListener(FocusListener listener)
1072: {
1073:
1074:
1075: }
1076:
1077: public void removeFocusListener(FocusListener listener)
1078: {
1079:
1080:
1081: }
1082:
1083: }
1084:
1085:
1089: private int lastSelectedRow;
1090:
1091:
1095: private int lastSelectedColumn;
1096:
1097:
1100: private Accessible caption;
1101:
1102:
1105: private Accessible summary;
1106:
1107:
1110: private Accessible[] rowDescriptions;
1111:
1112:
1115: private Accessible[] columnDescriptions;
1116:
1117:
1122: protected AccessibleJTable()
1123: {
1124: getModel().addTableModelListener(this);
1125: getSelectionModel().addListSelectionListener(this);
1126: getColumnModel().addColumnModelListener(this);
1127: lastSelectedRow = getSelectedRow();
1128: lastSelectedColumn = getSelectedColumn();
1129: TableCellEditor editor = getCellEditor();
1130: if (editor != null)
1131: editor.addCellEditorListener(this);
1132: }
1133:
1134:
1139: public AccessibleRole getAccessibleRole()
1140: {
1141: return AccessibleRole.TABLE;
1142: }
1143:
1144:
1149: public AccessibleTable getAccessibleTable()
1150: {
1151: return this;
1152: }
1153:
1154:
1157: public int getAccessibleSelectionCount()
1158: {
1159: return getSelectedColumnCount();
1160: }
1161:
1162:
1172: public Accessible getAccessibleSelection(int i)
1173: {
1174: Accessible found = null;
1175:
1176: int[] selectedRows = getSelectedRows();
1177: int[] selectedColumns = getSelectedColumns();
1178: int numCols = getColumnCount();
1179: int numRows = getRowCount();
1180:
1181:
1182:
1183:
1184: if (getRowSelectionAllowed() && getColumnSelectionAllowed())
1185: {
1186: int current = -1;
1187: int newIndex = current;
1188: int lastSelectedRow = -1;
1189:
1190:
1191: for (int j = 0; i < selectedRows.length; i++)
1192: {
1193:
1194:
1195: int selectedRow = selectedRows[j];
1196: int r = -1;
1197: int ci = -1;
1198: for (r = lastSelectedRow + 1;
1199: r < selectedRow && current < i; r++)
1200: {
1201: for (ci = 0; ci < selectedColumns.length && current < i;
1202: ci++)
1203: {
1204: current++;
1205: }
1206: }
1207: if (current == i)
1208: {
1209:
1210: found = getAccessibleChild(r * numCols
1211: + selectedColumns[ci]);
1212: break;
1213: }
1214:
1215:
1216: if (current < i && current + numCols >= i)
1217: {
1218:
1219: found = getAccessibleChild(r * numCols + (i - current));
1220: break;
1221: }
1222: current += numCols;
1223: }
1224: if (found == null)
1225: {
1226:
1227: int r = 0;
1228: int ci = 0;
1229: for (r = lastSelectedRow + 1;
1230: r < numRows && current < i; r++)
1231: {
1232: for (ci = 0; ci < selectedColumns.length && current < i;
1233: ci++)
1234: {
1235: current++;
1236: }
1237: }
1238: if (current == i)
1239: {
1240:
1241: found = getAccessibleChild(r * numCols
1242: + selectedColumns[ci]);
1243: }
1244: }
1245: }
1246:
1247: else if (getRowSelectionAllowed())
1248: {
1249: int c = i % numCols;
1250: int r = selectedRows[i / numCols];
1251: found = getAccessibleChild(r * numCols + c);
1252: }
1253:
1254: else if (getRowSelectionAllowed())
1255: {
1256: int numSelectedColumns = selectedColumns.length;
1257: int c = selectedColumns[i % numSelectedColumns];
1258: int r = i / numSelectedColumns;
1259: found = getAccessibleChild(r * numCols + c);
1260: }
1261:
1262: return found;
1263: }
1264:
1265:
1274: public boolean isAccessibleChildSelected(int i)
1275: {
1276: int r = getAccessibleRowAtIndex(i);
1277: int c = getAccessibleColumnAtIndex(i);
1278: return isCellSelected(r, c);
1279: }
1280:
1281:
1287: public void addAccessibleSelection(int i)
1288: {
1289: int r = getAccessibleRowAtIndex(i);
1290: int c = getAccessibleColumnAtIndex(i);
1291: changeSelection(r, c, true, false);
1292: }
1293:
1294:
1302: public void removeAccessibleSelection(int i)
1303: {
1304: if (! getRowSelectionAllowed() && ! getColumnSelectionAllowed())
1305: {
1306: int r = getAccessibleRowAtIndex(i);
1307: int c = getAccessibleColumnAtIndex(i);
1308: removeRowSelectionInterval(r, r);
1309: removeColumnSelectionInterval(c, c);
1310: }
1311: }
1312:
1313:
1316: public void clearAccessibleSelection()
1317: {
1318: clearSelection();
1319: }
1320:
1321:
1326: public void selectAllAccessibleSelection()
1327: {
1328: selectAll();
1329: }
1330:
1331:
1337: public void valueChanged(ListSelectionEvent event)
1338: {
1339: firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
1340: Boolean.FALSE, Boolean.TRUE);
1341: int r = getSelectedRow();
1342: int c = getSelectedColumn();
1343: if (r != lastSelectedRow || c != lastSelectedColumn)
1344: {
1345: Accessible o = getAccessibleAt(lastSelectedRow,
1346: lastSelectedColumn);
1347: Accessible n = getAccessibleAt(r, c);
1348: firePropertyChange(AccessibleContext
1349: .ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, o, n);
1350: lastSelectedRow = r;
1351: lastSelectedColumn = c;
1352: }
1353: }
1354:
1355:
1362: public void tableChanged(TableModelEvent event)
1363: {
1364: switch (event.getType())
1365: {
1366: case TableModelEvent.INSERT:
1367: tableRowsInserted(event);
1368: break;
1369: case TableModelEvent.DELETE:
1370: tableRowsDeleted(event);
1371: break;
1372: }
1373: }
1374:
1375:
1381: public void tableRowsInserted(TableModelEvent event)
1382: {
1383: handleRowChange(event);
1384: }
1385:
1386:
1392: public void tableRowsDeleted(TableModelEvent event)
1393: {
1394: handleRowChange(event);
1395: }
1396:
1397:
1402: private void handleRowChange(TableModelEvent event)
1403: {
1404: firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1405: null, null);
1406: int firstColumn = event.getColumn();
1407: int lastColumn = event.getColumn();
1408: if (firstColumn == TableModelEvent.ALL_COLUMNS)
1409: {
1410: firstColumn = 0;
1411: lastColumn = getColumnCount() - 1;
1412: }
1413: AccessibleJTableModelChange change = new AccessibleJTableModelChange
1414: (event.getType(), event.getFirstRow(), event.getLastRow(),
1415: firstColumn, lastColumn);
1416: firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
1417: null, change);
1418: }
1419:
1420: public void columnAdded(TableColumnModelEvent event)
1421: {
1422: firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1423: null, null);
1424: handleColumnChange(AccessibleTableModelChange.INSERT,
1425: event.getFromIndex(), event.getToIndex());
1426: }
1427:
1428: public void columnRemoved(TableColumnModelEvent event)
1429: {
1430: firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1431: null, null);
1432: handleColumnChange(AccessibleTableModelChange.DELETE,
1433: event.getFromIndex(), event.getToIndex());
1434: }
1435:
1436: public void columnMoved(TableColumnModelEvent event)
1437: {
1438: firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1439: null, null);
1440: handleColumnChange(AccessibleTableModelChange.DELETE,
1441: event.getFromIndex(), event.getFromIndex());
1442: handleColumnChange(AccessibleTableModelChange.INSERT,
1443: event.getFromIndex(), event.getToIndex());
1444: }
1445:
1446:
1453: private void handleColumnChange(int type, int from, int to)
1454: {
1455: AccessibleJTableModelChange change =
1456: new AccessibleJTableModelChange(type, 0, 0, from, to);
1457: firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED,
1458: null, change);
1459: }
1460:
1461: public void columnMarginChanged(ChangeEvent event)
1462: {
1463: firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1464: null, null);
1465: }
1466:
1467: public void columnSelectionChanged(ListSelectionEvent event)
1468: {
1469:
1470: }
1471:
1472: public void editingCanceled(ChangeEvent event)
1473: {
1474:
1475: }
1476:
1477: public void editingStopped(ChangeEvent event)
1478: {
1479: firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
1480: null, null);
1481: }
1482:
1483:
1490: public void propertyChange(PropertyChangeEvent e)
1491: {
1492: String propName = e.getPropertyName();
1493: if (propName.equals("tableModel"))
1494: {
1495: TableModel oldModel = (TableModel) e.getOldValue();
1496: oldModel.removeTableModelListener(this);
1497: TableModel newModel = (TableModel) e.getNewValue();
1498: newModel.addTableModelListener(this);
1499: }
1500: else if (propName.equals("columnModel"))
1501: {
1502: TableColumnModel oldModel = (TableColumnModel) e.getOldValue();
1503: oldModel.removeColumnModelListener(this);
1504: TableColumnModel newModel = (TableColumnModel) e.getNewValue();
1505: newModel.addColumnModelListener(this);
1506: }
1507: else if (propName.equals("selectionModel"))
1508: {
1509: ListSelectionModel oldModel = (ListSelectionModel) e.getOldValue();
1510: oldModel.removeListSelectionListener(this);
1511: ListSelectionModel newModel = (ListSelectionModel) e.getNewValue();
1512: newModel.addListSelectionListener(this);
1513: }
1514: else if (propName.equals("cellEditor"))
1515: {
1516: CellEditor oldEd = (CellEditor) e.getOldValue();
1517: oldEd.removeCellEditorListener(this);
1518: CellEditor newEd = (CellEditor) e.getNewValue();
1519: newEd.addCellEditorListener(this);
1520: }
1521: }
1522:
1523:
1532: public int getAccessibleRow(int index)
1533: {
1534: return getAccessibleRowAtIndex(index);
1535: }
1536:
1537:
1546: public int getAccessibleColumn(int index)
1547: {
1548: return getAccessibleColumnAtIndex(index);
1549: }
1550:
1551:
1561: public int getAccessibleIndex(int r, int c)
1562: {
1563: return getAccessibleIndexAt(r, c);
1564: }
1565:
1566:
1573: public Accessible getAccessibleCaption()
1574: {
1575: return caption;
1576: }
1577:
1578:
1583: public void setAccessibleCaption(Accessible c)
1584: {
1585: caption = c;
1586: }
1587:
1588:
1593: public Accessible getAccessibleSummary()
1594: {
1595: return summary;
1596: }
1597:
1598:
1603: public void setAccessibleSummary(Accessible s)
1604: {
1605: summary = s;
1606: }
1607:
1608:
1613: public int getAccessibleRowCount()
1614: {
1615: return getRowCount();
1616: }
1617:
1618:
1623: public int getAccessibleColumnCount()
1624: {
1625: return getColumnCount();
1626: }
1627:
1628:
1635: public Accessible getAccessibleChild(int index)
1636: {
1637: int r = getAccessibleRow(index);
1638: int c = getAccessibleColumn(index);
1639: return getAccessibleAt(r, c);
1640: }
1641:
1642:
1652: public Accessible getAccessibleAt(int r, int c)
1653: {
1654: TableCellRenderer cellRenderer = getCellRenderer(r, c);
1655: Component renderer = cellRenderer.getTableCellRendererComponent(
1656: JTable.this, getValueAt(r, c), isCellSelected(r, c), false, r, c);
1657: if (renderer instanceof Accessible)
1658: return (Accessible) renderer;
1659: return null;
1660: }
1661:
1662:
1672: public int getAccessibleRowExtentAt(int r, int c)
1673: {
1674: return 1;
1675: }
1676:
1677:
1687: public int getAccessibleColumnExtentAt(int r, int c)
1688: {
1689: return 1;
1690: }
1691:
1692:
1697: public AccessibleTable getAccessibleRowHeader()
1698: {
1699:
1700: return null;
1701: }
1702:
1703:
1708: public void setAccessibleRowHeader(AccessibleTable header)
1709: {
1710:
1711: }
1712:
1713:
1719: public AccessibleTable getAccessibleColumnHeader()
1720: {
1721: JTableHeader h = getTableHeader();
1722: AccessibleTable header = null;
1723: if (h != null)
1724: header = new AccessibleTableHeader(h);
1725: return header;
1726: }
1727:
1728:
1734: public void setAccessibleColumnHeader(AccessibleTable header)
1735: {
1736:
1737: }
1738:
1739:
1748: public Accessible getAccessibleRowDescription(int r)
1749: {
1750: Accessible descr = null;
1751: if (rowDescriptions != null)
1752: descr = rowDescriptions[r];
1753: return descr;
1754: }
1755:
1756:
1762: public void setAccessibleRowDescription(int r, Accessible description)
1763: {
1764: if (rowDescriptions == null)
1765: rowDescriptions = new Accessible[getAccessibleRowCount()];
1766: rowDescriptions[r] = description;
1767: }
1768:
1769:
1778: public Accessible getAccessibleColumnDescription(int c)
1779: {
1780: Accessible descr = null;
1781: if (columnDescriptions != null)
1782: descr = columnDescriptions[c];
1783: return descr;
1784: }
1785:
1786:
1792: public void setAccessibleColumnDescription(int c, Accessible description)
1793: {
1794: if (columnDescriptions == null)
1795: columnDescriptions = new Accessible[getAccessibleRowCount()];
1796: columnDescriptions[c] = description;
1797: }
1798:
1799:
1809: public boolean isAccessibleSelected(int r, int c)
1810: {
1811: return isCellSelected(r, c);
1812: }
1813:
1814:
1823: public boolean isAccessibleRowSelected(int r)
1824: {
1825: return isRowSelected(r);
1826: }
1827:
1828:
1837: public boolean isAccessibleColumnSelected(int c)
1838: {
1839: return isColumnSelected(c);
1840: }
1841:
1842:
1847: public int[] getSelectedAccessibleRows()
1848: {
1849: return getSelectedRows();
1850: }
1851:
1852:
1857: public int[] getSelectedAccessibleColumns()
1858: {
1859: return getSelectedColumns();
1860: }
1861:
1862:
1869: public int getAccessibleRowAtIndex(int index)
1870: {
1871:
1872: return index / getColumnCount();
1873: }
1874:
1875:
1882: public int getAccessibleColumnAtIndex(int index)
1883: {
1884:
1885: return index % getColumnCount();
1886: }
1887:
1888:
1897: public int getAccessibleIndexAt(int row, int column)
1898: {
1899:
1900: return row * getColumnCount() + column;
1901: }
1902: }
1903:
1910: class TableColumnPropertyChangeHandler implements PropertyChangeListener
1911: {
1912:
1918: public void propertyChange(PropertyChangeEvent ev)
1919: {
1920: if (ev.getPropertyName().equals("preferredWidth"))
1921: {
1922: JTableHeader header = getTableHeader();
1923: if (header != null)
1924:
1925: if (header.getResizingColumn() == null)
1926: {
1927: TableColumn col = (TableColumn) ev.getSource();
1928: header.setResizingColumn(col);
1929: doLayout();
1930: header.setResizingColumn(null);
1931: }
1932: }
1933: }
1934: }
1935:
1936:
1939: private class BooleanCellRenderer
1940: extends DefaultTableCellRenderer
1941: {
1942:
1945: private final JCheckBox checkBox;
1946:
1947:
1951: BooleanCellRenderer()
1952: {
1953: checkBox = new JCheckBox();
1954: checkBox.setHorizontalAlignment(SwingConstants.CENTER);
1955: }
1956:
1957:
1960: JCheckBox getCheckBox()
1961: {
1962: return checkBox;
1963: }
1964:
1965:
1976: public Component getTableCellRendererComponent(JTable table, Object value,
1977: boolean isSelected,
1978: boolean hasFocus, int row,
1979: int column)
1980: {
1981: if (isSelected)
1982: {
1983: checkBox.setBackground(table.getSelectionBackground());
1984: checkBox.setForeground(table.getSelectionForeground());
1985: }
1986: else
1987: {
1988: checkBox.setBackground(table.getBackground());
1989: checkBox.setForeground(table.getForeground());
1990: }
1991:
1992: if (hasFocus)
1993: {
1994: checkBox.setBorder(
1995: UIManager.getBorder("Table.focusCellHighlightBorder"));
1996: if (table.isCellEditable(row, column))
1997: {
1998: checkBox.setBackground(
1999: UIManager.getColor("Table.focusCellBackground"));
2000: checkBox.setForeground(
2001: UIManager.getColor("Table.focusCellForeground"));
2002: }
2003: }
2004: else
2005: checkBox.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
2006:
2007:
2008: if (value == null)
2009: checkBox.setSelected(false);
2010: else
2011: {
2012: Boolean boolValue = (Boolean) value;
2013: checkBox.setSelected(boolValue.booleanValue());
2014: }
2015: return checkBox;
2016: }
2017: }
2018:
2019:
2022: private class DateCellRenderer
2023: extends DefaultTableCellRenderer
2024: {
2025:
2037: public Component getTableCellRendererComponent(JTable table, Object value,
2038: boolean isSelected,
2039: boolean hasFocus, int row,
2040: int column)
2041: {
2042: super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
2043: row, column);
2044: if (value instanceof Date)
2045: {
2046: Date dateValue = (Date) value;
2047: DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
2048: setText(df.format(dateValue));
2049: }
2050: return this;
2051: }
2052: }
2053:
2054:
2057: private class DoubleCellRenderer
2058: extends DefaultTableCellRenderer
2059: {
2060:
2063: public DoubleCellRenderer()
2064: {
2065: setHorizontalAlignment(JLabel.RIGHT);
2066: }
2067:
2068:
2080: public Component getTableCellRendererComponent(JTable table, Object value,
2081: boolean isSelected,
2082: boolean hasFocus, int row,
2083: int column)
2084: {
2085: super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
2086: row, column);
2087: if (value instanceof Double)
2088: {
2089: Double doubleValue = (Double) value;
2090: NumberFormat nf = NumberFormat.getInstance();
2091: setText(nf.format(doubleValue.doubleValue()));
2092: }
2093: return this;
2094: }
2095: }
2096:
2097:
2100: private class FloatCellRenderer
2101: extends DefaultTableCellRenderer
2102: {
2103:
2106: public FloatCellRenderer()
2107: {
2108: setHorizontalAlignment(JLabel.RIGHT);
2109: }
2110:
2111:
2123: public Component getTableCellRendererComponent(JTable table, Object value,
2124: boolean isSelected,
2125: boolean hasFocus, int row,
2126: int column)
2127: {
2128: super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
2129: row, column);
2130: if (value instanceof Float)
2131: {
2132: Float floatValue = (Float) value;
2133: NumberFormat nf = NumberFormat.getInstance();
2134: setText(nf.format(floatValue.floatValue()));
2135: }
2136: return this;
2137: }
2138: }
2139:
2140:
2143: private class NumberCellRenderer
2144: extends DefaultTableCellRenderer
2145: {
2146:
2149: public NumberCellRenderer()
2150: {
2151: setHorizontalAlignment(JLabel.RIGHT);
2152: }
2153: }
2154:
2155:
2158: private class IconCellRenderer
2159: extends DefaultTableCellRenderer
2160: {
2161: IconCellRenderer()
2162: {
2163: setHorizontalAlignment(SwingConstants.CENTER);
2164: }
2165:
2166:
2167:
2179: public Component getTableCellRendererComponent(JTable table, Object value,
2180: boolean isSelected,
2181: boolean hasFocus, int row,
2182: int column)
2183: {
2184: super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
2185: row, column);
2186: if (value instanceof Icon)
2187: {
2188: Icon iconValue = (Icon) value;
2189: setIcon(iconValue);
2190: }
2191: else
2192: {
2193: setIcon(null);
2194: }
2195: setText("");
2196: return this;
2197: }
2198: }
2199:
2200:
2207: private class TableTextField extends JTextField
2208: {
2209:
2212: TableTextField()
2213: {
2214: setBorder(BorderFactory.createLineBorder(getGridColor(), 2));
2215: }
2216: }
2217:
2218:
2219: private static final long serialVersionUID = 3876025080382781659L;
2220:
2221:
2224: final JTable this_table = this;
2225:
2226:
2227:
2232: public static final int AUTO_RESIZE_OFF = 0;
2233:
2234:
2239: public static final int AUTO_RESIZE_NEXT_COLUMN = 1;
2240:
2241:
2246: public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS = 2;
2247:
2248:
2254: public static final int AUTO_RESIZE_ALL_COLUMNS = 4;
2255:
2256:
2261: public static final int AUTO_RESIZE_LAST_COLUMN = 3;
2262:
2263:
2268: protected Hashtable defaultEditorsByColumnClass = new Hashtable();
2269:
2270:
2275: protected Hashtable defaultRenderersByColumnClass = new Hashtable();
2276:
2277:
2280: protected int editingColumn;
2281:
2282:
2285: protected int editingRow;
2286:
2287:
2292: protected transient Component editorComp;
2293:
2294:
2295:
2306: protected boolean autoCreateColumnsFromModel;
2307:
2308:
2318: protected int autoResizeMode;
2319:
2320:
2331: protected int rowHeight;
2332:
2333:
2343: protected int rowMargin;
2344:
2345:
2358: protected boolean rowSelectionAllowed;
2359:
2360:
2365: protected boolean cellSelectionEnabled;
2366:
2367:
2376: protected TableModel dataModel;
2377:
2378:
2400: protected TableColumnModel columnModel;
2401:
2402:
2414: protected ListSelectionModel selectionModel;
2415:
2416:
2419: protected TableCellEditor cellEditor;
2420:
2421:
2427: private boolean dragEnabled;
2428:
2429:
2436: protected Color gridColor;
2437:
2438:
2445: protected Dimension preferredViewportSize;
2446:
2447:
2455: protected Color selectionBackground;
2456:
2457:
2461: private static final String SELECTION_BACKGROUND_CHANGED_PROPERTY = "selectionBackground";
2462:
2463:
2471: protected Color selectionForeground;
2472:
2473:
2477: private static final String SELECTION_FOREGROUND_CHANGED_PROPERTY = "selectionForeground";
2478:
2479:
2482: protected boolean showHorizontalLines;
2483:
2484:
2487: protected boolean showVerticalLines;
2488:
2489:
2492: protected JTableHeader tableHeader;
2493:
2494:
2497: TableColumnPropertyChangeHandler tableColumnPropertyChangeHandler =
2498: new TableColumnPropertyChangeHandler();
2499:
2500:
2504: private boolean surrendersFocusOnKeystroke = false;
2505:
2506:
2509: private Rectangle rectCache = new Rectangle();
2510:
2511:
2518: private boolean clientRowHeightSet = false;
2519:
2520:
2533: private SizeSequence rowHeights;
2534:
2535:
2539: private transient TableCellEditor booleanInvertingEditor;
2540:
2541:
2544: public JTable ()
2545: {
2546: this(null, null, null);
2547: }
2548:
2549:
2556: public JTable (int numRows, int numColumns)
2557: {
2558: this(new DefaultTableModel(numRows, numColumns));
2559: }
2560:
2561:
2569: public JTable(Object[][] data, Object[] columnNames)
2570: {
2571: this(new DefaultTableModel(data, columnNames));
2572: }
2573:
2574:
2583: public JTable (TableModel model)
2584: {
2585: this(model, null, null);
2586: }
2587:
2588:
2601: public JTable (TableModel dm, TableColumnModel cm)
2602: {
2603: this(dm, cm, null);
2604: }
2605:
2606:
2615: public JTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm)
2616: {
2617: boolean autoCreate = false;
2618: TableColumnModel columnModel;
2619: if (cm != null)
2620: columnModel = cm;
2621: else
2622: {
2623: columnModel = createDefaultColumnModel();
2624: autoCreate = true;
2625: }
2626:
2627:
2628:
2629:
2630: rowMargin = 1;
2631: columnModel.setColumnMargin(1);
2632: setColumnModel(columnModel);
2633:
2634: setSelectionModel(sm == null ? createDefaultSelectionModel() : sm);
2635: setModel(dm == null ? createDefaultDataModel() : dm);
2636: setAutoCreateColumnsFromModel(autoCreate);
2637: initializeLocalVars();
2638:
2639:
2640:
2641:
2642:
2643:
2644:
2645:
2646:
2647:
2648:
2649:
2650:
2651:
2652: updateUI();
2653: }
2654:
2655:
2662: public JTable(Vector data, Vector columnNames)
2663: {
2664: this(new DefaultTableModel(data, columnNames));
2665: }
2666:
2667:
2670: protected void initializeLocalVars()
2671: {
2672: setTableHeader(createDefaultTableHeader());
2673: if (autoCreateColumnsFromModel)
2674: createDefaultColumnsFromModel();
2675: this.columnModel.addColumnModelListener(this);
2676:
2677: this.autoResizeMode = AUTO_RESIZE_SUBSEQUENT_COLUMNS;
2678: setRowHeight(16);
2679: this.rowMargin = 1;
2680: this.rowSelectionAllowed = true;
2681:
2682:
2683: this.cellEditor = null;
2684:
2685:
2686: this.dragEnabled = false;
2687: this.preferredViewportSize = new Dimension(450,400);
2688: this.showHorizontalLines = true;
2689: this.showVerticalLines = true;
2690: this.editingColumn = -1;
2691: this.editingRow = -1;
2692: }
2693:
2694:
2705: public void addColumn(TableColumn column)
2706: {
2707: if (column.getHeaderValue() == null)
2708: {
2709: String name = dataModel.getColumnName(column.getModelIndex());
2710: column.setHeaderValue(name);
2711: }
2712:
2713: columnModel.addColumn(column);
2714: column.addPropertyChangeListener(tableColumnPropertyChangeHandler);
2715: }
2716:
2717:
2723: protected void createDefaultEditors()
2724: {
2725: JCheckBox box = new BooleanCellRenderer().getCheckBox();
2726: box.setBorder(BorderFactory.createLineBorder(getGridColor(), 2));
2727: box.setBorderPainted(true);
2728: booleanInvertingEditor = new DefaultCellEditor(box);
2729: setDefaultEditor(Boolean.class, booleanInvertingEditor);
2730: }
2731:
2732:
2737: protected void createDefaultRenderers()
2738: {
2739: setDefaultRenderer(Boolean.class, new BooleanCellRenderer());
2740: setDefaultRenderer(Number.class, new NumberCellRenderer());
2741: setDefaultRenderer(Double.class, new DoubleCellRenderer());
2742: setDefaultRenderer(Double.class, new FloatCellRenderer());
2743: setDefaultRenderer(Date.class, new DateCellRenderer());
2744: setDefaultRenderer(Icon.class, new IconCellRenderer());
2745: setDefaultRenderer(ImageIcon.class, new IconCellRenderer());
2746: }
2747:
2748:
2751: public static JScrollPane createScrollPaneForTable(JTable table)
2752: {
2753: return new JScrollPane(table);
2754: }
2755:
2756:
2763: protected TableColumnModel createDefaultColumnModel()
2764: {
2765: return new DefaultTableColumnModel();
2766: }
2767:
2768:
2775: protected TableModel createDefaultDataModel()
2776: {
2777: return new DefaultTableModel();
2778: }
2779:
2780:
2787: protected ListSelectionModel createDefaultSelectionModel()
2788: {
2789: return new DefaultListSelectionModel();
2790: }
2791:
2792:
2798: protected JTableHeader createDefaultTableHeader()
2799: {
2800: return new JTableHeader(columnModel);
2801: }
2802:
2803:
2806: public void columnAdded (TableColumnModelEvent event)
2807: {
2808: revalidate();
2809: repaint();
2810: }
2811:
2812:
2816: public void columnMarginChanged (ChangeEvent event)
2817: {
2818: revalidate();
2819: repaint();
2820: }
2821:
2822:
2825: public void columnMoved (TableColumnModelEvent event)
2826: {
2827: if (isEditing())
2828: editingCanceled(null);
2829: revalidate();
2830: repaint();
2831: }
2832:
2833:
2836: public void columnRemoved (TableColumnModelEvent event)
2837: {
2838: revalidate();
2839: repaint();
2840: }
2841:
2842:
2847: public void columnSelectionChanged (ListSelectionEvent event)
2848: {
2849:
2850:
2851:
2852: int idx0 = Math.max(0, Math.min(getColumnCount() - 1,
2853: event.getFirstIndex()));
2854: int idxn = Math.max(0, Math.min(getColumnCount() - 1,
2855: event.getLastIndex()));
2856:
2857: int minRow = 0;
2858: int maxRow = getRowCount() - 1;
2859: if (getRowSelectionAllowed())
2860: {
2861: minRow = selectionModel.getMinSelectionIndex();
2862: maxRow = selectionModel.getMaxSelectionIndex();
2863: int leadRow = selectionModel.getLeadSelectionIndex();
2864: if (minRow == -1 && maxRow == -1)
2865: {
2866: minRow = leadRow;
2867: maxRow = leadRow;
2868: }
2869: else
2870: {
2871:
2872:
2873: if (leadRow != -1)
2874: {
2875: minRow = Math.min(minRow, leadRow);
2876: maxRow = Math.max(maxRow, leadRow);
2877: }
2878: }
2879: }
2880: if (minRow != -1 && maxRow != -1)
2881: {
2882: Rectangle first = getCellRect(minRow, idx0, false);
2883: Rectangle last = getCellRect(maxRow, idxn, false);
2884: Rectangle dirty = SwingUtilities.computeUnion(first.x, first.y,
2885: first.width,
2886: first.height, last);
2887: repaint(dirty);
2888: }
2889: }
2890:
2891:
2894: public void editingCanceled (ChangeEvent event)
2895: {
2896: if (editorComp!=null)
2897: {
2898: remove(editorComp);
2899: repaint(editorComp.getBounds());
2900: editorComp = null;
2901: }
2902: }
2903:
2904:
2910: public void editingStopped (ChangeEvent event)
2911: {
2912: if (editorComp!=null)
2913: {
2914: remove(editorComp);
2915: setValueAt(cellEditor.getCellEditorValue(), editingRow, editingColumn);
2916: repaint(editorComp.getBounds());
2917: editorComp = null;
2918: }
2919: requestFocusInWindow();
2920: }
2921:
2922:
2926: public void tableChanged (TableModelEvent event)
2927: {
2928:
2929:
2930: if (event == null || (event.getFirstRow() == TableModelEvent.HEADER_ROW))
2931: handleCompleteChange(event);
2932: else if (event.getType() == TableModelEvent.INSERT)
2933: handleInsert(event);
2934: else if (event.getType() == TableModelEvent.DELETE)
2935: handleDelete(event);
2936: else
2937: handleUpdate(event);
2938: }
2939:
2940:
2946: private void handleCompleteChange(TableModelEvent ev)
2947: {
2948: clearSelection();
2949: checkSelection();
2950: rowHeights = null;
2951: if (getAutoCreateColumnsFromModel())
2952: createDefaultColumnsFromModel();
2953: else
2954: resizeAndRepaint();
2955: }
2956:
2957:
2962: private void handleInsert(TableModelEvent ev)
2963: {
2964:
2965: int first = ev.getFirstRow();
2966: if (first < 0)
2967: first = 0;
2968: int last = ev.getLastRow();
2969: if (last < 0)
2970: last = getRowCount() - 1;
2971: selectionModel.insertIndexInterval(first, last - first + 1, true);
2972: checkSelection();
2973:
2974:
2975: if (rowHeights != null)
2976: {
2977: rowHeights.insertEntries(first, last - first + 1, rowHeight);
2978:
2979:
2980: repaint();
2981: }
2982: else
2983: {
2984:
2985: int rowHeight = getRowHeight();
2986: Rectangle dirty = new Rectangle(0, first * rowHeight,
2987: getColumnModel().getTotalColumnWidth(),
2988: (getRowCount() - first) * rowHeight);
2989: repaint(dirty);
2990: }
2991: revalidate();
2992: }
2993:
2994:
2999: private void handleDelete(TableModelEvent ev)
3000: {
3001:
3002: int first = ev.getFirstRow();
3003: if (first < 0)
3004: first = 0;
3005: int last = ev.getLastRow();
3006: if (last < 0)
3007: last = getRowCount() - 1;
3008:
3009: selectionModel.removeIndexInterval(first, last);
3010:
3011: checkSelection();
3012:
3013: if (dataModel.getRowCount() == 0)
3014: clearSelection();
3015:
3016:
3017: if (rowHeights != null)
3018: {
3019: rowHeights.removeEntries(first, last - first + 1);
3020:
3021:
3022: repaint();
3023: }
3024: else
3025: {
3026:
3027: int rowHeight = getRowHeight();
3028: int oldRowCount = getRowCount() + last - first + 1;
3029: Rectangle dirty = new Rectangle(0, first * rowHeight,
3030: getColumnModel().getTotalColumnWidth(),
3031: (oldRowCount - first) * rowHeight);
3032: repaint(dirty);
3033: }
3034: revalidate();
3035: }
3036:
3037:
3042: private void handleUpdate(TableModelEvent ev)
3043: {
3044: if (rowHeights == null)
3045: {
3046:
3047:
3048: int firstRow = ev.getFirstRow();
3049: int lastRow = ev.getLastRow();
3050: int col = ev.getColumn();
3051: Rectangle dirty;
3052: if (col == TableModelEvent.ALL_COLUMNS)
3053: {
3054:
3055: dirty = new Rectangle(0, firstRow * getRowHeight(),
3056: getColumnModel().getTotalColumnWidth(), 0);
3057: }
3058: else
3059: {
3060:
3061:
3062: int column = convertColumnIndexToModel(col);
3063: dirty = getCellRect(firstRow, column, false);
3064: }
3065:
3066:
3067: dirty.height = (lastRow + 1) * getRowHeight();
3068:
3069: repaint(dirty);
3070: }
3071: else
3072: {
3073:
3074:
3075: repaint();
3076: }
3077: }
3078:
3079:
3085: private void checkSelection()
3086: {
3087: TableModel m = getModel();
3088: ListSelectionModel sm = selectionModel;
3089: if (m != null)
3090: {
3091: int lead = sm.getLeadSelectionIndex();
3092: int c = m.getRowCount();
3093: if (c == 0 && lead != -1)
3094: {
3095:
3096: sm.setValueIsAdjusting(true);
3097: sm.setAnchorSelectionIndex(-1);
3098: sm.setLeadSelectionIndex(-1);
3099: sm.setValueIsAdjusting(false);
3100: }
3101: else if (c != 0 && lead == -1)
3102: {
3103:
3104:
3105:
3106: if (sm.isSelectedIndex(0))
3107: sm.addSelectionInterval(0, 0);
3108: else
3109: sm.removeSelectionInterval(0, 0);
3110: }
3111:
3112: }
3113: }
3114:
3115:
3119: public void valueChanged (ListSelectionEvent event)
3120: {
3121:
3122: if (isEditing())
3123: editingStopped(null);
3124:
3125:
3126: int first = Math.max(0, Math.min(getRowCount() - 1, event.getFirstIndex()));
3127: int last = Math.max(0, Math.min(getRowCount() - 1, event.getLastIndex()));
3128: Rectangle rect1 = getCellRect(first, 0, false);
3129: Rectangle rect2 = getCellRect(last, getColumnCount() - 1, false);
3130: Rectangle dirty = SwingUtilities.computeUnion(rect2.x, rect2.y,
3131: rect2.width, rect2.height,
3132: rect1);
3133: repaint(dirty);
3134: }
3135:
3136:
3144: public int columnAtPoint(Point point)
3145: {
3146: int ncols = getColumnCount();
3147: Dimension gap = getIntercellSpacing();
3148: TableColumnModel cols = getColumnModel();
3149: int x = point.x;
3150:
3151: for (int i = 0; i < ncols; ++i)
3152: {
3153: int width = cols.getColumn(i).getWidth()
3154: + (gap == null ? 0 : gap.width);
3155: if (0 <= x && x < width)
3156: return i;
3157: x -= width;
3158: }
3159: return -1;
3160: }
3161:
3162:
3170: public int rowAtPoint(Point point)
3171: {
3172: if (point != null)
3173: {
3174: int nrows = getRowCount();
3175: int r;
3176: int y = point.y;
3177: if (rowHeights == null)
3178: {
3179: int height = getRowHeight();
3180: r = y / height;
3181: }
3182: else
3183: r = rowHeights.getIndex(y);
3184:
3185: if (r < 0 || r >= nrows)
3186: return -1;
3187: else
3188: return r;
3189: }
3190: else
3191: return -1;
3192: }
3193:
3194:
3210: public Rectangle getCellRect(int row,
3211: int column,
3212: boolean includeSpacing)
3213: {
3214: Rectangle cellRect = new Rectangle(0, 0, 0, 0);
3215:
3216:
3217: if (row >= getRowCount())
3218: {
3219: cellRect.height = getHeight();
3220: }
3221: else if (row >= 0)
3222: {
3223: cellRect.height = getRowHeight(row);
3224: if (rowHeights == null)
3225: cellRect.y = row * cellRect.height;
3226: else
3227: cellRect.y = rowHeights.getPosition(row);
3228:
3229: if (! includeSpacing)
3230: {
3231:
3232: int rMargin = getRowMargin();
3233: cellRect.y += rMargin / 2;
3234: cellRect.height -= rMargin;
3235: }
3236: }
3237:
3238:
3239:
3240: if (column < 0)
3241: {
3242: if (! getComponentOrientation().isLeftToRight())
3243: {
3244: cellRect.x = getWidth();
3245: }
3246: }
3247: else if (column >= getColumnCount())
3248: {
3249: if (getComponentOrientation().isLeftToRight())
3250: {
3251: cellRect.x = getWidth();
3252: }
3253: }
3254: else
3255: {
3256: TableColumnModel tcm = getColumnModel();
3257: if (getComponentOrientation().isLeftToRight())
3258: {
3259: for (int i = 0; i < column; i++)
3260: cellRect.x += tcm.getColumn(i).getWidth();
3261: }
3262: else
3263: {
3264: for (int i = tcm.getColumnCount() - 1; i > column; i--)
3265: cellRect.x += tcm.getColumn(i).getWidth();
3266: }
3267: cellRect.width = tcm.getColumn(column).getWidth();
3268: if (! includeSpacing)
3269: {
3270:
3271: int cMargin = tcm.getColumnMargin();
3272: cellRect.x += cMargin / 2;
3273: cellRect.width -= cMargin;
3274: }
3275: }
3276:
3277: return cellRect;
3278: }
3279:
3280: public void clearSelection()
3281: {
3282: selectionModel.clearSelection();
3283: getColumnModel().getSelectionModel().clearSelection();
3284: }
3285:
3286:
3293: public int getSelectedRow ()
3294: {
3295: return selectionModel.getMinSelectionIndex();
3296: }
3297:
3298:
3303: public ListSelectionModel getSelectionModel()
3304: {
3305:
3306: return selectionModel;
3307: }
3308:
3309: public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
3310: {
3311: int block;
3312: if (orientation == SwingConstants.HORIZONTAL)
3313: {
3314: block = visibleRect.width;
3315: }
3316: else
3317: {
3318: int rowHeight = getRowHeight();
3319: if (rowHeight > 0)
3320: block = Math.max(rowHeight,
3321: (visibleRect.height / rowHeight) * rowHeight);
3322: else
3323: block = visibleRect.height;
3324: }
3325: return block;
3326: }
3327:
3328:
3333: public boolean getScrollableTracksViewportHeight()
3334: {
3335: return false;
3336: }
3337:
3338:
3344: public boolean getScrollableTracksViewportWidth()
3345: {
3346: if (autoResizeMode == AUTO_RESIZE_OFF)
3347: return false;
3348: else
3349: return true;
3350: }
3351:
3352:
3367: public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
3368: int direction)
3369: {
3370: int unit;
3371: if (orientation == SwingConstants.HORIZONTAL)
3372: unit = 100;
3373: else
3374: {
3375: unit = getRowHeight();
3376:
3377:
3378: if (rowHeights == null)
3379: {
3380: if (direction > 0)
3381: {
3382:
3383:
3384: int exposed = (visibleRect.y + visibleRect.height) % unit;
3385: if (exposed > 0 && exposed < unit - 1)
3386: unit = unit - exposed - 1;
3387: }
3388: else
3389: {
3390:
3391: int exposed = visibleRect.y % unit;
3392: if (exposed > 0 && exposed < unit)
3393: unit = exposed;
3394: }
3395: }
3396: }
3397: return unit;
3398: }
3399:
3400:
3411: public TableCellEditor getCellEditor(int row, int column)
3412: {
3413: TableCellEditor editor = columnModel.getColumn(column).getCellEditor();
3414:
3415: if (editor == null)
3416: {
3417: int mcolumn = convertColumnIndexToModel(column);
3418: editor = getDefaultEditor(dataModel.getColumnClass(mcolumn));
3419: }
3420:
3421: return editor;
3422: }
3423:
3424:
3432: public TableCellEditor getDefaultEditor(Class<?> columnClass)
3433: {
3434: if (defaultEditorsByColumnClass.containsKey(columnClass))
3435: return (TableCellEditor) defaultEditorsByColumnClass.get(columnClass);
3436: else
3437: {
3438: JTextField t = new TableTextField();
3439: TableCellEditor r = new DefaultCellEditor(t);
3440: defaultEditorsByColumnClass.put(columnClass, r);
3441: return r;
3442: }
3443: }
3444:
3445:
3452: public TableCellRenderer getCellRenderer(int row, int column)
3453: {
3454: TableCellRenderer renderer = null;
3455: if (columnModel.getColumnCount() > 0)
3456: renderer = columnModel.getColumn(column).getCellRenderer();
3457: if (renderer == null)
3458: {
3459: int mcolumn = convertColumnIndexToModel(column);
3460: renderer = getDefaultRenderer(dataModel.getColumnClass(mcolumn));
3461: }
3462: return renderer;
3463: }
3464:
3465:
3472: public void setDefaultRenderer(Class<?> columnClass, TableCellRenderer rend)
3473: {
3474: defaultRenderersByColumnClass.put(columnClass, rend);
3475: }
3476:
3477:
3484: public TableCellRenderer getDefaultRenderer(Class<?> columnClass)
3485: {
3486: if (defaultRenderersByColumnClass.containsKey(columnClass))
3487: return (TableCellRenderer) defaultRenderersByColumnClass.get(columnClass);
3488: else
3489: {
3490: TableCellRenderer r = new DefaultTableCellRenderer();
3491: defaultRenderersByColumnClass.put(columnClass, r);
3492: return r;
3493: }
3494: }
3495:
3496:
3508: public int convertColumnIndexToModel(int vc)
3509: {
3510: if (vc < 0)
3511: return vc;
3512: else
3513: return columnModel.getColumn(vc).getModelIndex();
3514: }
3515:
3516:
3528: public int convertColumnIndexToView(int mc)
3529: {
3530: if (mc < 0)
3531: return mc;
3532: int ncols = getColumnCount();
3533: for (int vc = 0; vc < ncols; ++vc)
3534: {
3535: if (columnModel.getColumn(vc).getModelIndex() == mc)
3536: return vc;
3537: }
3538: return -1;
3539: }
3540:
3541:
3550: public Component prepareRenderer(TableCellRenderer renderer,
3551: int row,
3552: int column)
3553: {
3554: boolean rowSelAllowed = getRowSelectionAllowed();
3555: boolean colSelAllowed = getColumnSelectionAllowed();
3556: boolean isSel = false;
3557: if (rowSelAllowed && colSelAllowed || !rowSelAllowed && !colSelAllowed)
3558: isSel = isCellSelected(row, column);
3559: else
3560: isSel = isRowSelected(row) && getRowSelectionAllowed()
3561: || isColumnSelected(column) && getColumnSelectionAllowed();
3562:
3563:
3564:
3565: ListSelectionModel rowSel = getSelectionModel();
3566: ListSelectionModel colSel = getColumnModel().getSelectionModel();
3567: boolean hasFocus = hasFocus() && isEnabled()
3568: && rowSel.getLeadSelectionIndex() == row
3569: && colSel.getLeadSelectionIndex() == column;
3570:
3571: return renderer.getTableCellRendererComponent(this,
3572: dataModel.getValueAt(row,
3573: convertColumnIndexToModel(column)),
3574: isSel,
3575: hasFocus,
3576: row, column);
3577: }
3578:
3579:
3580:
3585: public boolean getAutoCreateColumnsFromModel()
3586: {
3587: return autoCreateColumnsFromModel;
3588: }
3589:
3590:
3595: public int getAutoResizeMode()
3596: {
3597: return autoResizeMode;
3598: }
3599:
3600:
3605: public int getRowHeight()
3606: {
3607: return rowHeight;
3608: }
3609:
3610:
3615: public int getRowHeight(int row)
3616: {
3617: int rh = rowHeight;
3618: if (rowHeights != null)
3619: rh = rowHeights.getSize(row);
3620: return rh;
3621: }
3622:
3623:
3624:
3629: public int getRowMargin()
3630: {
3631: return rowMargin;
3632: }
3633:
3634:
3641: public boolean getRowSelectionAllowed()
3642: {
3643: return rowSelectionAllowed;
3644: }
3645:
3646:
3651: public boolean getCellSelectionEnabled()
3652: {
3653: return getColumnSelectionAllowed() && getRowSelectionAllowed();
3654: }
3655:
3656:
3661: public TableModel getModel()
3662: {
3663: return dataModel;
3664: }
3665:
3666:
3672: public int getColumnCount()
3673: {
3674: return columnModel.getColumnCount();
3675: }
3676:
3677:
3683: public int getRowCount()
3684: {
3685: return dataModel.getRowCount();
3686: }
3687:
3688:
3693: public TableColumnModel getColumnModel()
3694: {
3695: return columnModel;
3696: }
3697:
3698:
3704: public int getSelectedColumn()
3705: {
3706: return columnModel.getSelectionModel().getMinSelectionIndex();
3707: }
3708:
3709: private static int countSelections(ListSelectionModel lsm)
3710: {
3711: int lo = lsm.getMinSelectionIndex();
3712: int hi = lsm.getMaxSelectionIndex();
3713: int sum = 0;
3714: if (lo != -1 && hi != -1)
3715: {
3716: switch (lsm.getSelectionMode())
3717: {
3718: case ListSelectionModel.SINGLE_SELECTION:
3719: sum = 1;
3720: break;
3721:
3722: case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
3723: sum = hi - lo + 1;
3724: break;
3725:
3726: case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
3727: for (int i = lo; i <= hi; ++i)
3728: if (lsm.isSelectedIndex(i))
3729: ++sum;
3730: break;
3731: }
3732: }
3733: return sum;
3734: }
3735:
3736: private static int[] getSelections(ListSelectionModel lsm)
3737: {
3738: int sz = countSelections(lsm);
3739: int [] ret = new int[sz];
3740:
3741: int lo = lsm.getMinSelectionIndex();
3742: int hi = lsm.getMaxSelectionIndex();
3743: int j = 0;
3744: if (lo != -1 && hi != -1)
3745: {
3746: switch (lsm.getSelectionMode())
3747: {
3748: case ListSelectionModel.SINGLE_SELECTION:
3749: ret[0] = lo;
3750: break;
3751:
3752: case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
3753: for (int i = lo; i <= hi; ++i)
3754: ret[j++] = i;
3755: break;
3756:
3757: case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
3758: for (int i = lo; i <= hi; ++i)
3759: if (lsm.isSelectedIndex(i))
3760: ret[j++] = i;
3761: break;
3762: }
3763: }
3764: return ret;
3765: }
3766:
3767:
3773: public int getSelectedColumnCount()
3774: {
3775: return countSelections(columnModel.getSelectionModel());
3776: }
3777:
3778:
3784: public int[] getSelectedColumns()
3785: {
3786: return getSelections(columnModel.getSelectionModel());
3787: }
3788:
3789:
3796: public boolean getColumnSelectionAllowed()
3797: {
3798: return getColumnModel().getColumnSelectionAllowed();
3799: }
3800:
3801:
3807: public int getSelectedRowCount()
3808: {
3809: return countSelections(selectionModel);
3810: }
3811:
3812:
3818: public int[] getSelectedRows()
3819: {
3820: return getSelections(selectionModel);
3821: }
3822:
3823:
3828: public AccessibleContext getAccessibleContext()
3829: {
3830: if (accessibleContext == null)
3831: {
3832: AccessibleJTable ctx = new AccessibleJTable();
3833: addPropertyChangeListener(ctx);
3834: TableColumnModel tcm = getColumnModel();
3835: tcm.addColumnModelListener(ctx);
3836: tcm.getSelectionModel().addListSelectionListener(ctx);
3837: getSelectionModel().addListSelectionListener(ctx);
3838:
3839: accessibleContext = ctx;
3840: }
3841: return accessibleContext;
3842: }
3843:
3844:
3849: public TableCellEditor getCellEditor()
3850: {
3851: return cellEditor;
3852: }
3853:
3854:
3859: public boolean getDragEnabled()
3860: {
3861: return dragEnabled;
3862: }
3863:
3864:
3869: public Color getGridColor()
3870: {
3871: return gridColor;
3872: }
3873:
3874:
3879: public Dimension getIntercellSpacing()
3880: {
3881: return new Dimension(columnModel.getColumnMargin(), rowMargin);
3882: }
3883:
3884:
3889: public Dimension getPreferredScrollableViewportSize()
3890: {
3891: return preferredViewportSize;
3892: }
3893:
3894:
3899: public Color getSelectionBackground()
3900: {
3901: return selectionBackground;
3902: }
3903:
3904:
3909: public Color getSelectionForeground()
3910: {
3911: return selectionForeground;
3912: }
3913:
3914:
3919: public boolean getShowHorizontalLines()
3920: {
3921: return showHorizontalLines;
3922: }
3923:
3924:
3929: public boolean getShowVerticalLines()
3930: {
3931: return showVerticalLines;
3932: }
3933:
3934:
3939: public JTableHeader getTableHeader()
3940: {
3941: return tableHeader;
3942: }
3943:
3944:
3949: public void removeColumn(TableColumn column)
3950: {
3951: columnModel.removeColumn(column);
3952: }
3953:
3954:
3960: public void moveColumn(int column,int targetColumn)
3961: {
3962: columnModel.moveColumn(column, targetColumn);
3963: }
3964:
3965:
3972: public void setAutoCreateColumnsFromModel(boolean autoCreate)
3973: {
3974: if (autoCreateColumnsFromModel != autoCreate)
3975: {
3976: autoCreateColumnsFromModel = autoCreate;
3977: if (autoCreate)
3978: createDefaultColumnsFromModel();
3979: }
3980: }
3981:
3982:
3987: public void setAutoResizeMode(int a)
3988: {
3989: autoResizeMode = a;
3990: revalidate();
3991: repaint();
3992: }
3993:
3994:
4004: public void setRowHeight(int r)
4005: {
4006: if (r < 1)
4007: throw new IllegalArgumentException();
4008:
4009: clientRowHeightSet = true;
4010:
4011: rowHeight = r;
4012: rowHeights = null;
4013: revalidate();
4014: repaint();
4015: }
4016:
4017:
4023: public void setRowHeight(int row, int rh)
4024: {
4025: if (rowHeights == null)
4026: {
4027: rowHeights = new SizeSequence(getRowCount(), rowHeight);
4028: }
4029: rowHeights.setSize(row, rh);
4030: }
4031:
4032:
4037: public void setRowMargin(int r)
4038: {
4039: rowMargin = r;
4040: revalidate();
4041: repaint();
4042: }
4043:
4044:
4051: public void setRowSelectionAllowed(boolean r)
4052: {
4053: if (rowSelectionAllowed != r)
4054: {
4055: rowSelectionAllowed = r;
4056: firePropertyChange("rowSelectionAllowed", !r, r);
4057: repaint();
4058: }
4059: }
4060:
4061:
4066: public void setCellSelectionEnabled(boolean c)
4067: {
4068: setColumnSelectionAllowed(c);
4069: setRowSelectionAllowed(c);
4070:
4071: cellSelectionEnabled = true;
4072: }
4073:
4074:
4083: public void setModel(TableModel m)
4084: {
4085:
4086: if (m == null)
4087: throw new IllegalArgumentException();
4088:
4089:
4090: if (dataModel == m)
4091: return;
4092:
4093: TableModel oldModel = dataModel;
4094:
4095:
4096: if (dataModel != null)
4097: dataModel.removeTableModelListener(this);
4098:
4099: if (m != null)
4100: {
4101:
4102: dataModel = m;
4103:
4104:
4105: dataModel.addTableModelListener(this);
4106:
4107:
4108: tableChanged(new TableModelEvent(dataModel,
4109: TableModelEvent.HEADER_ROW));
4110:
4111:
4112: if (autoCreateColumnsFromModel)
4113: createDefaultColumnsFromModel();
4114: }
4115:
4116:
4117: firePropertyChange("model", oldModel, dataModel);
4118:
4119:
4120: revalidate();
4121: repaint();
4122: }
4123:
4124:
4133: public void setColumnModel(TableColumnModel c)
4134: {
4135: if (c == null)
4136: throw new IllegalArgumentException();
4137: TableColumnModel tmp = columnModel;
4138: if (tmp != null)
4139: tmp.removeColumnModelListener(this);
4140: if (c != null)
4141: c.addColumnModelListener(this);
4142: columnModel = c;
4143: if (dataModel != null && columnModel != null)
4144: {
4145: int ncols = getColumnCount();
4146: TableColumn column;
4147: for (int i = 0; i < ncols; ++i)
4148: {
4149: column = columnModel.getColumn(i);
4150: if (column.getHeaderValue()==null)
4151: column.setHeaderValue(dataModel.getColumnName(i));
4152: }
4153: }
4154:
4155:
4156:
4157: if (tableHeader != null)
4158: tableHeader.setColumnModel(c);
4159:
4160: revalidate();
4161: repaint();
4162: }
4163:
4164:
4171: public void setColumnSelectionAllowed(boolean c)
4172: {
4173: if (columnModel.getColumnSelectionAllowed() != c)
4174: {
4175: columnModel.setColumnSelectionAllowed(c);
4176: firePropertyChange("columnSelectionAllowed", !c, c);
4177: repaint();
4178: }
4179: }
4180:
4181:
4190: public void setSelectionModel(ListSelectionModel s)
4191: {
4192: if (s == null)
4193: throw new IllegalArgumentException();
4194: ListSelectionModel tmp = selectionModel;
4195: if (tmp != null)
4196: tmp.removeListSelectionListener(this);
4197: if (s != null)
4198: s.addListSelectionListener(this);
4199: selectionModel = s;
4200: checkSelection();
4201: }
4202:
4203:
4210: public void setSelectionMode(int s)
4211: {
4212: selectionModel.setSelectionMode(s);
4213: columnModel.getSelectionModel().setSelectionMode(s);
4214:
4215: repaint();
4216: }
4217:
4218:
4227: public void setCellEditor(TableCellEditor c)
4228: {
4229: TableCellEditor tmp = cellEditor;
4230: if (tmp != null)
4231: tmp.removeCellEditorListener(this);
4232: if (c != null)
4233: c.addCellEditorListener(this);
4234: cellEditor = c;
4235: }
4236:
4237:
4242: public void setDragEnabled(boolean d)
4243: {
4244: dragEnabled = d;
4245: }
4246:
4247:
4252: public void setGridColor(Color g)
4253: {
4254: gridColor = g;
4255: repaint();
4256: }
4257:
4258:
4263: public void setIntercellSpacing(Dimension i)
4264: {
4265: rowMargin = i.height;
4266: columnModel.setColumnMargin(i.width);
4267: repaint();
4268: }
4269:
4270:
4275: public void setPreferredScrollableViewportSize(Dimension p)
4276: {
4277: preferredViewportSize = p;
4278: revalidate();
4279: repaint();
4280: }
4281:
4282:
4291: public void setSelectionBackground(Color s)
4292: {
4293: Color tmp = selectionBackground;
4294: selectionBackground = s;
4295: if (((tmp == null && s != null)
4296: || (s == null && tmp != null)
4297: || (tmp != null && s != null && !tmp.equals(s))))
4298: firePropertyChange(SELECTION_BACKGROUND_CHANGED_PROPERTY, tmp, s);
4299: repaint();
4300: }
4301:
4302:
4311: public void setSelectionForeground(Color s)
4312: {
4313: Color tmp = selectionForeground;
4314: selectionForeground = s;
4315: if (((tmp == null && s != null)
4316: || (s == null && tmp != null)
4317: || (tmp != null && s != null && !tmp.equals(s))))
4318: firePropertyChange(SELECTION_FOREGROUND_CHANGED_PROPERTY, tmp, s);
4319: repaint();
4320: }
4321:
4322:
4327: public void setShowGrid(boolean s)
4328: {
4329: setShowVerticalLines(s);
4330: setShowHorizontalLines(s);
4331: }
4332:
4333:
4338: public void setShowHorizontalLines(boolean s)
4339: {
4340: showHorizontalLines = s;
4341: repaint();
4342: }
4343:
4344:
4349: public void setShowVerticalLines(boolean s)
4350: {
4351: showVerticalLines = s;
4352: repaint();
4353: }
4354:
4355:
4360: public void setTableHeader(JTableHeader t)
4361: {
4362: if (tableHeader != null)
4363: tableHeader.setTable(null);
4364: tableHeader = t;
4365: if (tableHeader != null)
4366: tableHeader.setTable(this);
4367: revalidate();
4368: repaint();
4369: }
4370:
4371: protected void configureEnclosingScrollPane()
4372: {
4373: JScrollPane jsp = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this);
4374: if (jsp != null && tableHeader != null)
4375: {
4376: jsp.setColumnHeaderView(tableHeader);
4377: }
4378: }
4379:
4380: protected void unconfigureEnclosingScrollPane()
4381: {
4382: JScrollPane jsp = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, this);
4383: if (jsp != null)
4384: {
4385: jsp.setColumnHeaderView(null);
4386: }
4387: }
4388:
4389:
4390: public void addNotify()
4391: {
4392: super.addNotify();
4393: configureEnclosingScrollPane();
4394: }
4395:
4396: public void removeNotify()
4397: {
4398: super.addNotify();
4399: unconfigureEnclosingScrollPane();
4400: }
4401:
4402:
4403:
4411: private void distributeSpill(TableColumn[] cols, int spill)
4412: {
4413: int average = spill / cols.length;
4414: for (int i = 0; i < cols.length; i++)
4415: {
4416: if (cols[i] != null)
4417: cols[i].setWidth(cols[i].getPreferredWidth() + average);
4418: }
4419: }
4420:
4421:
4425: private void distributeSpillResizing(TableColumn[] cols, int spill,
4426: TableColumn resizeIt)
4427: {
4428: int average = 0;
4429: if (cols.length != 1)
4430: average = spill / (cols.length-1);
4431: for (int i = 0; i < cols.length; i++)
4432: {
4433: if (cols[i] != null && !cols[i].equals(resizeIt))
4434: cols[i].setWidth(cols[i].getPreferredWidth() + average);
4435: }
4436: resizeIt.setWidth(resizeIt.getPreferredWidth());
4437: }
4438:
4439:
4447: public void doLayout()
4448: {
4449: TableColumn resizingColumn = null;
4450:
4451: int ncols = columnModel.getColumnCount();
4452: if (ncols < 1)
4453: return;
4454:
4455: int prefSum = 0;
4456: int rCol = -1;
4457:
4458: if (tableHeader != null)
4459: resizingColumn = tableHeader.getResizingColumn();
4460:
4461: for (int i = 0; i < ncols; ++i)
4462: {
4463: TableColumn col = columnModel.getColumn(i);
4464: int p = col.getPreferredWidth();
4465: prefSum += p;
4466: if (resizingColumn == col)
4467: rCol = i;
4468: }
4469:
4470: int spill = getWidth() - prefSum;
4471:
4472: if (resizingColumn != null)
4473: {
4474: TableColumn col;
4475: TableColumn [] cols;
4476:
4477: switch (getAutoResizeMode())
4478: {
4479: case AUTO_RESIZE_LAST_COLUMN:
4480: col = columnModel.getColumn(ncols-1);
4481: col.setWidth(col.getPreferredWidth() + spill);
4482: break;
4483:
4484: case AUTO_RESIZE_NEXT_COLUMN:
4485: col = columnModel.getColumn(ncols-1);
4486: col.setWidth(col.getPreferredWidth() + spill);
4487: break;
4488:
4489: case AUTO_RESIZE_ALL_COLUMNS:
4490: cols = new TableColumn[ncols];
4491: for (int i = 0; i < ncols; ++i)
4492: cols[i] = columnModel.getColumn(i);
4493: distributeSpillResizing(cols, spill, resizingColumn);
4494: break;
4495:
4496: case AUTO_RESIZE_SUBSEQUENT_COLUMNS:
4497:
4498:
4499: int w = 0;
4500: int wp = 0;
4501: TableColumn column;
4502: for (int i = 0; i < rCol; i++)
4503: {
4504: column = columnModel.getColumn(i);
4505: w += column.getWidth();
4506: wp+= column.getPreferredWidth();
4507: }
4508:
4509:
4510: int n = ncols-rCol-1;
4511: if (n>0)
4512: {
4513:
4514: spill = (getWidth()-w) - (prefSum-wp);
4515: int average = spill / n;
4516:
4517:
4518: for (int i = rCol+1; i < ncols; i++)
4519: {
4520: column = columnModel.getColumn(i);
4521: column.setWidth(column.getPreferredWidth() + average);
4522: }
4523: }
4524: resizingColumn.setWidth(resizingColumn.getPreferredWidth());
4525: break;
4526:
4527: case AUTO_RESIZE_OFF:
4528: default:
4529: int prefWidth = resizingColumn.getPreferredWidth();
4530: resizingColumn.setWidth(prefWidth);
4531: }
4532: }
4533: else
4534: {
4535: TableColumn[] cols = new TableColumn[ncols];
4536:
4537: for (int i = 0; i < ncols; ++i)
4538: cols[i] = columnModel.getColumn(i);
4539:
4540: distributeSpill(cols, spill);
4541: }
4542:
4543: if (editorComp!=null)
4544: moveToCellBeingEdited(editorComp);
4545:
4546: int leftBoundary = getLeftResizingBoundary();
4547: int width = getWidth() - leftBoundary;
4548: repaint(leftBoundary, 0, width, getHeight());
4549: if (tableHeader != null)
4550: tableHeader.repaint(leftBoundary, 0, width, tableHeader.getHeight());
4551: }
4552:
4553:
4557: int getLeftResizingBoundary()
4558: {
4559: if (tableHeader == null || getAutoResizeMode() == AUTO_RESIZE_ALL_COLUMNS)
4560: return 0;
4561: else
4562: {
4563: TableColumn resizingColumn = tableHeader.getResizingColumn();
4564: if (resizingColumn == null)
4565: return 0;
4566:
4567: int rc = convertColumnIndexToView(resizingColumn.getModelIndex());
4568: int p = 0;
4569:
4570: for (int i = 0; i < rc; i++)
4571: p += columnModel.getColumn(i).getWidth();
4572:
4573: return p;
4574: }
4575: }
4576:
4577:
4578:
4581: public void sizeColumnsToFit(boolean lastColumnOnly)
4582: {
4583: doLayout();
4584: }
4585:
4586:
4589: public void sizeColumnsToFit(int resizingColumn)
4590: {
4591: doLayout();
4592: }
4593:
4594: public String getUIClassID()
4595: {
4596: return "TableUI";
4597: }
4598:
4599:
4604: public TableUI getUI()
4605: {
4606: return (TableUI) ui;
4607: }
4608:
4609:
4614: public void setUI(TableUI ui)
4615: {
4616: super.setUI(ui);
4617:
4618:
4619: createDefaultEditors();
4620: createDefaultRenderers();
4621: }
4622:
4623: public void updateUI()
4624: {
4625: setUI((TableUI) UIManager.getUI(this));
4626: }
4627:
4628:
4637: public Class<?> getColumnClass(int column)
4638: {
4639: return getModel().getColumnClass(convertColumnIndexToModel(column));
4640: }
4641:
4642:
4653: public String getColumnName(int column)
4654: {
4655: int modelColumn = columnModel.getColumn(column).getModelIndex();
4656: return dataModel.getColumnName(modelColumn);
4657: }
4658:
4659:
4664: public int getEditingColumn()
4665: {
4666: return editingColumn;
4667: }
4668:
4669:
4674: public void setEditingColumn(int column)
4675: {
4676: editingColumn = column;
4677: }
4678:
4679:
4684: public int getEditingRow()
4685: {
4686: return editingRow;
4687: }
4688:
4689:
4694: public void setEditingRow(int row)
4695: {
4696: editingRow = row;
4697: }
4698:
4699:
4705: public Component getEditorComponent()
4706: {
4707: return editorComp;
4708: }
4709:
4710:
4715: public boolean isEditing()
4716: {
4717: return editorComp != null;
4718: }
4719:
4720:
4730: public void setDefaultEditor(Class<?> columnClass, TableCellEditor editor)
4731: {
4732: if (editor != null)
4733: defaultEditorsByColumnClass.put(columnClass, editor);
4734: else
4735: defaultEditorsByColumnClass.remove(columnClass);
4736: }
4737:
4738: public void addColumnSelectionInterval(int index0, int index1)
4739: {
4740: if ((index0 < 0 || index0 > (getColumnCount()-1)
4741: || index1 < 0 || index1 > (getColumnCount()-1)))
4742: throw new IllegalArgumentException("Column index out of range.");
4743:
4744: getColumnModel().getSelectionModel().addSelectionInterval(index0, index1);
4745: }
4746:
4747: public void addRowSelectionInterval(int index0, int index1)
4748: {
4749: if ((index0 < 0 || index0 > (getRowCount()-1)
4750: || index1 < 0 || index1 > (getRowCount()-1)))
4751: throw new IllegalArgumentException("Row index out of range.");
4752:
4753: getSelectionModel().addSelectionInterval(index0, index1);
4754: }
4755:
4756: public void setColumnSelectionInterval(int index0, int index1)
4757: {
4758: if ((index0 < 0 || index0 > (getColumnCount()-1)
4759: || index1 < 0 || index1 > (getColumnCount()-1)))
4760: throw new IllegalArgumentException("Column index out of range.");
4761:
4762: getColumnModel().getSelectionModel().setSelectionInterval(index0, index1);
4763: }
4764:
4765: public void setRowSelectionInterval(int index0, int index1)
4766: {
4767: if ((index0 < 0 || index0 > (getRowCount()-1)
4768: || index1 < 0 || index1 > (getRowCount()-1)))
4769: throw new IllegalArgumentException("Row index out of range.");
4770:
4771: getSelectionModel().setSelectionInterval(index0, index1);
4772: }
4773:
4774: public void removeColumnSelectionInterval(int index0, int index1)
4775: {
4776: if ((index0 < 0 || index0 > (getColumnCount()-1)
4777: || index1 < 0 || index1 > (getColumnCount()-1)))
4778: throw new IllegalArgumentException("Column index out of range.");
4779:
4780: getColumnModel().getSelectionModel().removeSelectionInterval(index0, index1);
4781: }
4782:
4783: public void removeRowSelectionInterval(int index0, int index1)
4784: {
4785: if ((index0 < 0 || index0 > (getRowCount()-1)
4786: || index1 < 0 || index1 > (getRowCount()-1)))
4787: throw new IllegalArgumentException("Row index out of range.");
4788:
4789: getSelectionModel().removeSelectionInterval(index0, index1);
4790: }
4791:
4792:
4800: public boolean isColumnSelected(int column)
4801: {
4802: return getColumnModel().getSelectionModel().isSelectedIndex(column);
4803: }
4804:
4805:
4813: public boolean isRowSelected(int row)
4814: {
4815: return getSelectionModel().isSelectedIndex(row);
4816: }
4817:
4818:
4827: public boolean isCellSelected(int row, int column)
4828: {
4829: return isRowSelected(row) && isColumnSelected(column);
4830: }
4831:
4832:
4835: public void selectAll()
4836: {
4837:
4838: if (getRowCount() == 0 || getColumnCount() == 0)
4839: return;
4840:
4841:
4842: int rowLead = selectionModel.getLeadSelectionIndex();
4843: int colLead = getColumnModel().getSelectionModel().getLeadSelectionIndex();
4844:
4845:
4846: setColumnSelectionInterval(0, getColumnCount() - 1);
4847: setRowSelectionInterval(0, getRowCount() - 1);
4848:
4849:
4850: addColumnSelectionInterval(colLead,colLead);
4851: addRowSelectionInterval(rowLead, rowLead);
4852: }
4853:
4854:
4863: public Object getValueAt(int row, int column)
4864: {
4865: return dataModel.getValueAt(row, convertColumnIndexToModel(column));
4866: }
4867:
4868:
4876: public void setValueAt(Object value, int row, int column)
4877: {
4878: dataModel.setValueAt(value, row, convertColumnIndexToModel(column));
4879:
4880: repaint(getCellRect(row, column, true));
4881: }
4882:
4883:
4895: public TableColumn getColumn(Object identifier)
4896: {
4897: return columnModel.getColumn(columnModel.getColumnIndex(identifier));
4898: }
4899:
4900:
4909: public boolean isCellEditable(int row, int column)
4910: {
4911: return dataModel.isCellEditable(row, convertColumnIndexToModel(column));
4912: }
4913:
4914:
4921: public void createDefaultColumnsFromModel()
4922: {
4923: assert columnModel != null : "The columnModel must not be null.";
4924:
4925:
4926: int columnIndex = columnModel.getColumnCount() - 1;
4927: while (columnIndex >= 0)
4928: {
4929: columnModel.removeColumn(columnModel.getColumn(columnIndex));
4930: columnIndex--;
4931: }
4932:
4933:
4934: int columnCount = dataModel.getColumnCount();
4935: for (int c = 0; c < columnCount; c++)
4936: {
4937: TableColumn column = new TableColumn(c);
4938: column.setIdentifier(dataModel.getColumnName(c));
4939: column.setHeaderValue(dataModel.getColumnName(c));
4940: columnModel.addColumn(column);
4941: column.addPropertyChangeListener(tableColumnPropertyChangeHandler);
4942: }
4943: }
4944:
4945: public void changeSelection (int rowIndex, int columnIndex, boolean toggle, boolean extend)
4946: {
4947: if (toggle && extend)
4948: {
4949:
4950:
4951: selectionModel.setAnchorSelectionIndex(rowIndex);
4952: getColumnModel().getSelectionModel().setAnchorSelectionIndex(columnIndex);
4953: }
4954: else if (toggle)
4955: {
4956:
4957: if (isCellSelected(rowIndex,columnIndex))
4958: {
4959: selectionModel.removeSelectionInterval(rowIndex,rowIndex);
4960: getColumnModel().getSelectionModel().removeSelectionInterval(columnIndex,columnIndex);
4961: }
4962: else
4963: {
4964: selectionModel.addSelectionInterval(rowIndex,rowIndex);
4965: getColumnModel().getSelectionModel().addSelectionInterval(columnIndex,columnIndex);
4966: }
4967: }
4968: else if (extend)
4969: {
4970:
4971:
4972: selectionModel.setLeadSelectionIndex(rowIndex);
4973: getColumnModel().getSelectionModel().setLeadSelectionIndex(columnIndex);
4974: }
4975: else
4976: {
4977:
4978:
4979: selectionModel.clearSelection();
4980: selectionModel.setSelectionInterval(rowIndex,rowIndex);
4981: getColumnModel().getSelectionModel().clearSelection();
4982: getColumnModel().getSelectionModel().setSelectionInterval(columnIndex, columnIndex);
4983:
4984:
4985: }
4986: }
4987:
4988:
4994: public boolean editCellAt(int row, int column)
4995: {
4996:
4997: if (isEditing())
4998: editingStopped(new ChangeEvent("editingStopped"));
4999:
5000: TableCellEditor editor = getCellEditor(row, column);
5001:
5002:
5003:
5004: if (editor == booleanInvertingEditor && isCellEditable(row, column))
5005: {
5006: if (Boolean.TRUE.equals(getValueAt(row, column)))
5007: setValueAt(Boolean.FALSE, row, column);
5008: else
5009: setValueAt(Boolean.TRUE, row, column);
5010: return false;
5011: }
5012: else
5013: {
5014: editingRow = row;
5015: editingColumn = column;
5016:
5017: setCellEditor(editor);
5018: editorComp = prepareEditor(cellEditor, row, column);
5019:
5020:
5021:
5022: removeAll();
5023: add(editorComp);
5024: moveToCellBeingEdited(editorComp);
5025: scrollRectToVisible(editorComp.getBounds());
5026: editorComp.requestFocusInWindow();
5027:
5028:
5029: return editor.shouldSelectCell(null);
5030: }
5031: }
5032:
5033:
5039: private void moveToCellBeingEdited(Component component)
5040: {
5041: Rectangle r = getCellRect(editingRow, editingColumn, true);
5042:
5043:
5044:
5045: r.x -= 1;
5046: r.y -= 1;
5047: r.width += 1;
5048: r.height += 1;
5049: component.setBounds(r);
5050: }
5051:
5052:
5058: public boolean editCellAt (int row, int column, EventObject e)
5059: {
5060: return editCellAt(row, column);
5061: }
5062:
5063:
5066: public void removeEditor()
5067: {
5068: editingStopped(new ChangeEvent(this));
5069: }
5070:
5071:
5080: public Component prepareEditor (TableCellEditor editor, int row, int column)
5081: {
5082: return editor.getTableCellEditorComponent
5083: (this, getValueAt(row, column), isCellSelected(row, column), row, column);
5084: }
5085:
5086:
5089: protected void resizeAndRepaint()
5090: {
5091: revalidate();
5092: repaint();
5093: }
5094:
5095:
5105: public void setSurrendersFocusOnKeystroke(boolean value)
5106: {
5107:
5108: surrendersFocusOnKeystroke = value;
5109: }
5110:
5111:
5122: public boolean getSurrendersFocusOnKeystroke()
5123: {
5124:
5125: return surrendersFocusOnKeystroke;
5126: }
5127:
5128:
5142: void setUIProperty(String propertyName, Object value)
5143: {
5144: if (propertyName.equals("rowHeight"))
5145: {
5146: if (! clientRowHeightSet)
5147: {
5148: setRowHeight(((Integer) value).intValue());
5149: clientRowHeightSet = false;
5150: }
5151: }
5152: else
5153: {
5154: super.setUIProperty(propertyName, value);
5155: }
5156: }
5157: }