1 7 package javax.swing; 8 9 import java.util.EventListener ; 10 import java.awt.*; 11 import java.awt.event.*; 12 import java.awt.image.*; 13 14 import java.beans.PropertyChangeEvent ; 15 import java.beans.PropertyChangeListener ; 16 17 import java.io.Serializable ; 18 import java.io.ObjectOutputStream ; 19 import java.io.ObjectInputStream ; 20 import java.io.IOException ; 21 22 import javax.swing.plaf.*; 23 import javax.swing.plaf.basic.*; 24 import javax.swing.event.*; 25 import javax.accessibility.*; 26 27 59 public class JMenuItem extends AbstractButton implements Accessible,MenuElement { 60 61 65 private static final String uiClassID = "MenuItemUI"; 66 67 68 private static final boolean TRACE = false; private static final boolean VERBOSE = false; private static final boolean DEBUG = false; 72 private boolean isMouseDragged = false; 73 74 77 public JMenuItem() { 78 this(null, (Icon )null); 79 } 80 81 86 public JMenuItem(Icon icon) { 87 this(null, icon); 88 } 89 90 95 public JMenuItem(String text) { 96 this(text, (Icon )null); 97 } 98 99 106 public JMenuItem(Action a) { 107 this(); 108 setAction(a); 109 } 110 111 117 public JMenuItem(String text, Icon icon) { 118 setModel(new DefaultButtonModel ()); 119 init(text, icon); 120 initFocusability(); 121 } 122 123 130 public JMenuItem(String text, int mnemonic) { 131 setModel(new DefaultButtonModel ()); 132 init(text, null); 133 setMnemonic(mnemonic); 134 initFocusability(); 135 } 136 137 145 void initFocusability() { 146 setFocusable(false); 147 } 148 149 155 protected void init(String text, Icon icon) { 156 if(text != null) { 157 setText(text); 158 } 159 160 if(icon != null) { 161 setIcon(icon); 162 } 163 164 addFocusListener(new MenuItemFocusListener()); 166 setUIProperty("borderPainted", Boolean.FALSE); 167 setFocusPainted(false); 168 setHorizontalTextPosition(JButton.TRAILING); 169 setHorizontalAlignment(JButton.LEADING); 170 updateUI(); 171 } 172 173 private static class MenuItemFocusListener implements FocusListener, 174 Serializable { 175 public void focusGained(FocusEvent event) {} 176 public void focusLost(FocusEvent event) { 177 JMenuItem mi = (JMenuItem )event.getSource(); 180 if(mi.isFocusPainted()) { 181 mi.repaint(); 182 } 183 } 184 } 185 186 187 198 public void setUI(MenuItemUI ui) { 199 super.setUI(ui); 200 } 201 202 207 public void updateUI() { 208 setUI((MenuItemUI)UIManager.getUI(this)); 209 } 210 211 212 220 public String getUIClassID() { 221 return uiClassID; 222 } 223 224 225 236 public void setArmed(boolean b) { 237 ButtonModel model = (ButtonModel ) getModel(); 238 239 boolean oldValue = model.isArmed(); 240 if(model.isArmed() != b) { 241 model.setArmed(b); 242 } 243 } 244 245 251 public boolean isArmed() { 252 ButtonModel model = (ButtonModel ) getModel(); 253 return model.isArmed(); 254 } 255 256 265 public void setEnabled(boolean b) { 266 if (b == false) 268 setArmed(false); 269 super.setEnabled(b); 270 } 271 272 273 279 boolean alwaysOnTop() { 281 if (SwingUtilities.getAncestorOfClass(JInternalFrame .class, this) != 283 null) { 284 return false; 285 } 286 return true; 287 } 288 289 290 292 private KeyStroke accelerator; 293 294 310 public void setAccelerator(KeyStroke keyStroke) { 311 KeyStroke oldAccelerator = accelerator; 312 this.accelerator = keyStroke; 313 firePropertyChange("accelerator", oldAccelerator, accelerator); 314 } 315 316 322 public KeyStroke getAccelerator() { 323 return this.accelerator; 324 } 325 326 339 protected void configurePropertiesFromAction(Action a) { 340 super.configurePropertiesFromAction(a); 341 KeyStroke ks = (a==null) ? null : 342 (KeyStroke )a.getValue(Action.ACCELERATOR_KEY); 343 setAccelerator(ks==null ? null : ks); 344 } 345 346 365 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 366 return new MenuItemPropertyChangeListener(this, a); 367 } 368 369 private static class MenuItemPropertyChangeListener 370 extends AbstractActionPropertyChangeListener 371 implements Serializable { 372 373 MenuItemPropertyChangeListener(JMenuItem m, Action a) { 374 super(m, a); 375 } 376 377 public void propertyChange(PropertyChangeEvent e) { 378 String propertyName = e.getPropertyName(); 379 JMenuItem mi = (JMenuItem )getTarget(); 380 if (mi == null) { Action action = (Action )e.getSource(); 382 action.removePropertyChangeListener(this); 383 } else { 384 if (e.getPropertyName().equals(Action.NAME)) { 385 String text = (String ) e.getNewValue(); 386 mi.setText(text); 387 mi.repaint(); 388 } else if (propertyName.equals("enabled")) { 389 Boolean enabledState = (Boolean ) e.getNewValue(); 390 mi.setEnabled(enabledState.booleanValue()); 391 mi.repaint(); 392 } else if (e.getPropertyName().equals(Action.SMALL_ICON)) { 393 Icon icon = (Icon ) e.getNewValue(); 394 mi.setIcon(icon); 395 mi.invalidate(); 396 mi.repaint(); 397 } else 398 if (e.getPropertyName().equals(Action.MNEMONIC_KEY)) { 399 Integer mn = (Integer ) e.getNewValue(); 400 mi.setMnemonic(mn.intValue()); 401 mi.invalidate(); 402 mi.repaint(); 403 } 404 } 405 } 406 } 407 408 421 public void processMouseEvent(MouseEvent e,MenuElement path[],MenuSelectionManager manager) { 422 processMenuDragMouseEvent( 423 new MenuDragMouseEvent(e.getComponent(), e.getID(), 424 e.getWhen(), 425 e.getModifiers(), e.getX(), e.getY(), 426 e.getClickCount(), e.isPopupTrigger(), 427 path, manager)); 428 } 429 430 431 443 public void processKeyEvent(KeyEvent e,MenuElement path[],MenuSelectionManager manager) { 444 if (DEBUG) { 445 System.out.println("in JMenuItem.processKeyEvent/3 for " + getText() + 446 " " + KeyStroke.getKeyStrokeForEvent(e)); 447 } 448 MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(), 449 e.getWhen(), e.getModifiers(), 450 e.getKeyCode(), e.getKeyChar(), 451 path, manager); 452 processMenuKeyEvent(mke); 453 454 if (mke.isConsumed()) { 455 e.consume(); 456 } 457 } 458 459 460 461 466 public void processMenuDragMouseEvent(MenuDragMouseEvent e) { 467 switch (e.getID()) { 468 case MouseEvent.MOUSE_ENTERED: 469 isMouseDragged = false; fireMenuDragMouseEntered(e); break; 470 case MouseEvent.MOUSE_EXITED: 471 isMouseDragged = false; fireMenuDragMouseExited(e); break; 472 case MouseEvent.MOUSE_DRAGGED: 473 isMouseDragged = true; fireMenuDragMouseDragged(e); break; 474 case MouseEvent.MOUSE_RELEASED: 475 if(isMouseDragged) fireMenuDragMouseReleased(e); break; 476 default: 477 break; 478 } 479 } 480 481 486 public void processMenuKeyEvent(MenuKeyEvent e) { 487 if (DEBUG) { 488 System.out.println("in JMenuItem.processMenuKeyEvent for " + getText()+ 489 " " + KeyStroke.getKeyStrokeForEvent(e)); 490 } 491 switch (e.getID()) { 492 case KeyEvent.KEY_PRESSED: 493 fireMenuKeyPressed(e); break; 494 case KeyEvent.KEY_RELEASED: 495 fireMenuKeyReleased(e); break; 496 case KeyEvent.KEY_TYPED: 497 fireMenuKeyTyped(e); break; 498 default: 499 break; 500 } 501 } 502 503 510 protected void fireMenuDragMouseEntered(MenuDragMouseEvent event) { 511 Object [] listeners = listenerList.getListenerList(); 513 for (int i = listeners.length-2; i>=0; i-=2) { 516 if (listeners[i]==MenuDragMouseListener.class) { 517 ((MenuDragMouseListener)listeners[i+1]).menuDragMouseEntered(event); 519 } 520 } 521 } 522 523 530 protected void fireMenuDragMouseExited(MenuDragMouseEvent event) { 531 Object [] listeners = listenerList.getListenerList(); 533 for (int i = listeners.length-2; i>=0; i-=2) { 536 if (listeners[i]==MenuDragMouseListener.class) { 537 ((MenuDragMouseListener)listeners[i+1]).menuDragMouseExited(event); 539 } 540 } 541 } 542 543 550 protected void fireMenuDragMouseDragged(MenuDragMouseEvent event) { 551 Object [] listeners = listenerList.getListenerList(); 553 for (int i = listeners.length-2; i>=0; i-=2) { 556 if (listeners[i]==MenuDragMouseListener.class) { 557 ((MenuDragMouseListener)listeners[i+1]).menuDragMouseDragged(event); 559 } 560 } 561 } 562 563 570 protected void fireMenuDragMouseReleased(MenuDragMouseEvent event) { 571 Object [] listeners = listenerList.getListenerList(); 573 for (int i = listeners.length-2; i>=0; i-=2) { 576 if (listeners[i]==MenuDragMouseListener.class) { 577 ((MenuDragMouseListener)listeners[i+1]).menuDragMouseReleased(event); 579 } 580 } 581 } 582 583 590 protected void fireMenuKeyPressed(MenuKeyEvent event) { 591 if (DEBUG) { 592 System.out.println("in JMenuItem.fireMenuKeyPressed for " + getText()+ 593 " " + KeyStroke.getKeyStrokeForEvent(event)); 594 } 595 Object [] listeners = listenerList.getListenerList(); 597 for (int i = listeners.length-2; i>=0; i-=2) { 600 if (listeners[i]==MenuKeyListener.class) { 601 ((MenuKeyListener)listeners[i+1]).menuKeyPressed(event); 603 } 604 } 605 } 606 607 614 protected void fireMenuKeyReleased(MenuKeyEvent event) { 615 if (DEBUG) { 616 System.out.println("in JMenuItem.fireMenuKeyReleased for " + getText()+ 617 " " + KeyStroke.getKeyStrokeForEvent(event)); 618 } 619 Object [] listeners = listenerList.getListenerList(); 621 for (int i = listeners.length-2; i>=0; i-=2) { 624 if (listeners[i]==MenuKeyListener.class) { 625 ((MenuKeyListener)listeners[i+1]).menuKeyReleased(event); 627 } 628 } 629 } 630 631 638 protected void fireMenuKeyTyped(MenuKeyEvent event) { 639 if (DEBUG) { 640 System.out.println("in JMenuItem.fireMenuKeyTyped for " + getText()+ 641 " " + KeyStroke.getKeyStrokeForEvent(event)); 642 } 643 Object [] listeners = listenerList.getListenerList(); 645 for (int i = listeners.length-2; i>=0; i-=2) { 648 if (listeners[i]==MenuKeyListener.class) { 649 ((MenuKeyListener)listeners[i+1]).menuKeyTyped(event); 651 } 652 } 653 } 654 655 665 public void menuSelectionChanged(boolean isIncluded) { 666 setArmed(isIncluded); 667 } 668 669 675 public MenuElement [] getSubElements() { 676 return new MenuElement [0]; 677 } 678 679 686 public Component getComponent() { 687 return this; 688 } 689 690 695 public void addMenuDragMouseListener(MenuDragMouseListener l) { 696 listenerList.add(MenuDragMouseListener.class, l); 697 } 698 699 704 public void removeMenuDragMouseListener(MenuDragMouseListener l) { 705 listenerList.remove(MenuDragMouseListener.class, l); 706 } 707 708 716 public MenuDragMouseListener[] getMenuDragMouseListeners() { 717 return (MenuDragMouseListener[])listenerList.getListeners( 718 MenuDragMouseListener.class); 719 } 720 721 726 public void addMenuKeyListener(MenuKeyListener l) { 727 listenerList.add(MenuKeyListener.class, l); 728 } 729 730 735 public void removeMenuKeyListener(MenuKeyListener l) { 736 listenerList.remove(MenuKeyListener.class, l); 737 } 738 739 747 public MenuKeyListener[] getMenuKeyListeners() { 748 return (MenuKeyListener[])listenerList.getListeners( 749 MenuKeyListener.class); 750 } 751 752 756 private void readObject(ObjectInputStream s) 757 throws IOException , ClassNotFoundException 758 { 759 s.defaultReadObject(); 760 if (getUIClassID().equals(uiClassID)) { 761 updateUI(); 762 } 763 } 764 765 private void writeObject(ObjectOutputStream s) throws IOException { 766 s.defaultWriteObject(); 767 if (getUIClassID().equals(uiClassID)) { 768 byte count = JComponent.getWriteObjCounter(this); 769 JComponent.setWriteObjCounter(this, --count); 770 if (count == 0 && ui != null) { 771 ui.installUI(this); 772 } 773 } 774 } 775 776 777 786 protected String paramString() { 787 return super.paramString(); 788 } 789 790 794 804 public AccessibleContext getAccessibleContext() { 805 if (accessibleContext == null) { 806 accessibleContext = new AccessibleJMenuItem(); 807 } 808 return accessibleContext; 809 } 810 811 812 827 protected class AccessibleJMenuItem extends AccessibleAbstractButton implements ChangeListener { 828 829 private boolean isArmed = false; 830 private boolean hasFocus = false; 831 private boolean isPressed = false; 832 private boolean isSelected = false; 833 834 AccessibleJMenuItem() { 835 super(); 836 JMenuItem.this.addChangeListener(this); 837 } 838 839 845 public AccessibleRole getAccessibleRole() { 846 return AccessibleRole.MENU_ITEM; 847 } 848 849 private void fireAccessibilityFocusedEvent(JMenuItem toCheck) { 850 MenuElement [] path = 851 MenuSelectionManager.defaultManager().getSelectedPath(); 852 if (path.length > 0) { 853 Object menuItem = path[path.length - 1]; 854 if (toCheck == menuItem) { 855 firePropertyChange( 856 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 857 null, AccessibleState.FOCUSED); 858 } 859 } 860 } 861 862 865 public void stateChanged(ChangeEvent e) { 866 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 867 Boolean.valueOf(false), Boolean.valueOf(true)); 868 if (JMenuItem.this.getModel().isArmed()) { 869 if (!isArmed) { 870 isArmed = true; 871 firePropertyChange( 872 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 873 null, AccessibleState.ARMED); 874 fireAccessibilityFocusedEvent(JMenuItem.this); 878 } 879 } else { 880 if (isArmed) { 881 isArmed = false; 882 firePropertyChange( 883 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 884 AccessibleState.ARMED, null); 885 } 886 } 887 if (JMenuItem.this.isFocusOwner()) { 888 if (!hasFocus) { 889 hasFocus = true; 890 firePropertyChange( 891 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 892 null, AccessibleState.FOCUSED); 893 } 894 } else { 895 if (hasFocus) { 896 hasFocus = false; 897 firePropertyChange( 898 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 899 AccessibleState.FOCUSED, null); 900 } 901 } 902 if (JMenuItem.this.getModel().isPressed()) { 903 if (!isPressed) { 904 isPressed = true; 905 firePropertyChange( 906 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 907 null, AccessibleState.PRESSED); 908 } 909 } else { 910 if (isPressed) { 911 isPressed = false; 912 firePropertyChange( 913 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 914 AccessibleState.PRESSED, null); 915 } 916 } 917 if (JMenuItem.this.getModel().isSelected()) { 918 if (!isSelected) { 919 isSelected = true; 920 firePropertyChange( 921 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 922 null, AccessibleState.CHECKED); 923 924 fireAccessibilityFocusedEvent(JMenuItem.this); 928 } 929 } else { 930 if (isSelected) { 931 isSelected = false; 932 firePropertyChange( 933 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 934 AccessibleState.CHECKED, null); 935 } 936 } 937 938 } 939 } } 941 942 | Popular Tags |