1 7 package java.awt; 8 9 import java.util.*; 10 import java.awt.peer.ChoicePeer; 11 import java.awt.event.*; 12 import java.util.EventListener ; 13 import java.io.ObjectOutputStream ; 14 import java.io.ObjectInputStream ; 15 import java.io.IOException ; 16 17 import javax.accessibility.*; 18 19 55 public class Choice extends Component implements ItemSelectable , Accessible { 56 67 Vector pItems; 68 69 76 int selectedIndex = -1; 77 78 transient ItemListener itemListener; 79 80 private static final String base = "choice"; 81 private static int nameCounter = 0; 82 83 86 private static final long serialVersionUID = -4075310674757313071L; 87 88 100 public Choice() throws HeadlessException { 101 GraphicsEnvironment.checkHeadless(); 102 pItems = new Vector(); 103 } 104 105 109 String constructComponentName() { 110 synchronized (getClass()) { 111 return base + nameCounter++; 112 } 113 } 114 115 122 public void addNotify() { 123 synchronized (getTreeLock()) { 124 if (peer == null) 125 peer = getToolkit().createChoice(this); 126 super.addNotify(); 127 } 128 } 129 130 136 public int getItemCount() { 137 return countItems(); 138 } 139 140 144 @Deprecated 145 public int countItems() { 146 return pItems.size(); 147 } 148 149 155 public String getItem(int index) { 156 return getItemImpl(index); 157 } 158 159 163 final String getItemImpl(int index) { 164 return (String )pItems.elementAt(index); 165 } 166 167 174 public void add(String item) { 175 addItem(item); 176 } 177 178 187 public void addItem(String item) { 188 synchronized (this) { 189 insertNoInvalidate(item, pItems.size()); 190 } 191 192 if (valid) { 194 invalidate(); 195 } 196 } 197 198 208 private void insertNoInvalidate(String item, int index) { 209 if (item == null) { 210 throw new 211 NullPointerException ("cannot add null item to Choice"); 212 } 213 pItems.insertElementAt(item, index); 214 ChoicePeer peer = (ChoicePeer)this.peer; 215 if (peer != null) { 216 peer.addItem(item, index); 217 } 218 if (selectedIndex < 0 || selectedIndex >= index) { 220 select(0); 221 } 222 } 223 224 225 243 public void insert(String item, int index) { 244 synchronized (this) { 245 if (index < 0) { 246 throw new IllegalArgumentException ("index less than zero."); 247 } 248 249 index = Math.min(index, pItems.size()); 250 251 insertNoInvalidate(item, index); 252 } 253 254 if (valid) { 256 invalidate(); 257 } 258 } 259 260 273 public void remove(String item) { 274 synchronized (this) { 275 int index = pItems.indexOf(item); 276 if (index < 0) { 277 throw new IllegalArgumentException ("item " + item + 278 " not found in choice"); 279 } else { 280 removeNoInvalidate(index); 281 } 282 } 283 284 if (valid) { 286 invalidate(); 287 } 288 } 289 290 302 public void remove(int position) { 303 synchronized (this) { 304 removeNoInvalidate(position); 305 } 306 307 if (valid) { 309 invalidate(); 310 } 311 } 312 313 320 private void removeNoInvalidate(int position) { 321 pItems.removeElementAt(position); 322 ChoicePeer peer = (ChoicePeer)this.peer; 323 if (peer != null) { 324 peer.remove(position); 325 } 326 327 if (pItems.size() == 0) { 328 selectedIndex = -1; 329 } else if (selectedIndex == position) { 330 select(0); 331 } else if (selectedIndex > position) { 332 select(selectedIndex-1); 333 } 334 } 335 336 337 342 public void removeAll() { 343 synchronized (this) { 344 if (peer != null) { 345 ((ChoicePeer)peer).removeAll(); 346 } 347 pItems.removeAllElements(); 348 selectedIndex = -1; 349 } 350 351 if (valid) { 353 invalidate(); 354 } 355 } 356 357 363 public synchronized String getSelectedItem() { 364 return (selectedIndex >= 0) ? getItem(selectedIndex) : null; 365 } 366 367 372 public synchronized Object [] getSelectedObjects() { 373 if (selectedIndex >= 0) { 374 Object [] items = new Object [1]; 375 items[0] = getItem(selectedIndex); 376 return items; 377 } 378 return null; 379 } 380 381 389 public int getSelectedIndex() { 390 return selectedIndex; 391 } 392 393 410 public synchronized void select(int pos) { 411 if ((pos >= pItems.size()) || (pos < 0)) { 412 throw new IllegalArgumentException ("illegal Choice item position: " + pos); 413 } 414 if (pItems.size() > 0) { 415 selectedIndex = pos; 416 ChoicePeer peer = (ChoicePeer)this.peer; 417 if (peer != null) { 418 peer.select(pos); 419 } 420 } 421 } 422 423 439 public synchronized void select(String str) { 440 int index = pItems.indexOf(str); 441 if (index >= 0) { 442 select(index); 443 } 444 } 445 446 460 public synchronized void addItemListener(ItemListener l) { 461 if (l == null) { 462 return; 463 } 464 itemListener = AWTEventMulticaster.add(itemListener, l); 465 newEventsOnly = true; 466 } 467 468 480 public synchronized void removeItemListener(ItemListener l) { 481 if (l == null) { 482 return; 483 } 484 itemListener = AWTEventMulticaster.remove(itemListener, l); 485 } 486 487 501 public synchronized ItemListener[] getItemListeners() { 502 return (ItemListener[])(getListeners(ItemListener.class)); 503 } 504 505 538 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 539 EventListener l = null; 540 if (listenerType == ItemListener.class) { 541 l = itemListener; 542 } else { 543 return super.getListeners(listenerType); 544 } 545 return AWTEventMulticaster.getListeners(l, listenerType); 546 } 547 548 boolean eventEnabled(AWTEvent e) { 550 if (e.id == ItemEvent.ITEM_STATE_CHANGED) { 551 if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 || 552 itemListener != null) { 553 return true; 554 } 555 return false; 556 } 557 return super.eventEnabled(e); 558 } 559 560 574 protected void processEvent(AWTEvent e) { 575 if (e instanceof ItemEvent) { 576 processItemEvent((ItemEvent)e); 577 return; 578 } 579 super.processEvent(e); 580 } 581 582 606 protected void processItemEvent(ItemEvent e) { 607 ItemListener listener = itemListener; 608 if (listener != null) { 609 listener.itemStateChanged(e); 610 } 611 } 612 613 622 protected String paramString() { 623 return super.paramString() + ",current=" + getSelectedItem(); 624 } 625 626 627 629 630 634 private int choiceSerializedDataVersion = 1; 635 636 655 private void writeObject(ObjectOutputStream s) 656 throws java.io.IOException 657 { 658 s.defaultWriteObject(); 659 660 AWTEventMulticaster.save(s, itemListenerK, itemListener); 661 s.writeObject(null); 662 } 663 664 680 private void readObject(ObjectInputStream s) 681 throws ClassNotFoundException , IOException , HeadlessException 682 { 683 GraphicsEnvironment.checkHeadless(); 684 s.defaultReadObject(); 685 686 Object keyOrNull; 687 while(null != (keyOrNull = s.readObject())) { 688 String key = ((String )keyOrNull).intern(); 689 690 if (itemListenerK == key) 691 addItemListener((ItemListener)(s.readObject())); 692 693 else s.readObject(); 695 } 696 } 697 698 699 703 704 714 public AccessibleContext getAccessibleContext() { 715 if (accessibleContext == null) { 716 accessibleContext = new AccessibleAWTChoice(); 717 } 718 return accessibleContext; 719 } 720 721 726 protected class AccessibleAWTChoice extends AccessibleAWTComponent 727 implements AccessibleAction 728 { 729 732 private static final long serialVersionUID = 7175603582428509322L; 733 734 public AccessibleAWTChoice() { 735 super(); 736 } 737 738 747 public AccessibleAction getAccessibleAction() { 748 return this; 749 } 750 751 758 public AccessibleRole getAccessibleRole() { 759 return AccessibleRole.COMBO_BOX; 760 } 761 762 769 public int getAccessibleActionCount() { 770 return 0; } 772 773 780 public String getAccessibleActionDescription(int i) { 781 return null; } 783 784 791 public boolean doAccessibleAction(int i) { 792 return false; } 794 795 } 797 } 798 | Popular Tags |