1 7 package java.awt; 8 9 import java.awt.peer.MenuItemPeer; 10 import java.awt.event.*; 11 import java.util.EventListener ; 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectInputStream ; 14 import java.io.IOException ; 15 import javax.accessibility.*; 16 17 18 54 public class MenuItem extends MenuComponent implements Accessible { 55 56 static { 57 58 Toolkit.loadLibraries(); 59 if (!GraphicsEnvironment.isHeadless()) { 60 initIDs(); 61 } 62 } 63 64 74 boolean enabled = true; 75 76 84 String label; 85 86 97 String actionCommand; 98 99 107 long eventMask; 108 109 transient ActionListener actionListener; 110 111 123 private MenuShortcut shortcut = null; 124 125 private static final String base = "menuitem"; 126 private static int nameCounter = 0; 127 128 131 private static final long serialVersionUID = -21757335363267194L; 132 133 141 public MenuItem() throws HeadlessException { 142 this("", null); 143 } 144 145 157 public MenuItem(String label) throws HeadlessException { 158 this(label, null); 159 } 160 161 174 public MenuItem(String label, MenuShortcut s) throws HeadlessException { 175 this.label = label; 176 this.shortcut = s; 177 } 178 179 183 String constructComponentName() { 184 synchronized (getClass()) { 185 return base + nameCounter++; 186 } 187 } 188 189 193 public void addNotify() { 194 synchronized (getTreeLock()) { 195 if (peer == null) 196 peer = Toolkit.getDefaultToolkit().createMenuItem(this); 197 } 198 } 199 200 207 public String getLabel() { 208 return label; 209 } 210 211 217 public synchronized void setLabel(String label) { 218 this.label = label; 219 MenuItemPeer peer = (MenuItemPeer)this.peer; 220 if (peer != null) { 221 peer.setLabel(label); 222 } 223 } 224 225 230 public boolean isEnabled() { 231 return enabled; 232 } 233 234 241 public synchronized void setEnabled(boolean b) { 242 enable(b); 243 } 244 245 249 @Deprecated 250 public synchronized void enable() { 251 enabled = true; 252 MenuItemPeer peer = (MenuItemPeer)this.peer; 253 if (peer != null) { 254 peer.enable(); 255 } 256 } 257 258 262 @Deprecated 263 public void enable(boolean b) { 264 if (b) { 265 enable(); 266 } else { 267 disable(); 268 } 269 } 270 271 275 @Deprecated 276 public synchronized void disable() { 277 enabled = false; 278 MenuItemPeer peer = (MenuItemPeer)this.peer; 279 if (peer != null) { 280 peer.disable(); 281 } 282 } 283 284 292 public MenuShortcut getShortcut() { 293 return shortcut; 294 } 295 296 305 public void setShortcut(MenuShortcut s) { 306 shortcut = s; 307 MenuItemPeer peer = (MenuItemPeer)this.peer; 308 if (peer != null) { 309 peer.setLabel(label); 310 } 311 } 312 313 318 public void deleteShortcut() { 319 shortcut = null; 320 MenuItemPeer peer = (MenuItemPeer)this.peer; 321 if (peer != null) { 322 peer.setLabel(label); 323 } 324 } 325 326 330 void deleteShortcut(MenuShortcut s) { 331 if (s.equals(shortcut)) { 332 shortcut = null; 333 MenuItemPeer peer = (MenuItemPeer)this.peer; 334 if (peer != null) { 335 peer.setLabel(label); 336 } 337 } 338 } 339 340 346 void doMenuEvent(long when, int modifiers) { 347 Toolkit.getEventQueue().postEvent( 348 new ActionEvent(this, ActionEvent.ACTION_PERFORMED, 349 getActionCommand(), when, modifiers)); 350 } 351 352 357 boolean handleShortcut(KeyEvent e) { 358 MenuShortcut s = new MenuShortcut (e.getKeyCode(), 359 (e.getModifiers() & InputEvent.SHIFT_MASK) > 0); 360 if (s.equals(shortcut) && enabled) { 361 if (e.getID() == KeyEvent.KEY_PRESSED) { 363 doMenuEvent(e.getWhen(), e.getModifiers()); 364 } else { 365 } 367 return true; 368 } 369 return false; 370 } 371 372 MenuItem getShortcutMenuItem(MenuShortcut s) { 373 return (s.equals(shortcut)) ? this : null; 374 } 375 376 392 protected final void enableEvents(long eventsToEnable) { 393 eventMask |= eventsToEnable; 394 newEventsOnly = true; 395 } 396 397 407 protected final void disableEvents(long eventsToDisable) { 408 eventMask &= ~eventsToDisable; 409 } 410 411 422 public void setActionCommand(String command) { 423 actionCommand = command; 424 } 425 426 432 public String getActionCommand() { 433 return getActionCommandImpl(); 434 } 435 436 final String getActionCommandImpl() { 438 return (actionCommand == null? label : actionCommand); 439 } 440 441 453 public synchronized void addActionListener(ActionListener l) { 454 if (l == null) { 455 return; 456 } 457 actionListener = AWTEventMulticaster.add(actionListener, l); 458 newEventsOnly = true; 459 } 460 461 473 public synchronized void removeActionListener(ActionListener l) { 474 if (l == null) { 475 return; 476 } 477 actionListener = AWTEventMulticaster.remove(actionListener, l); 478 } 479 480 494 public synchronized ActionListener[] getActionListeners() { 495 return (ActionListener[])(getListeners(ActionListener.class)); 496 } 497 498 531 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 532 EventListener l = null; 533 if (listenerType == ActionListener.class) { 534 l = actionListener; 535 } 536 return AWTEventMulticaster.getListeners(l, listenerType); 537 } 538 539 554 protected void processEvent(AWTEvent e) { 555 if (e instanceof ActionEvent) { 556 processActionEvent((ActionEvent)e); 557 } 558 } 559 560 boolean eventEnabled(AWTEvent e) { 562 if (e.id == ActionEvent.ACTION_PERFORMED) { 563 if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 || 564 actionListener != null) { 565 return true; 566 } 567 return false; 568 } 569 return super.eventEnabled(e); 570 } 571 572 594 protected void processActionEvent(ActionEvent e) { 595 ActionListener listener = actionListener; 596 if (listener != null) { 597 listener.actionPerformed(e); 598 } 599 } 600 601 610 public String paramString() { 611 String str = ",label=" + label; 612 if (shortcut != null) { 613 str += ",shortcut=" + shortcut; 614 } 615 return super.paramString() + str; 616 } 617 618 619 621 622 627 private int menuItemSerializedDataVersion = 1; 628 629 646 private void writeObject(ObjectOutputStream s) 647 throws IOException 648 { 649 s.defaultWriteObject(); 650 651 AWTEventMulticaster.save(s, actionListenerK, actionListener); 652 s.writeObject(null); 653 } 654 655 669 private void readObject(ObjectInputStream s) 670 throws ClassNotFoundException , IOException , HeadlessException 671 { 672 s.defaultReadObject(); 674 675 Object keyOrNull; 676 while(null != (keyOrNull = s.readObject())) { 677 String key = ((String )keyOrNull).intern(); 678 679 if (actionListenerK == key) 680 addActionListener((ActionListener)(s.readObject())); 681 682 else s.readObject(); 684 } 685 } 686 687 690 private static native void initIDs(); 691 692 693 697 706 public AccessibleContext getAccessibleContext() { 707 if (accessibleContext == null) { 708 accessibleContext = new AccessibleAWTMenuItem(); 709 } 710 return accessibleContext; 711 } 712 713 723 protected class AccessibleAWTMenuItem extends AccessibleAWTMenuComponent 724 implements AccessibleAction, AccessibleValue 725 { 726 729 private static final long serialVersionUID = -217847831945965825L; 730 731 737 public String getAccessibleName() { 738 if (accessibleName != null) { 739 return accessibleName; 740 } else { 741 if (getLabel() == null) { 742 return super.getAccessibleName(); 743 } else { 744 return getLabel(); 745 } 746 } 747 } 748 749 755 public AccessibleRole getAccessibleRole() { 756 return AccessibleRole.MENU_ITEM; 757 } 758 759 767 public AccessibleAction getAccessibleAction() { 768 return this; 769 } 770 771 779 public AccessibleValue getAccessibleValue() { 780 return this; 781 } 782 783 789 public int getAccessibleActionCount() { 790 return 1; 791 } 792 793 798 public String getAccessibleActionDescription(int i) { 799 if (i == 0) { 800 return new String ("click"); 802 } else { 803 return null; 804 } 805 } 806 807 813 public boolean doAccessibleAction(int i) { 814 if (i == 0) { 815 Toolkit.getEventQueue().postEvent( 817 new ActionEvent(MenuItem.this, 818 ActionEvent.ACTION_PERFORMED, 819 MenuItem.this.getActionCommand(), 820 EventQueue.getMostRecentEventTime(), 821 0)); 822 return true; 823 } else { 824 return false; 825 } 826 } 827 828 835 public Number getCurrentAccessibleValue() { 836 return new Integer (0); 837 } 838 839 844 public boolean setCurrentAccessibleValue(Number n) { 845 return false; 846 } 847 848 853 public Number getMinimumAccessibleValue() { 854 return new Integer (0); 855 } 856 857 862 public Number getMaximumAccessibleValue() { 863 return new Integer (0); 864 } 865 866 } 868 } 869 | Popular Tags |