Showing the changes to java/pkg/javax/swing/JMenuItem.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,947 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | package javax.swing; | ||
| 26 | |||
| 27 | import java.util.EventListener; | ||
| 28 | import java.awt.*; | ||
| 29 | import java.awt.event.*; | ||
| 30 | import java.awt.image.*; | ||
| 31 | |||
| 32 | import java.beans.PropertyChangeEvent; | ||
| 33 | import java.beans.PropertyChangeListener; | ||
| 34 | |||
| 35 | import java.io.Serializable; | ||
| 36 | import java.io.ObjectOutputStream; | ||
| 37 | import java.io.ObjectInputStream; | ||
| 38 | import java.io.IOException; | ||
| 39 | |||
| 40 | import javax.swing.plaf.*; | ||
| 41 | import javax.swing.plaf.basic.*; | ||
| 42 | import javax.swing.event.*; | ||
| 43 | import javax.accessibility.*; | ||
| 44 | |||
| 45 | /** | ||
| 46 | * An implementation of an item in a menu. A menu item is essentially a button | ||
| 47 | * sitting in a list. When the user selects the "button", the action | ||
| 48 | * associated with the menu item is performed. A <code>JMenuItem</code> | ||
| 49 | * contained in a <code>JPopupMenu</code> performs exactly that function. | ||
| 50 | * <p> | ||
| 51 | * Menu items can be configured, and to some degree controlled, by | ||
| 52 | * <code><a href="Action.html">Action</a></code>s. Using an | ||
| 53 | * <code>Action</code> with a menu item has many benefits beyond directly | ||
| 54 | * configuring a menu item. Refer to <a href="Action.html#buttonActions"> | ||
| 55 | * Swing Components Supporting <code>Action</code></a> for more | ||
| 56 | * details, and you can find more information in <a | ||
| 57 | * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How | ||
| 58 | * to Use Actions</a>, a section in <em>The Java Tutorial</em>. | ||
| 59 | * <p> | ||
| 60 | * For further documentation and for examples, see | ||
| 61 | * <a | ||
| 62 | href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a> | ||
| 63 | * in <em>The Java Tutorial.</em> | ||
| 64 | * <p> | ||
| 65 | * <strong>Warning:</strong> Swing is not thread safe. For more | ||
| 66 | * information see <a | ||
| 67 | * href="package-summary.html#threading">Swing's Threading | ||
| 68 | * Policy</a>. | ||
| 69 | * <p> | ||
| 70 | * <strong>Warning:</strong> | ||
| 71 | * Serialized objects of this class will not be compatible with | ||
| 72 | * future Swing releases. The current serialization support is | ||
| 73 | * appropriate for short term storage or RMI between applications running | ||
| 74 | * the same version of Swing. As of 1.4, support for long term storage | ||
| 75 | * of all JavaBeans<sup><font size="-2">TM</font></sup> | ||
| 76 | * has been added to the <code>java.beans</code> package. | ||
| 77 | * Please see {@link java.beans.XMLEncoder}. | ||
| 78 | * | ||
| 79 | * @beaninfo | ||
| 80 | * attribute: isContainer false | ||
| 81 | * description: An item which can be selected in a menu. | ||
| 82 | * | ||
| 83 | * @author Georges Saab | ||
| 84 | * @author David Karlton | ||
| 85 | * @see JPopupMenu | ||
| 86 | * @see JMenu | ||
| 87 | * @see JCheckBoxMenuItem | ||
| 88 | * @see JRadioButtonMenuItem | ||
| 89 | */ | ||
| 90 | public class JMenuItem extends AbstractButton implements Accessible,MenuElement { | ||
| 91 | |||
| 92 | /** | ||
| 93 | * @see #getUIClassID | ||
| 94 | * @see #readObject | ||
| 95 | */ | ||
| 96 | private static final String uiClassID = "MenuItemUI"; | ||
| 97 | |||
| 98 | /* diagnostic aids -- should be false for production builds. */ | ||
| 99 | private static final boolean TRACE = false; // trace creates and disposes | ||
| 100 | private static final boolean VERBOSE = false; // show reuse hits/misses | ||
| 101 | private static final boolean DEBUG = false; // show bad params, misc. | ||
| 102 | |||
| 103 | private boolean isMouseDragged = false; | ||
| 104 | |||
| 105 | /** | ||
| 106 | * Creates a <code>JMenuItem</code> with no set text or icon. | ||
| 107 | */ | ||
| 108 | public JMenuItem() { | ||
| 109 | this(null, (Icon)null); | ||
| 110 | } | ||
| 111 | |||
| 112 | /** | ||
| 113 | * Creates a <code>JMenuItem</code> with the specified icon. | ||
| 114 | * | ||
| 115 | * @param icon the icon of the <code>JMenuItem</code> | ||
| 116 | */ | ||
| 117 | public JMenuItem(Icon icon) { | ||
| 118 | this(null, icon); | ||
| 119 | } | ||
| 120 | |||
| 121 | /** | ||
| 122 | * Creates a <code>JMenuItem</code> with the specified text. | ||
| 123 | * | ||
| 124 | * @param text the text of the <code>JMenuItem</code> | ||
| 125 | */ | ||
| 126 | public JMenuItem(String text) { | ||
| 127 | this(text, (Icon)null); | ||
| 128 | } | ||
| 129 | |||
| 130 | /** | ||
| 131 | * Creates a menu item whose properties are taken from the | ||
| 132 | * specified <code>Action</code>. | ||
| 133 | * | ||
| 134 | * @param a the action of the <code>JMenuItem</code> | ||
| 135 | * @since 1.3 | ||
| 136 | */ | ||
| 137 | public JMenuItem(Action a) { | ||
| 138 | this(); | ||
| 139 | setAction(a); | ||
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * Creates a <code>JMenuItem</code> with the specified text and icon. | ||
| 144 | * | ||
| 145 | * @param text the text of the <code>JMenuItem</code> | ||
| 146 | * @param icon the icon of the <code>JMenuItem</code> | ||
| 147 | */ | ||
| 148 | public JMenuItem(String text, Icon icon) { | ||
| 149 | setModel(new DefaultButtonModel()); | ||
| 150 | init(text, icon); | ||
| 151 | initFocusability(); | ||
| 152 | } | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Creates a <code>JMenuItem</code> with the specified text and | ||
| 156 | * keyboard mnemonic. | ||
| 157 | * | ||
| 158 | * @param text the text of the <code>JMenuItem</code> | ||
| 159 | * @param mnemonic the keyboard mnemonic for the <code>JMenuItem</code> | ||
| 160 | */ | ||
| 161 | public JMenuItem(String text, int mnemonic) { | ||
| 162 | setModel(new DefaultButtonModel()); | ||
| 163 | init(text, null); | ||
| 164 | setMnemonic(mnemonic); | ||
| 165 | initFocusability(); | ||
| 166 | } | ||
| 167 | |||
| 168 | /** | ||
| 169 | * {@inheritDoc} | ||
| 170 | */ | ||
| 171 | public void setModel(ButtonModel newModel) { | ||
| 172 | super.setModel(newModel); | ||
| 173 | if(newModel instanceof DefaultButtonModel) { | ||
| 174 | ((DefaultButtonModel)newModel).setMenuItem(true); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | /** | ||
| 179 | * Inititalizes the focusability of the the <code>JMenuItem</code>. | ||
| 180 | * <code>JMenuItem</code>'s are focusable, but subclasses may | ||
| 181 | * want to be, this provides them the opportunity to override this | ||
| 182 | * and invoke something else, or nothing at all. Refer to | ||
| 183 | * {@link javax.swing.JMenu#initFocusability} for the motivation of | ||
| 184 | * this. | ||
| 185 | */ | ||
| 186 | void initFocusability() { | ||
| 187 | setFocusable(false); | ||
| 188 | } | ||
| 189 | |||
| 190 | /** | ||
| 191 | * Initializes the menu item with the specified text and icon. | ||
| 192 | * | ||
| 193 | * @param text the text of the <code>JMenuItem</code> | ||
| 194 | * @param icon the icon of the <code>JMenuItem</code> | ||
| 195 | */ | ||
| 196 | protected void init(String text, Icon icon) { | ||
| 197 | if(text != null) { | ||
| 198 | setText(text); | ||
| 199 | } | ||
| 200 | |||
| 201 | if(icon != null) { | ||
| 202 | setIcon(icon); | ||
| 203 | } | ||
| 204 | |||
| 205 | // Listen for Focus events | ||
| 206 | addFocusListener(new MenuItemFocusListener()); | ||
| 207 | setUIProperty("borderPainted", Boolean.FALSE); | ||
| 208 | setFocusPainted(false); | ||
| 209 | setHorizontalTextPosition(JButton.TRAILING); | ||
| 210 | setHorizontalAlignment(JButton.LEADING); | ||
| 211 | updateUI(); | ||
| 212 | } | ||
| 213 | |||
| 214 | private static class MenuItemFocusListener implements FocusListener, | ||
| 215 | Serializable { | ||
| 216 | public void focusGained(FocusEvent event) {} | ||
| 217 | public void focusLost(FocusEvent event) { | ||
| 218 | // When focus is lost, repaint if | ||
| 219 | // the focus information is painted | ||
| 220 | JMenuItem mi = (JMenuItem)event.getSource(); | ||
| 221 | if(mi.isFocusPainted()) { | ||
| 222 | mi.repaint(); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | /** | ||
| 229 | * Sets the look and feel object that renders this component. | ||
| 230 | * | ||
| 231 | * @param ui the <code>JMenuItemUI</code> L&F object | ||
| 232 | * @see UIDefaults#getUI | ||
| 233 | * @beaninfo | ||
| 234 | * bound: true | ||
| 235 | * hidden: true | ||
| 236 | * attribute: visualUpdate true | ||
| 237 | * description: The UI object that implements the Component's LookAndFeel. | ||
| 238 | */ | ||
| 239 | public void setUI(MenuItemUI ui) { | ||
| 240 | super.setUI(ui); | ||
| 241 | } | ||
| 242 | |||
| 243 | /** | ||
| 244 | * Resets the UI property with a value from the current look and feel. | ||
| 245 | * | ||
| 246 | * @see JComponent#updateUI | ||
| 247 | */ | ||
| 248 | public void updateUI() { | ||
| 249 | setUI((MenuItemUI)UIManager.getUI(this)); | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | /** | ||
| 254 | * Returns the suffix used to construct the name of the L&F class used to | ||
| 255 | * render this component. | ||
| 256 | * | ||
| 257 | * @return the string "MenuItemUI" | ||
| 258 | * @see JComponent#getUIClassID | ||
| 259 | * @see UIDefaults#getUI | ||
| 260 | */ | ||
| 261 | public String getUIClassID() { | ||
| 262 | return uiClassID; | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | /** | ||
| 267 | * Identifies the menu item as "armed". If the mouse button is | ||
| 268 | * released while it is over this item, the menu's action event | ||
| 269 | * will fire. If the mouse button is released elsewhere, the | ||
| 270 | * event will not fire and the menu item will be disarmed. | ||
| 271 | * | ||
| 272 | * @param b true to arm the menu item so it can be selected | ||
| 273 | * @beaninfo | ||
| 274 | * description: Mouse release will fire an action event | ||
| 275 | * hidden: true | ||
| 276 | */ | ||
| 277 | public void setArmed(boolean b) { | ||
| 278 | ButtonModel model = getModel(); | ||
| 279 | |||
| 280 | boolean oldValue = model.isArmed(); | ||
| 281 | if(model.isArmed() != b) { | ||
| 282 | model.setArmed(b); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | /** | ||
| 287 | * Returns whether the menu item is "armed". | ||
| 288 | * | ||
| 289 | * @return true if the menu item is armed, and it can be selected | ||
| 290 | * @see #setArmed | ||
| 291 | */ | ||
| 292 | public boolean isArmed() { | ||
| 293 | ButtonModel model = getModel(); | ||
| 294 | return model.isArmed(); | ||
| 295 | } | ||
| 296 | |||
| 297 | /** | ||
| 298 | * Enables or disables the menu item. | ||
| 299 | * | ||
| 300 | * @param b true to enable the item | ||
| 301 | * @beaninfo | ||
| 302 | * description: Does the component react to user interaction | ||
| 303 | * bound: true | ||
| 304 | * preferred: true | ||
| 305 | */ | ||
| 306 | public void setEnabled(boolean b) { | ||
| 307 | // Make sure we aren't armed! | ||
| 308 | if (!b && !UIManager.getBoolean("MenuItem.disabledAreNavigable")) { | ||
| 309 | setArmed(false); | ||
| 310 | } | ||
| 311 | super.setEnabled(b); | ||
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | /** | ||
| 316 | * Returns true since <code>Menu</code>s, by definition, | ||
| 317 | * should always be on top of all other windows. If the menu is | ||
| 318 | * in an internal frame false is returned due to the rollover effect | ||
| 319 | * for windows laf where the menu is not always on top. | ||
| 320 | */ | ||
| 321 | // package private | ||
| 322 | boolean alwaysOnTop() { | ||
| 323 | // Fix for bug #4482165 | ||
| 324 | if (SwingUtilities.getAncestorOfClass(JInternalFrame.class, this) != | ||
| 325 | null) { | ||
| 326 | return false; | ||
| 327 | } | ||
| 328 | return true; | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | /* The keystroke which acts as the menu item's accelerator | ||
| 333 | */ | ||
| 334 | private KeyStroke accelerator; | ||
| 335 | |||
| 336 | /** | ||
| 337 | * Sets the key combination which invokes the menu item's | ||
| 338 | * action listeners without navigating the menu hierarchy. It is the | ||
| 339 | * UI's responsibility to install the correct action. Note that | ||
| 340 | * when the keyboard accelerator is typed, it will work whether or | ||
| 341 | * not the menu is currently displayed. | ||
| 342 | * | ||
| 343 | * @param keyStroke the <code>KeyStroke</code> which will | ||
| 344 | * serve as an accelerator | ||
| 345 | * @beaninfo | ||
| 346 | * description: The keystroke combination which will invoke the | ||
| 347 | * JMenuItem's actionlisteners without navigating the | ||
| 348 | * menu hierarchy | ||
| 349 | * bound: true | ||
| 350 | * preferred: true | ||
| 351 | */ | ||
| 352 | public void setAccelerator(KeyStroke keyStroke) { | ||
| 353 | KeyStroke oldAccelerator = accelerator; | ||
| 354 | this.accelerator = keyStroke; | ||
| 355 | repaint(); | ||
| 356 | revalidate(); | ||
| 357 | firePropertyChange("accelerator", oldAccelerator, accelerator); | ||
| 358 | } | ||
| 359 | |||
| 360 | /** | ||
| 361 | * Returns the <code>KeyStroke</code> which serves as an accelerator | ||
| 362 | * for the menu item. | ||
| 363 | * @return a <code>KeyStroke</code> object identifying the | ||
| 364 | * accelerator key | ||
| 365 | */ | ||
| 366 | public KeyStroke getAccelerator() { | ||
| 367 | return this.accelerator; | ||
| 368 | } | ||
| 369 | |||
| 370 | /** | ||
| 371 | * {@inheritDoc} | ||
| 372 | * | ||
| 373 | * @since 1.3 | ||
| 374 | */ | ||
| 375 | protected void configurePropertiesFromAction(Action a) { | ||
| 376 | super.configurePropertiesFromAction(a); | ||
| 377 | configureAcceleratorFromAction(a); | ||
| 378 | } | ||
| 379 | |||
| 380 | void setIconFromAction(Action a) { | ||
| 381 | Icon icon = null; | ||
| 382 | if (a != null) { | ||
| 383 | icon = (Icon)a.getValue(Action.SMALL_ICON); | ||
| 384 | } | ||
| 385 | setIcon(icon); | ||
| 386 | } | ||
| 387 | |||
| 388 | void largeIconChanged(Action a) { | ||
| 389 | } | ||
| 390 | |||
| 391 | void smallIconChanged(Action a) { | ||
| 392 | setIconFromAction(a); | ||
| 393 | } | ||
| 394 | |||
| 395 | void configureAcceleratorFromAction(Action a) { | ||
| 396 | KeyStroke ks = (a==null) ? null : | ||
| 397 | (KeyStroke)a.getValue(Action.ACCELERATOR_KEY); | ||
| 398 | setAccelerator(ks); | ||
| 399 | } | ||
| 400 | |||
| 401 | /** | ||
| 402 | * {@inheritDoc} | ||
| 403 | * @since 1.6 | ||
| 404 | */ | ||
| 405 | protected void actionPropertyChanged(Action action, String propertyName) { | ||
| 406 | if (propertyName == Action.ACCELERATOR_KEY) { | ||
| 407 | configureAcceleratorFromAction(action); | ||
| 408 | } | ||
| 409 | else { | ||
| 410 | super.actionPropertyChanged(action, propertyName); | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | /** | ||
| 415 | * Processes a mouse event forwarded from the | ||
| 416 | * <code>MenuSelectionManager</code> and changes the menu | ||
| 417 | * selection, if necessary, by using the | ||
| 418 | * <code>MenuSelectionManager</code>'s API. | ||
| 419 | * <p> | ||
| 420 | * Note: you do not have to forward the event to sub-components. | ||
| 421 | * This is done automatically by the <code>MenuSelectionManager</code>. | ||
| 422 | * | ||
| 423 | * @param e a <code>MouseEvent</code> | ||
| 424 | * @param path the <code>MenuElement</code> path array | ||
| 425 | * @param manager the <code>MenuSelectionManager</code> | ||
| 426 | */ | ||
| 427 | public void processMouseEvent(MouseEvent e,MenuElement path[],MenuSelectionManager manager) { | ||
| 428 | processMenuDragMouseEvent( | ||
| 429 | new MenuDragMouseEvent(e.getComponent(), e.getID(), | ||
| 430 | e.getWhen(), | ||
| 431 | e.getModifiers(), e.getX(), e.getY(), | ||
| 432 | e.getXOnScreen(), e.getYOnScreen(), | ||
| 433 | e.getClickCount(), e.isPopupTrigger(), | ||
| 434 | path, manager)); | ||
| 435 | } | ||
| 436 | |||
| 437 | |||
| 438 | /** | ||
| 439 | * Processes a key event forwarded from the | ||
| 440 | * <code>MenuSelectionManager</code> and changes the menu selection, | ||
| 441 | * if necessary, by using <code>MenuSelectionManager</code>'s API. | ||
| 442 | * <p> | ||
| 443 | * Note: you do not have to forward the event to sub-components. | ||
| 444 | * This is done automatically by the <code>MenuSelectionManager</code>. | ||
| 445 | * | ||
| 446 | * @param e a <code>KeyEvent</code> | ||
| 447 | * @param path the <code>MenuElement</code> path array | ||
| 448 | * @param manager the <code>MenuSelectionManager</code> | ||
| 449 | */ | ||
| 450 | public void processKeyEvent(KeyEvent e,MenuElement path[],MenuSelectionManager manager) { | ||
| 451 | if (DEBUG) { | ||
| 452 | System.out.println("in JMenuItem.processKeyEvent/3 for " + getText() + | ||
| 453 | " " + KeyStroke.getKeyStrokeForEvent(e)); | ||
| 454 | } | ||
| 455 | MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(), | ||
| 456 | e.getWhen(), e.getModifiers(), | ||
| 457 | e.getKeyCode(), e.getKeyChar(), | ||
| 458 | path, manager); | ||
| 459 | processMenuKeyEvent(mke); | ||
| 460 | |||
| 461 | if (mke.isConsumed()) { | ||
| 462 | e.consume(); | ||
| 463 | } | ||
| 464 | } | ||
| 465 | |||
| 466 | |||
| 467 | |||
| 468 | /** | ||
| 469 | * Handles mouse drag in a menu. | ||
| 470 | * | ||
| 471 | * @param e a <code>MenuDragMouseEvent</code> object | ||
| 472 | */ | ||
| 473 | public void processMenuDragMouseEvent(MenuDragMouseEvent e) { | ||
| 474 | switch (e.getID()) { | ||
| 475 | case MouseEvent.MOUSE_ENTERED: | ||
| 476 | isMouseDragged = false; fireMenuDragMouseEntered(e); break; | ||
| 477 | case MouseEvent.MOUSE_EXITED: | ||
| 478 | isMouseDragged = false; fireMenuDragMouseExited(e); break; | ||
| 479 | case MouseEvent.MOUSE_DRAGGED: | ||
| 480 | isMouseDragged = true; fireMenuDragMouseDragged(e); break; | ||
| 481 | case MouseEvent.MOUSE_RELEASED: | ||
| 482 | if(isMouseDragged) fireMenuDragMouseReleased(e); break; | ||
| 483 | default: | ||
| 484 | break; | ||
| 485 | } | ||
| 486 | } | ||
| 487 | |||
| 488 | /** | ||
| 489 | * Handles a keystroke in a menu. | ||
| 490 | * | ||
| 491 | * @param e a <code>MenuKeyEvent</code> object | ||
| 492 | */ | ||
| 493 | public void processMenuKeyEvent(MenuKeyEvent e) { | ||
| 494 | if (DEBUG) { | ||
| 495 | System.out.println("in JMenuItem.processMenuKeyEvent for " + getText()+ | ||
| 496 | " " + KeyStroke.getKeyStrokeForEvent(e)); | ||
| 497 | } | ||
| 498 | switch (e.getID()) { | ||
| 499 | case KeyEvent.KEY_PRESSED: | ||
| 500 | fireMenuKeyPressed(e); break; | ||
| 501 | case KeyEvent.KEY_RELEASED: | ||
| 502 | fireMenuKeyReleased(e); break; | ||
| 503 | case KeyEvent.KEY_TYPED: | ||
| 504 | fireMenuKeyTyped(e); break; | ||
| 505 | default: | ||
| 506 | break; | ||
| 507 | } | ||
| 508 | } | ||
| 509 | |||
| 510 | /** | ||
| 511 | * Notifies all listeners that have registered interest for | ||
| 512 | * notification on this event type. | ||
| 513 | * | ||
| 514 | * @param event a <code>MenuMouseDragEvent</code> | ||
| 515 | * @see EventListenerList | ||
| 516 | */ | ||
| 517 | protected void fireMenuDragMouseEntered(MenuDragMouseEvent event) { | ||
| 518 | // Guaranteed to return a non-null array | ||
| 519 | Object[] listeners = listenerList.getListenerList(); | ||
| 520 | // Process the listeners last to first, notifying | ||
| 521 | // those that are interested in this event | ||
| 522 | for (int i = listeners.length-2; i>=0; i-=2) { | ||
| 523 | if (listeners[i]==MenuDragMouseListener.class) { | ||
| 524 | // Lazily create the event: | ||
| 525 | ((MenuDragMouseListener)listeners[i+1]).menuDragMouseEntered(event); | ||
| 526 | } | ||
| 527 | } | ||
| 528 | } | ||
| 529 | |||
| 530 | /** | ||
| 531 | * Notifies all listeners that have registered interest for | ||
| 532 | * notification on this event type. | ||
| 533 | * | ||
| 534 | * @param event a <code>MenuDragMouseEvent</code> | ||
| 535 | * @see EventListenerList | ||
| 536 | */ | ||
| 537 | protected void fireMenuDragMouseExited(MenuDragMouseEvent event) { | ||
| 538 | // Guaranteed to return a non-null array | ||
| 539 | Object[] listeners = listenerList.getListenerList(); | ||
| 540 | // Process the listeners last to first, notifying | ||
| 541 | // those that are interested in this event | ||
| 542 | for (int i = listeners.length-2; i>=0; i-=2) { | ||
| 543 | if (listeners[i]==MenuDragMouseListener.class) { | ||
| 544 | // Lazily create the event: | ||
| 545 | ((MenuDragMouseListener)listeners[i+1]).menuDragMouseExited(event); | ||
| 546 | } | ||
| 547 | } | ||
| 548 | } | ||
| 549 | |||
| 550 | /** | ||
| 551 | * Notifies all listeners that have registered interest for | ||
| 552 | * notification on this event type. | ||
| 553 | * | ||
| 554 | * @param event a <code>MenuDragMouseEvent</code> | ||
| 555 | * @see EventListenerList | ||
| 556 | */ | ||
| 557 | protected void fireMenuDragMouseDragged(MenuDragMouseEvent event) { | ||
| 558 | // Guaranteed to return a non-null array | ||
| 559 | Object[] listeners = listenerList.getListenerList(); | ||
| 560 | // Process the listeners last to first, notifying | ||
| 561 | // those that are interested in this event | ||
| 562 | for (int i = listeners.length-2; i>=0; i-=2) { | ||
| 563 | if (listeners[i]==MenuDragMouseListener.class) { | ||
| 564 | // Lazily create the event: | ||
| 565 | ((MenuDragMouseListener)listeners[i+1]).menuDragMouseDragged(event); | ||
| 566 | } | ||
| 567 | } | ||
| 568 | } | ||
| 569 | |||
| 570 | /** | ||
| 571 | * Notifies all listeners that have registered interest for | ||
| 572 | * notification on this event type. | ||
| 573 | * | ||
| 574 | * @param event a <code>MenuDragMouseEvent</code> | ||
| 575 | * @see EventListenerList | ||
| 576 | */ | ||
| 577 | protected void fireMenuDragMouseReleased(MenuDragMouseEvent event) { | ||
| 578 | // Guaranteed to return a non-null array | ||
| 579 | Object[] listeners = listenerList.getListenerList(); | ||
| 580 | // Process the listeners last to first, notifying | ||
| 581 | // those that are interested in this event | ||
| 582 | for (int i = listeners.length-2; i>=0; i-=2) { | ||
| 583 | if (listeners[i]==MenuDragMouseListener.class) { | ||
| 584 | // Lazily create the event: | ||
| 585 | ((MenuDragMouseListener)listeners[i+1]).menuDragMouseReleased(event); | ||
| 586 | } | ||
| 587 | } | ||
| 588 | } | ||
| 589 | |||
| 590 | /** | ||
| 591 | * Notifies all listeners that have registered interest for | ||
| 592 | * notification on this event type. | ||
| 593 | * | ||
| 594 | * @param event a <code>MenuKeyEvent</code> | ||
| 595 | * @see EventListenerList | ||
| 596 | */ | ||
| 597 | protected void fireMenuKeyPressed(MenuKeyEvent event) { | ||
| 598 | if (DEBUG) { | ||
| 599 | System.out.println("in JMenuItem.fireMenuKeyPressed for " + getText()+ | ||
| 600 | " " + KeyStroke.getKeyStrokeForEvent(event)); | ||
| 601 | } | ||
| 602 | // Guaranteed to return a non-null array | ||
| 603 | Object[] listeners = listenerList.getListenerList(); | ||
| 604 | // Process the listeners last to first, notifying | ||
| 605 | // those that are interested in this event | ||
| 606 | for (int i = listeners.length-2; i>=0; i-=2) { | ||
| 607 | if (listeners[i]==MenuKeyListener.class) { | ||
| 608 | // Lazily create the event: | ||
| 609 | ((MenuKeyListener)listeners[i+1]).menuKeyPressed(event); | ||
| 610 | } | ||
| 611 | } | ||
| 612 | } | ||
| 613 | |||
| 614 | /** | ||
| 615 | * Notifies all listeners that have registered interest for | ||
| 616 | * notification on this event type. | ||
| 617 | * | ||
| 618 | * @param event a <code>MenuKeyEvent</code> | ||
| 619 | * @see EventListenerList | ||
| 620 | */ | ||
| 621 | protected void fireMenuKeyReleased(MenuKeyEvent event) { | ||
| 622 | if (DEBUG) { | ||
| 623 | System.out.println("in JMenuItem.fireMenuKeyReleased for " + getText()+ | ||
| 624 | " " + KeyStroke.getKeyStrokeForEvent(event)); | ||
| 625 | } | ||
| 626 | // Guaranteed to return a non-null array | ||
| 627 | Object[] listeners = listenerList.getListenerList(); | ||
| 628 | // Process the listeners last to first, notifying | ||
| 629 | // those that are interested in this event | ||
| 630 | for (int i = listeners.length-2; i>=0; i-=2) { | ||
| 631 | if (listeners[i]==MenuKeyListener.class) { | ||
| 632 | // Lazily create the event: | ||
| 633 | ((MenuKeyListener)listeners[i+1]).menuKeyReleased(event); | ||
| 634 | } | ||
| 635 | } | ||
| 636 | } | ||
| 637 | |||
| 638 | /** | ||
| 639 | * Notifies all listeners that have registered interest for | ||
| 640 | * notification on this event type. | ||
| 641 | * | ||
| 642 | * @param event a <code>MenuKeyEvent</code> | ||
| 643 | * @see EventListenerList | ||
| 644 | */ | ||
| 645 | protected void fireMenuKeyTyped(MenuKeyEvent event) { | ||
| 646 | if (DEBUG) { | ||
| 647 | System.out.println("in JMenuItem.fireMenuKeyTyped for " + getText()+ | ||
| 648 | " " + KeyStroke.getKeyStrokeForEvent(event)); | ||
| 649 | } | ||
| 650 | // Guaranteed to return a non-null array | ||
| 651 | Object[] listeners = listenerList.getListenerList(); | ||
| 652 | // Process the listeners last to first, notifying | ||
| 653 | // those that are interested in this event | ||
| 654 | for (int i = listeners.length-2; i>=0; i-=2) { | ||
| 655 | if (listeners[i]==MenuKeyListener.class) { | ||
| 656 | // Lazily create the event: | ||
| 657 | ((MenuKeyListener)listeners[i+1]).menuKeyTyped(event); | ||
| 658 | } | ||
| 659 | } | ||
| 660 | } | ||
| 661 | |||
| 662 | /** | ||
| 663 | * Called by the <code>MenuSelectionManager</code> when the | ||
| 664 | * <code>MenuElement</code> is selected or unselected. | ||
| 665 | * | ||
| 666 | * @param isIncluded true if this menu item is on the part of the menu | ||
| 667 | * path that changed, false if this menu is part of the | ||
| 668 | * a menu path that changed, but this particular part of | ||
| 669 | * that path is still the same | ||
| 670 | * @see MenuSelectionManager#setSelectedPath(MenuElement[]) | ||
| 671 | */ | ||
| 672 | public void menuSelectionChanged(boolean isIncluded) { | ||
| 673 | setArmed(isIncluded); | ||
| 674 | } | ||
| 675 | |||
| 676 | /** | ||
| 677 | * This method returns an array containing the sub-menu | ||
| 678 | * components for this menu component. | ||
| 679 | * | ||
| 680 | * @return an array of <code>MenuElement</code>s | ||
| 681 | */ | ||
| 682 | public MenuElement[] getSubElements() { | ||
| 683 | return new MenuElement[0]; | ||
| 684 | } | ||
| 685 | |||
| 686 | /** | ||
| 687 | * Returns the <code>java.awt.Component</code> used to paint | ||
| 688 | * this object. The returned component will be used to convert | ||
| 689 | * events and detect if an event is inside a menu component. | ||
| 690 | * | ||
| 691 | * @return the <code>Component</code> that paints this menu item | ||
| 692 | */ | ||
| 693 | public Component getComponent() { | ||
| 694 | return this; | ||
| 695 | } | ||
| 696 | |||
| 697 | /** | ||
| 698 | * Adds a <code>MenuDragMouseListener</code> to the menu item. | ||
| 699 | * | ||
| 700 | * @param l the <code>MenuDragMouseListener</code> to be added | ||
| 701 | */ | ||
| 702 | public void addMenuDragMouseListener(MenuDragMouseListener l) { | ||
| 703 | listenerList.add(MenuDragMouseListener.class, l); | ||
| 704 | } | ||
| 705 | |||
| 706 | /** | ||
| 707 | * Removes a <code>MenuDragMouseListener</code> from the menu item. | ||
| 708 | * | ||
| 709 | * @param l the <code>MenuDragMouseListener</code> to be removed | ||
| 710 | */ | ||
| 711 | public void removeMenuDragMouseListener(MenuDragMouseListener l) { | ||
| 712 | listenerList.remove(MenuDragMouseListener.class, l); | ||
| 713 | } | ||
| 714 | |||
| 715 | /** | ||
| 716 | * Returns an array of all the <code>MenuDragMouseListener</code>s added | ||
| 717 | * to this JMenuItem with addMenuDragMouseListener(). | ||
| 718 | * | ||
| 719 | * @return all of the <code>MenuDragMouseListener</code>s added or an empty | ||
| 720 | * array if no listeners have been added | ||
| 721 | * @since 1.4 | ||
| 722 | */ | ||
| 723 | public MenuDragMouseListener[] getMenuDragMouseListeners() { | ||
| 724 | return listenerList.getListeners(MenuDragMouseListener.class); | ||
| 725 | } | ||
| 726 | |||
| 727 | /** | ||
| 728 | * Adds a <code>MenuKeyListener</code> to the menu item. | ||
| 729 | * | ||
| 730 | * @param l the <code>MenuKeyListener</code> to be added | ||
| 731 | */ | ||
| 732 | public void addMenuKeyListener(MenuKeyListener l) { | ||
| 733 | listenerList.add(MenuKeyListener.class, l); | ||
| 734 | } | ||
| 735 | |||
| 736 | /** | ||
| 737 | * Removes a <code>MenuKeyListener</code> from the menu item. | ||
| 738 | * | ||
| 739 | * @param l the <code>MenuKeyListener</code> to be removed | ||
| 740 | */ | ||
| 741 | public void removeMenuKeyListener(MenuKeyListener l) { | ||
| 742 | listenerList.remove(MenuKeyListener.class, l); | ||
| 743 | } | ||
| 744 | |||
| 745 | /** | ||
| 746 | * Returns an array of all the <code>MenuKeyListener</code>s added | ||
| 747 | * to this JMenuItem with addMenuKeyListener(). | ||
| 748 | * | ||
| 749 | * @return all of the <code>MenuKeyListener</code>s added or an empty | ||
| 750 | * array if no listeners have been added | ||
| 751 | * @since 1.4 | ||
| 752 | */ | ||
| 753 | public MenuKeyListener[] getMenuKeyListeners() { | ||
| 754 | return listenerList.getListeners(MenuKeyListener.class); | ||
| 755 | } | ||
| 756 | |||
| 757 | /** | ||
| 758 | * See JComponent.readObject() for information about serialization | ||
| 759 | * in Swing. | ||
| 760 | */ | ||
| 761 | private void readObject(ObjectInputStream s) | ||
| 762 | throws IOException, ClassNotFoundException | ||
| 763 | { | ||
| 764 | s.defaultReadObject(); | ||
| 765 | if (getUIClassID().equals(uiClassID)) { | ||
| 766 | updateUI(); | ||
| 767 | } | ||
| 768 | } | ||
| 769 | |||
| 770 | private void writeObject(ObjectOutputStream s) throws IOException { | ||
| 771 | s.defaultWriteObject(); | ||
| 772 | if (getUIClassID().equals(uiClassID)) { | ||
| 773 | byte count = JComponent.getWriteObjCounter(this); | ||
| 774 | JComponent.setWriteObjCounter(this, --count); | ||
| 775 | if (count == 0 && ui != null) { | ||
| 776 | ui.installUI(this); | ||
| 777 | } | ||
| 778 | } | ||
| 779 | } | ||
| 780 | |||
| 781 | |||
| 782 | /** | ||
| 783 | * Returns a string representation of this <code>JMenuItem</code>. | ||
| 784 | * This method is intended to be used only for debugging purposes, | ||
| 785 | * and the content and format of the returned string may vary between | ||
| 786 | * implementations. The returned string may be empty but may not | ||
| 787 | * be <code>null</code>. | ||
| 788 | * | ||
| 789 | * @return a string representation of this <code>JMenuItem</code> | ||
| 790 | */ | ||
| 791 | protected String paramString() { | ||
| 792 | return super.paramString(); | ||
| 793 | } | ||
| 794 | |||
| 795 | ///////////////// | ||
| 796 | // Accessibility support | ||
| 797 | //////////////// | ||
| 798 | |||
| 799 | /** | ||
| 800 | * Returns the <code>AccessibleContext</code> associated with this | ||
| 801 | * <code>JMenuItem</code>. For <code>JMenuItem</code>s, | ||
| 802 | * the <code>AccessibleContext</code> takes the form of an | ||
| 803 | * <code>AccessibleJMenuItem</code>. | ||
| 804 | * A new AccessibleJMenuItme instance is created if necessary. | ||
| 805 | * | ||
| 806 | * @return an <code>AccessibleJMenuItem</code> that serves as the | ||
| 807 | * <code>AccessibleContext</code> of this <code>JMenuItem</code> | ||
| 808 | */ | ||
| 809 | public AccessibleContext getAccessibleContext() { | ||
| 810 | if (accessibleContext == null) { | ||
| 811 | accessibleContext = new AccessibleJMenuItem(); | ||
| 812 | } | ||
| 813 | return accessibleContext; | ||
| 814 | } | ||
| 815 | |||
| 816 | |||
| 817 | /** | ||
| 818 | * This class implements accessibility support for the | ||
| 819 | * <code>JMenuItem</code> class. It provides an implementation of the | ||
| 820 | * Java Accessibility API appropriate to menu item user-interface | ||
| 821 | * elements. | ||
| 822 | * <p> | ||
| 823 | * <strong>Warning:</strong> | ||
| 824 | * Serialized objects of this class will not be compatible with | ||
| 825 | * future Swing releases. The current serialization support is | ||
| 826 | * appropriate for short term storage or RMI between applications running | ||
| 827 | * the same version of Swing. As of 1.4, support for long term storage | ||
| 828 | * of all JavaBeans<sup><font size="-2">TM</font></sup> | ||
| 829 | * has been added to the <code>java.beans</code> package. | ||
| 830 | * Please see {@link java.beans.XMLEncoder}. | ||
| 831 | */ | ||
| 832 | protected class AccessibleJMenuItem extends AccessibleAbstractButton implements ChangeListener { | ||
| 833 | |||
| 834 | private boolean isArmed = false; | ||
| 835 | private boolean hasFocus = false; | ||
| 836 | private boolean isPressed = false; | ||
| 837 | private boolean isSelected = false; | ||
| 838 | |||
| 839 | AccessibleJMenuItem() { | ||
| 840 | super(); | ||
| 841 | JMenuItem.this.addChangeListener(this); | ||
| 842 | } | ||
| 843 | |||
| 844 | /** | ||
| 845 | * Get the role of this object. | ||
| 846 | * | ||
| 847 | * @return an instance of AccessibleRole describing the role of the | ||
| 848 | * object | ||
| 849 | */ | ||
| 850 | public AccessibleRole getAccessibleRole() { | ||
| 851 | return AccessibleRole.MENU_ITEM; | ||
| 852 | } | ||
| 853 | |||
| 854 | private void fireAccessibilityFocusedEvent(JMenuItem toCheck) { | ||
| 855 | MenuElement [] path = | ||
| 856 | MenuSelectionManager.defaultManager().getSelectedPath(); | ||
| 857 | if (path.length > 0) { | ||
| 858 | Object menuItem = path[path.length - 1]; | ||
| 859 | if (toCheck == menuItem) { | ||
| 860 | firePropertyChange( | ||
| 861 | AccessibleContext.ACCESSIBLE_STATE_PROPERTY, | ||
| 862 | null, AccessibleState.FOCUSED); | ||
| 863 | } | ||
| 864 | } | ||
| 865 | } | ||
| 866 | |||
| 867 | /** | ||
| 868 | * Supports the change listener interface and fires property changes. | ||
| 869 | */ | ||
| 870 | public void stateChanged(ChangeEvent e) { | ||
| 871 | firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, | ||
| 872 | Boolean.valueOf(false), Boolean.valueOf(true)); | ||
| 873 | if (JMenuItem.this.getModel().isArmed()) { | ||
| 874 | if (!isArmed) { | ||
| 875 | isArmed = true; | ||
| 876 | firePropertyChange( | ||
| 877 | AccessibleContext.ACCESSIBLE_STATE_PROPERTY, | ||
| 878 | null, AccessibleState.ARMED); | ||
| 879 | // Fix for 4848220 moved here to avoid major memory leak | ||
| 880 | // Here we will fire the event in case of JMenuItem | ||
| 881 | // See bug 4910323 for details [zav] | ||
| 882 | fireAccessibilityFocusedEvent(JMenuItem.this); | ||
| 883 | } | ||
| 884 | } else { | ||
| 885 | if (isArmed) { | ||
| 886 | isArmed = false; | ||
| 887 | firePropertyChange( | ||
| 888 | AccessibleContext.ACCESSIBLE_STATE_PROPERTY, | ||
| 889 | AccessibleState.ARMED, null); | ||
| 890 | } | ||
| 891 | } | ||
| 892 | if (JMenuItem.this.isFocusOwner()) { | ||
| 893 | if (!hasFocus) { | ||
| 894 | hasFocus = true; | ||
| 895 | firePropertyChange( | ||
| 896 | AccessibleContext.ACCESSIBLE_STATE_PROPERTY, | ||
| 897 | null, AccessibleState.FOCUSED); | ||
| 898 | } | ||
| 899 | } else { | ||
| 900 | if (hasFocus) { | ||
| 901 | hasFocus = false; | ||
| 902 | firePropertyChange( | ||
| 903 | AccessibleContext.ACCESSIBLE_STATE_PROPERTY, | ||
| 904 | AccessibleState.FOCUSED, null); | ||
| 905 | } | ||
| 906 | } | ||
| 907 | if (JMenuItem.this.getModel().isPressed()) { | ||
| 908 | if (!isPressed) { | ||
| 909 | isPressed = true; | ||
| 910 | firePropertyChange( | ||
| 911 | AccessibleContext.ACCESSIBLE_STATE_PROPERTY, | ||
| 912 | null, AccessibleState.PRESSED); | ||
| 913 | } | ||
| 914 | } else { | ||
| 915 | if (isPressed) { | ||
| 916 | isPressed = false; | ||
| 917 | firePropertyChange( | ||
| 918 | AccessibleContext.ACCESSIBLE_STATE_PROPERTY, | ||
| 919 | AccessibleState.PRESSED, null); | ||
| 920 | } | ||
| 921 | } | ||
| 922 | if (JMenuItem.this.getModel().isSelected()) { | ||
| 923 | if (!isSelected) { | ||
| 924 | isSelected = true; | ||
| 925 | firePropertyChange( | ||
| 926 | AccessibleContext.ACCESSIBLE_STATE_PROPERTY, | ||
| 927 | null, AccessibleState.CHECKED); | ||
| 928 | |||
| 929 | // Fix for 4848220 moved here to avoid major memory leak | ||
| 930 | // Here we will fire the event in case of JMenu | ||
| 931 | // See bug 4910323 for details [zav] | ||
| 932 | fireAccessibilityFocusedEvent(JMenuItem.this); | ||
| 933 | } | ||
| 934 | } else { | ||
| 935 | if (isSelected) { | ||
| 936 | isSelected = false; | ||
| 937 | firePropertyChange( | ||
| 938 | AccessibleContext.ACCESSIBLE_STATE_PROPERTY, | ||
| 939 | AccessibleState.CHECKED, null); | ||
| 940 | } | ||
| 941 | } | ||
| 942 | |||
| 943 | } | ||
| 944 | } // inner class AccessibleJMenuItem | ||
| 945 | |||
| 946 | |||
| 947 | } | ||