1 7 8 package javax.swing; 9 10 import java.awt.event.*; 11 import java.awt.*; 12 13 import java.util.Vector ; 14 import java.util.Locale ; 15 16 import java.beans.*; 17 18 import javax.swing.event.*; 19 import javax.accessibility.*; 20 import javax.swing.plaf.*; 21 import javax.swing.text.Position ; 22 23 import java.io.ObjectOutputStream ; 24 import java.io.ObjectInputStream ; 25 import java.io.IOException ; 26 import java.io.Serializable ; 27 28 29 224 public class JList extends JComponent implements Scrollable , Accessible 225 { 226 230 private static final String uiClassID = "ListUI"; 231 232 237 public static final int VERTICAL = 0; 238 239 245 public static final int VERTICAL_WRAP = 1; 246 247 253 public static final int HORIZONTAL_WRAP = 2; 254 255 private int fixedCellWidth = -1; 256 private int fixedCellHeight = -1; 257 private int horizontalScrollIncrement = -1; 258 private Object prototypeCellValue; 259 private int visibleRowCount = 8; 260 private Color selectionForeground; 261 private Color selectionBackground; 262 private boolean dragEnabled; 263 264 private ListSelectionModel selectionModel; 265 private ListModel dataModel; 266 private ListCellRenderer cellRenderer; 267 private ListSelectionListener selectionListener; 268 269 272 private int layoutOrientation; 273 274 283 public JList(ListModel dataModel) 284 { 285 if (dataModel == null) { 286 throw new IllegalArgumentException ("dataModel must be non null"); 287 } 288 289 ToolTipManager toolTipManager = ToolTipManager.sharedInstance(); 292 toolTipManager.registerComponent(this); 293 294 layoutOrientation = VERTICAL; 295 296 this.dataModel = dataModel; 297 selectionModel = createSelectionModel(); 298 setAutoscrolls(true); 299 setOpaque(true); 300 updateUI(); 301 } 302 303 304 311 public JList(final Object [] listData) 312 { 313 this ( 314 new AbstractListModel () { 315 public int getSize() { return listData.length; } 316 public Object getElementAt(int i) { return listData[i]; } 317 } 318 ); 319 } 320 321 322 330 public JList(final Vector <?> listData) { 331 this ( 332 new AbstractListModel () { 333 public int getSize() { return listData.size(); } 334 public Object getElementAt(int i) { return listData.elementAt(i); } 335 } 336 ); 337 } 338 339 340 343 public JList() { 344 this ( 345 new AbstractListModel () { 346 public int getSize() { return 0; } 347 public Object getElementAt(int i) { return "No Data Model"; } 348 } 349 ); 350 } 351 352 353 358 public ListUI getUI() { 359 return (ListUI)ui; 360 } 361 362 363 374 public void setUI(ListUI ui) { 375 super.setUI(ui); 376 } 377 378 379 384 public void updateUI() { 385 setUI((ListUI)UIManager.getUI(this)); 386 invalidate(); 387 } 388 389 390 398 public String getUIClassID() { 399 return uiClassID; 400 } 401 402 403 414 private void updateFixedCellSize() 415 { 416 ListCellRenderer cr = getCellRenderer(); 417 Object value = getPrototypeCellValue(); 418 419 if ((cr != null) && (value != null)) { 420 Component c = cr.getListCellRendererComponent(this, value, 0, false, false); 421 422 428 Font f = c.getFont(); 429 c.setFont(getFont()); 430 431 Dimension d = c.getPreferredSize(); 432 fixedCellWidth = d.width; 433 fixedCellHeight = d.height; 434 435 c.setFont(f); 436 } 437 } 438 439 440 448 public Object getPrototypeCellValue() { 449 return prototypeCellValue; 450 } 451 452 485 public void setPrototypeCellValue(Object prototypeCellValue) { 486 Object oldValue = this.prototypeCellValue; 487 this.prototypeCellValue = prototypeCellValue; 488 489 492 493 if ((prototypeCellValue != null) && !prototypeCellValue.equals(oldValue)) { 494 updateFixedCellSize(); 495 } 496 497 firePropertyChange("prototypeCellValue", oldValue, prototypeCellValue); 498 } 499 500 501 509 public int getFixedCellWidth() { 510 return fixedCellWidth; 511 } 512 513 531 public void setFixedCellWidth(int width) { 532 int oldValue = fixedCellWidth; 533 fixedCellWidth = width; 534 firePropertyChange("fixedCellWidth", oldValue, fixedCellWidth); 535 } 536 537 538 546 public int getFixedCellHeight() { 547 return fixedCellHeight; 548 } 549 550 570 public void setFixedCellHeight(int height) { 571 int oldValue = fixedCellHeight; 572 fixedCellHeight = height; 573 firePropertyChange("fixedCellHeight", oldValue, fixedCellHeight); 574 } 575 576 577 583 public ListCellRenderer getCellRenderer() { 584 return cellRenderer; 585 } 586 587 610 public void setCellRenderer(ListCellRenderer cellRenderer) { 611 ListCellRenderer oldValue = this.cellRenderer; 612 this.cellRenderer = cellRenderer; 613 614 617 if ((cellRenderer != null) && !cellRenderer.equals(oldValue)) { 618 updateFixedCellSize(); 619 } 620 621 firePropertyChange("cellRenderer", oldValue, cellRenderer); 622 } 623 624 625 632 public Color getSelectionForeground() { 633 return selectionForeground; 634 } 635 636 637 659 public void setSelectionForeground(Color selectionForeground) { 660 Color oldValue = this.selectionForeground; 661 this.selectionForeground = selectionForeground; 662 firePropertyChange("selectionForeground", oldValue, selectionForeground); 663 } 664 665 666 674 public Color getSelectionBackground() { 675 return selectionBackground; 676 } 677 678 679 700 public void setSelectionBackground(Color selectionBackground) { 701 Color oldValue = this.selectionBackground; 702 this.selectionBackground = selectionBackground; 703 firePropertyChange("selectionBackground", oldValue, selectionBackground); 704 } 705 706 707 714 public int getVisibleRowCount() { 715 return visibleRowCount; 716 } 717 718 739 public void setVisibleRowCount(int visibleRowCount) { 740 int oldValue = this.visibleRowCount; 741 this.visibleRowCount = Math.max(0, visibleRowCount); 742 firePropertyChange("visibleRowCount", oldValue, visibleRowCount); 743 } 744 745 746 758 public int getLayoutOrientation() { 759 return layoutOrientation; 760 } 761 762 763 825 public void setLayoutOrientation(int layoutOrientation) { 826 int oldValue = this.layoutOrientation; 827 switch (layoutOrientation) { 828 case VERTICAL: 829 case VERTICAL_WRAP: 830 case HORIZONTAL_WRAP: 831 this.layoutOrientation = layoutOrientation; 832 firePropertyChange("layoutOrientation", oldValue, layoutOrientation); 833 break; 834 default: 835 throw new IllegalArgumentException ("layoutOrientation must be one of: VERTICAL, HORIZONTAL_WRAP or VERTICAL_WRAP"); 836 } 837 } 838 839 840 854 public int getFirstVisibleIndex() { 855 Rectangle r = getVisibleRect(); 856 int first; 857 if (this.getComponentOrientation().isLeftToRight()) { 858 first = locationToIndex(r.getLocation()); 859 } else { 860 first = locationToIndex(new Point((r.x + r.width) - 1, r.y)); 861 } 862 if (first != -1) { 863 Rectangle bounds = getCellBounds(first, first); 864 if (bounds != null) { 865 SwingUtilities.computeIntersection(r.x, r.y, r.width, r.height, bounds); 866 if (bounds.width == 0 || bounds.height == 0) { 867 first = -1; 868 } 869 } 870 } 871 return first; 872 } 873 874 875 889 public int getLastVisibleIndex() { 890 boolean leftToRight = this.getComponentOrientation().isLeftToRight(); 891 Rectangle r = getVisibleRect(); 892 Point lastPoint; 893 if (leftToRight) { 894 lastPoint = new Point((r.x + r.width) - 1, (r.y + r.height) - 1); 895 } else { 896 lastPoint = new Point(r.x, (r.y + r.height) - 1); 897 } 898 int location = locationToIndex(lastPoint); 899 900 if (location != -1) { 901 Rectangle bounds = getCellBounds(location, location); 902 903 if (bounds != null) { 904 SwingUtilities.computeIntersection(r.x, r.y, r.width, r.height, bounds); 905 if (bounds.width == 0 || bounds.height == 0) { 906 Point visibleLL = new Point(r.x, lastPoint.y); 909 int last; 910 int llIndex = -1; 911 int lrIndex = location; 912 location = -1; 913 914 do { 915 last = llIndex; 916 llIndex = locationToIndex(visibleLL); 917 918 if (llIndex != -1) { 919 bounds = getCellBounds(llIndex, llIndex); 920 if (llIndex != lrIndex && bounds != null && 921 bounds.contains(visibleLL)) { 922 location = llIndex; 923 visibleLL.x = bounds.x + bounds.width + 1; 924 if (visibleLL.x >= lastPoint.x) { 925 last = llIndex; 927 } 928 } 929 else { 930 last = llIndex; 931 } 932 } 933 } while (llIndex != -1 && last != llIndex); 934 } 935 } 936 } 937 return location; 938 } 939 940 941 950 public void ensureIndexIsVisible(int index) { 951 Rectangle cellBounds = getCellBounds(index, index); 952 if (cellBounds != null) { 953 scrollRectToVisible(cellBounds); 954 } 955 } 956 957 1001 public void setDragEnabled(boolean b) { 1002 if (b && GraphicsEnvironment.isHeadless()) { 1003 throw new HeadlessException(); 1004 } 1005 dragEnabled = b; 1006 } 1007 1008 1015 public boolean getDragEnabled() { 1016 return dragEnabled; 1017 } 1018 1019 1033 public int getNextMatch(String prefix, int startIndex, Position.Bias bias) { 1034 ListModel model = getModel(); 1035 int max = model.getSize(); 1036 if (prefix == null) { 1037 throw new IllegalArgumentException (); 1038 } 1039 if (startIndex < 0 || startIndex >= max) { 1040 throw new IllegalArgumentException (); 1041 } 1042 prefix = prefix.toUpperCase(); 1043 1044 int increment = (bias == Position.Bias.Forward) ? 1 : -1; 1046 int index = startIndex; 1047 do { 1048 Object o = model.getElementAt(index); 1049 1050 if (o != null) { 1051 String string; 1052 1053 if (o instanceof String ) { 1054 string = ((String )o).toUpperCase(); 1055 } 1056 else { 1057 string = o.toString(); 1058 if (string != null) { 1059 string = string.toUpperCase(); 1060 } 1061 } 1062 1063 if (string != null && string.startsWith(prefix)) { 1064 return index; 1065 } 1066 } 1067 index = (index + increment + max) % max; 1068 } while (index != startIndex); 1069 return -1; 1070 } 1071 1072 1088 public String getToolTipText(MouseEvent event) { 1089 if(event != null) { 1090 Point p = event.getPoint(); 1091 int index = locationToIndex(p); 1092 ListCellRenderer r = getCellRenderer(); 1093 Rectangle cellBounds; 1094 1095 if (index != -1 && r != null && (cellBounds = 1096 getCellBounds(index, index)) != null && 1097 cellBounds.contains(p.x, p.y)) { 1098 ListSelectionModel lsm = getSelectionModel(); 1099 Component rComponent = r.getListCellRendererComponent( 1100 this, getModel().getElementAt(index), index, 1101 lsm.isSelectedIndex(index), 1102 (hasFocus() && (lsm.getLeadSelectionIndex() == 1103 index))); 1104 1105 if(rComponent instanceof JComponent ) { 1106 MouseEvent newEvent; 1107 1108 p.translate(-cellBounds.x, -cellBounds.y); 1109 newEvent = new MouseEvent(rComponent, event.getID(), 1110 event.getWhen(), 1111 event.getModifiers(), 1112 p.x, p.y, event.getClickCount(), 1113 event.isPopupTrigger()); 1114 1115 String tip = ((JComponent )rComponent).getToolTipText( 1116 newEvent); 1117 1118 if (tip != null) { 1119 return tip; 1120 } 1121 } 1122 } 1123 } 1124 return super.getToolTipText(); 1125 } 1126 1127 1130 1131 1132 1142 public int locationToIndex(Point location) { 1143 ListUI ui = getUI(); 1144 return (ui != null) ? ui.locationToIndex(this, location) : -1; 1145 } 1146 1147 1148 1155 public Point indexToLocation(int index) { 1156 ListUI ui = getUI(); 1157 return (ui != null) ? ui.indexToLocation(this, index) : null; 1158 } 1159 1160 1161 1169 public Rectangle getCellBounds(int index0, int index1) { 1170 ListUI ui = getUI(); 1171 return (ui != null) ? ui.getCellBounds(this, index0, index1) : null; 1172 } 1173 1174 1175 1178 1179 1180 1188 public ListModel getModel() { 1189 return dataModel; 1190 } 1191 1192 1209 public void setModel(ListModel model) { 1210 if (model == null) { 1211 throw new IllegalArgumentException ("model must be non null"); 1212 } 1213 ListModel oldValue = dataModel; 1214 dataModel = model; 1215 firePropertyChange("model", oldValue, dataModel); 1216 clearSelection(); 1217 } 1218 1219 1220 1228 public void setListData(final Object [] listData) { 1229 setModel ( 1230 new AbstractListModel () { 1231 public int getSize() { return listData.length; } 1232 public Object getElementAt(int i) { return listData[i]; } 1233 } 1234 ); 1235 } 1236 1237 1238 1246 public void setListData(final Vector <?> listData) { 1247 setModel ( 1248 new AbstractListModel () { 1249 public int getSize() { return listData.size(); } 1250 public Object getElementAt(int i) { return listData.elementAt(i); } 1251 } 1252 ); 1253 } 1254 1255 1256 1259 1260 1261 1271 protected ListSelectionModel createSelectionModel() { 1272 return new DefaultListSelectionModel (); 1273 } 1274 1275 1276 1286 public ListSelectionModel getSelectionModel() { 1287 return selectionModel; 1288 } 1289 1290 1291 1305 protected void fireSelectionValueChanged(int firstIndex, int lastIndex, 1306 boolean isAdjusting) 1307 { 1308 Object [] listeners = listenerList.getListenerList(); 1309 ListSelectionEvent e = null; 1310 1311 for (int i = listeners.length - 2; i >= 0; i -= 2) { 1312 if (listeners[i] == ListSelectionListener.class) { 1313 if (e == null) { 1314 e = new ListSelectionEvent(this, firstIndex, lastIndex, 1315 isAdjusting); 1316 } 1317 ((ListSelectionListener)listeners[i+1]).valueChanged(e); 1318 } 1319 } 1320 } 1321 1322 1323 1328 private class ListSelectionHandler implements ListSelectionListener, Serializable 1329 { 1330 public void valueChanged(ListSelectionEvent e) { 1331 fireSelectionValueChanged(e.getFirstIndex(), 1332 e.getLastIndex(), 1333 e.getValueIsAdjusting()); 1334 } 1335 } 1336 1337 1338 1350 public void addListSelectionListener(ListSelectionListener listener) 1351 { 1352 if (selectionListener == null) { 1353 selectionListener = new ListSelectionHandler(); 1354 getSelectionModel().addListSelectionListener(selectionListener); 1355 } 1356 1357 listenerList.add(ListSelectionListener.class, listener); 1358 } 1359 1360 1361 1369 public void removeListSelectionListener(ListSelectionListener listener) { 1370 listenerList.remove(ListSelectionListener.class, listener); 1371 } 1372 1373 1374 1383 public ListSelectionListener[] getListSelectionListeners() { 1384 return (ListSelectionListener[])listenerList.getListeners( 1385 ListSelectionListener.class); 1386 } 1387 1388 1389 1407 public void setSelectionModel(ListSelectionModel selectionModel) { 1408 if (selectionModel == null) { 1409 throw new IllegalArgumentException ("selectionModel must be non null"); 1410 } 1411 1412 1415 if (selectionListener != null) { 1416 this.selectionModel.removeListSelectionListener(selectionListener); 1417 selectionModel.addListSelectionListener(selectionListener); 1418 } 1419 1420 ListSelectionModel oldValue = this.selectionModel; 1421 this.selectionModel = selectionModel; 1422 firePropertyChange("selectionModel", oldValue, selectionModel); 1423 } 1424 1425 1426 1456 public void setSelectionMode(int selectionMode) { 1457 getSelectionModel().setSelectionMode(selectionMode); 1458 } 1459 1460 1466 public int getSelectionMode() { 1467 return getSelectionModel().getSelectionMode(); 1468 } 1469 1470 1471 1483 public int getAnchorSelectionIndex() { 1484 return getSelectionModel().getAnchorSelectionIndex(); 1485 } 1486 1487 1488 1503 public int getLeadSelectionIndex() { 1504 return getSelectionModel().getLeadSelectionIndex(); 1505 } 1506 1507 1508 1517 public int getMinSelectionIndex() { 1518 return getSelectionModel().getMinSelectionIndex(); 1519 } 1520 1521 1522 1531 public int getMaxSelectionIndex() { 1532 return getSelectionModel().getMaxSelectionIndex(); 1533 } 1534 1535 1536 1547 public boolean isSelectedIndex(int index) { 1548 return getSelectionModel().isSelectedIndex(index); 1549 } 1550 1551 1552 1562 public boolean isSelectionEmpty() { 1563 return getSelectionModel().isSelectionEmpty(); 1564 } 1565 1566 1567 1577 public void clearSelection() { 1578 getSelectionModel().clearSelection(); 1579 } 1580 1581 1582 1603 public void setSelectionInterval(int anchor, int lead) { 1604 getSelectionModel().setSelectionInterval(anchor, lead); 1605 } 1606 1607 1608 1629 public void addSelectionInterval(int anchor, int lead) { 1630 getSelectionModel().addSelectionInterval(anchor, lead); 1631 } 1632 1633 1634 1656 public void removeSelectionInterval(int index0, int index1) { 1657 getSelectionModel().removeSelectionInterval(index0, index1); 1658 } 1659 1660 1661 1670 public void setValueIsAdjusting(boolean b) { 1671 getSelectionModel().setValueIsAdjusting(b); 1672 } 1673 1674 1675 1683 public boolean getValueIsAdjusting() { 1684 return getSelectionModel().getValueIsAdjusting(); 1685 } 1686 1687 1688 1696 public int[] getSelectedIndices() { 1697 ListSelectionModel sm = getSelectionModel(); 1698 int iMin = sm.getMinSelectionIndex(); 1699 int iMax = sm.getMaxSelectionIndex(); 1700 1701 if ((iMin < 0) || (iMax < 0)) { 1702 return new int[0]; 1703 } 1704 1705 int[] rvTmp = new int[1+ (iMax - iMin)]; 1706 int n = 0; 1707 for(int i = iMin; i <= iMax; i++) { 1708 if (sm.isSelectedIndex(i)) { 1709 rvTmp[n++] = i; 1710 } 1711 } 1712 int[] rv = new int[n]; 1713 System.arraycopy(rvTmp, 0, rv, 0, n); 1714 return rv; 1715 } 1716 1717 1718 1728 public void setSelectedIndex(int index) { 1729 if (index >= getModel().getSize()) { 1730 return; 1731 } 1732 getSelectionModel().setSelectionInterval(index, index); 1733 } 1734 1735 1736 1744 public void setSelectedIndices(int[] indices) { 1745 ListSelectionModel sm = getSelectionModel(); 1746 sm.clearSelection(); 1747 int size = getModel().getSize(); 1748 for(int i = 0; i < indices.length; i++) { 1749 if (indices[i] < size) { 1750 sm.addSelectionInterval(indices[i], indices[i]); 1751 } 1752 } 1753 } 1754 1755 1756 1766 public Object [] getSelectedValues() { 1767 ListSelectionModel sm = getSelectionModel(); 1768 ListModel dm = getModel(); 1769 1770 int iMin = sm.getMinSelectionIndex(); 1771 int iMax = sm.getMaxSelectionIndex(); 1772 1773 if ((iMin < 0) || (iMax < 0)) { 1774 return new Object [0]; 1775 } 1776 1777 Object [] rvTmp = new Object [1+ (iMax - iMin)]; 1778 int n = 0; 1779 for(int i = iMin; i <= iMax; i++) { 1780 if (sm.isSelectedIndex(i)) { 1781 rvTmp[n++] = dm.getElementAt(i); 1782 } 1783 } 1784 Object [] rv = new Object [n]; 1785 System.arraycopy(rvTmp, 0, rv, 0, n); 1786 return rv; 1787 } 1788 1789 1790 1798 public int getSelectedIndex() { 1799 return getMinSelectionIndex(); 1800 } 1801 1802 1803 1812 public Object getSelectedValue() { 1813 int i = getMinSelectionIndex(); 1814 return (i == -1) ? null : getModel().getElementAt(i); 1815 } 1816 1817 1818 1826 public void setSelectedValue(Object anObject,boolean shouldScroll) { 1827 if(anObject == null) 1828 setSelectedIndex(-1); 1829 else if(!anObject.equals(getSelectedValue())) { 1830 int i,c; 1831 ListModel dm = getModel(); 1832 for(i=0,c=dm.getSize();i<c;i++) 1833 if(anObject.equals(dm.getElementAt(i))){ 1834 setSelectedIndex(i); 1835 if(shouldScroll) 1836 ensureIndexIsVisible(i); 1837 repaint(); 1838 return; 1839 } 1840 setSelectedIndex(-1); 1841 } 1842 repaint(); 1843 } 1844 1845 1846 1847 1850 1851 private void checkScrollableParameters(Rectangle visibleRect, int orientation) { 1852 if (visibleRect == null) { 1853 throw new IllegalArgumentException ("visibleRect must be non-null"); 1854 } 1855 switch (orientation) { 1856 case SwingConstants.VERTICAL: 1857 case SwingConstants.HORIZONTAL: 1858 break; 1859 default: 1860 throw new IllegalArgumentException ("orientation must be one of: VERTICAL, HORIZONTAL"); 1861 } 1862 } 1863 1864 1865 1896 public Dimension getPreferredScrollableViewportSize() 1897 { 1898 if (getLayoutOrientation() != VERTICAL) { 1899 return getPreferredSize(); 1900 } 1901 Insets insets = getInsets(); 1902 int dx = insets.left + insets.right; 1903 int dy = insets.top + insets.bottom; 1904 1905 int visibleRowCount = getVisibleRowCount(); 1906 int fixedCellWidth = getFixedCellWidth(); 1907 int fixedCellHeight = getFixedCellHeight(); 1908 1909 if ((fixedCellWidth > 0) && (fixedCellHeight > 0)) { 1910 int width = fixedCellWidth + dx; 1911 int height = (visibleRowCount * fixedCellHeight) + dy; 1912 return new Dimension(width, height); 1913 } 1914 else if (getModel().getSize() > 0) { 1915 int width = getPreferredSize().width; 1916 int height; 1917 Rectangle r = getCellBounds(0, 0); 1918 if (r != null) { 1919 height = (visibleRowCount * r.height) + dy; 1920 } 1921 else { 1922 height = 1; 1924 } 1925 return new Dimension(width, height); 1926 } 1927 else { 1928 fixedCellWidth = (fixedCellWidth > 0) ? fixedCellWidth : 256; 1929 fixedCellHeight = (fixedCellHeight > 0) ? fixedCellHeight : 16; 1930 return new Dimension(fixedCellWidth, fixedCellHeight * visibleRowCount); 1931 } 1932 } 1933 1934 1935 1957 public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) 1958 { 1959 checkScrollableParameters(visibleRect, orientation); 1960 1961 if (orientation == SwingConstants.VERTICAL) { 1962 int row = getFirstVisibleIndex(); 1963 1964 if (row == -1) { 1965 return 0; 1966 } 1967 else { 1968 1969 if (direction > 0) { 1970 Rectangle r = getCellBounds(row, row); 1971 return (r == null) ? 0 : r.height - (visibleRect.y - r.y); 1972 } 1973 1974 else { 1975 Rectangle r = getCellBounds(row, row); 1976 1977 1980 if ((r.y == visibleRect.y) && (row == 0)) { 1981 return 0; 1982 } 1983 1987 else if (r.y == visibleRect.y) { 1988 Point loc = r.getLocation(); 1989 loc.y--; 1990 int prevIndex = locationToIndex(loc); 1991 Rectangle prevR = getCellBounds(prevIndex, prevIndex); 1992 1993 if (prevR == null || prevR.y >= r.y) { 1994 return 0; 1995 } 1996 return prevR.height; 1997 } 1998 2001 else { 2002 return visibleRect.y - r.y; 2003 } 2004 } 2005 } 2006 } else if (orientation == SwingConstants.HORIZONTAL && 2007 getLayoutOrientation() != JList.VERTICAL) { 2008 int index = locationToIndex(visibleRect.getLocation()); 2009 2010 if (index != -1) { 2011 Rectangle cellBounds = getCellBounds(index, index); 2012 2013 if (cellBounds != null) { 2014 if (cellBounds.x != visibleRect.x) { 2015 if (direction < 0) { 2016 return Math.abs(cellBounds.x - visibleRect.x); 2017 } 2018 return cellBounds.width + cellBounds.x - visibleRect.x; 2019 } 2020 return cellBounds.width; 2021 } 2022 } 2023 } 2024 Font f = getFont(); 2025 return (f != null) ? f.getSize() : 1; 2026 } 2027 2028 2029 2068 public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { 2069 checkScrollableParameters(visibleRect, orientation); 2070 if (orientation == SwingConstants.VERTICAL) { 2071 int inc = visibleRect.height; 2072 2073 if (direction > 0) { 2074 int last = locationToIndex(new Point(visibleRect.x, visibleRect.y+visibleRect.height-1)); 2076 if (last != -1) { 2077 Rectangle lastRect = getCellBounds(last,last); 2078 if (lastRect != null) { 2079 inc = lastRect.y - visibleRect.y; 2080 if ( (inc == 0) && (last < getModel().getSize()-1) ) { 2081 inc = lastRect.height; 2082 } 2083 } 2084 } 2085 } 2086 2087 else { 2088 int newFirst = locationToIndex(new Point(visibleRect.x, visibleRect.y-visibleRect.height)); 2089 int first = getFirstVisibleIndex(); 2090 if (newFirst != -1) { 2091 if (first == -1) { 2092 first = locationToIndex(visibleRect.getLocation()); 2093 } 2094 Rectangle newFirstRect = getCellBounds(newFirst,newFirst); 2095 Rectangle firstRect = getCellBounds(first,first); 2096 if ((newFirstRect != null) && (firstRect!=null)) { 2097 while ( (newFirstRect.y + visibleRect.height < 2098 firstRect.y + firstRect.height) && 2099 (newFirstRect.y < firstRect.y) ) { 2100 newFirst++; 2101 newFirstRect = getCellBounds(newFirst,newFirst); 2102 } 2103 inc = visibleRect.y - newFirstRect.y; 2104 if ( (inc <= 0) && (newFirstRect.y > 0)) { 2105 newFirst--; 2106 newFirstRect = getCellBounds(newFirst,newFirst); 2107 if (newFirstRect != null) { 2108 inc = visibleRect.y - newFirstRect.y; 2109 } 2110 } 2111 } 2112 } 2113 } 2114 return inc; 2115 } 2116 else if (orientation == SwingConstants.HORIZONTAL && 2117 getLayoutOrientation() != JList.VERTICAL) { 2118 int inc = visibleRect.width; 2119 2120 if (direction > 0) { 2121 int last = locationToIndex(new Point(visibleRect.x + visibleRect.width - 1, 2123 visibleRect.y)); 2124 if (last != -1) { 2125 Rectangle lastRect = getCellBounds(last,last); 2126 if (lastRect != null) { 2127 inc = lastRect.x - visibleRect.x; 2128 if (inc < 0) { 2129 inc += lastRect.width; 2130 } else if ( (inc == 0) && (last < getModel().getSize()-1) ) { 2131 inc = lastRect.width; 2132 } 2133 } 2134 } 2135 } 2136 2137 else { 2138 int first = locationToIndex(new Point(visibleRect.x - visibleRect.width, 2141 visibleRect.y)); 2142 if (first != -1) { 2143 Rectangle firstRect = getCellBounds(first,first); 2144 if (firstRect != null) { 2145 if (firstRect.x < visibleRect.x - visibleRect.width) { 2146 if (firstRect.x + firstRect.width >= visibleRect.x) { 2147 inc = visibleRect.x - firstRect.x; 2148 } else { 2149 inc = visibleRect.x - firstRect.x - firstRect.width; 2150 } 2151 } else { 2152 inc = visibleRect.x - firstRect.x; 2153 } 2154 } 2155 } 2156 } 2157 return inc; 2158 } 2159 return visibleRect.width; 2160 } 2161 2162 2163 2178 public boolean getScrollableTracksViewportWidth() { 2179 if (getLayoutOrientation() == HORIZONTAL_WRAP && 2180 getVisibleRowCount() <= 0) { 2181 return true; 2182 } 2183 if (getParent() instanceof JViewport ) { 2184 return (((JViewport )getParent()).getWidth() > getPreferredSize().width); 2185 } 2186 return false; 2187 } 2188 2189 2203 public boolean getScrollableTracksViewportHeight() { 2204 if (getLayoutOrientation() == VERTICAL_WRAP && 2205 getVisibleRowCount() <= 0) { 2206 return true; 2207 } 2208 if (getParent() instanceof JViewport ) { 2209 return (((JViewport )getParent()).getHeight() > getPreferredSize().height); 2210 } 2211 return false; 2212 } 2213 2214 2215 2219 private void writeObject(ObjectOutputStream s) throws IOException { 2220 s.defaultWriteObject(); 2221 if (getUIClassID().equals(uiClassID)) { 2222 byte count = JComponent.getWriteObjCounter(this); 2223 JComponent.setWriteObjCounter(this, --count); 2224 if (count == 0 && ui != null) { 2225 ui.installUI(this); 2226 } 2227 } 2228 } 2229 2230 2231 2241 protected String paramString() { 2242 String selectionForegroundString = (selectionForeground != null ? 2243 selectionForeground.toString() : 2244 ""); 2245 String selectionBackgroundString = (selectionBackground != null ? 2246 selectionBackground.toString() : 2247 ""); 2248 2249 return super.paramString() + 2250 ",fixedCellHeight=" + fixedCellHeight + 2251 ",fixedCellWidth=" + fixedCellWidth + 2252 ",horizontalScrollIncrement=" + horizontalScrollIncrement + 2253 ",selectionBackground=" + selectionBackgroundString + 2254 ",selectionForeground=" + selectionForegroundString + 2255 ",visibleRowCount=" + visibleRowCount + 2256 ",layoutOrientation=" + layoutOrientation; 2257 } 2258 2259 2260 2263 2264 2273 public AccessibleContext getAccessibleContext() { 2274 if (accessibleContext == null) { 2275 accessibleContext = new AccessibleJList(); 2276 } 2277 return accessibleContext; 2278 } 2279 2280 2295 protected class AccessibleJList extends AccessibleJComponent 2296 implements AccessibleSelection, PropertyChangeListener, 2297 ListSelectionListener, ListDataListener { 2298 2299 int leadSelectionIndex; 2300 2301 public AccessibleJList() { 2302 super(); 2303 JList.this.addPropertyChangeListener(this); 2304 JList.this.getSelectionModel().addListSelectionListener(this); 2305 JList.this.getModel().addListDataListener(this); 2306 leadSelectionIndex = JList.this.getLeadSelectionIndex(); 2307 } 2308 2309 2317 public void propertyChange(PropertyChangeEvent e) { 2318 String name = e.getPropertyName(); 2319 Object oldValue = e.getOldValue(); 2320 Object newValue = e.getNewValue(); 2321 2322 if (name.compareTo("model") == 0) { 2324 2325 if (oldValue != null && oldValue instanceof ListModel ) { 2326 ((ListModel ) oldValue).removeListDataListener(this); 2327 } 2328 if (newValue != null && newValue instanceof ListModel ) { 2329 ((ListModel ) newValue).addListDataListener(this); 2330 } 2331 2332 } else if (name.compareTo("selectionModel") == 0) { 2334 2335 if (oldValue != null && oldValue instanceof ListSelectionModel ) { 2336 ((ListSelectionModel ) oldValue).removeListSelectionListener(this); 2337 } 2338 if (newValue != null && newValue instanceof ListSelectionModel ) { 2339 ((ListSelectionModel ) newValue).addListSelectionListener(this); 2340 } 2341 2342 firePropertyChange( 2343 AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY, 2344 Boolean.valueOf(false), Boolean.valueOf(true)); 2345 } 2346 } 2347 2348 2355 public void valueChanged(ListSelectionEvent e) { 2356 int oldLeadSelectionIndex = leadSelectionIndex; 2357 leadSelectionIndex = JList.this.getLeadSelectionIndex(); 2358 if (oldLeadSelectionIndex != leadSelectionIndex) { 2359 Accessible oldLS, newLS; 2360 oldLS = (oldLeadSelectionIndex >= 0) 2361 ? getAccessibleChild(oldLeadSelectionIndex) 2362 : null; 2363 newLS = (leadSelectionIndex >= 0) 2364 ? getAccessibleChild(leadSelectionIndex) 2365 : null; 2366 firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, 2367 oldLS, newLS); 2368 } 2369 2370 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 2371 Boolean.valueOf(false), Boolean.valueOf(true)); 2372 firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY, 2373 Boolean.valueOf(false), Boolean.valueOf(true)); 2374 2375 AccessibleStateSet s = getAccessibleStateSet(); 2377 ListSelectionModel lsm = JList.this.getSelectionModel(); 2378 if (lsm.getSelectionMode() != ListSelectionModel.SINGLE_SELECTION) { 2379 if (!s.contains(AccessibleState.MULTISELECTABLE)) { 2380 s.add(AccessibleState.MULTISELECTABLE); 2381 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 2382 null, AccessibleState.MULTISELECTABLE); 2383 } 2384 } else { 2385 if (s.contains(AccessibleState.MULTISELECTABLE)) { 2386 s.remove(AccessibleState.MULTISELECTABLE); 2387 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 2388 AccessibleState.MULTISELECTABLE, null); 2389 } 2390 } 2391 } 2392 2393 2399 public void intervalAdded(ListDataEvent e) { 2400 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 2401 Boolean.valueOf(false), Boolean.valueOf(true)); 2402 } 2403 2404 2410 public void intervalRemoved(ListDataEvent e) { 2411 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 2412 Boolean.valueOf(false), Boolean.valueOf(true)); 2413 } 2414 2415 2421 public void contentsChanged(ListDataEvent e) { 2422 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 2423 Boolean.valueOf(false), Boolean.valueOf(true)); 2424 } 2425 2426 2428 2435 public AccessibleStateSet getAccessibleStateSet() { 2436 AccessibleStateSet states = super.getAccessibleStateSet(); 2437 if (selectionModel.getSelectionMode() != 2438 ListSelectionModel.SINGLE_SELECTION) { 2439 states.add(AccessibleState.MULTISELECTABLE); 2440 } 2441 return states; 2442 } 2443 2444 2451 public AccessibleRole getAccessibleRole() { 2452 return AccessibleRole.LIST; 2453 } 2454 2455 2463 public Accessible getAccessibleAt(Point p) { 2464 int i = locationToIndex(p); 2465 if (i >= 0) { 2466 return new AccessibleJListChild(JList.this, i); 2467 } else { 2468 return null; 2469 } 2470 } 2471 2472 2479 public int getAccessibleChildrenCount() { 2480 return getModel().getSize(); 2481 } 2482 2483 2489 public Accessible getAccessibleChild(int i) { 2490 if (i >= getModel().getSize()) { 2491 return null; 2492 } else { 2493 return new AccessibleJListChild(JList.this, i); 2494 } 2495 } 2496 2497 2505 public AccessibleSelection getAccessibleSelection() { 2506 return this; 2507 } 2508 2509 2510 2512 2518 public int getAccessibleSelectionCount() { 2519 return JList.this.getSelectedIndices().length; 2520 } 2521 2522 2531 public Accessible getAccessibleSelection(int i) { 2532 int len = getAccessibleSelectionCount(); 2533 if (i < 0 || i >= len) { 2534 return null; 2535 } else { 2536 return getAccessibleChild(JList.this.getSelectedIndices()[i]); 2537 } 2538 } 2539 2540 2547 public boolean isAccessibleChildSelected(int i) { 2548 return isSelectedIndex(i); 2549 } 2550 2551 2560 public void addAccessibleSelection(int i) { 2561 JList.this.addSelectionInterval(i, i); 2562 } 2563 2564 2571 public void removeAccessibleSelection(int i) { 2572 JList.this.removeSelectionInterval(i, i); 2573 } 2574 2575 2579 public void clearAccessibleSelection() { 2580 JList.this.clearSelection(); 2581 } 2582 2583 2587 public void selectAllAccessibleSelection() { 2588 JList.this.addSelectionInterval(0, getAccessibleChildrenCount() -1); 2589 } 2590 2591 2595 protected class AccessibleJListChild extends AccessibleContext 2596 implements Accessible, AccessibleComponent { 2597 private JList parent = null; 2598 private int indexInParent; 2599 private Component component = null; 2600 private AccessibleContext accessibleContext = null; 2601 private ListModel listModel; 2602 private ListCellRenderer cellRenderer = null; 2603 2604 public AccessibleJListChild(JList parent, int indexInParent) { 2605 this.parent = parent; 2606 this.setAccessibleParent(parent); 2607 this.indexInParent = indexInParent; 2608 if (parent != null) { 2609 listModel = parent.getModel(); 2610 cellRenderer = parent.getCellRenderer(); 2611 } 2612 } 2613 2614 private Component getCurrentComponent() { 2615 return getComponentAtIndex(indexInParent); 2616 } 2617 2618 private AccessibleContext getCurrentAccessibleContext() { 2619 Component c = getComponentAtIndex(indexInParent); 2620 if (c instanceof Accessible) { 2621 return ((Accessible) c).getAccessibleContext(); 2622 } else { 2623 return null; 2624 } 2625 } 2626 2627 private Component getComponentAtIndex(int index) { 2628 if (index < 0 || index >= listModel.getSize()) { 2629 return null; 2630 } 2631 if ((parent != null) 2632 && (listModel != null) 2633 && cellRenderer != null) { 2634 Object value = listModel.getElementAt(index); 2635 boolean isSelected = parent.isSelectedIndex(index); 2636 boolean isFocussed = parent.isFocusOwner() 2637 && (index == parent.getLeadSelectionIndex()); 2638 return cellRenderer.getListCellRendererComponent( 2639 parent, 2640 value, 2641 index, 2642 isSelected, 2643 isFocussed); 2644 } else { 2645 return null; 2646 } 2647 } 2648 2649 2650 2658 public AccessibleContext getAccessibleContext() { 2659 return this; 2660 } 2661 2662 2663 2665 public String getAccessibleName() { 2666 AccessibleContext ac = getCurrentAccessibleContext(); 2667 if (ac != null) { 2668 return ac.getAccessibleName(); 2669 } else { 2670 return null; 2671 } 2672 } 2673 2674 public void setAccessibleName(String s) { 2675 AccessibleContext ac = getCurrentAccessibleContext(); 2676 if (ac != null) { 2677 ac.setAccessibleName(s); 2678 } 2679 } 2680 2681 public String getAccessibleDescription() { 2682 AccessibleContext ac = getCurrentAccessibleContext(); 2683 if (ac != null) { 2684 return ac.getAccessibleDescription(); 2685 } else { 2686 return null; 2687 } 2688 } 2689 2690 public void setAccessibleDescription(String s) { 2691 AccessibleContext ac = getCurrentAccessibleContext(); 2692 if (ac != null) { 2693 ac.setAccessibleDescription(s); 2694 } 2695 } 2696 2697 public AccessibleRole getAccessibleRole() { 2698 AccessibleContext ac = getCurrentAccessibleContext(); 2699 if (ac != null) { 2700 return ac.getAccessibleRole(); 2701 } else { 2702 return null; 2703 } 2704 } 2705 2706 public AccessibleStateSet getAccessibleStateSet() { 2707 AccessibleContext ac = getCurrentAccessibleContext(); 2708 AccessibleStateSet s; 2709 if (ac != null) { 2710 s = ac.getAccessibleStateSet(); 2711 } else { 2712 s = new AccessibleStateSet(); 2713 } 2714 s = ac.getAccessibleStateSet(); 2715 s.add(AccessibleState.SELECTABLE); 2716 if (parent.isFocusOwner() 2717 && (indexInParent == parent.getLeadSelectionIndex())) { 2718 s.add(AccessibleState.ACTIVE); 2719 } 2720 if (parent.isSelectedIndex(indexInParent)) { 2721 s.add(AccessibleState.SELECTED); 2722 } 2723 if (this.isShowing()) { 2724 s.add(AccessibleState.SHOWING); 2725 } else if (s.contains(AccessibleState.SHOWING)) { 2726 s.remove(AccessibleState.SHOWING); 2727 } 2728 if (this.isVisible()) { 2729 s.add(AccessibleState.VISIBLE); 2730 } else if (s.contains(AccessibleState.VISIBLE)) { 2731 s.remove(AccessibleState.VISIBLE); 2732 } 2733 s.add(AccessibleState.TRANSIENT); return s; 2735 } 2736 2737 public int getAccessibleIndexInParent() { 2738 return indexInParent; 2739 } 2740 2741 public int getAccessibleChildrenCount() { 2742 AccessibleContext ac = getCurrentAccessibleContext(); 2743 if (ac != null) { 2744 return ac.getAccessibleChildrenCount(); 2745 } else { 2746 return 0; 2747 } 2748 } 2749 2750 public Accessible getAccessibleChild(int i) { 2751 AccessibleContext ac = getCurrentAccessibleContext(); 2752 if (ac != null) { 2753 Accessible accessibleChild = ac.getAccessibleChild(i); 2754 ac.setAccessibleParent(this); 2755 return accessibleChild; 2756 } else { 2757 return null; 2758 } 2759 } 2760 2761 public Locale getLocale() { 2762 AccessibleContext ac = getCurrentAccessibleContext(); 2763 if (ac != null) { 2764 return ac.getLocale(); 2765 } else { 2766 return null; 2767 } 2768 } 2769 2770 public void addPropertyChangeListener(PropertyChangeListener l) { 2771 AccessibleContext ac = getCurrentAccessibleContext(); 2772 if (ac != null) { 2773 ac.addPropertyChangeListener(l); 2774 } 2775 } 2776 2777 public void removePropertyChangeListener(PropertyChangeListener l) { 2778 AccessibleContext ac = getCurrentAccessibleContext(); 2779 if (ac != null) { 2780 ac.removePropertyChangeListener(l); 2781 } 2782 } 2783 2784 public AccessibleAction getAccessibleAction() { 2785 return getCurrentAccessibleContext().getAccessibleAction(); 2786 } 2787 2788 2796 public AccessibleComponent getAccessibleComponent() { 2797 return this; } 2799 2800 public AccessibleSelection getAccessibleSelection() { 2801 return getCurrentAccessibleContext().getAccessibleSelection(); 2802 } 2803 2804 public AccessibleText getAccessibleText() { 2805 return getCurrentAccessibleContext().getAccessibleText(); 2806 } 2807 2808 public AccessibleValue getAccessibleValue() { 2809 return getCurrentAccessibleContext().getAccessibleValue(); 2810 } 2811 2812 2813 2815 public Color getBackground() { 2816 AccessibleContext ac = getCurrentAccessibleContext(); 2817 if (ac instanceof AccessibleComponent) { 2818 return ((AccessibleComponent) ac).getBackground(); 2819 } else { 2820 Component c = getCurrentComponent(); 2821 if (c != null) { 2822 return c.getBackground(); 2823 } else { 2824 return null; 2825 } 2826 } 2827 } 2828 2829 public void setBackground(Color c) { 2830 AccessibleContext ac = getCurrentAccessibleContext(); 2831 if (ac instanceof AccessibleComponent) { 2832 ((AccessibleComponent) ac).setBackground(c); 2833 } else { 2834 Component cp = getCurrentComponent(); 2835 if (cp != null) { 2836 cp.setBackground(c); 2837 } 2838 } 2839 } 2840 2841 public Color getForeground() { 2842 AccessibleContext ac = getCurrentAccessibleContext(); 2843 if (ac instanceof AccessibleComponent) { 2844 return ((AccessibleComponent) ac).getForeground(); 2845 } else { 2846 Component c = getCurrentComponent(); 2847 if (c != null) { 2848 return c.getForeground(); 2849 } else { 2850 return null; 2851 } 2852 } 2853 } 2854 2855 public void setForeground(Color c) { 2856 AccessibleContext ac = getCurrentAccessibleContext(); 2857 if (ac instanceof AccessibleComponent) { 2858 ((AccessibleComponent) ac).setForeground(c); 2859 } else { 2860 Component cp = getCurrentComponent(); 2861 if (cp != null) { 2862 cp.setForeground(c); 2863 } 2864 } 2865 } 2866 2867 public Cursor getCursor() { 2868 AccessibleContext ac = getCurrentAccessibleContext(); 2869 if (ac instanceof AccessibleComponent) { 2870 return ((AccessibleComponent) ac).getCursor(); 2871 } else { 2872 Component c = getCurrentComponent(); 2873 if (c != null) { 2874 return c.getCursor(); 2875 } else { 2876 Accessible ap = getAccessibleParent(); 2877 if (ap instanceof AccessibleComponent) { 2878 return ((AccessibleComponent) ap).getCursor(); 2879 } else { 2880 return null; 2881 } 2882 } 2883 } 2884 } 2885 2886 public void setCursor(Cursor c) { 2887 AccessibleContext ac = getCurrentAccessibleContext(); 2888 if (ac instanceof AccessibleComponent) { 2889 ((AccessibleComponent) ac).setCursor(c); 2890 } else { 2891 Component cp = getCurrentComponent(); 2892 if (cp != null) { 2893 cp.setCursor(c); 2894 } 2895 } 2896 } 2897 2898 public Font getFont() { 2899 AccessibleContext ac = getCurrentAccessibleContext(); 2900 if (ac instanceof AccessibleComponent) { 2901 return ((AccessibleComponent) ac).getFont(); 2902 } else { 2903 Component c = getCurrentComponent(); 2904 if (c != null) { 2905 return c.getFont(); 2906 } else { 2907 return null; 2908 } 2909 } 2910 } 2911 2912 public void setFont(Font f) { 2913 AccessibleContext ac = getCurrentAccessibleContext(); 2914 if (ac instanceof AccessibleComponent) { 2915 ((AccessibleComponent) ac).setFont(f); 2916 } else { 2917 Component c = getCurrentComponent(); 2918 if (c != null) { 2919 c.setFont(f); 2920 } 2921 } 2922 } 2923 2924 public FontMetrics getFontMetrics(Font f) { 2925 AccessibleContext ac = getCurrentAccessibleContext(); 2926 if (ac instanceof AccessibleComponent) { 2927 return ((AccessibleComponent) ac).getFontMetrics(f); 2928 } else { 2929 Component c = getCurrentComponent(); 2930 if (c != null) { 2931 return c.getFontMetrics(f); 2932 } else { 2933 return null; 2934 } 2935 } 2936 } 2937 2938 public boolean isEnabled() { 2939 AccessibleContext ac = getCurrentAccessibleContext(); 2940 if (ac instanceof AccessibleComponent) { 2941 return ((AccessibleComponent) ac).isEnabled(); 2942 } else { 2943 Component c = getCurrentComponent(); 2944 if (c != null) { 2945 return c.isEnabled(); 2946 } else { 2947 return false; 2948 } 2949 } 2950 } 2951 2952 public void setEnabled(boolean b) { 2953 AccessibleContext ac = getCurrentAccessibleContext(); 2954 if (ac instanceof AccessibleComponent) { 2955 ((AccessibleComponent) ac).setEnabled(b); 2956 } else { 2957 Component c = getCurrentComponent(); 2958 if (c != null) { 2959 c.setEnabled(b); 2960 } 2961 } 2962 } 2963 2964 public boolean isVisible() { 2965 int fi = parent.getFirstVisibleIndex(); 2966 int li = parent.getLastVisibleIndex(); 2967 if (li == -1) { 2971 li = parent.getModel().getSize() - 1; 2972 } 2973 return ((indexInParent >= fi) 2974 && (indexInParent <= li)); 2975 } 2976 2977 public void setVisible(boolean b) { 2978 } 2979 2980 public boolean isShowing() { 2981 return (parent.isShowing() && isVisible()); 2982 } 2983 2984 public boolean contains(Point p) { 2985 AccessibleContext ac = getCurrentAccessibleContext(); 2986 if (ac instanceof AccessibleComponent) { 2987 Rectangle r = ((AccessibleComponent) ac).getBounds(); 2988 return r.contains(p); 2989 } else { 2990 Component c = getCurrentComponent(); 2991 if (c != null) { 2992 Rectangle r = c.getBounds(); 2993 return r.contains(p); 2994 } else { 2995 return getBounds().contains(p); 2996 } 2997 } 2998 } 2999 3000 public Point getLocationOnScreen() { 3001 if (parent != null) { 3002 Point listLocation = parent.getLocationOnScreen(); 3003 Point componentLocation = parent.indexToLocation(indexInParent); 3004 if (componentLocation != null) { 3005 componentLocation.translate(listLocation.x, listLocation.y); 3006 return componentLocation; 3007 } else { 3008 return null; 3009 } 3010 } else { 3011 return null; 3012 } 3013 } 3014 3015 public Point getLocation() { 3016 if (parent != null) { 3017 return parent.indexToLocation(indexInParent); 3018 } else { 3019 return null; 3020 } 3021 } 3022 3023 public void setLocation(Point p) { 3024 if ((parent != null) && (parent.contains(p))) { 3025 ensureIndexIsVisible(indexInParent); 3026 } 3027 } 3028 3029 public Rectangle getBounds() { 3030 if (parent != null) { 3031 return parent.getCellBounds(indexInParent,indexInParent); 3032 } else { 3033 return null; 3034 } 3035 } 3036 3037 public void setBounds(Rectangle r) { 3038 AccessibleContext ac = getCurrentAccessibleContext(); 3039 if (ac instanceof AccessibleComponent) { 3040 ((AccessibleComponent) ac).setBounds(r); 3041 } 3042 } 3043 3044 public Dimension getSize() { 3045 Rectangle cellBounds = this.getBounds(); 3046 if (cellBounds != null) { 3047 return cellBounds.getSize(); 3048 } else { 3049 return null; 3050 } 3051 } 3052 3053 public void setSize (Dimension d) { 3054 AccessibleContext ac = getCurrentAccessibleContext(); 3055 if (ac instanceof AccessibleComponent) { 3056 ((AccessibleComponent) ac).setSize(d); 3057 } else { 3058 Component c = getCurrentComponent(); 3059 if (c != null) { 3060 c.setSize(d); 3061 } 3062 } 3063 } 3064 3065 public Accessible getAccessibleAt(Point p) { 3066 AccessibleContext ac = getCurrentAccessibleContext(); 3067 if (ac instanceof AccessibleComponent) { 3068 return ((AccessibleComponent) ac).getAccessibleAt(p); 3069 } else { 3070 return null; 3071 } 3072 } 3073 3074 public boolean isFocusTraversable() { 3075 AccessibleContext ac = getCurrentAccessibleContext(); 3076 if (ac instanceof AccessibleComponent) { 3077 return ((AccessibleComponent) ac).isFocusTraversable(); 3078 } else { 3079 Component c = getCurrentComponent(); 3080 if (c != null) { 3081 return c.isFocusTraversable(); 3082 } else { 3083 return false; 3084 } 3085 } 3086 } 3087 3088 public void requestFocus() { 3089 AccessibleContext ac = getCurrentAccessibleContext(); 3090 if (ac instanceof AccessibleComponent) { 3091 ((AccessibleComponent) ac).requestFocus(); 3092 } else { 3093 Component c = getCurrentComponent(); 3094 if (c != null) { 3095 c.requestFocus(); 3096 } 3097 } 3098 } 3099 3100 public void addFocusListener(FocusListener l) { 3101 AccessibleContext ac = getCurrentAccessibleContext(); 3102 if (ac instanceof AccessibleComponent) { 3103 ((AccessibleComponent) ac).addFocusListener(l); 3104 } else { 3105 Component c = getCurrentComponent(); 3106 if (c != null) { 3107 c.addFocusListener(l); 3108 } 3109 } 3110 } 3111 3112 public void removeFocusListener(FocusListener l) { 3113 AccessibleContext ac = getCurrentAccessibleContext(); 3114 if (ac instanceof AccessibleComponent) { 3115 ((AccessibleComponent) ac).removeFocusListener(l); 3116 } else { 3117 Component c = getCurrentComponent(); 3118 if (c != null) { 3119 c.removeFocusListener(l); 3120 } 3121 } 3122 } 3123 3124 3130 public AccessibleIcon [] getAccessibleIcon() { 3131 AccessibleContext ac = getCurrentAccessibleContext(); 3132 if (ac != null) { 3133 return ac.getAccessibleIcon(); 3134 } else { 3135 return null; 3136 } 3137 } 3138 } } } 3141 3142 | Popular Tags |