1 7 8 package javax.swing; 9 10 import java.util.*; 11 12 import java.applet.Applet ; 13 import java.awt.*; 14 import java.awt.event.*; 15 import java.awt.print.*; 16 17 import java.beans.*; 18 19 import java.io.Serializable ; 20 import java.io.ObjectOutputStream ; 21 import java.io.ObjectInputStream ; 22 import java.io.IOException ; 23 24 import javax.accessibility.*; 25 26 import javax.swing.event.*; 27 import javax.swing.plaf.*; 28 import javax.swing.table.*; 29 import javax.swing.border.*; 30 31 import java.text.NumberFormat ; 32 import java.text.DateFormat ; 33 import java.text.MessageFormat ; 34 35 import javax.print.attribute.*; 36 37 134 137 public class JTable extends JComponent implements TableModelListener, Scrollable , 138 TableColumnModelListener, ListSelectionListener, CellEditorListener, 139 Accessible 140 { 141 145 149 private static final String uiClassID = "TableUI"; 150 151 152 public static final int AUTO_RESIZE_OFF = 0; 153 154 155 public static final int AUTO_RESIZE_NEXT_COLUMN = 1; 156 157 159 public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS = 2; 160 161 162 public static final int AUTO_RESIZE_LAST_COLUMN = 3; 163 164 165 public static final int AUTO_RESIZE_ALL_COLUMNS = 4; 166 167 168 176 public enum PrintMode { 177 178 182 NORMAL, 183 184 189 FIT_WIDTH 190 } 191 192 193 197 198 protected TableModel dataModel; 199 200 201 protected TableColumnModel columnModel; 202 203 204 protected ListSelectionModel selectionModel; 205 206 207 protected JTableHeader tableHeader; 208 209 210 protected int rowHeight; 211 212 213 protected int rowMargin; 214 215 216 protected Color gridColor; 217 218 219 protected boolean showHorizontalLines; 220 221 222 protected boolean showVerticalLines; 223 224 229 protected int autoResizeMode; 230 231 235 protected boolean autoCreateColumnsFromModel; 236 237 238 protected Dimension preferredViewportSize; 239 240 241 protected boolean rowSelectionAllowed; 242 243 250 255 protected boolean cellSelectionEnabled; 256 257 258 transient protected Component editorComp; 259 260 265 transient protected TableCellEditor cellEditor; 266 267 268 transient protected int editingColumn; 269 270 271 transient protected int editingRow; 272 273 278 transient protected Hashtable defaultRenderersByColumnClass; 279 280 285 transient protected Hashtable defaultEditorsByColumnClass; 286 287 288 protected Color selectionForeground; 289 290 291 protected Color selectionBackground; 292 293 297 private SizeSequence rowModel; 298 private boolean dragEnabled; 299 private boolean surrendersFocusOnKeystroke; 300 private PropertyChangeListener editorRemover = null; 301 306 private boolean columnSelectionAdjusting; 307 311 private boolean rowSelectionAdjusting; 312 313 318 private boolean isPrinting = false; 319 320 323 private Throwable printError; 324 325 328 private boolean isRowHeightSet; 329 330 334 343 public JTable() { 344 this(null, null, null); 345 } 346 347 356 public JTable(TableModel dm) { 357 this(dm, null, null); 358 } 359 360 369 public JTable(TableModel dm, TableColumnModel cm) { 370 this(dm, cm, null); 371 } 372 373 391 public JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) { 392 super(); 393 setLayout(null); 394 395 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, 396 JComponent.getManagingFocusForwardTraversalKeys()); 397 setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, 398 JComponent.getManagingFocusBackwardTraversalKeys()); 399 400 if (cm == null) { 401 cm = createDefaultColumnModel(); 402 autoCreateColumnsFromModel = true; 403 } 404 setColumnModel(cm); 405 406 if (sm == null) { 407 sm = createDefaultSelectionModel(); 408 } 409 setSelectionModel(sm); 410 411 if (dm == null) { 415 dm = createDefaultDataModel(); 416 } 417 setModel(dm); 418 419 initializeLocalVars(); 420 updateUI(); 421 } 422 423 433 public JTable(int numRows, int numColumns) { 434 this(new DefaultTableModel(numRows, numColumns)); 435 } 436 437 451 public JTable(Vector rowData, Vector columnNames) { 452 this(new DefaultTableModel(rowData, columnNames)); 453 } 454 455 468 public JTable(final Object [][] rowData, final Object [] columnNames) { 469 this(new AbstractTableModel() { 470 public String getColumnName(int column) { return columnNames[column].toString(); } 471 public int getRowCount() { return rowData.length; } 472 public int getColumnCount() { return columnNames.length; } 473 public Object getValueAt(int row, int col) { return rowData[row][col]; } 474 public boolean isCellEditable(int row, int column) { return true; } 475 public void setValueAt(Object value, int row, int col) { 476 rowData[row][col] = value; 477 fireTableCellUpdated(row, col); 478 } 479 }); 480 } 481 482 487 public void addNotify() { 488 super.addNotify(); 489 configureEnclosingScrollPane(); 490 } 491 492 505 protected void configureEnclosingScrollPane() { 506 Container p = getParent(); 507 if (p instanceof JViewport ) { 508 Container gp = p.getParent(); 509 if (gp instanceof JScrollPane ) { 510 JScrollPane scrollPane = (JScrollPane )gp; 511 JViewport viewport = scrollPane.getViewport(); 515 if (viewport == null || viewport.getView() != this) { 516 return; 517 } 518 scrollPane.setColumnHeaderView(getTableHeader()); 519 Border border = scrollPane.getBorder(); 521 if (border == null || border instanceof UIResource) { 522 scrollPane.setBorder(UIManager.getBorder("Table.scrollPaneBorder")); 523 } 524 } 525 } 526 } 527 528 533 public void removeNotify() { 534 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 535 removePropertyChangeListener("permanentFocusOwner", editorRemover); 536 editorRemover = null; 537 unconfigureEnclosingScrollPane(); 538 super.removeNotify(); 539 } 540 541 552 protected void unconfigureEnclosingScrollPane() { 553 Container p = getParent(); 554 if (p instanceof JViewport ) { 555 Container gp = p.getParent(); 556 if (gp instanceof JScrollPane ) { 557 JScrollPane scrollPane = (JScrollPane )gp; 558 JViewport viewport = scrollPane.getViewport(); 562 if (viewport == null || viewport.getView() != this) { 563 return; 564 } 565 scrollPane.setColumnHeaderView(null); 566 } 567 } 568 } 569 570 void setUIProperty(String propertyName, Object value) { 571 if (propertyName == "rowHeight") { 572 if (!isRowHeightSet) { 573 setRowHeight(((Number )value).intValue()); 574 isRowHeightSet = false; 575 } 576 return; 577 } 578 super.setUIProperty(propertyName, value); 579 } 580 581 585 591 @Deprecated 592 static public JScrollPane createScrollPaneForTable(JTable aTable) { 593 return new JScrollPane (aTable); 594 } 595 596 600 610 public void setTableHeader(JTableHeader tableHeader) { 611 if (this.tableHeader != tableHeader) { 612 JTableHeader old = this.tableHeader; 613 if (old != null) { 615 old.setTable(null); 616 } 617 this.tableHeader = tableHeader; 618 if (tableHeader != null) { 619 tableHeader.setTable(this); 620 } 621 firePropertyChange("tableHeader", old, tableHeader); 622 } 623 } 624 625 631 public JTableHeader getTableHeader() { 632 return tableHeader; 633 } 634 635 649 public void setRowHeight(int rowHeight) { 650 if (rowHeight <= 0) { 651 throw new IllegalArgumentException ("New row height less than 1"); 652 } 653 int old = this.rowHeight; 654 this.rowHeight = rowHeight; 655 rowModel = null; 656 isRowHeightSet = true; 657 resizeAndRepaint(); 658 firePropertyChange("rowHeight", old, rowHeight); 659 } 660 661 668 public int getRowHeight() { 669 return rowHeight; 670 } 671 672 private SizeSequence getRowModel() { 673 if (rowModel == null) { 674 rowModel = new SizeSequence (getRowCount(), getRowHeight()); 675 } 676 return rowModel; 677 } 678 679 693 public void setRowHeight(int row, int rowHeight) { 694 if (rowHeight <= 0) { 695 throw new IllegalArgumentException ("New row height less than 1"); 696 } 697 getRowModel().setSize(row, rowHeight); 698 resizeAndRepaint(); 699 } 700 701 706 public int getRowHeight(int row) { 707 return (rowModel == null) ? getRowHeight() : rowModel.getSize(row); 708 } 709 710 719 public void setRowMargin(int rowMargin) { 720 int old = this.rowMargin; 721 this.rowMargin = rowMargin; 722 resizeAndRepaint(); 723 firePropertyChange("rowMargin", old, rowMargin); 724 } 725 726 733 public int getRowMargin() { 734 return rowMargin; 735 } 736 737 750 public void setIntercellSpacing(Dimension intercellSpacing) { 751 setRowMargin(intercellSpacing.height); 753 getColumnModel().setColumnMargin(intercellSpacing.width); 754 755 resizeAndRepaint(); 756 } 757 758 765 public Dimension getIntercellSpacing() { 766 return new Dimension(getColumnModel().getColumnMargin(), rowMargin); 767 } 768 769 780 public void setGridColor(Color gridColor) { 781 if (gridColor == null) { 782 throw new IllegalArgumentException ("New color is null"); 783 } 784 Color old = this.gridColor; 785 this.gridColor = gridColor; 786 firePropertyChange("gridColor", old, gridColor); 787 repaint(); 789 } 790 791 798 public Color getGridColor() { 799 return gridColor; 800 } 801 802 816 public void setShowGrid(boolean showGrid) { 817 setShowHorizontalLines(showGrid); 818 setShowVerticalLines(showGrid); 819 820 repaint(); 822 } 823 824 836 public void setShowHorizontalLines(boolean showHorizontalLines) { 837 boolean old = this.showHorizontalLines; 838 this.showHorizontalLines = showHorizontalLines; 839 firePropertyChange("showHorizontalLines", old, showHorizontalLines); 840 841 repaint(); 843 } 844 845 857 public void setShowVerticalLines(boolean showVerticalLines) { 858 boolean old = this.showVerticalLines; 859 this.showVerticalLines = showVerticalLines; 860 firePropertyChange("showVerticalLines", old, showVerticalLines); 861 repaint(); 863 } 864 865 873 public boolean getShowHorizontalLines() { 874 return showHorizontalLines; 875 } 876 877 885 public boolean getShowVerticalLines() { 886 return showVerticalLines; 887 } 888 889 910 public void setAutoResizeMode(int mode) { 911 if ((mode == AUTO_RESIZE_OFF) || 912 (mode == AUTO_RESIZE_NEXT_COLUMN) || 913 (mode == AUTO_RESIZE_SUBSEQUENT_COLUMNS) || 914 (mode == AUTO_RESIZE_LAST_COLUMN) || 915 (mode == AUTO_RESIZE_ALL_COLUMNS)) { 916 int old = autoResizeMode; 917 autoResizeMode = mode; 918 resizeAndRepaint(); 919 if (tableHeader != null) { 920 tableHeader.resizeAndRepaint(); 921 } 922 firePropertyChange("autoResizeMode", old, autoResizeMode); 923 } 924 } 925 926 935 public int getAutoResizeMode() { 936 return autoResizeMode; 937 } 938 939 951 public void setAutoCreateColumnsFromModel(boolean autoCreateColumnsFromModel) { 952 if (this.autoCreateColumnsFromModel != autoCreateColumnsFromModel) { 953 boolean old = this.autoCreateColumnsFromModel; 954 this.autoCreateColumnsFromModel = autoCreateColumnsFromModel; 955 if (autoCreateColumnsFromModel) { 956 createDefaultColumnsFromModel(); 957 } 958 firePropertyChange("autoCreateColumnsFromModel", old, autoCreateColumnsFromModel); 959 } 960 } 961 962 974 public boolean getAutoCreateColumnsFromModel() { 975 return autoCreateColumnsFromModel; 976 } 977 978 988 public void createDefaultColumnsFromModel() { 989 TableModel m = getModel(); 990 if (m != null) { 991 TableColumnModel cm = getColumnModel(); 993 while (cm.getColumnCount() > 0) { 994 cm.removeColumn(cm.getColumn(0)); 995 } 996 997 for (int i = 0; i < m.getColumnCount(); i++) { 999 TableColumn newColumn = new TableColumn(i); 1000 addColumn(newColumn); 1001 } 1002 } 1003 } 1004 1005 1016 public void setDefaultRenderer(Class <?> columnClass, TableCellRenderer renderer) { 1017 if (renderer != null) { 1018 defaultRenderersByColumnClass.put(columnClass, renderer); 1019 } 1020 else { 1021 defaultRenderersByColumnClass.remove(columnClass); 1022 } 1023 } 1024 1025 1040 public TableCellRenderer getDefaultRenderer(Class <?> columnClass) { 1041 if (columnClass == null) { 1042 return null; 1043 } 1044 else { 1045 Object renderer = defaultRenderersByColumnClass.get(columnClass); 1046 if (renderer != null) { 1047 return (TableCellRenderer)renderer; 1048 } 1049 else { 1050 return getDefaultRenderer(columnClass.getSuperclass()); 1051 } 1052 } 1053 } 1054 1055 1070 public void setDefaultEditor(Class <?> columnClass, TableCellEditor editor) { 1071 if (editor != null) { 1072 defaultEditorsByColumnClass.put(columnClass, editor); 1073 } 1074 else { 1075 defaultEditorsByColumnClass.remove(columnClass); 1076 } 1077 } 1078 1079 1093 public TableCellEditor getDefaultEditor(Class <?> columnClass) { 1094 if (columnClass == null) { 1095 return null; 1096 } 1097 else { 1098 Object editor = defaultEditorsByColumnClass.get(columnClass); 1099 if (editor != null) { 1100 return (TableCellEditor)editor; 1101 } 1102 else { 1103 return getDefaultEditor(columnClass.getSuperclass()); 1104 } 1105 } 1106 } 1107 1108 1152 public void setDragEnabled(boolean b) { 1153 if (b && GraphicsEnvironment.isHeadless()) { 1154 throw new HeadlessException(); 1155 } 1156 dragEnabled = b; 1157 } 1158 1159 1166 public boolean getDragEnabled() { 1167 return dragEnabled; 1168 } 1169 1170 1171 1200 public void setSelectionMode(int selectionMode) { 1201 clearSelection(); 1202 getSelectionModel().setSelectionMode(selectionMode); 1203 getColumnModel().getSelectionModel().setSelectionMode(selectionMode); 1204 } 1205 1206 1216 public void setRowSelectionAllowed(boolean rowSelectionAllowed) { 1217 boolean old = this.rowSelectionAllowed; 1218 this.rowSelectionAllowed = rowSelectionAllowed; 1219 if (old != rowSelectionAllowed) { 1220 repaint(); 1221 } 1222 firePropertyChange("rowSelectionAllowed", old, rowSelectionAllowed); 1223 } 1224 1225 1231 public boolean getRowSelectionAllowed() { 1232 return rowSelectionAllowed; 1233 } 1234 1235 1245 public void setColumnSelectionAllowed(boolean columnSelectionAllowed) { 1246 boolean old = columnModel.getColumnSelectionAllowed(); 1247 columnModel.setColumnSelectionAllowed(columnSelectionAllowed); 1248 if (old != columnSelectionAllowed) { 1249 repaint(); 1250 } 1251 firePropertyChange("columnSelectionAllowed", old, columnSelectionAllowed); 1252 } 1253 1254 1260 public boolean getColumnSelectionAllowed() { 1261 return columnModel.getColumnSelectionAllowed(); 1262 } 1263 1264 1284 public void setCellSelectionEnabled(boolean cellSelectionEnabled) { 1285 setRowSelectionAllowed(cellSelectionEnabled); 1286 setColumnSelectionAllowed(cellSelectionEnabled); 1287 boolean old = this.cellSelectionEnabled; 1288 this.cellSelectionEnabled = cellSelectionEnabled; 1289 firePropertyChange("cellSelectionEnabled", old, cellSelectionEnabled); 1290 } 1291 1292 1301 public boolean getCellSelectionEnabled() { 1302 return getRowSelectionAllowed() && getColumnSelectionAllowed(); 1303 } 1304 1305 1308 public void selectAll() { 1309 if (isEditing()) { 1311 removeEditor(); 1312 } 1313 if (getRowCount() > 0 && getColumnCount() > 0) { 1314 int oldLead; 1315 int oldAnchor; 1316 ListSelectionModel selModel; 1317 1318 selModel = selectionModel; 1319 selModel.setValueIsAdjusting(true); 1320 oldLead = getAdjustedIndex(selModel.getLeadSelectionIndex(), true); 1321 oldAnchor = getAdjustedIndex(selModel.getAnchorSelectionIndex(), true); 1322 1323 setRowSelectionInterval(0, getRowCount()-1); 1324 1325 if (oldAnchor == -1) { 1326 oldAnchor = oldLead; 1327 } 1328 1329 if (oldLead == -1) { 1331 selModel.setAnchorSelectionIndex(-1); 1332 selModel.setLeadSelectionIndex(-1); 1333 } else { 1334 selModel.addSelectionInterval(oldAnchor, oldLead); 1335 } 1336 1337 selModel.setValueIsAdjusting(false); 1338 1339 selModel = columnModel.getSelectionModel(); 1340 selModel.setValueIsAdjusting(true); 1341 oldLead = getAdjustedIndex(selModel.getLeadSelectionIndex(), false); 1342 oldAnchor = getAdjustedIndex(selModel.getAnchorSelectionIndex(), false); 1343 1344 setColumnSelectionInterval(0, getColumnCount()-1); 1345 1346 if (oldAnchor == -1) { 1347 oldAnchor = oldLead; 1348 } 1349 1350 if (oldLead == -1) { 1352 selModel.setAnchorSelectionIndex(-1); 1353 selModel.setLeadSelectionIndex(-1); 1354 } else { 1355 selModel.addSelectionInterval(oldAnchor, oldLead); 1356 } 1357 1358 selModel.setValueIsAdjusting(false); 1359 } 1360 } 1361 1362 1365 public void clearSelection() { 1366 selectionModel.clearSelection(); 1367 columnModel.getSelectionModel().clearSelection(); 1368 } 1369 1370 private void clearSelectionAndLeadAnchor() { 1371 selectionModel.setValueIsAdjusting(true); 1372 columnModel.getSelectionModel().setValueIsAdjusting(true); 1373 1374 clearSelection(); 1375 1376 selectionModel.setAnchorSelectionIndex(-1); 1377 selectionModel.setLeadSelectionIndex(-1); 1378 columnModel.getSelectionModel().setAnchorSelectionIndex(-1); 1379 columnModel.getSelectionModel().setLeadSelectionIndex(-1); 1380 1381 selectionModel.setValueIsAdjusting(false); 1382 columnModel.getSelectionModel().setValueIsAdjusting(false); 1383 } 1384 1385 private int getAdjustedIndex(int index, boolean row) { 1386 int compare = row ? getRowCount() : getColumnCount(); 1387 return index < compare ? index : -1; 1388 } 1389 1390 private int boundRow(int row) throws IllegalArgumentException { 1391 if (row < 0 || row >= getRowCount()) { 1392 throw new IllegalArgumentException ("Row index out of range"); 1393 } 1394 return row; 1395 } 1396 1397 private int boundColumn(int col) { 1398 if (col< 0 || col >= getColumnCount()) { 1399 throw new IllegalArgumentException ("Column index out of range"); 1400 } 1401 return col; 1402 } 1403 1404 1414 public void setRowSelectionInterval(int index0, int index1) { 1415 selectionModel.setSelectionInterval(boundRow(index0), boundRow(index1)); 1416 } 1417 1418 1428 public void setColumnSelectionInterval(int index0, int index1) { 1429 columnModel.getSelectionModel().setSelectionInterval(boundColumn(index0), boundColumn(index1)); 1430 } 1431 1432 1441 public void addRowSelectionInterval(int index0, int index1) { 1442 selectionModel.addSelectionInterval(boundRow(index0), boundRow(index1)); 1443 } 1444 1445 1455 public void addColumnSelectionInterval(int index0, int index1) { 1456 columnModel.getSelectionModel().addSelectionInterval(boundColumn(index0), boundColumn(index1)); 1457 } 1458 1459 1468 public void removeRowSelectionInterval(int index0, int index1) { 1469 selectionModel.removeSelectionInterval(boundRow(index0), boundRow(index1)); 1470 } 1471 1472 1481 public void removeColumnSelectionInterval(int index0, int index1) { 1482 columnModel.getSelectionModel().removeSelectionInterval(boundColumn(index0), boundColumn(index1)); 1483 } 1484 1485 1489 public int getSelectedRow() { 1490 return selectionModel.getMinSelectionIndex(); 1491 } 1492 1493 1498 public int getSelectedColumn() { 1499 return columnModel.getSelectionModel().getMinSelectionIndex(); 1500 } 1501 1502 1509 public int[] getSelectedRows() { 1510 int iMin = selectionModel.getMinSelectionIndex(); 1511 int iMax = selectionModel.getMaxSelectionIndex(); 1512 1513 if ((iMin == -1) || (iMax == -1)) { 1514 return new int[0]; 1515 } 1516 1517 int[] rvTmp = new int[1+ (iMax - iMin)]; 1518 int n = 0; 1519 for(int i = iMin; i <= iMax; i++) { 1520 if (selectionModel.isSelectedIndex(i)) { 1521 rvTmp[n++] = i; 1522 } 1523 } 1524 int[] rv = new int[n]; 1525 System.arraycopy(rvTmp, 0, rv, 0, n); 1526 return rv; 1527 } 1528 1529 1536 public int[] getSelectedColumns() { 1537 return columnModel.getSelectedColumns(); 1538 } 1539 1540 1545 public int getSelectedRowCount() { 1546 int iMin = selectionModel.getMinSelectionIndex(); 1547 int iMax = selectionModel.getMaxSelectionIndex(); 1548 int count = 0; 1549 1550 for(int i = iMin; i <= iMax; i++) { 1551 if (selectionModel.isSelectedIndex(i)) { 1552 count++; 1553 } 1554 } 1555 return count; 1556 } 1557 1558 1563 public int getSelectedColumnCount() { 1564 return columnModel.getSelectedColumnCount(); 1565 } 1566 1567 1574 public boolean isRowSelected(int row) { 1575 return selectionModel.isSelectedIndex(row); 1576 } 1577 1578 1586 public boolean isColumnSelected(int column) { 1587 return columnModel.getSelectionModel().isSelectedIndex(column); 1588 } 1589 1590 1600 public boolean isCellSelected(int row, int column) { 1601 if (!getRowSelectionAllowed() && !getColumnSelectionAllowed()) { 1602 return false; 1603 } 1604 return (!getRowSelectionAllowed() || isRowSelected(row)) && 1605 (!getColumnSelectionAllowed() || isColumnSelected(column)); 1606 } 1607 1608 private void changeSelectionModel(ListSelectionModel sm, int index, 1609 boolean toggle, boolean extend, boolean selected, 1610 boolean row) { 1611 if (extend) { 1612 if (toggle) { 1613 sm.setAnchorSelectionIndex(index); 1614 } 1615 else { 1616 int anchorIndex = getAdjustedIndex(sm.getAnchorSelectionIndex(), row); 1617 if (anchorIndex == -1) { 1618 anchorIndex = 0; 1619 } 1620 1621 sm.setSelectionInterval(anchorIndex, index); 1622 } 1623 } 1624 else { 1625 if (toggle) { 1626 if (selected) { 1627 sm.removeSelectionInterval(index, index); 1628 } 1629 else { 1630 sm.addSelectionInterval(index, index); 1631 } 1632 } 1633 else { 1634 sm.setSelectionInterval(index, index); 1635 } 1636 } 1637 } 1638 1639 1666 public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) { 1667 ListSelectionModel rsm = getSelectionModel(); 1668 ListSelectionModel csm = getColumnModel().getSelectionModel(); 1669 1670 boolean selected = isCellSelected(rowIndex, columnIndex); 1678 1679 changeSelectionModel(csm, columnIndex, toggle, extend, selected, false); 1680 changeSelectionModel(rsm, rowIndex, toggle, extend, selected, true); 1681 1682 if (getAutoscrolls()) { 1686 Rectangle cellRect = getCellRect(rowIndex, columnIndex, false); 1687 if (cellRect != null) { 1688 scrollRectToVisible(cellRect); 1689 } 1690 } 1691 } 1692 1693 1700 public Color getSelectionForeground() { 1701 return selectionForeground; 1702 } 1703 1704 1725 public void setSelectionForeground(Color selectionForeground) { 1726 Color old = this.selectionForeground; 1727 this.selectionForeground = selectionForeground; 1728 firePropertyChange("selectionForeground", old, selectionForeground); 1729 if ( !selectionForeground.equals(old) ) 1730 { 1731 repaint(); 1732 } 1733 } 1734 1735 1742 public Color getSelectionBackground() { 1743 return selectionBackground; 1744 } 1745 1746 1766 public void setSelectionBackground(Color selectionBackground) { 1767 Color old = this.selectionBackground; 1768 this.selectionBackground = selectionBackground; 1769 firePropertyChange("selectionBackground", old, selectionBackground); 1770 if ( !selectionBackground.equals(old) ) 1771 { 1772 repaint(); 1773 } 1774 } 1775 1776 1786 public TableColumn getColumn(Object identifier) { 1787 TableColumnModel cm = getColumnModel(); 1788 int columnIndex = cm.getColumnIndex(identifier); 1789 return cm.getColumn(columnIndex); 1790 } 1791 1792 1796 1808 public int convertColumnIndexToModel(int viewColumnIndex) { 1809 if (viewColumnIndex < 0) { 1810 return viewColumnIndex; 1811 } 1812 return getColumnModel().getColumn(viewColumnIndex).getModelIndex(); 1813 } 1814 1815 1828 public int convertColumnIndexToView(int modelColumnIndex) { 1829 if (modelColumnIndex < 0) { 1830 return modelColumnIndex; 1831 } 1832 TableColumnModel cm = getColumnModel(); 1833 for (int column = 0; column < getColumnCount(); column++) { 1834 if (cm.getColumn(column).getModelIndex() == modelColumnIndex) { 1835 return column; 1836 } 1837 } 1838 return -1; 1839 } 1840 1841 1847 public int getRowCount() { 1848 return getModel().getRowCount(); 1849 } 1850 1851 1859 public int getColumnCount() { 1860 return getColumnModel().getColumnCount(); 1861 } 1862 1863 1871 public String getColumnName(int column) { 1872 return getModel().getColumnName(convertColumnIndexToModel(column)); 1873 } 1874 1875 1883 public Class <?> getColumnClass(int column) { 1884 return getModel().getColumnClass(convertColumnIndexToModel(column)); 1885 } 1886 1887 1902 public Object getValueAt(int row, int column) { 1903 return getModel().getValueAt(row, convertColumnIndexToModel(column)); 1904 } 1905 1906 1925 public void setValueAt(Object aValue, int row, int column) { 1926 getModel().setValueAt(aValue, row, convertColumnIndexToModel(column)); 1927 } 1928 1929 1948 public boolean isCellEditable(int row, int column) { 1949 return getModel().isCellEditable(row, convertColumnIndexToModel(column)); 1950 } 1951 1955 1981 public void addColumn(TableColumn aColumn) { 1982 if (aColumn.getHeaderValue() == null) { 1983 int modelColumn = aColumn.getModelIndex(); 1984 String columnName = getModel().getColumnName(modelColumn); 1985 aColumn.setHeaderValue(columnName); 1986 } 1987 getColumnModel().addColumn(aColumn); 1988 } 1989 1990 1999 public void removeColumn(TableColumn aColumn) { 2000 getColumnModel().removeColumn(aColumn); 2001 } 2002 2003 2012 public void moveColumn(int column, int targetColumn) { 2013 getColumnModel().moveColumn(column, targetColumn); 2014 } 2015 2016 2020 2031 public int columnAtPoint(Point point) { 2032 int x = point.x; 2033 if( !getComponentOrientation().isLeftToRight() ) { 2034 x = getWidth() - x; 2035 } 2036 return getColumnModel().getColumnIndexAtX(x); 2037 } 2038 2039 2050 public int rowAtPoint(Point point) { 2051 int y = point.y; 2052 int result = (rowModel == null) ? y/getRowHeight() : rowModel.getIndex(y); 2053 if (result < 0) { 2054 return -1; 2055 } 2056 else if (result >= getRowCount()) { 2057 return -1; 2058 } 2059 else { 2060 return result; 2061 } 2062 } 2063 2064 2107 public Rectangle getCellRect(int row, int column, boolean includeSpacing) { 2108 Rectangle r = new Rectangle(); 2109 boolean valid = true; 2110 if (row < 0) { 2111 valid = false; 2113 } 2114 else if (row >= getRowCount()) { 2115 r.y = getHeight(); 2116 valid = false; 2117 } 2118 else { 2119 r.height = getRowHeight(row); 2120 r.y = (rowModel == null) ? row * r.height : rowModel.getPosition(row); 2121 } 2122 2123 if (column < 0) { 2124 if( !getComponentOrientation().isLeftToRight() ) { 2125 r.x = getWidth(); 2126 } 2127 valid = false; 2129 } 2130 else if (column >= getColumnCount()) { 2131 if( getComponentOrientation().isLeftToRight() ) { 2132 r.x = getWidth(); 2133 } 2134 valid = false; 2136 } 2137 else { 2138 TableColumnModel cm = getColumnModel(); 2139 if( getComponentOrientation().isLeftToRight() ) { 2140 for(int i = 0; i < column; i++) { 2141 r.x += cm.getColumn(i).getWidth(); 2142 } 2143 } else { 2144 for(int i = cm.getColumnCount()-1; i > column; i--) { 2145 r.x += cm.getColumn(i).getWidth(); 2146 } 2147 } 2148 r.width = cm.getColumn(column).getWidth(); 2149 } 2150 2151 if (valid && !includeSpacing) { 2152 int rm = getRowMargin(); 2153 int cm = getColumnModel().getColumnMargin(); 2154 r.setBounds(r.x + cm/2, r.y + rm/2, r.width - cm, r.height - rm); 2156 } 2157 return r; 2158 } 2159 2160 private int viewIndexForColumn(TableColumn aColumn) { 2161 TableColumnModel cm = getColumnModel(); 2162 for (int column = 0; column < cm.getColumnCount(); column++) { 2163 if (cm.getColumn(column) == aColumn) { 2164 return column; 2165 } 2166 } 2167 return -1; 2168 } 2169 2170 2305 public void doLayout() { 2306 TableColumn resizingColumn = getResizingColumn(); 2307 if (resizingColumn == null) { 2308 setWidthsFromPreferredWidths(false); 2309 } 2310 else { 2311 2315 int columnIndex = viewIndexForColumn(resizingColumn); 2318 int delta = getWidth() - getColumnModel().getTotalColumnWidth(); 2319 accommodateDelta(columnIndex, delta); 2320 delta = getWidth() - getColumnModel().getTotalColumnWidth(); 2321 2322 if (delta != 0) { 2333 resizingColumn.setWidth(resizingColumn.getWidth() + delta); 2334 } 2335 2336 setWidthsFromPreferredWidths(true); 2343 } 2344 2345 super.doLayout(); 2346 } 2347 2348 private TableColumn getResizingColumn() { 2349 return (tableHeader == null) ? null 2350 : tableHeader.getResizingColumn(); 2351 } 2352 2353 2359 @Deprecated 2360 public void sizeColumnsToFit(boolean lastColumnOnly) { 2361 int oldAutoResizeMode = autoResizeMode; 2362 setAutoResizeMode(lastColumnOnly ? AUTO_RESIZE_LAST_COLUMN 2363 : AUTO_RESIZE_ALL_COLUMNS); 2364 sizeColumnsToFit(-1); 2365 setAutoResizeMode(oldAutoResizeMode); 2366 } 2367 2368 2375 public void sizeColumnsToFit(int resizingColumn) { 2376 if (resizingColumn == -1) { 2377 setWidthsFromPreferredWidths(false); 2378 } 2379 else { 2380 if (autoResizeMode == AUTO_RESIZE_OFF) { 2381 TableColumn aColumn = getColumnModel().getColumn(resizingColumn); 2382 aColumn.setPreferredWidth(aColumn.getWidth()); 2383 } 2384 else { 2385 int delta = getWidth() - getColumnModel().getTotalColumnWidth(); 2386 accommodateDelta(resizingColumn, delta); 2387 setWidthsFromPreferredWidths(true); 2388 } 2389 } 2390 } 2391 2392 private void setWidthsFromPreferredWidths(final boolean inverse) { 2393 int totalWidth = getWidth(); 2394 int totalPreferred = getPreferredSize().width; 2395 int target = !inverse ? totalWidth : totalPreferred; 2396 2397 final TableColumnModel cm = columnModel; 2398 Resizable3 r = new Resizable3() { 2399 public int getElementCount() { return cm.getColumnCount(); } 2400 public int getLowerBoundAt(int i) { return cm.getColumn(i).getMinWidth(); } 2401 public int getUpperBoundAt(int i) { return cm.getColumn(i).getMaxWidth(); } 2402 public int getMidPointAt(int i) { 2403 if (!inverse) { 2404 return cm.getColumn(i).getPreferredWidth(); 2405 } 2406 else { 2407 return cm.getColumn(i).getWidth(); 2408 } 2409 } 2410 public void setSizeAt(int s, int i) { 2411 if (!inverse) { 2412 cm.getColumn(i).setWidth(s); 2413 } 2414 else { 2415 cm.getColumn(i).setPreferredWidth(s); 2416 } 2417 } 2418 }; 2419 2420 adjustSizes(target, r, inverse); 2421 } 2422 2423 2424 private void accommodateDelta(int resizingColumnIndex, int delta) { 2426 int columnCount = getColumnCount(); 2427 int from = resizingColumnIndex; 2428 int to = columnCount; 2429 2430 switch(autoResizeMode) { 2432 case AUTO_RESIZE_NEXT_COLUMN: 2433 from = from + 1; 2434 to = Math.min(from + 1, columnCount); break; 2435 case AUTO_RESIZE_SUBSEQUENT_COLUMNS: 2436 from = from + 1; 2437 to = columnCount; break; 2438 case AUTO_RESIZE_LAST_COLUMN: 2439 from = columnCount - 1; 2440 to = from + 1; break; 2441 case AUTO_RESIZE_ALL_COLUMNS: 2442 from = 0; 2443 to = columnCount; break; 2444 default: 2445 return; 2446 } 2447 2448 final int start = from; 2449 final int end = to; 2450 final TableColumnModel cm = columnModel; 2451 Resizable3 r = new Resizable3() { 2452 public int getElementCount() { return end-start; } 2453 public int getLowerBoundAt(int i) { return cm.getColumn(i+start).getMinWidth(); } 2454 public int getUpperBoundAt(int i) { return cm.getColumn(i+start).getMaxWidth(); } 2455 public int getMidPointAt(int i) { return cm.getColumn(i+start).getWidth(); } 2456 public void setSizeAt(int s, int i) { cm.getColumn(i+start).setWidth(s); } 2457 }; 2458 2459 int totalWidth = 0; 2460 for(int i = from; i < to; i++) { 2461 TableColumn aColumn = columnModel.getColumn(i); 2462 int input = aColumn.getWidth(); 2463 totalWidth = totalWidth + input; 2464 } 2465 2466 adjustSizes(totalWidth + delta, r, false); 2467 2468 return; 2469 } 2470 2471 private interface Resizable2 { 2472 public int getElementCount(); 2473 public int getLowerBoundAt(int i); 2474 public int getUpperBoundAt(int i); 2475 public void setSizeAt(int newSize, int i); 2476 } 2477 2478 private interface Resizable3 extends Resizable2 { 2479 public int getMidPointAt(int i); 2480 } 2481 2482 2483 private void adjustSizes(long target, final Resizable3 r, boolean inverse) { 2484 int N = r.getElementCount(); 2485 long totalPreferred = 0; 2486 for(int i = 0; i < N; i++) { 2487 totalPreferred += r.getMidPointAt(i); 2488 } 2489 Resizable2 s; 2490 if ((target < totalPreferred) == !inverse) { 2491 s = new Resizable2() { 2492 public int getElementCount() { return r.getElementCount(); } 2493 public int getLowerBoundAt(int i) { return r.getLowerBoundAt(i); } 2494 public int getUpperBoundAt(int i) { return r.getMidPointAt(i); } 2495 public void setSizeAt(int newSize, int i) { r.setSizeAt(newSize, i); } 2496 2497 }; 2498 } 2499 else { 2500 s = new Resizable2() { 2501 public int getElementCount() { return r.getElementCount(); } 2502 public int getLowerBoundAt(int i) { return r.getMidPointAt(i); } 2503 public int getUpperBoundAt(int i) { return r.getUpperBoundAt(i); } 2504 public void setSizeAt(int newSize, int i) { r.setSizeAt(newSize, i); } 2505 2506 }; 2507 } 2508 adjustSizes(target, s, !inverse); 2509 } 2510 2511 private void adjustSizes(long target, Resizable2 r, boolean limitToRange) { 2512 long totalLowerBound = 0; 2513 long totalUpperBound = 0; 2514 for(int i = 0; i < r.getElementCount(); i++) { 2515 totalLowerBound += r.getLowerBoundAt(i); 2516 totalUpperBound += r.getUpperBoundAt(i); 2517 } 2518 2519 if (limitToRange) { 2520 target = Math.min(Math.max(totalLowerBound, target), totalUpperBound); 2521 } 2522 2523 for(int i = 0; i < r.getElementCount(); i++) { 2524 int lowerBound = r.getLowerBoundAt(i); 2525 int upperBound = r.getUpperBoundAt(i); 2526 int newSize; 2530 if (totalLowerBound == totalUpperBound) { 2531 newSize = lowerBound; 2532 } 2533 else { 2534 double f = (double)(target - totalLowerBound)/(totalUpperBound - totalLowerBound); 2535 newSize = (int)Math.round(lowerBound+f*(upperBound - lowerBound)); 2536 } 2540 r.setSizeAt(newSize, i); 2541 target -= newSize; 2542 totalLowerBound -= lowerBound; 2543 totalUpperBound -= upperBound; 2544 } 2545 } 2546 2547 2563 public String getToolTipText(MouseEvent event) { 2564 String tip = null; 2565 Point p = event.getPoint(); 2566 2567 int hitColumnIndex = columnAtPoint(p); 2569 int hitRowIndex = rowAtPoint(p); 2570 2571 if ((hitColumnIndex != -1) && (hitRowIndex != -1)) { 2572 TableCellRenderer renderer = getCellRenderer(hitRowIndex, hitColumnIndex); 2573 Component component = prepareRenderer(renderer, hitRowIndex, hitColumnIndex); 2574 2575 if (component instanceof JComponent ) { 2578 Rectangle cellRect = getCellRect(hitRowIndex, hitColumnIndex, false); 2580 p.translate(-cellRect.x, -cellRect.y); 2581 MouseEvent newEvent = new MouseEvent(component, event.getID(), 2582 event.getWhen(), event.getModifiers(), 2583 p.x, p.y, event.getClickCount(), 2584 event.isPopupTrigger()); 2585 2586 tip = ((JComponent )component).getToolTipText(newEvent); 2587 } 2588 } 2589 2590 if (tip == null) 2592 tip = getToolTipText(); 2593 2594 return tip; 2595 } 2596 2597 2601 2615 public void setSurrendersFocusOnKeystroke(boolean surrendersFocusOnKeystroke) { 2616 this.surrendersFocusOnKeystroke = surrendersFocusOnKeystroke; 2617 } 2618 2619 2629 public boolean getSurrendersFocusOnKeystroke() { 2630 return surrendersFocusOnKeystroke; 2631 } 2632 2633 2645 public boolean editCellAt(int row, int column) { 2646 return editCellAt(row, column, null); 2647 } 2648 2649 2666 public boolean editCellAt(int row, int column, EventObject e){ 2667 if (cellEditor != null && !cellEditor.stopCellEditing()) { 2668 return false; 2669 } 2670 2671 if (row < 0 || row >= getRowCount() || 2672 column < 0 || column >= getColumnCount()) { 2673 return false; 2674 } 2675 2676 if (!isCellEditable(row, column)) 2677 return false; 2678 2679 if (editorRemover == null) { 2680 KeyboardFocusManager fm = 2681 KeyboardFocusManager.getCurrentKeyboardFocusManager(); 2682 editorRemover = new CellEditorRemover(fm); 2683 fm.addPropertyChangeListener("permanentFocusOwner", editorRemover); 2684 } 2685 2686 TableCellEditor editor = getCellEditor(row, column); 2687 if (editor != null && editor.isCellEditable(e)) { 2688 editorComp = prepareEditor(editor, row, column); 2689 if (editorComp == null) { 2690 removeEditor(); 2691 return false; 2692 } 2693 editorComp.setBounds(getCellRect(row, column, false)); 2694 add(editorComp); 2695 editorComp.validate(); 2696 2697 setCellEditor(editor); 2698 setEditingRow(row); 2699 setEditingColumn(column); 2700 editor.addCellEditorListener(this); 2701 2702 return true; 2703 } 2704 return false; 2705 } 2706 2707 2714 public boolean isEditing() { 2715 return (cellEditor == null)? false : true; 2716 } 2717 2718 2724 public Component getEditorComponent() { 2725 return editorComp; 2726 } 2727 2728 2736 public int getEditingColumn() { 2737 return editingColumn; 2738 } 2739 2740 2748 public int getEditingRow() { 2749 return editingRow; 2750 } 2751 2752 2756 2761 public TableUI getUI() { 2762 return (TableUI)ui; 2763 } 2764 2765 2776 public void setUI(TableUI ui) { 2777 if (this.ui != ui) { 2778 super.setUI(ui); 2779 repaint(); 2780 } 2781 } 2782 2783 private void updateSubComponentUI(Object componentShell) { 2784 if (componentShell == null) { 2785 return; 2786 } 2787 Component component = null; 2788 if (componentShell instanceof Component) { 2789 component = (Component)componentShell; 2790 } 2791 if (componentShell instanceof DefaultCellEditor ) { 2792 component = ((DefaultCellEditor )componentShell).getComponent(); 2793 } 2794 2795 if (component != null && component instanceof JComponent ) { 2796 ((JComponent )component).updateUI(); 2797 } 2798 } 2799 2800 2807 public void updateUI() { 2808 TableColumnModel cm = getColumnModel(); 2810 for(int column = 0; column < cm.getColumnCount(); column++) { 2811 TableColumn aColumn = cm.getColumn(column); 2812 updateSubComponentUI(aColumn.getCellRenderer()); 2813 updateSubComponentUI(aColumn.getCellEditor()); 2814 updateSubComponentUI(aColumn.getHeaderRenderer()); 2815 } 2816 2817 Enumeration defaultRenderers = defaultRenderersByColumnClass.elements(); 2819 while (defaultRenderers.hasMoreElements()) { 2820 updateSubComponentUI(defaultRenderers.nextElement()); 2821 } 2822 2823 Enumeration defaultEditors = defaultEditorsByColumnClass.elements(); 2825 while (defaultEditors.hasMoreElements()) { 2826 updateSubComponentUI(defaultEditors.nextElement()); 2827 } 2828 2829 if (tableHeader != null && tableHeader.getParent() == null) { 2831 tableHeader.updateUI(); 2832 } 2833 2834 setUI((TableUI)UIManager.getUI(this)); 2835 resizeAndRepaint(); 2836 } 2837 2838 2846 public String getUIClassID() { 2847 return uiClassID; 2848 } 2849 2850 2851 2855 2866 public void setModel(TableModel dataModel) { 2867 if (dataModel == null) { 2868 throw new IllegalArgumentException ("Cannot set a null TableModel"); 2869 } 2870 if (this.dataModel != dataModel) { 2871 TableModel old = this.dataModel; 2872 if (old != null) { 2873 old.removeTableModelListener(this); 2874 } 2875 this.dataModel = dataModel; 2876 dataModel.addTableModelListener(this); 2877 2878 tableChanged(new TableModelEvent(dataModel, TableModelEvent.HEADER_ROW)); 2879 2880 firePropertyChange("model", old, dataModel); 2881 } 2882 } 2883 2884 2891 public TableModel getModel() { 2892 return dataModel; 2893 } 2894 2895 2907 public void setColumnModel(TableColumnModel columnModel) { 2908 if (columnModel == null) { 2909 throw new IllegalArgumentException ("Cannot set a null ColumnModel"); 2910 } 2911 TableColumnModel old = this.columnModel; 2912 if (columnModel != old) { 2913 if (old != null) { 2914 old.removeColumnModelListener(this); 2915 } 2916 this.columnModel = columnModel; 2917 columnModel.addColumnModelListener(this); 2918 2919 if (tableHeader != null) { 2921 tableHeader.setColumnModel(columnModel); 2922 } 2923 2924 firePropertyChange("columnModel", old, columnModel); 2925 resizeAndRepaint(); 2926 } 2927 } 2928 2929 2936 public TableColumnModel getColumnModel() { 2937 return columnModel; 2938 } 2939 2940 2951 public void setSelectionModel(ListSelectionModel newModel) { 2952 if (newModel == null) { 2953 throw new IllegalArgumentException ("Cannot set a null SelectionModel"); 2954 } 2955 2956 ListSelectionModel oldModel = selectionModel; 2957 2958 if (newModel != oldModel) { 2959 if (oldModel != null) { 2960 oldModel.removeListSelectionListener(this); 2961 } 2962 2963 selectionModel = newModel; 2964 newModel.addListSelectionListener(this); 2965 2966 firePropertyChange("selectionModel", oldModel, newModel); 2967 repaint(); 2968 } 2969 } 2970 2971 2979 public ListSelectionModel getSelectionModel() { 2980 return selectionModel; 2981 } 2982 2983 2987 3000 public void tableChanged(TableModelEvent e) { 3001 if (e == null || e.getFirstRow() == TableModelEvent.HEADER_ROW) { 3002 clearSelectionAndLeadAnchor(); 3004 3005 rowModel = null; 3006 3007 if (getAutoCreateColumnsFromModel()) { 3008 createDefaultColumnsFromModel(); 3010 return; 3011 } 3012 3013 resizeAndRepaint(); 3014 return; 3015 } 3016 3017 if (rowModel != null) { 3021 repaint(); 3022 } 3023 3024 if (e.getType() == TableModelEvent.INSERT) { 3025 tableRowsInserted(e); 3026 return; 3027 } 3028 3029 if (e.getType() == TableModelEvent.DELETE) { 3030 tableRowsDeleted(e); 3031 return; 3032 } 3033 3034 int modelColumn = e.getColumn(); 3035 int start = e.getFirstRow(); 3036 int end = e.getLastRow(); 3037 3038 Rectangle dirtyRegion; 3039 if (modelColumn == TableModelEvent.ALL_COLUMNS) { 3040 dirtyRegion = new Rectangle(0, start * getRowHeight(), 3042 getColumnModel().getTotalColumnWidth(), 0); 3043 } 3044 else { 3045 int column = convertColumnIndexToView(modelColumn); 3051 dirtyRegion = getCellRect(start, column, false); 3052 } 3053 3054 if (end != Integer.MAX_VALUE) { 3057 dirtyRegion.height = (end-start+1)*getRowHeight(); 3058 repaint(dirtyRegion.x, dirtyRegion.y, dirtyRegion.width, dirtyRegion.height); 3059 } 3060 else { 3063 clearSelectionAndLeadAnchor(); 3064 resizeAndRepaint(); 3065 rowModel = null; 3066 } 3067 } 3068 3069 3077 private void tableRowsInserted(TableModelEvent e) { 3078 int start = e.getFirstRow(); 3079 int end = e.getLastRow(); 3080 if (start < 0) { 3081 start = 0; 3082 } 3083 if (end < 0) { 3084 end = getRowCount()-1; 3085 } 3086 3087 int length = end - start + 1; 3089 selectionModel.insertIndexInterval(start, length, true); 3090 3091 if (rowModel != null) { 3093 rowModel.insertEntries(start, length, getRowHeight()); 3094 } 3095 int rh = getRowHeight() ; 3096 Rectangle drawRect = new Rectangle(0, start * rh, 3097 getColumnModel().getTotalColumnWidth(), 3098 (getRowCount()-start) * rh); 3099 3100 revalidate(); 3101 repaint(drawRect); 3104 } 3105 3106 3114 private void tableRowsDeleted(TableModelEvent e) { 3115 int start = e.getFirstRow(); 3116 int end = e.getLastRow(); 3117 if (start < 0) { 3118 start = 0; 3119 } 3120 if (end < 0) { 3121 end = getRowCount()-1; 3122 } 3123 3124 int deletedCount = end - start + 1; 3125 int previousRowCount = getRowCount() + deletedCount; 3126 selectionModel.removeIndexInterval(start, end); 3128 3129 if (rowModel != null) { 3131 rowModel.removeEntries(start, deletedCount); 3132 } 3133 3134 int rh = getRowHeight(); 3135 Rectangle drawRect = new Rectangle(0, start * rh, 3136 getColumnModel().getTotalColumnWidth(), 3137 (previousRowCount - start) * rh); 3138 3139 revalidate(); 3140 repaint(drawRect); 3143 } 3144 3145 3149 3157 public void columnAdded(TableColumnModelEvent e) { 3158 if (isEditing()) { 3160 removeEditor(); 3161 } 3162 resizeAndRepaint(); 3163 } 3164 3165 3173 public void columnRemoved(TableColumnModelEvent e) { 3174 if (isEditing()) { 3176 removeEditor(); 3177 } 3178 resizeAndRepaint(); 3179 } 3180 3181 3191 public void columnMoved(TableColumnModelEvent e) { 3192 if (isEditing()) { 3194 removeEditor(); 3195 } 3196 repaint(); 3197 } 3198 3199 3210 public void columnMarginChanged(ChangeEvent e) { 3211 if (isEditing()) { 3212 removeEditor(); 3213 } 3214 TableColumn resizingColumn = getResizingColumn(); 3215 if (resizingColumn != null && autoResizeMode == AUTO_RESIZE_OFF) { 3218 resizingColumn.setPreferredWidth(resizingColumn.getWidth()); 3219 } 3220 resizeAndRepaint(); 3221 } 3222 3223 private int limit(int i, int a, int b) { 3224 return Math.min(b, Math.max(i, a)); 3225 } 3226 3227 3237 public void columnSelectionChanged(ListSelectionEvent e) { 3238 boolean isAdjusting = e.getValueIsAdjusting(); 3239 if (columnSelectionAdjusting && !isAdjusting) { 3240 columnSelectionAdjusting = false; 3244 return; 3245 } 3246 columnSelectionAdjusting = isAdjusting; 3247 if (getRowCount() <= 0 || getColumnCount() <= 0) { 3249 return; 3250 } 3251 int firstIndex = limit(e.getFirstIndex(), 0, getColumnCount()-1); 3252 int lastIndex = limit(e.getLastIndex(), 0, getColumnCount()-1); 3253 int minRow = 0; 3254 int maxRow = getRowCount() - 1; 3255 if (getRowSelectionAllowed()) { 3256 minRow = selectionModel.getMinSelectionIndex(); 3257 maxRow = selectionModel.getMaxSelectionIndex(); 3258 int leadRow = getAdjustedIndex(selectionModel.getLeadSelectionIndex(), true); 3259 3260 if (minRow == -1 || maxRow == -1) { 3261 if (leadRow == -1) { 3262 return; 3264 } 3265 3266 minRow = maxRow = leadRow; 3268 } else { 3269 if (leadRow != -1) { 3273 minRow = Math.min(minRow, leadRow); 3274 maxRow = Math.max(maxRow, leadRow); 3275 } 3276 } 3277 } 3278 Rectangle firstColumnRect = getCellRect(minRow, firstIndex, false); 3279 Rectangle lastColumnRect = getCellRect(maxRow, lastIndex, false); 3280 Rectangle dirtyRegion = firstColumnRect.union(lastColumnRect); 3281 repaint(dirtyRegion); 3282 } 3283 3284 3288 3298 public void valueChanged(ListSelectionEvent e) { 3299 boolean isAdjusting = e.getValueIsAdjusting(); 3300 if (rowSelectionAdjusting && !isAdjusting) { 3301 rowSelectionAdjusting = false; 3305 return; 3306 } 3307 rowSelectionAdjusting = isAdjusting; 3308 if (getRowCount() <= 0 || getColumnCount() <= 0) { 3310 return; 3311 } 3312 int firstIndex = limit(e.getFirstIndex(), 0, getRowCount()-1); 3313 int lastIndex = limit(e.getLastIndex(), 0, getRowCount()-1); 3314 Rectangle firstRowRect = getCellRect(firstIndex, 0, false); 3315 Rectangle lastRowRect = getCellRect(lastIndex, getColumnCount()-1, false); 3316 Rectangle dirtyRegion = firstRowRect.union(lastRowRect); 3317 repaint(dirtyRegion); 3318 } 3319 3320 3324 3334 public void editingStopped(ChangeEvent e) { 3335 TableCellEditor editor = getCellEditor(); 3337 if (editor != null) { 3338 Object value = editor.getCellEditorValue(); 3339 setValueAt(value, editingRow, editingColumn); 3340 removeEditor(); 3341 } 3342 } 3343 3344 3354 public void editingCanceled(ChangeEvent e) { 3355 removeEditor(); 3356 } 3357 3358 3362 3371 public void setPreferredScrollableViewportSize(Dimension size) { 3372 preferredViewportSize = size; 3373 } 3374 3375 3382 public Dimension getPreferredScrollableViewportSize() { 3383 return preferredViewportSize; 3384 } 3385 3386 3400 public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, 3401 int direction) { 3402 if (orientation == SwingConstants.HORIZONTAL) { 3404 return 100; 3405 } 3406 return getRowHeight(); 3407 } 3408 3409 3422 public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, 3423 int direction) { 3424 if (orientation == SwingConstants.VERTICAL) { 3425 int rh = getRowHeight(); 3426 return (rh > 0) ? Math.max(rh, (visibleRect.height / rh) * rh) : visibleRect.height; 3427 } 3428 else { 3429 return visibleRect.width; 3430 } 3431 } 3432 3433 3443 public boolean getScrollableTracksViewportWidth() { 3444 return !(autoResizeMode == AUTO_RESIZE_OFF); 3445 } 3446 3447 3454 public boolean getScrollableTracksViewportHeight() { 3455 return false; 3456 } 3457 3458 3462 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, 3463 int condition, boolean pressed) { 3464 boolean retValue = super.processKeyBinding(ks, e, condition, pressed); 3465 3466 if (!retValue && condition == WHEN_ANCESTOR_OF_FOCUSED_COMPONENT && 3469 isFocusOwner() && 3470 !Boolean.FALSE.equals((Boolean )getClientProperty("JTable.autoStartsEdit"))) { 3471 Component editorComponent = getEditorComponent(); 3473 if (editorComponent == null) { 3474 if (e == null || e.getID() != KeyEvent.KEY_PRESSED) { 3476 return false; 3477 } 3478 int code = e.getKeyCode(); 3480 if (code == KeyEvent.VK_SHIFT || code == KeyEvent.VK_CONTROL || 3481 code == KeyEvent.VK_ALT) { 3482 return false; 3483 } 3484 int leadRow = getSelectionModel().getLeadSelectionIndex(); 3486 int leadColumn = getColumnModel().getSelectionModel(). 3487 getLeadSelectionIndex(); 3488 if (leadRow != -1 && leadColumn != -1 && !isEditing()) { 3489 if (!editCellAt(leadRow, leadColumn)) { 3490 return false; 3491 } 3492 } 3493 editorComponent = getEditorComponent(); 3494 if (editorComponent == null) { 3495 return false; 3496 } 3497 } 3498 if (editorComponent instanceof JComponent ) { 3500 retValue = ((JComponent )editorComponent).processKeyBinding 3501 (ks, e, WHEN_FOCUSED, pressed); 3502 if (getSurrendersFocusOnKeystroke()) { 3506 editorComponent.requestFocus(); 3507 } 3508 } 3509 } 3510 return retValue; 3511 } 3512 3513 private void setLazyValue(Hashtable h, Class c, String s) { 3514 h.put(c, new UIDefaults.ProxyLazyValue (s)); 3515 } 3516 3517 private void setLazyRenderer(Class c, String s) { 3518 setLazyValue(defaultRenderersByColumnClass, c, s); 3519 } 3520 3521 3527 protected void createDefaultRenderers() { 3528 defaultRenderersByColumnClass = new UIDefaults (); 3529 3530 setLazyRenderer(Object .class, "javax.swing.table.DefaultTableCellRenderer$UIResource"); 3532 3533 setLazyRenderer(Number .class, "javax.swing.JTable$NumberRenderer"); 3535 3536 setLazyRenderer(Float .class, "javax.swing.JTable$DoubleRenderer"); 3538 setLazyRenderer(Double .class, "javax.swing.JTable$DoubleRenderer"); 3539 3540 setLazyRenderer(Date.class, "javax.swing.JTable$DateRenderer"); 3542 3543 setLazyRenderer(Icon .class, "javax.swing.JTable$IconRenderer"); 3545 setLazyRenderer(ImageIcon .class, "javax.swing.JTable$IconRenderer"); 3546 3547 setLazyRenderer(Boolean .class, "javax.swing.JTable$BooleanRenderer"); 3549 } 3550 3551 3554 static class NumberRenderer extends DefaultTableCellRenderer.UIResource { 3555 public NumberRenderer() { 3556 super(); 3557 setHorizontalAlignment(JLabel.RIGHT); 3558 } 3559 } 3560 3561 static class DoubleRenderer extends NumberRenderer { 3562 NumberFormat formatter; 3563 public DoubleRenderer() { super(); } 3564 3565 public void setValue(Object value) { 3566 if (formatter == null) { 3567 formatter = NumberFormat.getInstance(); 3568 } 3569 setText((value == null) ? "" : formatter.format(value)); 3570 } 3571 } 3572 3573 static class DateRenderer extends DefaultTableCellRenderer.UIResource { 3574 DateFormat formatter; 3575 public DateRenderer() { super(); } 3576 3577 public void setValue(Object value) { 3578 if (formatter==null) { 3579 formatter = DateFormat.getDateInstance(); 3580 } 3581 setText((value == null) ? "" : formatter.format(value)); 3582 } 3583 } 3584 3585 static class IconRenderer extends DefaultTableCellRenderer.UIResource { 3586 public IconRenderer() { 3587 super(); 3588 setHorizontalAlignment(JLabel.CENTER); 3589 } 3590 public void setValue(Object value) { setIcon((value instanceof Icon ) ? (Icon )value : null); } 3591 } 3592 3593 3594 static class BooleanRenderer extends JCheckBox implements TableCellRenderer, UIResource 3595 { 3596 private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 3597 3598 public BooleanRenderer() { 3599 super(); 3600 setHorizontalAlignment(JLabel.CENTER); 3601 setBorderPainted(true); 3602 } 3603 3604 public Component getTableCellRendererComponent(JTable table, Object value, 3605 boolean isSelected, boolean hasFocus, int row, int column) { 3606 if (isSelected) { 3607 setForeground(table.getSelectionForeground()); 3608 super.setBackground(table.getSelectionBackground()); 3609 } 3610 else { 3611 setForeground(table.getForeground()); 3612 setBackground(table.getBackground()); 3613 } 3614 setSelected((value != null && ((Boolean )value).booleanValue())); 3615 3616 if (hasFocus) { 3617 setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); 3618 } else { 3619 setBorder(noFocusBorder); 3620 } 3621 3622 return this; 3623 } 3624 } 3625 3626 private void setLazyEditor(Class c, String s) { 3627 setLazyValue(defaultEditorsByColumnClass, c, s); 3628 } 3629 3630 3634 protected void createDefaultEditors() { 3635 defaultEditorsByColumnClass = new UIDefaults (); 3636 3637 setLazyEditor(Object .class, "javax.swing.JTable$GenericEditor"); 3639 3640 setLazyEditor(Number .class, "javax.swing.JTable$NumberEditor"); 3642 3643 setLazyEditor(Boolean .class, "javax.swing.JTable$BooleanEditor"); 3645 } 3646 3647 3650 static class GenericEditor extends DefaultCellEditor { 3651 3652 Class [] argTypes = new Class []{String .class}; 3653 java.lang.reflect.Constructor constructor; 3654 Object value; 3655 3656 public GenericEditor() { 3657 super(new JTextField ()); 3658 getComponent().setName("Table.editor"); 3659 } 3660 3661 public boolean stopCellEditing() { 3662 String s = (String )super.getCellEditorValue(); 3663 if ("".equals(s)) { 3670 if (constructor.getDeclaringClass() == String .class) { 3671 value = s; 3672 } 3673 super.stopCellEditing(); 3674 } 3675 3676 try { 3677 value = constructor.newInstance(new Object []{s}); 3678 } 3679 catch (Exception e) { 3680 ((JComponent )getComponent()).setBorder(new LineBorder(Color.red)); 3681 return false; 3682 } 3683 return super.stopCellEditing(); 3684 } 3685 3686 public Component getTableCellEditorComponent(JTable table, Object value, 3687 boolean isSelected, 3688 int row, int column) { 3689 this.value = null; 3690 ((JComponent )getComponent()).setBorder(new LineBorder(Color.black)); 3691 try { 3692 Class type = table.getColumnClass(column); 3693 if (type == Object .class) { 3698 type = String .class; 3699 } 3700 constructor = type.getConstructor(argTypes); 3701 } 3702 catch (Exception e) { 3703 return null; 3704 } 3705 return super.getTableCellEditorComponent(table, value, isSelected, row, column); 3706 } 3707 3708 public Object getCellEditorValue() { 3709 return value; 3710 } 3711 } 3712 3713 static class NumberEditor extends GenericEditor { 3714 3715 public NumberEditor() { 3716 ((JTextField )getComponent()).setHorizontalAlignment(JTextField.RIGHT); 3717 } 3718 } 3719 3720 static class BooleanEditor extends DefaultCellEditor { 3721 public BooleanEditor() { 3722 super(new JCheckBox ()); 3723 JCheckBox checkBox = (JCheckBox )getComponent(); 3724 checkBox.setHorizontalAlignment(JCheckBox.CENTER); 3725 } 3726 } 3727 3728 3731 protected void initializeLocalVars() { 3732 setOpaque(true); 3733 createDefaultRenderers(); 3734 createDefaultEditors(); 3735 3736 setTableHeader(createDefaultTableHeader()); 3737 3738 setShowGrid(true); 3739 setAutoResizeMode(AUTO_RESIZE_SUBSEQUENT_COLUMNS); 3740 setRowHeight(16); 3741 isRowHeightSet = false; 3742 setRowMargin(1); 3743 setRowSelectionAllowed(true); 3744 setCellEditor(null); 3745 setEditingColumn(-1); 3746 setEditingRow(-1); 3747 setSurrendersFocusOnKeystroke(false); 3748 setPreferredScrollableViewportSize(new Dimension(450, 400)); 3749 3750 ToolTipManager toolTipManager = ToolTipManager.sharedInstance(); 3752 toolTipManager.registerComponent(this); 3753 3754 setAutoscrolls(true); 3755 } 3756 3757 3765 protected TableModel createDefaultDataModel() { 3766 return new DefaultTableModel(); 3767 } 3768 3769 3777 protected TableColumnModel createDefaultColumnModel() { 3778 return new DefaultTableColumnModel(); 3779 } 3780 3781 3789 protected ListSelectionModel createDefaultSelectionModel() { 3790 return new DefaultListSelectionModel (); 3791 } 3792 3793 3801 protected JTableHeader createDefaultTableHeader() { 3802 return new JTableHeader(columnModel); 3803 } 3804 3805 3808 protected void resizeAndRepaint() { 3809 revalidate(); 3810 repaint(); 3811 } 3812 3813 3822 public TableCellEditor getCellEditor() { 3823 return cellEditor; 3824 } 3825 3826 3835 public void setCellEditor(TableCellEditor anEditor) { 3836 TableCellEditor oldEditor = cellEditor; 3837 cellEditor = anEditor; 3838 firePropertyChange("tableCellEditor", oldEditor, anEditor); 3839 } 3840 3841 3847 public void setEditingColumn(int aColumn) { 3848 editingColumn = aColumn; 3849 } 3850 3851 3857 public void setEditingRow(int aRow) { 3858 editingRow = aRow; 3859 } 3860 3861 3883 public TableCellRenderer getCellRenderer(int row, int column) { 3884 TableColumn tableColumn = getColumnModel().getColumn(column); 3885 TableCellRenderer renderer = tableColumn.getCellRenderer(); 3886 if (renderer == null) { 3887 renderer = getDefaultRenderer(getColumnClass(column)); 3888 } 3889 return renderer; 3890 } 3891 3892 3910 public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 3911 Object value = getValueAt(row, column); 3912 3913 boolean isSelected = false; 3914 boolean hasFocus = false; 3915 3916 if (!isPrinting) { 3918 isSelected = isCellSelected(row, column); 3919 3920 boolean rowIsLead = 3921 (selectionModel.getLeadSelectionIndex() == row); 3922 boolean colIsLead = 3923 (columnModel.getSelectionModel().getLeadSelectionIndex() == column); 3924 3925 hasFocus = (rowIsLead && colIsLead) && isFocusOwner(); 3926 } 3927 3928 return renderer.getTableCellRendererComponent(this, value, 3929 isSelected, hasFocus, 3930 row, column); 3931 } 3932 3933 3954 public TableCellEditor getCellEditor(int row, int column) { 3955 TableColumn tableColumn = getColumnModel().getColumn(column); 3956 TableCellEditor editor = tableColumn.getCellEditor(); 3957 if (editor == null) { 3958 editor = getDefaultEditor(getColumnClass(column)); 3959 } 3960 return editor; 3961 } 3962 3963 3964 3980 public Component prepareEditor(TableCellEditor editor, int row, int column) { 3981 Object value = getValueAt(row, column); 3982 boolean isSelected = isCellSelected(row, column); 3983 Component comp = editor.getTableCellEditorComponent(this, value, isSelected, 3984 row, column); 3985 if (comp instanceof JComponent ) { 3986 JComponent jComp = (JComponent )comp; 3987 if (jComp.getNextFocusableComponent() == null) { 3988 jComp.setNextFocusableComponent(this); 3989 } 3990 } 3991 return comp; 3992 } 3993 3994 3998 public void removeEditor() { 3999 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 4000 removePropertyChangeListener("permanentFocusOwner", editorRemover); 4001 editorRemover = null; 4002 4003 TableCellEditor editor = getCellEditor(); 4004 if(editor != null) { 4005 editor.removeCellEditorListener(this); 4006 4007 if (editorComp != null) { 4008 remove(editorComp); 4009 } 4010 4011 Rectangle cellRect = getCellRect(editingRow, editingColumn, false); 4012 4013 setCellEditor(null); 4014 setEditingColumn(-1); 4015 setEditingRow(-1); 4016 editorComp = null; 4017 4018 repaint(cellRect); 4019 } 4020 } 4021 4022 4026 4030 private void writeObject(ObjectOutputStream s) throws IOException { 4031 s.defaultWriteObject(); 4032 if (getUIClassID().equals(uiClassID)) { 4033 byte count = JComponent.getWriteObjCounter(this); 4034 JComponent.setWriteObjCounter(this, --count); 4035 if (count == 0 && ui != null) { 4036 ui.installUI(this); 4037 } 4038 } 4039 } 4040 4041 private void readObject(ObjectInputStream s) 4042 throws IOException , ClassNotFoundException 4043 { 4044 s.defaultReadObject(); 4045 if ((ui != null) && (getUIClassID().equals(uiClassID))) { 4046 ui.installUI(this); 4047 } 4048 createDefaultRenderers(); 4049 createDefaultEditors(); 4050 4051 if (getToolTipText() == null) { 4055 ToolTipManager.sharedInstance().registerComponent(this); 4056 } 4057 } 4058 4059 4062 void compWriteObjectNotify() { 4063 super.compWriteObjectNotify(); 4064 if (getToolTipText() == null) { 4067 ToolTipManager.sharedInstance().unregisterComponent(this); 4068 } 4069 } 4070 4071 4080 protected String paramString() { 4081 String gridColorString = (gridColor != null ? 4082 gridColor.toString() : ""); 4083 String showHorizontalLinesString = (showHorizontalLines ? 4084 "true" : "false"); 4085 String showVerticalLinesString = (showVerticalLines ? 4086 "true" : "false"); 4087 String autoResizeModeString; 4088 if (autoResizeMode == AUTO_RESIZE_OFF) { 4089 autoResizeModeString = "AUTO_RESIZE_OFF"; 4090 } else if (autoResizeMode == AUTO_RESIZE_NEXT_COLUMN) { 4091 autoResizeModeString = "AUTO_RESIZE_NEXT_COLUMN"; 4092 } else if (autoResizeMode == AUTO_RESIZE_SUBSEQUENT_COLUMNS) { 4093 autoResizeModeString = "AUTO_RESIZE_SUBSEQUENT_COLUMNS"; 4094 } else if (autoResizeMode == AUTO_RESIZE_LAST_COLUMN) { 4095 autoResizeModeString = "AUTO_RESIZE_LAST_COLUMN"; 4096 } else if (autoResizeMode == AUTO_RESIZE_ALL_COLUMNS) { 4097 autoResizeModeString = "AUTO_RESIZE_ALL_COLUMNS"; 4098 } else autoResizeModeString = ""; 4099 String autoCreateColumnsFromModelString = (autoCreateColumnsFromModel ? 4100 "true" : "false"); 4101 String preferredViewportSizeString = (preferredViewportSize != null ? 4102 preferredViewportSize.toString() 4103 : ""); 4104 String rowSelectionAllowedString = (rowSelectionAllowed ? 4105 "true" : "false"); 4106 String cellSelectionEnabledString = (cellSelectionEnabled ? 4107 "true" : "false"); 4108 String selectionForegroundString = (selectionForeground != null ? 4109 selectionForeground.toString() : 4110 ""); 4111 String selectionBackgroundString = (selectionBackground != null ? 4112 selectionBackground.toString() : 4113 ""); 4114 4115 return super.paramString() + 4116 ",autoCreateColumnsFromModel=" + autoCreateColumnsFromModelString + 4117 ",autoResizeMode=" + autoResizeModeString + 4118 ",cellSelectionEnabled=" + cellSelectionEnabledString + 4119 ",editingColumn=" + editingColumn + 4120 ",editingRow=" + editingRow + 4121 ",gridColor=" + gridColorString + 4122 ",preferredViewportSize=" + preferredViewportSizeString + 4123 ",rowHeight=" + rowHeight + 4124 ",rowMargin=" + rowMargin + 4125 ",rowSelectionAllowed=" + rowSelectionAllowedString + 4126 ",selectionBackground=" + selectionBackgroundString + 4127 ",selectionForeground=" + selectionForegroundString + 4128 ",showHorizontalLines=" + showHorizontalLinesString + 4129 ",showVerticalLines=" + showVerticalLinesString; 4130 } 4131 4132 class CellEditorRemover implements PropertyChangeListener { 4137 KeyboardFocusManager focusManager; 4138 4139 public CellEditorRemover(KeyboardFocusManager fm) { 4140 this.focusManager = fm; 4141 } 4142 4143 public void propertyChange(PropertyChangeEvent ev) { 4144 if (!isEditing() || getClientProperty("terminateEditOnFocusLost") != Boolean.TRUE) { 4145 return; 4146 } 4147 4148 Component c = focusManager.getPermanentFocusOwner(); 4149 while (c != null) { 4150 if (c == JTable.this) { 4151 return; 4153 } else if ((c instanceof Window) || 4154 (c instanceof Applet && c.getParent() == null)) { 4155 if (c == SwingUtilities.getRoot(JTable.this)) { 4156 if (!getCellEditor().stopCellEditing()) { 4157 getCellEditor().cancelCellEditing(); 4158 } 4159 } 4160 break; 4161 } 4162 c = c.getParent(); 4163 } 4164 } 4165 } 4166 4167 4171 4188 public boolean print() throws PrinterException { 4189 4190 return print(PrintMode.FIT_WIDTH); 4191 } 4192 4193 4211 public boolean print(PrintMode printMode) throws PrinterException { 4212 4213 return print(printMode, null, null); 4214 } 4215 4216 4240 public boolean print(PrintMode printMode, 4241 MessageFormat headerFormat, 4242 MessageFormat footerFormat) throws PrinterException { 4243 4244 boolean showDialogs = !GraphicsEnvironment.isHeadless(); 4245 return print(printMode, headerFormat, footerFormat, 4246 showDialogs, null, showDialogs); 4247 } 4248 4249 4322 public boolean print(PrintMode printMode, 4323 MessageFormat headerFormat, 4324 MessageFormat footerFormat, 4325 boolean showPrintDialog, 4326 PrintRequestAttributeSet attr, 4327 boolean interactive) throws PrinterException, 4328 HeadlessException { 4329 4330 boolean isHeadless = GraphicsEnvironment.isHeadless(); 4332 if (isHeadless) { 4333 if (showPrintDialog) { 4334 throw new HeadlessException("Can't show print dialog."); 4335 } 4336 4337 if (interactive) { 4338 throw new HeadlessException("Can't run interactively."); 4339 } 4340 } 4341 4342 if (isEditing()) { 4343 if (!getCellEditor().stopCellEditing()) { 4345 getCellEditor().cancelCellEditing(); 4346 } 4347 } 4348 4349 if (attr == null) { 4350 attr = new HashPrintRequestAttributeSet(); 4351 } 4352 4353 final PrinterJob job = PrinterJob.getPrinterJob(); 4355 4356 Printable printable = 4358 getPrintable(printMode, headerFormat, footerFormat); 4359 4360 if (interactive) { 4361 printable = new ThreadSafePrintable(printable); 4363 } 4364 4365 job.setPrintable(printable); 4367 4368 if (showPrintDialog && !job.printDialog(attr)) { 4370 return false; 4372 } 4373 4374 if (!interactive) { 4376 isPrinting = true; 4378 4379 try { 4380 job.print(attr); 4382 } finally { 4383 isPrinting = false; 4385 } 4386 4387 return true; 4389 } 4390 4391 4394 String progressTitle = 4396 UIManager.getString("PrintingDialog.titleProgressText"); 4397 4398 String dialogInitialContent = 4399 UIManager.getString("PrintingDialog.contentInitialText"); 4400 4401 MessageFormat statusFormat = 4404 new MessageFormat ( 4405 UIManager.getString("PrintingDialog.contentProgressText")); 4406 4407 String abortText = 4408 UIManager.getString("PrintingDialog.abortButtonText"); 4409 String abortTooltip = 4410 UIManager.getString("PrintingDialog.abortButtonToolTipText"); 4411 int abortMnemonic = 4412 UIManager.getInt("PrintingDialog.abortButtonMnemonic", -1); 4413 int abortMnemonicIndex = 4414 UIManager.getInt("PrintingDialog.abortButtonDisplayedMnemonicIndex", -1); 4415 4416 final JButton abortButton = new JButton (abortText); 4417 abortButton.setToolTipText(abortTooltip); 4418 if (abortMnemonic != -1) { 4419 abortButton.setMnemonic(abortMnemonic); 4420 } 4421 if (abortMnemonicIndex != -1) { 4422 abortButton.setDisplayedMnemonicIndex(abortMnemonicIndex); 4423 } 4424 4425 final JLabel statusLabel = new JLabel (dialogInitialContent); 4426 4427 JOptionPane abortPane = new JOptionPane (statusLabel, 4428 JOptionPane.INFORMATION_MESSAGE, 4429 JOptionPane.DEFAULT_OPTION, 4430 null, new Object [] {abortButton}, 4431 abortButton); 4432 4433 final ThreadSafePrintable wrappedPrintable = 4435 (ThreadSafePrintable)printable; 4436 4437 wrappedPrintable.startUpdatingStatus(statusFormat, statusLabel); 4439 4440 Container parentComp = getParent() instanceof JViewport ? getParent() : this; 4442 4443 final JDialog abortDialog = abortPane.createDialog(parentComp, progressTitle); 4445 abortDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 4447 4448 final Action abortAction = new AbstractAction () { 4450 boolean isAborted = false; 4451 public void actionPerformed(ActionEvent ae) { 4452 if (!isAborted) { 4453 isAborted = true; 4454 4455 abortButton.setEnabled(false); 4457 abortDialog.setTitle( 4458 UIManager.getString("PrintingDialog.titleAbortingText")); 4459 statusLabel.setText( 4460 UIManager.getString("PrintingDialog.contentAbortingText")); 4461 4462 wrappedPrintable.stopUpdatingStatus(); 4464 4465 job.cancel(); 4467 } 4468 } 4469 }; 4470 4471 abortButton.addActionListener(abortAction); 4473 4474 abortPane.getActionMap().put("close", abortAction); 4478 4479 final WindowAdapter closeListener = new WindowAdapter() { 4481 public void windowClosing(WindowEvent we) { 4482 abortAction.actionPerformed(null); 4483 } 4484 }; 4485 abortDialog.addWindowListener(closeListener); 4486 4487 printError = null; 4489 4490 final Object lock = new Object (); 4492 4493 final PrintRequestAttributeSet copyAttr = attr; 4495 4496 Runnable runnable = new Runnable () { 4499 public void run() { 4500 try { 4501 job.print(copyAttr); 4503 } catch (Throwable t) { 4504 synchronized(lock) { 4506 printError = t; 4507 } 4508 } finally { 4509 SwingUtilities.invokeLater(new Runnable () { 4512 public void run() { 4513 abortDialog.removeWindowListener(closeListener); 4515 abortDialog.dispose(); 4516 } 4517 }); 4518 } 4519 } 4520 }; 4521 4522 Thread th = new Thread (runnable); 4524 th.start(); 4525 4526 abortDialog.setVisible(true); 4528 4529 4531 Throwable pe; 4533 synchronized(lock) { 4534 pe = printError; 4535 printError = null; 4536 } 4537 4538 if (pe != null) { 4540 if (pe instanceof PrinterAbortException) { 4543 return false; 4544 } else if (pe instanceof PrinterException) { 4545 throw (PrinterException)pe; 4546 } else if (pe instanceof RuntimeException ) { 4547 throw (RuntimeException )pe; 4548 } else if (pe instanceof Error ) { 4549 throw (Error )pe; 4550 } 4551 4552 throw new AssertionError (pe); 4554 } 4555 4556 return true; 4557 } 4558 4559 4687 public Printable getPrintable(PrintMode printMode, 4688 MessageFormat headerFormat, 4689 MessageFormat footerFormat) { 4690 4691 return new TablePrintable (this, printMode, headerFormat, footerFormat); 4692 } 4693 4694 4695 4704 private class ThreadSafePrintable implements Printable { 4705 4706 4707 private Printable printDelegate; 4708 4709 4710 private MessageFormat statusFormat; 4711 4712 4713 private JLabel statusLabel; 4714 4715 4718 private int retVal; 4719 4720 4723 private Throwable retThrowable; 4724 4725 4731 public ThreadSafePrintable(Printable printDelegate) { 4732 this.printDelegate = printDelegate; 4733 } 4734 4735 4742 public void startUpdatingStatus(MessageFormat statusFormat, 4743 JLabel statusLabel) { 4744 this.statusFormat = statusFormat; 4745 this.statusLabel = statusLabel; 4746 } 4747 4748 4752 public void stopUpdatingStatus() { 4753 statusFormat = null; 4754 statusLabel = null; 4755 } 4756 4757 4771 public int print(final Graphics graphics, 4772 final PageFormat pageFormat, 4773 final int pageIndex) throws PrinterException { 4774 4775 Runnable runnable = new Runnable () { 4777 public synchronized void run() { 4778 isPrinting = true; 4780 4781 try { 4782 if (statusLabel != null) { 4783 Object [] pageNumber = new Object []{ 4786 new Integer (pageIndex + 1)}; 4787 statusLabel.setText(statusFormat.format(pageNumber)); 4788 } 4789 4790 retVal = printDelegate.print(graphics, pageFormat, pageIndex); 4792 } catch (Throwable throwable) { 4793 retThrowable = throwable; 4795 } finally { 4796 isPrinting = false; 4798 4799 notifyAll(); 4801 } 4802 } 4803 }; 4804 4805 synchronized(runnable) { 4806 retVal = -1; 4808 retThrowable = null; 4809 4810 SwingUtilities.invokeLater(runnable); 4812 4813 while (retVal == -1 && retThrowable == null) { 4815 try { 4816 runnable.wait(); 4817 } catch (InterruptedException ie) { 4818 } 4820 } 4821 4822 if (retThrowable != null) { 4824 if (retThrowable instanceof PrinterException) { 4825 throw (PrinterException)retThrowable; 4826 } else if (retThrowable instanceof RuntimeException ) { 4827 throw (RuntimeException )retThrowable; 4828 } else if (retThrowable instanceof Error ) { 4829 throw (Error )retThrowable; 4830 } 4831 4832 throw new AssertionError (retThrowable); 4834 } 4835 4836 return retVal; 4837 } 4838 } 4839 } 4840 4841 4842 4846 4855 public AccessibleContext getAccessibleContext() { 4856 if (accessibleContext == null) { 4857 accessibleContext = new AccessibleJTable(); 4858 } 4859 return accessibleContext; 4860 } 4861 4862 4880 protected class AccessibleJTable extends AccessibleJComponent 4881 implements AccessibleSelection, ListSelectionListener, TableModelListener, 4882 TableColumnModelListener, CellEditorListener, PropertyChangeListener, 4883 AccessibleExtendedTable { 4884 4885 int lastSelectedRow; 4886 int lastSelectedCol; 4887 4888 4893 protected AccessibleJTable() { 4894 super(); 4895 JTable.this.addPropertyChangeListener(this); 4896 JTable.this.getSelectionModel().addListSelectionListener(this); 4897 TableColumnModel tcm = JTable.this.getColumnModel(); 4898 tcm.addColumnModelListener(this); 4899 tcm.getSelectionModel().addListSelectionListener(this); 4900 JTable.this.getModel().addTableModelListener(this); 4901 lastSelectedRow = JTable.this.getSelectedRow(); 4902 lastSelectedCol = JTable.this.getSelectedColumn(); 4903 } 4904 4905 4908 4913 public void propertyChange(PropertyChangeEvent e) { 4914 String name = e.getPropertyName(); 4915 Object oldValue = e.getOldValue(); 4916 Object newValue = e.getNewValue(); 4917 4918 if (name.compareTo("model") == 0) { 4920 4921 if (oldValue != null && oldValue instanceof TableModel) { 4922 ((TableModel) oldValue).removeTableModelListener(this); 4923 } 4924 if (newValue != null && newValue instanceof TableModel) { 4925 ((TableModel) newValue).addTableModelListener(this); 4926 } 4927 4928 } else if (name.compareTo("selectionModel") == 0) { 4930 4931 Object source = e.getSource(); 4932 if (source == JTable.this) { 4934 if (oldValue != null && 4935 oldValue instanceof ListSelectionModel ) { 4936 ((ListSelectionModel ) oldValue).removeListSelectionListener(this); 4937 } 4938 if (newValue != null && 4939 newValue instanceof ListSelectionModel ) { 4940 ((ListSelectionModel ) newValue).addListSelectionListener(this); 4941 } 4942 4943 } else if (source == JTable.this.getColumnModel()) { 4944 4945 if (oldValue != null && 4946 oldValue instanceof ListSelectionModel ) { 4947 ((ListSelectionModel ) oldValue).removeListSelectionListener(this); 4948 } 4949 if (newValue != null && 4950 newValue instanceof ListSelectionModel ) { 4951 ((ListSelectionModel ) newValue).addListSelectionListener(this); 4952 } 4953 4954 } else { 4955 } 4957 4958 } else if (name.compareTo("columnModel") == 0) { 4961 4962 if (oldValue != null && oldValue instanceof TableColumnModel) { 4963 TableColumnModel tcm = (TableColumnModel) oldValue; 4964 tcm.removeColumnModelListener(this); 4965 tcm.getSelectionModel().removeListSelectionListener(this); 4966 } 4967 if (newValue != null && newValue instanceof TableColumnModel) { 4968 TableColumnModel tcm = (TableColumnModel) newValue; 4969 tcm.addColumnModelListener(this); 4970 tcm.getSelectionModel().addListSelectionListener(this); 4971 } 4972 4973 } else if (name.compareTo("tableCellEditor") == 0) { 4975 4976 if (oldValue != null && oldValue instanceof TableCellEditor) { 4977 ((TableCellEditor) oldValue).removeCellEditorListener((CellEditorListener) this); 4978 } 4979 if (newValue != null && newValue instanceof TableCellEditor) { 4980 ((TableCellEditor) newValue).addCellEditorListener((CellEditorListener) this); 4981 } 4982 } 4983 } 4984 4985 4986 4988 4991 protected class AccessibleJTableModelChange 4992 implements AccessibleTableModelChange { 4993 4994 protected int type; 4995 protected int firstRow; 4996 protected int lastRow; 4997 protected int firstColumn; 4998 protected int lastColumn; 4999 5000 protected AccessibleJTableModelChange(int type, int firstRow, 5001 int lastRow, int firstColumn, 5002 int lastColumn) { 5003 this.type = type; 5004 this.firstRow = firstRow; 5005 this.lastRow = lastRow; 5006 this.firstColumn = firstColumn; 5007 this.lastColumn = lastColumn; 5008 } 5009 5010 public int getType() { 5011 return type; 5012 } 5013 5014 public int getFirstRow() { 5015 return firstRow; 5016 } 5017 5018 public int getLastRow() { 5019 return lastRow; 5020 } 5021 5022 public int getFirstColumn() { 5023 return firstColumn; 5024 } 5025 5026 public int getLastColumn() { 5027 return lastColumn; 5028 } 5029 } 5030 5031 5034 public void tableChanged(TableModelEvent e) { 5035 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 5036 null, null); 5037 if (e != null) { 5038 int firstColumn = e.getColumn(); 5039 int lastColumn = e.getColumn(); 5040 if (firstColumn == TableModelEvent.ALL_COLUMNS) { 5041 firstColumn = 0; 5042 lastColumn = getColumnCount() - 1; 5043 } 5044 5045 AccessibleJTableModelChange change = 5048 new AccessibleJTableModelChange(e.getType(), 5049 e.getFirstRow(), 5050 e.getLastRow(), 5051 firstColumn, 5052 lastColumn); 5053 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED, 5054 null, change); 5055 } 5056 } 5057 5058 5061 public void tableRowsInserted(TableModelEvent e) { 5062 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 5063 null, null); 5064 5065 int firstColumn = e.getColumn(); 5068 int lastColumn = e.getColumn(); 5069 if (firstColumn == TableModelEvent.ALL_COLUMNS) { 5070 firstColumn = 0; 5071 lastColumn = getColumnCount() - 1; 5072 } 5073 AccessibleJTableModelChange change = 5074 new AccessibleJTableModelChange(e.getType(), 5075 e.getFirstRow(), 5076 e.getLastRow(), 5077 firstColumn, 5078 lastColumn); 5079 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED, 5080 null, change); 5081 } 5082 5083 5086 public void tableRowsDeleted(TableModelEvent e) { 5087 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 5088 null, null); 5089 5090 int firstColumn = e.getColumn(); 5093 int lastColumn = e.getColumn(); 5094 if (firstColumn == TableModelEvent.ALL_COLUMNS) { 5095 firstColumn = 0; 5096 lastColumn = getColumnCount() - 1; 5097 } 5098 AccessibleJTableModelChange change = 5099 new AccessibleJTableModelChange(e.getType(), 5100 e.getFirstRow(), 5101 e.getLastRow(), 5102 firstColumn, 5103 lastColumn); 5104 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED, 5105 null, change); 5106 } 5107 5108 5111 public void columnAdded(TableColumnModelEvent e) { 5112 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 5113 null, null); 5114 5115 int type = AccessibleTableModelChange.INSERT; 5118 AccessibleJTableModelChange change = 5119 new AccessibleJTableModelChange(type, 5120 0, 5121 0, 5122 e.getFromIndex(), 5123 e.getToIndex()); 5124 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED, 5125 null, change); 5126 } 5127 5128 5131 public void columnRemoved(TableColumnModelEvent e) { 5132 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 5133 null, null); 5134 int type = AccessibleTableModelChange.DELETE; 5137 AccessibleJTableModelChange change = 5138 new AccessibleJTableModelChange(type, 5139 0, 5140 0, 5141 e.getFromIndex(), 5142 e.getToIndex()); 5143 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED, 5144 null, change); 5145 } 5146 5147 5152 public void columnMoved(TableColumnModelEvent e) { 5153 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 5154 null, null); 5155 5156 int type = AccessibleTableModelChange.DELETE; 5159 AccessibleJTableModelChange change = 5160 new AccessibleJTableModelChange(type, 5161 0, 5162 0, 5163 e.getFromIndex(), 5164 e.getFromIndex()); 5165 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED, 5166 null, change); 5167 5168 int type2 = AccessibleTableModelChange.INSERT; 5169 AccessibleJTableModelChange change2 = 5170 new AccessibleJTableModelChange(type2, 5171 0, 5172 0, 5173 e.getToIndex(), 5174 e.getToIndex()); 5175 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_MODEL_CHANGED, 5176 null, change2); 5177 } 5178 5179 5184 public void columnMarginChanged(ChangeEvent e) { 5185 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 5186 null, null); 5187 } 5188 5189 5194 public void columnSelectionChanged(ListSelectionEvent e) { 5195 } 5197 5198 5206 public void editingStopped(ChangeEvent e) { 5207 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 5210 null, null); 5211 } 5212 5213 5219 public void editingCanceled(ChangeEvent e) { 5220 } 5222 5223 5226 public void valueChanged(ListSelectionEvent e) { 5227 firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY, 5228 Boolean.valueOf(false), Boolean.valueOf(true)); 5229 5230 int selectedRow = JTable.this.getSelectedRow(); 5231 int selectedCol = JTable.this.getSelectedColumn(); 5232 if (selectedRow != lastSelectedRow || 5233 selectedCol != lastSelectedCol) { 5234 Accessible oldA = getAccessibleAt(lastSelectedRow, 5235 lastSelectedCol); 5236 Accessible newA = getAccessibleAt(selectedRow, selectedCol); 5237 firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, 5238 oldA, newA); 5239 lastSelectedRow = selectedRow; 5240 lastSelectedCol = selectedCol; 5241 } 5242 } 5243 5244 5245 5246 5247 5249 5257 public AccessibleSelection getAccessibleSelection() { 5258 return this; 5259 } 5260 5261 5268 public AccessibleRole getAccessibleRole() { 5269 return AccessibleRole.TABLE; 5270 } 5271 5272 5282 public Accessible getAccessibleAt(Point p) { 5283 int column = columnAtPoint(p); 5284 int row = rowAtPoint(p); 5285 5286 if ((column != -1) && (row != -1)) { 5287 TableColumn aColumn = getColumnModel().getColumn(column); 5288 TableCellRenderer renderer = aColumn.getCellRenderer(); 5289 if (renderer == null) { 5290 Class <?> columnClass = getColumnClass(column); 5291 renderer = getDefaultRenderer(columnClass); 5292 } 5293 Component component = renderer.getTableCellRendererComponent( 5294 JTable.this, null, false, false, 5295 row, column); 5296 return new AccessibleJTableCell(JTable.this, row, column, 5297 getAccessibleIndexAt(row, column)); 5298 } 5299 return null; 5300 } 5301 5302 5309 public int getAccessibleChildrenCount() { 5310 return (JTable.this.getColumnCount() * JTable.this.getRowCount()); 5311 } 5312 5313 5319 public Accessible getAccessibleChild(int i) { 5320 if (i < 0 || i >= getAccessibleChildrenCount()) { 5321 return null; 5322 } else { 5323 int column = getAccessibleColumnAtIndex(i); 5326 int row = getAccessibleRowAtIndex(i); 5327 5328 TableColumn aColumn = getColumnModel().getColumn(column); 5329 TableCellRenderer renderer = aColumn.getCellRenderer(); 5330 if (renderer == null) { 5331 Class <?> columnClass = getColumnClass(column); 5332 renderer = getDefaultRenderer(columnClass); 5333 } 5334 Component component = renderer.getTableCellRendererComponent( 5335 JTable.this, null, false, false, 5336 row, column); 5337 return new AccessibleJTableCell(JTable.this, row, column, 5338 getAccessibleIndexAt(row, column)); 5339 } 5340 } 5341 5342 5344 5351 public int getAccessibleSelectionCount() { 5352 int rowsSel = JTable.this.getSelectedRowCount(); 5353 int colsSel = JTable.this.getSelectedColumnCount(); 5354 5355 if (JTable.this.cellSelectionEnabled) { return rowsSel * colsSel; 5357 5358 } else { 5359 if (JTable.this.getRowSelectionAllowed() && 5361 JTable.this.getColumnSelectionAllowed()) { 5362 return rowsSel * JTable.this.getColumnCount() + 5363 colsSel * JTable.this.getRowCount() - 5364 rowsSel * colsSel; 5365 5366 } else if (JTable.this.getRowSelectionAllowed()) { 5368 return rowsSel * JTable.this.getColumnCount(); 5369 5370 } else if (JTable.this.getColumnSelectionAllowed()) { 5372 return colsSel * JTable.this.getRowCount(); 5373 5374 } else { 5375 return 0; } 5377 } 5378 } 5379 5380 5393 public Accessible getAccessibleSelection(int i) { 5394 if (i < 0 || i > getAccessibleSelectionCount()) { 5395 return (Accessible) null; 5396 } 5397 5398 int rowsSel = JTable.this.getSelectedRowCount(); 5399 int colsSel = JTable.this.getSelectedColumnCount(); 5400 int rowIndicies[] = getSelectedRows(); 5401 int colIndicies[] = getSelectedColumns(); 5402 int ttlCols = JTable.this.getColumnCount(); 5403 int ttlRows = JTable.this.getRowCount(); 5404 int r; 5405 int c; 5406 5407 if (JTable.this.cellSelectionEnabled) { r = rowIndicies[i / colsSel]; 5409 c = colIndicies[i % colsSel]; 5410 return getAccessibleChild((r * ttlCols) + c); 5411 } else { 5412 5413 if (JTable.this.getRowSelectionAllowed() && 5415 JTable.this.getColumnSelectionAllowed()) { 5416 5417 5427 int curIndex = i; 5439 final int IN_ROW = 0; 5440 final int NOT_IN_ROW = 1; 5441 int state = (rowIndicies[0] == 0 ? IN_ROW : NOT_IN_ROW); 5442 int j = 0; 5443 int prevRow = -1; 5444 while (j < rowIndicies.length) { 5445 switch (state) { 5446 5447 case IN_ROW: if (curIndex < ttlCols) { c = curIndex % ttlCols; 5450 r = rowIndicies[j]; 5451 return getAccessibleChild((r * ttlCols) + c); 5452 } else { curIndex -= ttlCols; 5454 } 5455 if (j + 1 == rowIndicies.length || 5457 rowIndicies[j] != rowIndicies[j+1] - 1) { 5458 state = NOT_IN_ROW; 5459 prevRow = rowIndicies[j]; 5460 } 5461 j++; break; 5463 5464 case NOT_IN_ROW: if (curIndex < 5466 (colsSel * (rowIndicies[j] - 5467 (prevRow == -1 ? 0 : (prevRow + 1))))) { 5468 5469 c = colIndicies[curIndex % colsSel]; 5471 r = (j > 0 ? rowIndicies[j-1] + 1 : 0) 5472 + curIndex / colsSel; 5473 return getAccessibleChild((r * ttlCols) + c); 5474 } else { curIndex -= colsSel * (rowIndicies[j] - 5476 (prevRow == -1 ? 0 : (prevRow + 1))); 5477 } 5478 state = IN_ROW; 5479 break; 5480 } 5481 } 5482 if (curIndex < 5485 (colsSel * (ttlRows - 5486 (prevRow == -1 ? 0 : (prevRow + 1))))) { c = colIndicies[curIndex % colsSel]; 5488 r = rowIndicies[j-1] + curIndex / colsSel + 1; 5489 return getAccessibleChild((r * ttlCols) + c); 5490 } else { } 5494 5495 } else if (JTable.this.getRowSelectionAllowed()) { 5497 c = i % ttlCols; 5498 r = rowIndicies[i / ttlCols]; 5499 return getAccessibleChild((r * ttlCols) + c); 5500 5501 } else if (JTable.this.getColumnSelectionAllowed()) { 5503 c = colIndicies[i % colsSel]; 5504 r = i / colsSel; 5505 return getAccessibleChild((r * ttlCols) + c); 5506 } 5507 } 5508 return (Accessible) null; 5509 } 5510 5511 5519 public boolean isAccessibleChildSelected(int i) { 5520 int column = getAccessibleColumnAtIndex(i); 5521 int row = getAccessibleRowAtIndex(i); 5522 return JTable.this.isCellSelected(row, column); 5523 } 5524 5525 5539 public void addAccessibleSelection(int i) { 5540 int column = getAccessibleColumnAtIndex(i); 5542 int row = getAccessibleRowAtIndex(i); 5543 JTable.this.changeSelection(row, column, true, false); 5544 } 5545 5546 5557 public void removeAccessibleSelection(int i) { 5558 if (JTable.this.cellSelectionEnabled) { 5559 int column = getAccessibleColumnAtIndex(i); 5560 int row = getAccessibleRowAtIndex(i); 5561 JTable.this.removeRowSelectionInterval(row, row); 5562 JTable.this.removeColumnSelectionInterval(column, column); 5563 } 5564 } 5565 5566 5570 public void clearAccessibleSelection() { 5571 JTable.this.clearSelection(); 5572 } 5573 5574 5579 public void selectAllAccessibleSelection() { 5580 if (JTable.this.cellSelectionEnabled) { 5581 JTable.this.selectAll(); 5582 } 5583 } 5584 5585 5587 5594 public int getAccessibleRow(int index) { 5595 return getAccessibleRowAtIndex(index); 5596 } 5597 5598 5605 public int getAccessibleColumn(int index) { 5606 return getAccessibleColumnAtIndex(index); 5607 } 5608 5609 5617 public int getAccessibleIndex(int r, int c) { 5618 return getAccessibleIndexAt(r, c); 5619 } 5620 5621 5623 5625 private Accessible caption; 5626 private Accessible summary; 5627 private Accessible [] rowDescription; 5628 private Accessible [] columnDescription; 5629 5630 5639 public AccessibleTable getAccessibleTable() { 5640 return this; 5641 } 5642 5643 5648 public Accessible getAccessibleCaption() { 5649 return this.caption; 5650 } 5651 5652 5657 public void setAccessibleCaption(Accessible a) { 5658 Accessible oldCaption = caption; 5659 this.caption = a; 5660 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_CAPTION_CHANGED, 5661 oldCaption, this.caption); 5662 } 5663 5664 5669 public Accessible getAccessibleSummary() { 5670 return this.summary; 5671 } 5672 5673 5678 public void setAccessibleSummary(Accessible a) { 5679 Accessible oldSummary = summary; 5680 this.summary = a; 5681 firePropertyChange(AccessibleContext.ACCESSIBLE_TABLE_SUMMARY_CHANGED, 5682 oldSummary, this.summary); 5683 } 5684 5685 5690 public int getAccessibleRowCount() { 5691 return JTable.this.getRowCount(); 5692 } 5693 5694 5699 public int getAccessibleColumnCount() { 5700 return JTable.this.getColumnCount(); 5701 } 5702 5703 5712 public Accessible getAccessibleAt(int r, int c) { 5713 return getAccessibleChild((r * getAccessibleColumnCount()) + c); 5714 } 5715 5716 5723 public int getAccessibleRowExtentAt(int r, int c) { 5724 return 1; 5725 } 5726 5727 5734 public int getAccessibleColumnExtentAt(int r, int c) { 5735 return 1; 5736 } 5737 5738 5744 public AccessibleTable getAccessibleRowHeader() { 5745 return null; 5747 } 5748 5749 5755 public void setAccessibleRowHeader(AccessibleTable a) { 5756 } 5758 5759 5766 public AccessibleTable getAccessibleColumnHeader() { 5767 JTableHeader header = JTable.this.getTableHeader(); 5768 return header == null ? null : new AccessibleTableHeader(header); 5769 } 5770 5771 5774 private class AccessibleTableHeader implements AccessibleTable { 5775 private JTableHeader header; 5776 private TableColumnModel headerModel; 5777 5778 AccessibleTableHeader(JTableHeader header) { 5779 this.header = header; 5780 this.headerModel = header.getColumnModel(); 5781 } 5782 5783 5788 public Accessible getAccessibleCaption() { return null; } 5789 5790 5791 5796 public void setAccessibleCaption(Accessible a) {} 5797 5798 5803 public Accessible getAccessibleSummary() { return null; } 5804 5805 5810 public void setAccessibleSummary(Accessible a) {} 5811 5812 5817 public int getAccessibleRowCount() { return 1; } 5818 5819 5824 public int getAccessibleColumnCount() { 5825 return headerModel.getColumnCount(); 5826 } 5827 5828 5836 public Accessible getAccessibleAt(int row, int column) { 5837 5838 5839 TableColumn aColumn = headerModel.getColumn(column); 5841 TableCellRenderer renderer = aColumn.getHeaderRenderer(); 5842 if (renderer == null) { 5843 renderer = header.getDefaultRenderer(); 5844 } 5845 Component component = renderer.getTableCellRendererComponent( 5846 header.getTable(), 5847 aColumn.getHeaderValue(), false, false, 5848 -1, column); 5849 5850 return new AccessibleJTableHeaderCell(row, column, 5851 JTable.this.getTableHeader(), 5852 component); 5853 } 5854 5855 5862 public int getAccessibleRowExtentAt(int r, int c) { return 1; } 5863 5864 5871 public int getAccessibleColumnExtentAt(int r, int c) { return 1; } 5872 5873 5879 public AccessibleTable getAccessibleRowHeader() { return null; } 5880 5881 5887 public void setAccessibleRowHeader(AccessibleTable table) {} 5888 5889 5895 public AccessibleTable getAccessibleColumnHeader() { return null; } 5896 5897 5903 public void setAccessibleColumnHeader(AccessibleTable table) {} 5904 5905 5911 public Accessible getAccessibleRowDescription(int r) { return null; } 5912 5913 5919 public void setAccessibleRowDescription(int r, Accessible a) {} 5920 5921 5927 public Accessible getAccessibleColumnDescription(int c) { return null; } 5928 5929 5935 public void setAccessibleColumnDescription(int c, Accessible a) {} 5936 5937 5947 public boolean isAccessibleSelected(int r, int c) { return false; } 5948 5949 5957 public boolean isAccessibleRowSelected(int r) { return false; } 5958 5959 5967 public boolean isAccessibleColumnSelected(int c) { return false; } 5968 5969 5975 public int [] getSelectedAccessibleRows() { return new int[0]; } 5976 5977 5983 public int [] getSelectedAccessibleColumns() { return new int[0]; } 5984 } 5985 5986 5987 5993 public void setAccessibleColumnHeader(AccessibleTable a) { 5994 } 5996 5997 6003 public Accessible getAccessibleRowDescription(int r) { 6004 if (r < 0 || r >= getAccessibleRowCount()) { 6005 throw new IllegalArgumentException (new Integer (r).toString()); 6006 } 6007 if (rowDescription == null) { 6008 return null; 6009 } else { 6010 return rowDescription[r]; 6011 } 6012 } 6013 6014 6020 public void setAccessibleRowDescription(int r, Accessible a) { 6021 if (r < 0 || r >= getAccessibleRowCount()) { 6022 throw new IllegalArgumentException (new Integer (r).toString()); 6023 } 6024 if (rowDescription == null) { 6025 int numRows = getAccessibleRowCount(); 6026 rowDescription = new Accessible[numRows]; 6027 } 6028 rowDescription[r] = a; 6029 } 6030 6031 6037 public Accessible getAccessibleColumnDescription(int c) { 6038 if (c < 0 || c >= getAccessibleColumnCount()) { 6039 throw new IllegalArgumentException (new Integer (c).toString()); 6040 } 6041 if (columnDescription == null) { 6042 return null; 6043 } else { 6044 return columnDescription[c]; 6045 } 6046 } 6047 6048 6054 public void setAccessibleColumnDescription(int c, Accessible a) { 6055 if (c < 0 || c >= getAccessibleColumnCount()) { 6056 throw new IllegalArgumentException (new Integer (c).toString()); 6057 } 6058 if (columnDescription == null) { 6059 int numColumns = getAccessibleColumnCount(); 6060 columnDescription = new Accessible[numColumns]; 6061 } 6062 columnDescription[c] = a; 6063 } 6064 6065 6074 public boolean isAccessibleSelected(int r, int c) { 6075 return JTable.this.isCellSelected(r, c); 6076 } 6077 6078 6086 public boolean isAccessibleRowSelected(int r) { 6087 return JTable.this.isRowSelected(r); 6088 } 6089 6090 6098 public boolean isAccessibleColumnSelected(int c) { 6099 return JTable.this.isColumnSelected(c); 6100 } 6101 6102 6108 public int [] getSelectedAccessibleRows() { 6109 return JTable.this.getSelectedRows(); 6110 } 6111 6112 6118 public int [] getSelectedAccessibleColumns() { 6119 return JTable.this.getSelectedColumns(); 6120 } 6121 6122 6128 public int getAccessibleRowAtIndex(int i) { 6129 int columnCount = getAccessibleColumnCount(); 6130 if (columnCount == 0) { 6131 return -1; 6132 } else { 6133 return (i / columnCount); 6134 } 6135 } 6136 6137 6143 public int getAccessibleColumnAtIndex(int i) { 6144 int columnCount = getAccessibleColumnCount(); 6145 if (columnCount == 0) { 6146 return -1; 6147 } else { 6148 return (i % columnCount); 6149 } 6150 } 6151 6152 6159 public int getAccessibleIndexAt(int r, int c) { 6160 return ((r * getAccessibleColumnCount()) + c); 6161 } 6162 6163 6165 6169 protected class AccessibleJTableCell extends AccessibleContext 6170 implements Accessible, AccessibleComponent { 6171 6172 private JTable parent; 6173 private int row; 6174 private int column; 6175 private int index; 6176 6177 6180 public AccessibleJTableCell(JTable t, int r, int c, int i) { 6181 parent = t; 6182 row = r; 6183 column = c; 6184 index = i; 6185 this.setAccessibleParent(parent); 6186 } 6187 6188 6196 public AccessibleContext getAccessibleContext() { 6197 return this; 6198 } 6199 6200 private AccessibleContext getCurrentAccessibleContext() { 6201 TableColumn aColumn = getColumnModel().getColumn(column); 6202 TableCellRenderer renderer = aColumn.getCellRenderer(); 6203 if (renderer == null) { 6204 Class <?> columnClass = getColumnClass(column); 6205 renderer = getDefaultRenderer(columnClass); 6206 } 6207 Component component = renderer.getTableCellRendererComponent( 6208 JTable.this, getValueAt(row, column), 6209 false, false, row, column); 6210 if (component instanceof Accessible) { 6211 return ((Accessible) component).getAccessibleContext(); 6212 } else { 6213 return null; 6214 } 6215 } 6216 6217 private Component getCurrentComponent() { 6218 TableColumn aColumn = getColumnModel().getColumn(column); 6219 TableCellRenderer renderer = aColumn.getCellRenderer(); 6220 if (renderer == null) { 6221 Class <?> columnClass = getColumnClass(column); 6222 renderer = getDefaultRenderer(columnClass); 6223 } 6224 return renderer.getTableCellRendererComponent( 6225 JTable.this, null, false, false, 6226 row, column); 6227 } 6228 6229 6231 6237 public String getAccessibleName() { 6238 AccessibleContext ac = getCurrentAccessibleContext(); 6239 if (ac != null) { 6240 String name = ac.getAccessibleName(); 6241 if ((name != null) && (name != "")) { 6242 return ac.getAccessibleName(); 6243 } 6244 } 6245 if ((accessibleName != null) && (accessibleName != "")) { 6246 return accessibleName; 6247 } else { 6248 return null; 6249 } 6250 } 6251 6252 6257 public void setAccessibleName(String s) { 6258 AccessibleContext ac = getCurrentAccessibleContext(); 6259 if (ac != null) { 6260 ac.setAccessibleName(s); 6261 } else { 6262 super.setAccessibleName(s); 6263 } 6264 } 6265 6266 6276 public String getAccessibleDescription() { 6277 AccessibleContext ac = getCurrentAccessibleContext(); 6278 if (ac != null) { 6279 return ac.getAccessibleDescription(); 6280 } else { 6281 return super.getAccessibleDescription(); 6282 } 6283 } 6284 6285 6290 public void setAccessibleDescription(String s) { 6291 AccessibleContext ac = getCurrentAccessibleContext(); 6292 if (ac != null) { 6293 ac.setAccessibleDescription(s); 6294 } else { 6295 super.setAccessibleDescription(s); 6296 } 6297 } 6298 6299 6306 public AccessibleRole getAccessibleRole() { 6307 AccessibleContext ac = getCurrentAccessibleContext(); 6308 if (ac != null) { 6309 return ac.getAccessibleRole(); 6310 } else { 6311 return AccessibleRole.UNKNOWN; 6312 } 6313 } 6314 6315 6322 public AccessibleStateSet getAccessibleStateSet() { 6323 AccessibleContext ac = getCurrentAccessibleContext(); 6324 AccessibleStateSet as = null; 6325 6326 if (ac != null) { 6327 as = ac.getAccessibleStateSet(); 6328 } 6329 if (as == null) { 6330 as = new AccessibleStateSet(); 6331 } 6332 Rectangle rjt = JTable.this.getVisibleRect(); 6333 Rectangle rcell = JTable.this.getCellRect(row, column, false); 6334 if (rjt.intersects(rcell)) { 6335 as.add(AccessibleState.SHOWING); 6336 } else { 6337 if (as.contains(AccessibleState.SHOWING)) { 6338 as.remove(AccessibleState.SHOWING); 6339 } 6340 } 6341 if (parent.isCellSelected(row, column)) { 6342 as.add(AccessibleState.SELECTED); 6343 } else if (as.contains(AccessibleState.SELECTED)) { 6344 as.remove(AccessibleState.SELECTED); 6345 } 6346 if ((row == getSelectedRow()) && (column == getSelectedColumn())) { 6347 as.add(AccessibleState.ACTIVE); 6348 } 6349 as.add(AccessibleState.TRANSIENT); 6350 return as; 6351 } 6352 6353 6360 public Accessible getAccessibleParent() { 6361 return parent; 6362 } 6363 6364 6371 public int getAccessibleIndexInParent() { 6372 return index; 6373 } 6374 6375 6380 public int getAccessibleChildrenCount() { 6381 AccessibleContext ac = getCurrentAccessibleContext(); 6382 if (ac != null) { 6383 return ac.getAccessibleChildrenCount(); 6384 } else { 6385 return 0; 6386 } 6387 } 6388 6389 6396 public Accessible getAccessibleChild(int i) { 6397 AccessibleContext ac = getCurrentAccessibleContext(); 6398 if (ac != null) { 6399 Accessible accessibleChild = ac.getAccessibleChild(i); 6400 ac.setAccessibleParent(this); 6401 return accessibleChild; 6402 } else { 6403 return null; 6404 } 6405 } 6406 6407 6421 public Locale getLocale() { 6422 AccessibleContext ac = getCurrentAccessibleContext(); 6423 if (ac != null) { 6424 return ac.getLocale(); 6425 } else { 6426 return null; 6427 } 6428 } 6429 6430 6437 public void addPropertyChangeListener(PropertyChangeListener l) { 6438 AccessibleContext ac = getCurrentAccessibleContext(); 6439 if (ac != null) { 6440 ac.addPropertyChangeListener(l); 6441 } else { 6442 super.addPropertyChangeListener(l); 6443 } 6444 } 6445 6446 6454 public void removePropertyChangeListener(PropertyChangeListener l) { 6455 AccessibleContext ac = getCurrentAccessibleContext(); 6456 if (ac != null) { 6457 ac.removePropertyChangeListener(l); 6458 } else { 6459 super.removePropertyChangeListener(l); 6460 } 6461 } 6462 6463 6469 public AccessibleAction getAccessibleAction() { 6470 return getCurrentAccessibleContext().getAccessibleAction(); 6471 } 6472 6473 6480 public AccessibleComponent getAccessibleComponent() { 6481 return this; } 6483 6484 6491 public AccessibleSelection getAccessibleSelection() { 6492 return getCurrentAccessibleContext().getAccessibleSelection(); 6493 } 6494 6495 6501 public AccessibleText getAccessibleText() { 6502 return getCurrentAccessibleContext().getAccessibleText(); 6503 } 6504 6505 6511 public AccessibleValue getAccessibleValue() { 6512 return getCurrentAccessibleContext().getAccessibleValue(); 6513 } 6514 6515 6516 6518 6524 public Color getBackground() { 6525 AccessibleContext ac = getCurrentAccessibleContext(); 6526 if (ac instanceof AccessibleComponent) { 6527 return ((AccessibleComponent) ac).getBackground(); 6528 } else { 6529 Component c = getCurrentComponent(); 6530 if (c != null) { 6531 return c.getBackground(); 6532 } else { 6533 return null; 6534 } 6535 } 6536 } 6537 6538 6543 public void setBackground(Color c) { 6544 AccessibleContext ac = getCurrentAccessibleContext(); 6545 if (ac instanceof AccessibleComponent) { 6546 ((AccessibleComponent) ac).setBackground(c); 6547 } else { 6548 Component cp = getCurrentComponent(); 6549 if (cp != null) { 6550 cp.setBackground(c); 6551 } 6552 } 6553 } 6554 6555 6561 public Color getForeground() { 6562 AccessibleContext ac = getCurrentAccessibleContext(); 6563 if (ac instanceof AccessibleComponent) { 6564 return ((AccessibleComponent) ac).getForeground(); 6565 } else { 6566 Component c = getCurrentComponent(); 6567 if (c != null) { 6568 return c.getForeground(); 6569 } else { 6570 return null; 6571 } 6572 } 6573 } 6574 6575 6580 public void setForeground(Color c) { 6581 AccessibleContext ac = getCurrentAccessibleContext(); 6582 if (ac instanceof AccessibleComponent) { 6583 ((AccessibleComponent) ac).setForeground(c); 6584 } else { 6585 Component cp = getCurrentComponent(); 6586 if (cp != null) { 6587 cp.setForeground(c); 6588 } 6589 } 6590 } 6591 6592 6598 public Cursor getCursor() { 6599 AccessibleContext ac = getCurrentAccessibleContext(); 6600 if (ac instanceof AccessibleComponent) { 6601 return ((AccessibleComponent) ac).getCursor(); 6602 } else { 6603 Component c = getCurrentComponent(); 6604 if (c != null) { 6605 return c.getCursor(); 6606 } else { 6607 Accessible ap = getAccessibleParent(); 6608 if (ap instanceof AccessibleComponent) { 6609 return ((AccessibleComponent) ap).getCursor(); 6610 } else { 6611 return null; 6612 } 6613 } 6614 } 6615 } 6616 6617 6622 public void setCursor(Cursor c) { 6623 AccessibleContext ac = getCurrentAccessibleContext(); 6624 if (ac instanceof AccessibleComponent) { 6625 ((AccessibleComponent) ac).setCursor(c); 6626 } else { 6627 Component cp = getCurrentComponent(); 6628 if (cp != null) { 6629 cp.setCursor(c); 6630 } 6631 } 6632 } 6633 6634 6640 public Font getFont() { 6641 AccessibleContext ac = getCurrentAccessibleContext(); 6642 if (ac instanceof AccessibleComponent) { 6643 return ((AccessibleComponent) ac).getFont(); 6644 } else { 6645 Component c = getCurrentComponent(); 6646 if (c != null) { 6647 return c.getFont(); 6648 } else { 6649 return null; 6650 } 6651 } 6652 } 6653 6654 6659 public void setFont(Font f) { 6660 AccessibleContext ac = getCurrentAccessibleContext(); 6661 if (ac instanceof AccessibleComponent) { 6662 ((AccessibleComponent) ac).setFont(f); 6663 } else { 6664 Component c = getCurrentComponent(); 6665 if (c != null) { 6666 c.setFont(f); 6667 } 6668 } 6669 } 6670 6671 6679 public FontMetrics getFontMetrics(Font f) { 6680 AccessibleContext ac = getCurrentAccessibleContext(); 6681 if (ac instanceof AccessibleComponent) { 6682 return ((AccessibleComponent) ac).getFontMetrics(f); 6683 } else { 6684 Component c = getCurrentComponent(); 6685 if (c != null) { 6686 return c.getFontMetrics(f); 6687 } else { 6688 return null; 6689 } 6690 } 6691 } 6692 6693 6698 public boolean isEnabled() { 6699 AccessibleContext ac = getCurrentAccessibleContext(); 6700 if (ac instanceof AccessibleComponent) { 6701 return ((AccessibleComponent) ac).isEnabled(); 6702 } else { 6703 Component c = getCurrentComponent(); 6704 if (c != null) { 6705 return c.isEnabled(); 6706 } else { 6707 return false; 6708 } 6709 } 6710 } 6711 6712 6717 public void setEnabled(boolean b) { 6718 AccessibleContext ac = getCurrentAccessibleContext(); 6719 if (ac instanceof AccessibleComponent) { 6720 ((AccessibleComponent) ac).setEnabled(b); 6721 } else { 6722 Component c = getCurrentComponent(); 6723 if (c != null) { 6724 c.setEnabled(b); 6725 } 6726 } 6727 } 6728 6729 6738 public boolean isVisible() { 6739 AccessibleContext ac = getCurrentAccessibleContext(); 6740 if (ac instanceof AccessibleComponent) { 6741 return ((AccessibleComponent) ac).isVisible(); 6742 } else { 6743 Component c = getCurrentComponent(); 6744 if (c != null) { 6745 return c.isVisible(); 6746 } else { 6747 return false; 6748 } 6749 } 6750 } 6751 6752 6757 public void setVisible(boolean b) { 6758 AccessibleContext ac = getCurrentAccessibleContext(); 6759 if (ac instanceof AccessibleComponent) { 6760 ((AccessibleComponent) ac).setVisible(b); 6761 } else { 6762 Component c = getCurrentComponent(); 6763 if (c != null) { 6764 c.setVisible(b); 6765 } 6766 } 6767 } 6768 6769 6778 public boolean isShowing() { 6779 AccessibleContext ac = getCurrentAccessibleContext(); 6780 if (ac instanceof AccessibleComponent) { 6781 if (ac.getAccessibleParent() != null) { 6782 return ((AccessibleComponent) ac).isShowing(); 6783 } else { 6784 return isVisible(); 6788 } 6789 } else { 6790 Component c = getCurrentComponent(); 6791 if (c != null) { 6792 return c.isShowing(); 6793 } else { 6794 return false; 6795 } 6796 } 6797 } 6798 6799 6810 public boolean contains(Point p) { 6811 AccessibleContext ac = getCurrentAccessibleContext(); 6812 if (ac instanceof AccessibleComponent) { 6813 Rectangle r = ((AccessibleComponent) ac).getBounds(); 6814 return r.contains(p); 6815 } else { 6816 Component c = getCurrentComponent(); 6817 if (c != null) { 6818 Rectangle r = c.getBounds(); 6819 return r.contains(p); 6820 } else { 6821 return getBounds().contains(p); 6822 } 6823 } 6824 } 6825 6826 6832 public Point getLocationOnScreen() { 6833 if (parent != null) { 6834 Point parentLocation = parent.getLocationOnScreen(); 6835 Point componentLocation = getLocation(); 6836 componentLocation.translate(parentLocation.x, parentLocation.y); 6837 return componentLocation; 6838 } else { 6839 return null; 6840 } 6841 } 6842 6843 6853 public Point getLocation() { 6854 if (parent != null) { 6855 Rectangle r = parent.getCellRect(row, column, false); 6856 if (r != null) { 6857 return r.getLocation(); 6858 } 6859 } 6860 return null; 6861 } 6862 6863 6866 public void setLocation(Point p) { 6867 } 6871 6872 public Rectangle getBounds() { 6873 if (parent != null) { 6874 return parent.getCellRect(row, column, false); 6875 } else { 6876 return null; 6877 } 6878 } 6879 6880 public void setBounds(Rectangle r) { 6881 AccessibleContext ac = getCurrentAccessibleContext(); 6882 if (ac instanceof AccessibleComponent) { 6883 ((AccessibleComponent) ac).setBounds(r); 6884 } else { 6885 Component c = getCurrentComponent(); 6886 if (c != null) { 6887 c.setBounds(r); 6888 } 6889 } 6890 } 6891 6892 public Dimension getSize() { 6893 if (parent != null) { 6894 Rectangle r = parent.getCellRect(row, column, false); 6895 if (r != null) { 6896 return r.getSize(); 6897 } 6898 } 6899 return null; 6900 } 6901 6902 public void setSize (Dimension d) { 6903 AccessibleContext ac = getCurrentAccessibleContext(); 6904 if (ac instanceof AccessibleComponent) { 6905 ((AccessibleComponent) ac).setSize(d); 6906 } else { 6907 Component c = getCurrentComponent(); 6908 if (c != null) { 6909 c.setSize(d); 6910 } 6911 } 6912 } 6913 6914 public Accessible getAccessibleAt(Point p) { 6915 AccessibleContext ac = getCurrentAccessibleContext(); 6916 if (ac instanceof AccessibleComponent) { 6917 return ((AccessibleComponent) ac).getAccessibleAt(p); 6918 } else { 6919 return null; 6920 } 6921 } 6922 6923 public boolean isFocusTraversable() { 6924 AccessibleContext ac = getCurrentAccessibleContext(); 6925 if (ac instanceof AccessibleComponent) { 6926 return ((AccessibleComponent) ac).isFocusTraversable(); 6927 } else { 6928 Component c = getCurrentComponent(); 6929 if (c != null) { 6930 return c.isFocusTraversable(); 6931 } else { 6932 return false; 6933 } 6934 } 6935 } 6936 6937 public void requestFocus() { 6938 AccessibleContext ac = getCurrentAccessibleContext(); 6939 if (ac instanceof AccessibleComponent) { 6940 ((AccessibleComponent) ac).requestFocus(); 6941 } else { 6942 Component c = getCurrentComponent(); 6943 if (c != null) { 6944 c.requestFocus(); 6945 } 6946 } 6947 } 6948 6949 public void addFocusListener(FocusListener l) { 6950 AccessibleContext ac = getCurrentAccessibleContext(); 6951 if (ac instanceof AccessibleComponent) { 6952 ((AccessibleComponent) ac).addFocusListener(l); 6953 } else { 6954 Component c = getCurrentComponent(); 6955 if (c != null) { 6956 c.addFocusListener(l); 6957 } 6958 } 6959 } 6960 6961 public void removeFocusListener(FocusListener l) { 6962 AccessibleContext ac = getCurrentAccessibleContext(); 6963 if (ac instanceof AccessibleComponent) { 6964 ((AccessibleComponent) ac).removeFocusListener(l); 6965 } else { 6966 Component c = getCurrentComponent(); 6967 if (c != null) { 6968 c.removeFocusListener(l); 6969 } 6970 } 6971 } 6972 6973 } 6975 6977 6980 private class AccessibleJTableHeaderCell extends AccessibleContext 6981 implements Accessible, AccessibleComponent { 6982 6983 private int row; 6984 private int column; 6985 private JTableHeader parent; 6986 private Component rendererComponent; 6987 6988 6996 public AccessibleJTableHeaderCell(int row, int column, 6997 JTableHeader parent, 6998 Component rendererComponent) { 6999 this.row = row; 7000 this.column = column; 7001 this.parent = parent; 7002 this.rendererComponent = rendererComponent; 7003 this.setAccessibleParent(parent); 7004 } 7005 7006 7014 public AccessibleContext getAccessibleContext() { 7015 return this; 7016 } 7017 7018 7022 private AccessibleContext getCurrentAccessibleContext() { 7023 return rendererComponent.getAccessibleContext(); 7024 } 7025 7026 7029 private Component getCurrentComponent() { 7030 return rendererComponent; 7031 } 7032 7033 7035 7041 public String getAccessibleName() { 7042 AccessibleContext ac = getCurrentAccessibleContext(); 7043 if (ac != null) { 7044 String name = ac.getAccessibleName(); 7045 if ((name != null) && (name != "")) { 7046 return ac.getAccessibleName(); 7047 } 7048 } 7049 if ((accessibleName != null) && (accessibleName != "")) { 7050 return accessibleName; 7051 } else { 7052 return null; 7053 } 7054 } 7055 7056 7061 public void setAccessibleName(String s) { 7062 AccessibleContext ac = getCurrentAccessibleContext(); 7063 if (ac != null) { 7064 ac.setAccessibleName(s); 7065 } else { 7066 super.setAccessibleName(s); 7067 } 7068 } 7069 7070 7077 public String getAccessibleDescription() { 7078 AccessibleContext ac = getCurrentAccessibleContext(); 7079 if (ac != null) { 7080 return ac.getAccessibleDescription(); 7081 } else { 7082 return super.getAccessibleDescription(); 7083 } 7084 } 7085 7086 7091 public void setAccessibleDescription(String s) { 7092 AccessibleContext ac = getCurrentAccessibleContext(); 7093 if (ac != null) { 7094 ac.setAccessibleDescription(s); 7095 } else { 7096 super.setAccessibleDescription(s); 7097 } 7098 } 7099 7100 7107 public AccessibleRole getAccessibleRole() { 7108 AccessibleContext ac = getCurrentAccessibleContext(); 7109 if (ac != null) { 7110 return ac.getAccessibleRole(); 7111 } else { 7112 return AccessibleRole.UNKNOWN; 7113 } 7114 } 7115 7116 7123 public AccessibleStateSet getAccessibleStateSet() { 7124 AccessibleContext ac = getCurrentAccessibleContext(); 7125 AccessibleStateSet as = null; 7126 7127 if (ac != null) { 7128 as = ac.getAccessibleStateSet(); 7129 } 7130 if (as == null) { 7131 as = new AccessibleStateSet(); 7132 } 7133 Rectangle rjt = JTable.this.getVisibleRect(); 7134 Rectangle rcell = JTable.this.getCellRect(row, column, false); 7135 if (rjt.intersects(rcell)) { 7136 as.add(AccessibleState.SHOWING); 7137 } else { 7138 if (as.contains(AccessibleState.SHOWING)) { 7139 as.remove(AccessibleState.SHOWING); 7140 } 7141 } 7142 if (JTable.this.isCellSelected(row, column)) { 7143 as.add(AccessibleState.SELECTED); 7144 } else if (as.contains(AccessibleState.SELECTED)) { 7145 as.remove(AccessibleState.SELECTED); 7146 } 7147 if ((row == getSelectedRow()) && (column == getSelectedColumn())) { 7148 as.add(AccessibleState.ACTIVE); 7149 } 7150 as.add(AccessibleState.TRANSIENT); 7151 return as; 7152 } 7153 7154 7161 public Accessible getAccessibleParent() { 7162 return parent; 7163 } 7164 7165 7172 public int getAccessibleIndexInParent() { 7173 return column; 7174 } 7175 7176 7181 public int getAccessibleChildrenCount() { 7182 AccessibleContext ac = getCurrentAccessibleContext(); 7183 if (ac != null) { 7184 return ac.getAccessibleChildrenCount(); 7185 } else { 7186 return 0; 7187 } 7188 } 7189 7190 7197 public Accessible getAccessibleChild(int i) { 7198 AccessibleContext ac = getCurrentAccessibleContext(); 7199 if (ac != null) { 7200 Accessible accessibleChild = ac.getAccessibleChild(i); 7201 ac.setAccessibleParent(this); 7202 return accessibleChild; 7203 } else { 7204 return null; 7205 } 7206 } 7207 7208 7222 public Locale getLocale() { 7223 AccessibleContext ac = getCurrentAccessibleContext(); 7224 if (ac != null) { 7225 return ac.getLocale(); 7226 } else { 7227 return null; 7228 } 7229 } 7230 7231 7238 public void addPropertyChangeListener(PropertyChangeListener l) { 7239 AccessibleContext ac = getCurrentAccessibleContext(); 7240 if (ac != null) { 7241 ac.addPropertyChangeListener(l); 7242 } else { 7243 super.addPropertyChangeListener(l); 7244 } 7245 } 7246 7247 7255 public void removePropertyChangeListener(PropertyChangeListener l) { 7256 AccessibleContext ac = getCurrentAccessibleContext(); 7257 if (ac != null) { 7258 ac.removePropertyChangeListener(l); 7259 } else { 7260 super.removePropertyChangeListener(l); 7261 } 7262 } 7263 7264 7270 public AccessibleAction getAccessibleAction() { 7271 return getCurrentAccessibleContext().getAccessibleAction(); 7272 } 7273 7274 7281 public AccessibleComponent getAccessibleComponent() { 7282 return this; } 7284 7285 7292 public AccessibleSelection getAccessibleSelection() { 7293 return getCurrentAccessibleContext().getAccessibleSelection(); 7294 } 7295 7296 7302 public AccessibleText getAccessibleText() { 7303 return getCurrentAccessibleContext().getAccessibleText(); 7304 } 7305 7306 7312 public AccessibleValue getAccessibleValue() { 7313 return getCurrentAccessibleContext().getAccessibleValue(); 7314 } 7315 7316 7317 7319 7325 public Color getBackground() { 7326 AccessibleContext ac = getCurrentAccessibleContext(); 7327 if (ac instanceof AccessibleComponent) { 7328 return ((AccessibleComponent) ac).getBackground(); 7329 } else { 7330 Component c = getCurrentComponent(); 7331 if (c != null) { 7332 return c.getBackground(); 7333 } else { 7334 return null; 7335 } 7336 } 7337 } 7338 7339 7344 public void setBackground(Color c) { 7345 AccessibleContext ac = getCurrentAccessibleContext(); 7346 if (ac instanceof AccessibleComponent) { 7347 ((AccessibleComponent) ac).setBackground(c); 7348 } else { 7349 Component cp = getCurrentComponent(); 7350 if (cp != null) { 7351 cp.setBackground(c); 7352 } 7353 } 7354 } 7355 7356 7362 public Color getForeground() { 7363 AccessibleContext ac = getCurrentAccessibleContext(); 7364 if (ac instanceof AccessibleComponent) { 7365 return ((AccessibleComponent) ac).getForeground(); 7366 } else { 7367 Component c = getCurrentComponent(); 7368 if (c != null) { 7369 return c.getForeground(); 7370 } else { 7371 return null; 7372 } 7373 } 7374 } 7375 7376 7381 public void setForeground(Color c) { 7382 AccessibleContext ac = getCurrentAccessibleContext(); 7383 if (ac instanceof AccessibleComponent) { 7384 ((AccessibleComponent) ac).setForeground(c); 7385 } else { 7386 Component cp = getCurrentComponent(); 7387 if (cp != null) { 7388 cp.setForeground(c); 7389 } 7390 } 7391 } 7392 7393 7399 public Cursor getCursor() { 7400 AccessibleContext ac = getCurrentAccessibleContext(); 7401 if (ac instanceof AccessibleComponent) { 7402 return ((AccessibleComponent) ac).getCursor(); 7403 } else { 7404 Component c = getCurrentComponent(); 7405 if (c != null) { 7406 return c.getCursor(); 7407 } else { 7408 Accessible ap = getAccessibleParent(); 7409 if (ap instanceof AccessibleComponent) { 7410 return ((AccessibleComponent) ap).getCursor(); 7411 } else { 7412 return null; 7413 } 7414 } 7415 } 7416 } 7417 7418 7423 public void setCursor(Cursor c) { 7424 AccessibleContext ac = getCurrentAccessibleContext(); 7425 if (ac instanceof AccessibleComponent) { 7426 ((AccessibleComponent) ac).setCursor(c); 7427 } else { 7428 Component cp = getCurrentComponent(); 7429 if (cp != null) { 7430 cp.setCursor(c); 7431 } 7432 } 7433 } 7434 7435 7441 public Font getFont() { 7442 AccessibleContext ac = getCurrentAccessibleContext(); 7443 if (ac instanceof AccessibleComponent) { 7444 return ((AccessibleComponent) ac).getFont(); 7445 } else { 7446 Component c = getCurrentComponent(); 7447 if (c != null) { 7448 return c.getFont(); 7449 } else { 7450 return null; 7451 } 7452 } 7453 } 7454 7455 7460 public void setFont(Font f) { 7461 AccessibleContext ac = getCurrentAccessibleContext(); 7462 if (ac instanceof AccessibleComponent) { 7463 ((AccessibleComponent) ac).setFont(f); 7464 } else { 7465 Component c = getCurrentComponent(); 7466 if (c != null) { 7467 c.setFont(f); 7468 } 7469 } 7470 } 7471 7472 7480 public FontMetrics getFontMetrics(Font f) { 7481 AccessibleContext ac = getCurrentAccessibleContext(); 7482 if (ac instanceof AccessibleComponent) { 7483 return ((AccessibleComponent) ac).getFontMetrics(f); 7484 } else { 7485 Component c = getCurrentComponent(); 7486 if (c != null) { 7487 return c.getFontMetrics(f); 7488 } else { 7489 return null; 7490 } 7491 } 7492 } 7493 7494 7499 public boolean isEnabled() { 7500 AccessibleContext ac = getCurrentAccessibleContext(); 7501 if (ac instanceof AccessibleComponent) { 7502 return ((AccessibleComponent) ac).isEnabled(); 7503 } else { 7504 Component c = getCurrentComponent(); 7505 if (c != null) { 7506 return c.isEnabled(); 7507 } else { 7508 return false; 7509 } 7510 } 7511 } 7512 7513 7518 public void setEnabled(boolean b) { 7519 AccessibleContext ac = getCurrentAccessibleContext(); 7520 if (ac instanceof AccessibleComponent) { 7521 ((AccessibleComponent) ac).setEnabled(b); 7522 } else { 7523 Component c = getCurrentComponent(); 7524 if (c != null) { 7525 c.setEnabled(b); 7526 } 7527 } 7528 } 7529 7530 7539 public boolean isVisible() { 7540 AccessibleContext ac = getCurrentAccessibleContext(); 7541 if (ac instanceof AccessibleComponent) { 7542 return ((AccessibleComponent) ac).isVisible(); 7543 } else { 7544 Component c = getCurrentComponent(); 7545 if (c != null) { 7546 return c.isVisible(); 7547 } else { 7548 return false; 7549 } 7550 } 7551 } 7552 7553 7558 public void setVisible(boolean b) { 7559 AccessibleContext ac = getCurrentAccessibleContext(); 7560 if (ac instanceof AccessibleComponent) { 7561 ((AccessibleComponent) ac).setVisible(b); 7562 } else { 7563 Component c = getCurrentComponent(); 7564 if (c != null) { 7565 c.setVisible(b); 7566 } 7567 } 7568 } 7569 7570 7579 public boolean isShowing() { 7580 AccessibleContext ac = getCurrentAccessibleContext(); 7581 if (ac instanceof AccessibleComponent) { 7582 if (ac.getAccessibleParent() != null) { 7583 return ((AccessibleComponent) ac).isShowing(); 7584 } else { 7585 return isVisible(); 7589 } 7590 } else { 7591 Component c = getCurrentComponent(); 7592 if (c != null) { 7593 return c.isShowing(); 7594 } else { 7595 return false; 7596 } 7597 } 7598 } 7599 7600 7611 public boolean contains(Point p) { 7612 AccessibleContext ac = getCurrentAccessibleContext(); 7613 if (ac instanceof AccessibleComponent) { 7614 Rectangle r = ((AccessibleComponent) ac).getBounds(); 7615 return r.contains(p); 7616 } else { 7617 Component c = getCurrentComponent(); 7618 if (c != null) { 7619 Rectangle r = c.getBounds(); 7620 return r.contains(p); 7621 } else { 7622 return getBounds().contains(p); 7623 } 7624 } 7625 } 7626 7627 7633 public Point getLocationOnScreen() { 7634 if (parent != null) { 7635 Point parentLocation = parent.getLocationOnScreen(); 7636 Point componentLocation = getLocation(); 7637 componentLocation.translate(parentLocation.x, parentLocation.y); 7638 return componentLocation; 7639 } else { 7640 return null; 7641 } 7642 } 7643 7644 7654 public Point getLocation() { 7655 if (parent != null) { 7656 Rectangle r = parent.getHeaderRect(column); 7657 if (r != null) { 7658 return r.getLocation(); 7659 } 7660 } 7661 return null; 7662 } 7663 7664 7669 public void setLocation(Point p) { 7670 } 7671 7672 7681 public Rectangle getBounds() { 7682 if (parent != null) { 7683 return parent.getHeaderRect(column); 7684 } else { 7685 return null; 7686 } 7687 } 7688 7689 7697 public void setBounds(Rectangle r) { 7698 AccessibleContext ac = getCurrentAccessibleContext(); 7699 if (ac instanceof AccessibleComponent) { 7700 ((AccessibleComponent) ac).setBounds(r); 7701 } else { 7702 Component c = getCurrentComponent(); 7703 if (c != null) { 7704 c.setBounds(r); 7705 } 7706 } 7707 } 7708 7709 7719 public Dimension getSize() { 7720 if (parent != null) { 7721 Rectangle r = parent.getHeaderRect(column); 7722 if (r != null) { 7723 return r.getSize(); 7724 } 7725 } 7726 return null; 7727 } 7728 7729 7735 public void setSize (Dimension d) { 7736 AccessibleContext ac = getCurrentAccessibleContext(); 7737 if (ac instanceof AccessibleComponent) { 7738 ((AccessibleComponent) ac).setSize(d); 7739 } else { 7740 Component c = getCurrentComponent(); 7741 if (c != null) { 7742 c.setSize(d); 7743 } 7744 } 7745 } 7746 7747 7755 public Accessible getAccessibleAt(Point p) { 7756 AccessibleContext ac = getCurrentAccessibleContext(); 7757 if (ac instanceof AccessibleComponent) { 7758 return ((AccessibleComponent) ac).getAccessibleAt(p); 7759 } else { 7760 return null; 7761 } 7762 } 7763 7764 7775 public boolean isFocusTraversable() { 7776 AccessibleContext ac = getCurrentAccessibleContext(); 7777 if (ac instanceof AccessibleComponent) { 7778 return ((AccessibleComponent) ac).isFocusTraversable(); 7779 } else { 7780 Component c = getCurrentComponent(); 7781 if (c != null) { 7782 return c.isFocusTraversable(); 7783 } else { 7784 return false; 7785 } 7786 } 7787 } 7788 7789 7795 public void requestFocus() { 7796 AccessibleContext ac = getCurrentAccessibleContext(); 7797 if (ac instanceof AccessibleComponent) { 7798 ((AccessibleComponent) ac).requestFocus(); 7799 } else { 7800 Component c = getCurrentComponent(); 7801 if (c != null) { 7802 c.requestFocus(); 7803 } 7804 } 7805 } 7806 7807 7814 public void addFocusListener(FocusListener l) { 7815 AccessibleContext ac = getCurrentAccessibleContext(); 7816 if (ac instanceof AccessibleComponent) { 7817 ((AccessibleComponent) ac).addFocusListener(l); 7818 } else { 7819 Component c = getCurrentComponent(); 7820 if (c != null) { 7821 c.addFocusListener(l); 7822 } 7823 } 7824 } 7825 7826 7833 public void removeFocusListener(FocusListener l) { 7834 AccessibleContext ac = getCurrentAccessibleContext(); 7835 if (ac instanceof AccessibleComponent) { 7836 ((AccessibleComponent) ac).removeFocusListener(l); 7837 } else { 7838 Component c = getCurrentComponent(); 7839 if (c != null) { 7840 c.removeFocusListener(l); 7841 } 7842 } 7843 } 7844 7845 } 7847 } 7849} 7851 7852 7853 | Popular Tags |