| 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:
|