1 7 package java.awt; 8 9 import java.util.Vector ; 10 import java.util.Locale ; 11 import java.util.EventListener ; 12 import java.awt.peer.ListPeer; 13 import java.awt.event.*; 14 import java.io.ObjectOutputStream ; 15 import java.io.ObjectInputStream ; 16 import java.io.IOException ; 17 import javax.accessibility.*; 18 19 20 92 public class List extends Component implements ItemSelectable , Accessible { 93 101 Vector items = new Vector (); 102 103 112 int rows = 0; 113 114 128 boolean multipleMode = false; 129 130 138 int selected[] = new int[0]; 139 140 147 int visibleIndex = -1; 148 149 transient ActionListener actionListener; 150 transient ItemListener itemListener; 151 152 private static final String base = "list"; 153 private static int nameCounter = 0; 154 155 158 private static final long serialVersionUID = -3304312411574666869L; 159 160 170 public List() throws HeadlessException { 171 this(0, false); 172 } 173 174 187 public List(int rows) throws HeadlessException { 188 this(rows, false); 189 } 190 191 195 final static int DEFAULT_VISIBLE_ROWS = 4; 196 197 215 public List(int rows, boolean multipleMode) throws HeadlessException { 216 GraphicsEnvironment.checkHeadless(); 217 this.rows = (rows != 0) ? rows : DEFAULT_VISIBLE_ROWS; 218 this.multipleMode = multipleMode; 219 } 220 221 225 String constructComponentName() { 226 synchronized (getClass()) { 227 return base + nameCounter++; 228 } 229 } 230 231 235 public void addNotify() { 236 synchronized (getTreeLock()) { 237 if (peer == null) 238 peer = getToolkit().createList(this); 239 super.addNotify(); 240 } 241 } 242 243 247 public void removeNotify() { 248 synchronized (getTreeLock()) { 249 ListPeer peer = (ListPeer)this.peer; 250 if (peer != null) { 251 selected = peer.getSelectedIndexes(); 252 } 253 super.removeNotify(); 254 } 255 } 256 257 263 public int getItemCount() { 264 return countItems(); 265 } 266 267 271 @Deprecated 272 public int countItems() { 273 return items.size(); 274 } 275 276 283 public String getItem(int index) { 284 return getItemImpl(index); 285 } 286 287 final String getItemImpl(int index) { 292 return (String )items.elementAt(index); 293 } 294 295 303 public synchronized String [] getItems() { 304 String itemCopies[] = new String [items.size()]; 305 items.copyInto(itemCopies); 306 return itemCopies; 307 } 308 309 314 public void add(String item) { 315 addItem(item); 316 } 317 318 321 @Deprecated 322 public void addItem(String item) { 323 addItem(item, -1); 324 } 325 326 339 public void add(String item, int index) { 340 addItem(item, index); 341 } 342 343 346 @Deprecated 347 public synchronized void addItem(String item, int index) { 348 if (index < -1 || index >= items.size()) { 349 index = -1; 350 } 351 352 if (item == null) { 353 item = ""; 354 } 355 356 if (index == -1) { 357 items.addElement(item); 358 } else { 359 items.insertElementAt(item, index); 360 } 361 362 ListPeer peer = (ListPeer)this.peer; 363 if (peer != null) { 364 peer.addItem(item, index); 365 } 366 } 367 368 376 public synchronized void replaceItem(String newValue, int index) { 377 remove(index); 378 add(newValue, index); 379 } 380 381 387 public void removeAll() { 388 clear(); 389 } 390 391 395 @Deprecated 396 public synchronized void clear() { 397 ListPeer peer = (ListPeer)this.peer; 398 if (peer != null) { 399 peer.clear(); 400 } 401 items = new Vector (); 402 selected = new int[0]; 403 } 404 405 414 public synchronized void remove(String item) { 415 int index = items.indexOf(item); 416 if (index < 0) { 417 throw new IllegalArgumentException ("item " + item + 418 " not found in list"); 419 } else { 420 remove(index); 421 } 422 } 423 424 436 public void remove(int position) { 437 delItem(position); 438 } 439 440 444 @Deprecated 445 public void delItem(int position) { 446 delItems(position, position); 447 } 448 449 459 public synchronized int getSelectedIndex() { 460 int sel[] = getSelectedIndexes(); 461 return (sel.length == 1) ? sel[0] : -1; 462 } 463 464 473 public synchronized int[] getSelectedIndexes() { 474 ListPeer peer = (ListPeer)this.peer; 475 if (peer != null) { 476 selected = ((ListPeer)peer).getSelectedIndexes(); 477 } 478 return (int[])selected.clone(); 479 } 480 481 491 public synchronized String getSelectedItem() { 492 int index = getSelectedIndex(); 493 return (index < 0) ? null : getItem(index); 494 } 495 496 505 public synchronized String [] getSelectedItems() { 506 int sel[] = getSelectedIndexes(); 507 String str[] = new String [sel.length]; 508 for (int i = 0 ; i < sel.length ; i++) { 509 str[i] = getItem(sel[i]); 510 } 511 return str; 512 } 513 514 522 public Object [] getSelectedObjects() { 523 return getSelectedItems(); 524 } 525 526 543 public void select(int index) { 544 550 ListPeer peer; 551 do { 552 peer = (ListPeer)this.peer; 553 if (peer != null) { 554 peer.select(index); 555 return; 556 } 557 558 synchronized(this) 559 { 560 boolean alreadySelected = false; 561 562 for (int i = 0 ; i < selected.length ; i++) { 563 if (selected[i] == index) { 564 alreadySelected = true; 565 break; 566 } 567 } 568 569 if (!alreadySelected) { 570 if (!multipleMode) { 571 selected = new int[1]; 572 selected[0] = index; 573 } else { 574 int newsel[] = new int[selected.length + 1]; 575 System.arraycopy(selected, 0, newsel, 0, 576 selected.length); 577 newsel[selected.length] = index; 578 selected = newsel; 579 } 580 } 581 } 582 } while (peer != this.peer); 583 } 584 585 598 public synchronized void deselect(int index) { 599 ListPeer peer = (ListPeer)this.peer; 600 if (peer != null) { 601 peer.deselect(index); 602 } 603 604 for (int i = 0 ; i < selected.length ; i++) { 605 if (selected[i] == index) { 606 int newsel[] = new int[selected.length - 1]; 607 System.arraycopy(selected, 0, newsel, 0, i); 608 System.arraycopy(selected, i+1, newsel, i, selected.length - (i+1)); 609 selected = newsel; 610 return; 611 } 612 } 613 } 614 615 625 public boolean isIndexSelected(int index) { 626 return isSelected(index); 627 } 628 629 633 @Deprecated 634 public boolean isSelected(int index) { 635 int sel[] = getSelectedIndexes(); 636 for (int i = 0 ; i < sel.length ; i++) { 637 if (sel[i] == index) { 638 return true; 639 } 640 } 641 return false; 642 } 643 644 650 public int getRows() { 651 return rows; 652 } 653 654 661 public boolean isMultipleMode() { 662 return allowsMultipleSelections(); 663 } 664 665 669 @Deprecated 670 public boolean allowsMultipleSelections() { 671 return multipleMode; 672 } 673 674 688 public void setMultipleMode(boolean b) { 689 setMultipleSelections(b); 690 } 691 692 696 @Deprecated 697 public synchronized void setMultipleSelections(boolean b) { 698 if (b != multipleMode) { 699 multipleMode = b; 700 ListPeer peer = (ListPeer)this.peer; 701 if (peer != null) { 702 peer.setMultipleSelections(b); 703 } 704 } 705 } 706 707 713 public int getVisibleIndex() { 714 return visibleIndex; 715 } 716 717 722 public synchronized void makeVisible(int index) { 723 visibleIndex = index; 724 ListPeer peer = (ListPeer)this.peer; 725 if (peer != null) { 726 peer.makeVisible(index); 727 } 728 } 729 730 739 public Dimension getPreferredSize(int rows) { 740 return preferredSize(rows); 741 } 742 743 747 @Deprecated 748 public Dimension preferredSize(int rows) { 749 synchronized (getTreeLock()) { 750 ListPeer peer = (ListPeer)this.peer; 751 return (peer != null) ? 752 peer.preferredSize(rows) : 753 super.preferredSize(); 754 } 755 } 756 757 763 public Dimension getPreferredSize() { 764 return preferredSize(); 765 } 766 767 771 @Deprecated 772 public Dimension preferredSize() { 773 synchronized (getTreeLock()) { 774 return (rows > 0) ? 775 preferredSize(rows) : 776 super.preferredSize(); 777 } 778 } 779 780 789 public Dimension getMinimumSize(int rows) { 790 return minimumSize(rows); 791 } 792 793 797 @Deprecated 798 public Dimension minimumSize(int rows) { 799 synchronized (getTreeLock()) { 800 ListPeer peer = (ListPeer)this.peer; 801 return (peer != null) ? 802 peer.minimumSize(rows) : 803 super.minimumSize(); 804 } 805 } 806 807 814 public Dimension getMinimumSize() { 815 return minimumSize(); 816 } 817 818 822 @Deprecated 823 public Dimension minimumSize() { 824 synchronized (getTreeLock()) { 825 return (rows > 0) ? minimumSize(rows) : super.minimumSize(); 826 } 827 } 828 829 845 public synchronized void addItemListener(ItemListener l) { 846 if (l == null) { 847 return; 848 } 849 itemListener = AWTEventMulticaster.add(itemListener, l); 850 newEventsOnly = true; 851 } 852 853 866 public synchronized void removeItemListener(ItemListener l) { 867 if (l == null) { 868 return; 869 } 870 itemListener = AWTEventMulticaster.remove(itemListener, l); 871 } 872 873 887 public synchronized ItemListener[] getItemListeners() { 888 return (ItemListener[])(getListeners(ItemListener.class)); 889 } 890 891 907 public synchronized void addActionListener(ActionListener l) { 908 if (l == null) { 909 return; 910 } 911 actionListener = AWTEventMulticaster.add(actionListener, l); 912 newEventsOnly = true; 913 } 914 915 929 public synchronized void removeActionListener(ActionListener l) { 930 if (l == null) { 931 return; 932 } 933 actionListener = AWTEventMulticaster.remove(actionListener, l); 934 } 935 936 950 public synchronized ActionListener[] getActionListeners() { 951 return (ActionListener[])(getListeners(ActionListener.class)); 952 } 953 954 987 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 988 EventListener l = null; 989 if (listenerType == ActionListener.class) { 990 l = actionListener; 991 } else if (listenerType == ItemListener.class) { 992 l = itemListener; 993 } else { 994 return super.getListeners(listenerType); 995 } 996 return AWTEventMulticaster.getListeners(l, listenerType); 997 } 998 999 boolean eventEnabled(AWTEvent e) { 1001 switch(e.id) { 1002 case ActionEvent.ACTION_PERFORMED: 1003 if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 || 1004 actionListener != null) { 1005 return true; 1006 } 1007 return false; 1008 case ItemEvent.ITEM_STATE_CHANGED: 1009 if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 || 1010 itemListener != null) { 1011 return true; 1012 } 1013 return false; 1014 default: 1015 break; 1016 } 1017 return super.eventEnabled(e); 1018 } 1019 1020 1039 protected void processEvent(AWTEvent e) { 1040 if (e instanceof ItemEvent) { 1041 processItemEvent((ItemEvent)e); 1042 return; 1043 } else if (e instanceof ActionEvent) { 1044 processActionEvent((ActionEvent)e); 1045 return; 1046 } 1047 super.processEvent(e); 1048 } 1049 1050 1074 protected void processItemEvent(ItemEvent e) { 1075 ItemListener listener = itemListener; 1076 if (listener != null) { 1077 listener.itemStateChanged(e); 1078 } 1079 } 1080 1081 1105 protected void processActionEvent(ActionEvent e) { 1106 ActionListener listener = actionListener; 1107 if (listener != null) { 1108 listener.actionPerformed(e); 1109 } 1110 } 1111 1112 1117 protected String paramString() { 1118 return super.paramString() + ",selected=" + getSelectedItem(); 1119 } 1120 1121 1127 @Deprecated 1128 public synchronized void delItems(int start, int end) { 1129 for (int i = end; i >= start; i--) { 1130 items.removeElementAt(i); 1131 } 1132 ListPeer peer = (ListPeer)this.peer; 1133 if (peer != null) { 1134 peer.delItems(start, end); 1135 } 1136 } 1137 1138 1143 1144 1150 private int listSerializedDataVersion = 1; 1151 1152 1175 private void writeObject(ObjectOutputStream s) 1176 throws IOException 1177 { 1178 synchronized (this) { 1179 ListPeer peer = (ListPeer)this.peer; 1180 if (peer != null) { 1181 selected = peer.getSelectedIndexes(); 1182 } 1183 } 1184 s.defaultWriteObject(); 1185 1186 AWTEventMulticaster.save(s, itemListenerK, itemListener); 1187 AWTEventMulticaster.save(s, actionListenerK, actionListener); 1188 s.writeObject(null); 1189 } 1190 1191 1208 private void readObject(ObjectInputStream s) 1209 throws ClassNotFoundException , IOException , HeadlessException 1210 { 1211 GraphicsEnvironment.checkHeadless(); 1212 s.defaultReadObject(); 1213 1214 Object keyOrNull; 1215 while(null != (keyOrNull = s.readObject())) { 1216 String key = ((String )keyOrNull).intern(); 1217 1218 if (itemListenerK == key) 1219 addItemListener((ItemListener)(s.readObject())); 1220 1221 else if (actionListenerK == key) 1222 addActionListener((ActionListener)(s.readObject())); 1223 1224 else s.readObject(); 1226 } 1227 } 1228 1229 1230 1234 1235 1244 public AccessibleContext getAccessibleContext() { 1245 if (accessibleContext == null) { 1246 accessibleContext = new AccessibleAWTList(); 1247 } 1248 return accessibleContext; 1249 } 1250 1251 1256 protected class AccessibleAWTList extends AccessibleAWTComponent 1257 implements AccessibleSelection, ItemListener, ActionListener 1258 { 1259 1262 private static final long serialVersionUID = 7924617370136012829L; 1263 1264 public AccessibleAWTList() { 1265 super(); 1266 List.this.addActionListener(this); 1267 List.this.addItemListener(this); 1268 } 1269 1270 public void actionPerformed(ActionEvent event) { 1271 } 1272 1273 public void itemStateChanged(ItemEvent event) { 1274 } 1275 1276 1283 public AccessibleStateSet getAccessibleStateSet() { 1284 AccessibleStateSet states = super.getAccessibleStateSet(); 1285 if (List.this.isMultipleMode()) { 1286 states.add(AccessibleState.MULTISELECTABLE); 1287 } 1288 return states; 1289 } 1290 1291 1298 public AccessibleRole getAccessibleRole() { 1299 return AccessibleRole.LIST; 1300 } 1301 1302 1308 public Accessible getAccessibleAt(Point p) { 1309 return null; } 1311 1312 1319 public int getAccessibleChildrenCount() { 1320 return List.this.getItemCount(); 1321 } 1322 1323 1329 public Accessible getAccessibleChild(int i) { 1330 synchronized(List.this) { 1331 if (i >= List.this.getItemCount()) { 1332 return null; 1333 } else { 1334 return new AccessibleAWTListChild(List.this, i); 1335 } 1336 } 1337 } 1338 1339 1347 public AccessibleSelection getAccessibleSelection() { 1348 return this; 1349 } 1350 1351 1353 1359 public int getAccessibleSelectionCount() { 1360 return List.this.getSelectedIndexes().length; 1361 } 1362 1363 1372 public Accessible getAccessibleSelection(int i) { 1373 synchronized(List.this) { 1374 int len = getAccessibleSelectionCount(); 1375 if (i < 0 || i >= len) { 1376 return null; 1377 } else { 1378 return getAccessibleChild(List.this.getSelectedIndexes()[i]); 1379 } 1380 } 1381 } 1382 1383 1390 public boolean isAccessibleChildSelected(int i) { 1391 return List.this.isIndexSelected(i); 1392 } 1393 1394 1403 public void addAccessibleSelection(int i) { 1404 List.this.select(i); 1405 } 1406 1407 1414 public void removeAccessibleSelection(int i) { 1415 List.this.deselect(i); 1416 } 1417 1418 1422 public void clearAccessibleSelection() { 1423 synchronized(List.this) { 1424 int selectedIndexes[] = List.this.getSelectedIndexes(); 1425 if (selectedIndexes == null) 1426 return; 1427 for (int i = selectedIndexes.length - 1; i >= 0; i--) { 1428 List.this.deselect(selectedIndexes[i]); 1429 } 1430 } 1431 } 1432 1433 1437 public void selectAllAccessibleSelection() { 1438 synchronized(List.this) { 1439 for (int i = List.this.getItemCount() - 1; i >= 0; i--) { 1440 List.this.select(i); 1441 } 1442 } 1443 } 1444 1445 1451 protected class AccessibleAWTListChild extends AccessibleAWTComponent 1452 implements Accessible 1453 { 1454 1457 private static final long serialVersionUID = 4412022926028300317L; 1458 1459 1461 private List parent; 1462 private int indexInParent; 1463 1464 public AccessibleAWTListChild(List parent, int indexInParent) { 1465 this.parent = parent; 1466 this.setAccessibleParent(parent); 1467 this.indexInParent = indexInParent; 1468 } 1469 1470 1480 public AccessibleContext getAccessibleContext() { 1481 return this; 1482 } 1483 1484 1488 1495 public AccessibleRole getAccessibleRole() { 1496 return AccessibleRole.LIST_ITEM; 1497 } 1498 1499 1512 public AccessibleStateSet getAccessibleStateSet() { 1513 AccessibleStateSet states = super.getAccessibleStateSet(); 1514 if (parent.isIndexSelected(indexInParent)) { 1515 states.add(AccessibleState.SELECTED); 1516 } 1517 return states; 1518 } 1519 1520 1532 public Locale getLocale() { 1533 return parent.getLocale(); 1534 } 1535 1536 1546 public int getAccessibleIndexInParent() { 1547 return indexInParent; 1548 } 1549 1550 1555 public int getAccessibleChildrenCount() { 1556 return 0; } 1558 1559 1569 public Accessible getAccessibleChild(int i) { 1570 return null; } 1572 1573 1574 1578 1585 public Color getBackground() { 1586 return parent.getBackground(); 1587 } 1588 1589 1595 public void setBackground(Color c) { 1596 parent.setBackground(c); 1597 } 1598 1599 1606 public Color getForeground() { 1607 return parent.getForeground(); 1608 } 1609 1610 1616 public void setForeground(Color c) { 1617 parent.setForeground(c); 1618 } 1619 1620 1626 public Cursor getCursor() { 1627 return parent.getCursor(); 1628 } 1629 1630 1636 public void setCursor(Cursor cursor) { 1637 parent.setCursor(cursor); 1638 } 1639 1640 1646 public Font getFont() { 1647 return parent.getFont(); 1648 } 1649 1650 1656 public void setFont(Font f) { 1657 parent.setFont(f); 1658 } 1659 1660 1667 public FontMetrics getFontMetrics(Font f) { 1668 return parent.getFontMetrics(f); 1669 } 1670 1671 1682 public boolean isEnabled() { 1683 return parent.isEnabled(); 1684 } 1685 1686 1692 public void setEnabled(boolean b) { 1693 parent.setEnabled(b); 1694 } 1695 1696 1711 public boolean isVisible() { 1712 return false; 1714 } 1716 1717 1723 public void setVisible(boolean b) { 1724 parent.setVisible(b); 1726 } 1727 1728 1738 public boolean isShowing() { 1739 return false; 1741 } 1743 1744 1754 public boolean contains(Point p) { 1755 return false; 1757 } 1759 1760 1768 public Point getLocationOnScreen() { 1769 return null; 1771 } 1772 1773 1784 public Point getLocation() { 1785 return null; 1787 } 1788 1789 1794 public void setLocation(Point p) { 1795 } 1797 1798 1807 public Rectangle getBounds() { 1808 return null; 1810 } 1811 1812 1820 public void setBounds(Rectangle r) { 1821 } 1823 1824 1834 public Dimension getSize() { 1835 return null; 1837 } 1838 1839 1845 public void setSize(Dimension d) { 1846 } 1848 1849 1858 public Accessible getAccessibleAt(Point p) { 1859 return null; } 1861 1862 1874 public boolean isFocusTraversable() { 1875 return false; } 1877 1878 1884 public void requestFocus() { 1885 } 1887 1888 1895 public void addFocusListener(FocusListener l) { 1896 } 1898 1899 1906 public void removeFocusListener(FocusListener l) { 1907 } 1909 1910 1911 1912 } 1914 } 1916} 1917 | Popular Tags |