Showing the changes to java/pkg/javax/swing/JMenu.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,1624 @@ | ||
| 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 | + | |
| 26 | +package javax.swing; | |
| 27 | + | |
| 28 | +import java.awt.AWTEvent; | |
| 29 | +import java.awt.Component; | |
| 30 | +import java.awt.ComponentOrientation; | |
| 31 | +import java.awt.Container; | |
| 32 | +import java.awt.Dimension; | |
| 33 | +import java.awt.Frame; | |
| 34 | +import java.awt.Graphics; | |
| 35 | +import java.awt.GraphicsConfiguration; | |
| 36 | +import java.awt.GraphicsDevice; | |
| 37 | +import java.awt.GraphicsEnvironment; | |
| 38 | +import java.awt.Insets; | |
| 39 | +import java.awt.Point; | |
| 40 | +import java.awt.Polygon; | |
| 41 | +import java.awt.Rectangle; | |
| 42 | +import java.awt.Toolkit; | |
| 43 | +import java.awt.event.*; | |
| 44 | +import java.beans.*; | |
| 45 | + | |
| 46 | +import java.util.*; | |
| 47 | + | |
| 48 | +import java.io.Serializable; | |
| 49 | +import java.io.ObjectOutputStream; | |
| 50 | +import java.io.ObjectInputStream; | |
| 51 | +import java.io.IOException; | |
| 52 | + | |
| 53 | +import javax.swing.event.*; | |
| 54 | +import javax.swing.plaf.*; | |
| 55 | +import javax.swing.plaf.basic.*; | |
| 56 | +import javax.accessibility.*; | |
| 57 | + | |
| 58 | +import java.lang.ref.WeakReference; | |
| 59 | + | |
| 60 | +/** | |
| 61 | + * An implementation of a menu -- a popup window containing | |
| 62 | + * <code>JMenuItem</code>s that | |
| 63 | + * is displayed when the user selects an item on the <code>JMenuBar</code>. | |
| 64 | + * In addition to <code>JMenuItem</code>s, a <code>JMenu</code> can | |
| 65 | + * also contain <code>JSeparator</code>s. | |
| 66 | + * <p> | |
| 67 | + * In essence, a menu is a button with an associated <code>JPopupMenu</code>. | |
| 68 | + * When the "button" is pressed, the <code>JPopupMenu</code> appears. If the | |
| 69 | + * "button" is on the <code>JMenuBar</code>, the menu is a top-level window. | |
| 70 | + * If the "button" is another menu item, then the <code>JPopupMenu</code> is | |
| 71 | + * "pull-right" menu. | |
| 72 | + * <p> | |
| 73 | + * Menus can be configured, and to some degree controlled, by | |
| 74 | + * <code><a href="Action.html">Action</a></code>s. Using an | |
| 75 | + * <code>Action</code> with a menu has many benefits beyond directly | |
| 76 | + * configuring a menu. Refer to <a href="Action.html#buttonActions"> | |
| 77 | + * Swing Components Supporting <code>Action</code></a> for more | |
| 78 | + * details, and you can find more information in <a | |
| 79 | + * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How | |
| 80 | + * to Use Actions</a>, a section in <em>The Java Tutorial</em>. | |
| 81 | + * <p> | |
| 82 | + * For information and examples of using menus see | |
| 83 | + * <a href="http://java.sun.com/doc/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>, | |
| 84 | + * a section in <em>The Java Tutorial.</em> | |
| 85 | + * <p> | |
| 86 | + * <strong>Warning:</strong> Swing is not thread safe. For more | |
| 87 | + * information see <a | |
| 88 | + * href="package-summary.html#threading">Swing's Threading | |
| 89 | + * Policy</a>. | |
| 90 | + * <p> | |
| 91 | + * <strong>Warning:</strong> | |
| 92 | + * Serialized objects of this class will not be compatible with | |
| 93 | + * future Swing releases. The current serialization support is | |
| 94 | + * appropriate for short term storage or RMI between applications running | |
| 95 | + * the same version of Swing. As of 1.4, support for long term storage | |
| 96 | + * of all JavaBeans<sup><font size="-2">TM</font></sup> | |
| 97 | + * has been added to the <code>java.beans</code> package. | |
| 98 | + * Please see {@link java.beans.XMLEncoder}. | |
| 99 | + * | |
| 100 | + * @beaninfo | |
| 101 | + * attribute: isContainer true | |
| 102 | + * description: A popup window containing menu items displayed in a menu bar. | |
| 103 | + * | |
| 104 | + * @author Georges Saab | |
| 105 | + * @author David Karlton | |
| 106 | + * @author Arnaud Weber | |
| 107 | + * @see JMenuItem | |
| 108 | + * @see JSeparator | |
| 109 | + * @see JMenuBar | |
| 110 | + * @see JPopupMenu | |
| 111 | + */ | |
| 112 | +public class JMenu extends JMenuItem implements Accessible,MenuElement | |
| 113 | +{ | |
| 114 | + /** | |
| 115 | + * @see #getUIClassID | |
| 116 | + * @see #readObject | |
| 117 | + */ | |
| 118 | + private static final String uiClassID = "MenuUI"; | |
| 119 | + | |
| 120 | + /* | |
| 121 | + * The popup menu portion of the menu. | |
| 122 | + */ | |
| 123 | + private JPopupMenu popupMenu; | |
| 124 | + | |
| 125 | + /* | |
| 126 | + * The button's model listeners. Default is <code>null</code>. | |
| 127 | + */ | |
| 128 | + private ChangeListener menuChangeListener = null; | |
| 129 | + | |
| 130 | + /* | |
| 131 | + * Only one <code>MenuEvent</code> is needed for each menu since the | |
| 132 | + * event's only state is the source property. The source of events | |
| 133 | + * generated is always "this". Default is <code>null</code>. | |
| 134 | + */ | |
| 135 | + private MenuEvent menuEvent = null; | |
| 136 | + | |
| 137 | + /* Registry of listeners created for <code>Action-JMenuItem</code> | |
| 138 | + * linkage. This is needed so that references can | |
| 139 | + * be cleaned up at remove time to allow garbage collection | |
| 140 | + * Default is <code>null</code>. | |
| 141 | + */ | |
| 142 | + private static Hashtable listenerRegistry = null; | |
| 143 | + | |
| 144 | + /* | |
| 145 | + * Used by the look and feel (L&F) code to handle | |
| 146 | + * implementation specific menu behaviors. | |
| 147 | + */ | |
| 148 | + private int delay; | |
| 149 | + | |
| 150 | + /* | |
| 151 | + * Location of the popup component. Location is <code>null</code> | |
| 152 | + * if it was not customized by <code>setMenuLocation</code> | |
| 153 | + */ | |
| 154 | + private Point customMenuLocation = null; | |
| 155 | + | |
| 156 | + /* Diagnostic aids -- should be false for production builds. */ | |
| 157 | + private static final boolean TRACE = false; // trace creates and disposes | |
| 158 | + private static final boolean VERBOSE = false; // show reuse hits/misses | |
| 159 | + private static final boolean DEBUG = false; // show bad params, misc. | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * Constructs a new <code>JMenu</code> with no text. | |
| 163 | + */ | |
| 164 | + public JMenu() { | |
| 165 | + this(""); | |
| 166 | + } | |
| 167 | + | |
| 168 | + /** | |
| 169 | + * Constructs a new <code>JMenu</code> with the supplied string | |
| 170 | + * as its text. | |
| 171 | + * | |
| 172 | + * @param s the text for the menu label | |
| 173 | + */ | |
| 174 | + public JMenu(String s) { | |
| 175 | + super(s); | |
| 176 | + } | |
| 177 | + | |
| 178 | + /** | |
| 179 | + * Constructs a menu whose properties are taken from the | |
| 180 | + * <code>Action</code> supplied. | |
| 181 | + * @param a an <code>Action</code> | |
| 182 | + * | |
| 183 | + * @since 1.3 | |
| 184 | + */ | |
| 185 | + public JMenu(Action a) { | |
| 186 | + this(); | |
| 187 | + setAction(a); | |
| 188 | + } | |
| 189 | + | |
| 190 | + /** | |
| 191 | + * Constructs a new <code>JMenu</code> with the supplied string as | |
| 192 | + * its text and specified as a tear-off menu or not. | |
| 193 | + * | |
| 194 | + * @param s the text for the menu label | |
| 195 | + * @param b can the menu be torn off (not yet implemented) | |
| 196 | + */ | |
| 197 | + public JMenu(String s, boolean b) { | |
| 198 | + this(s); | |
| 199 | + } | |
| 200 | + | |
| 201 | + | |
| 202 | + /** | |
| 203 | + * Overriden to do nothing. We want JMenu to be focusable, but | |
| 204 | + * <code>JMenuItem</code> doesn't want to be, thus we override this | |
| 205 | + * do nothing. We don't invoke <code>setFocusable(true)</code> after | |
| 206 | + * super's constructor has completed as this has the side effect that | |
| 207 | + * <code>JMenu</code> will be considered traversable via the | |
| 208 | + * keyboard, which we don't want. Making a Component traversable by | |
| 209 | + * the keyboard after invoking <code>setFocusable(true)</code> is OK, | |
| 210 | + * as <code>setFocusable</code> is new API | |
| 211 | + * and is speced as such, but internally we don't want to use it like | |
| 212 | + * this else we change the keyboard traversability. | |
| 213 | + */ | |
| 214 | + void initFocusability() { | |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * Resets the UI property with a value from the current look and feel. | |
| 219 | + * | |
| 220 | + * @see JComponent#updateUI | |
| 221 | + */ | |
| 222 | + public void updateUI() { | |
| 223 | + setUI((MenuItemUI)UIManager.getUI(this)); | |
| 224 | + | |
| 225 | + if ( popupMenu != null ) | |
| 226 | + { | |
| 227 | + popupMenu.setUI((PopupMenuUI)UIManager.getUI(popupMenu)); | |
| 228 | + } | |
| 229 | + | |
| 230 | + } | |
| 231 | + | |
| 232 | + | |
| 233 | + /** | |
| 234 | + * Returns the name of the L&F class that renders this component. | |
| 235 | + * | |
| 236 | + * @return the string "MenuUI" | |
| 237 | + * @see JComponent#getUIClassID | |
| 238 | + * @see UIDefaults#getUI | |
| 239 | + */ | |
| 240 | + public String getUIClassID() { | |
| 241 | + return uiClassID; | |
| 242 | + } | |
| 243 | + | |
| 244 | + // public void repaint(long tm, int x, int y, int width, int height) { | |
| 245 | + // Thread.currentThread().dumpStack(); | |
| 246 | + // super.repaint(tm,x,y,width,height); | |
| 247 | + // } | |
| 248 | + | |
| 249 | + /** | |
| 250 | + * Sets the data model for the "menu button" -- the label | |
| 251 | + * that the user clicks to open or close the menu. | |
| 252 | + * | |
| 253 | + * @param newModel the <code>ButtonModel</code> | |
| 254 | + * @see #getModel | |
| 255 | + * @beaninfo | |
| 256 | + * description: The menu's model | |
| 257 | + * bound: true | |
| 258 | + * expert: true | |
| 259 | + * hidden: true | |
| 260 | + */ | |
| 261 | + public void setModel(ButtonModel newModel) { | |
| 262 | + ButtonModel oldModel = getModel(); | |
| 263 | + | |
| 264 | + super.setModel(newModel); | |
| 265 | + | |
| 266 | + if (oldModel != null && menuChangeListener != null) { | |
| 267 | + oldModel.removeChangeListener(menuChangeListener); | |
| 268 | + menuChangeListener = null; | |
| 269 | + } | |
| 270 | + | |
| 271 | + model = newModel; | |
| 272 | + | |
| 273 | + if (newModel != null) { | |
| 274 | + menuChangeListener = createMenuChangeListener(); | |
| 275 | + newModel.addChangeListener(menuChangeListener); | |
| 276 | + } | |
| 277 | + } | |
| 278 | + | |
| 279 | + /** | |
| 280 | + * Returns true if the menu is currently selected (highlighted). | |
| 281 | + * | |
| 282 | + * @return true if the menu is selected, else false | |
| 283 | + */ | |
| 284 | + public boolean isSelected() { | |
| 285 | + return getModel().isSelected(); | |
| 286 | + } | |
| 287 | + | |
| 288 | + /** | |
| 289 | + * Sets the selection status of the menu. | |
| 290 | + * | |
| 291 | + * @param b true to select (highlight) the menu; false to de-select | |
| 292 | + * the menu | |
| 293 | + * @beaninfo | |
| 294 | + * description: When the menu is selected, its popup child is shown. | |
| 295 | + * expert: true | |
| 296 | + * hidden: true | |
| 297 | + */ | |
| 298 | + public void setSelected(boolean b) { | |
| 299 | + ButtonModel model = getModel(); | |
| 300 | + boolean oldValue = model.isSelected(); | |
| 301 | + | |
| 302 | + // TIGER - 4840653 | |
| 303 | + // Removed code which fired an AccessibleState.SELECTED | |
| 304 | + // PropertyChangeEvent since this resulted in two | |
| 305 | + // identical events being fired since | |
| 306 | + // AbstractButton.fireItemStateChanged also fires the | |
| 307 | + // same event. This caused screen readers to speak the | |
| 308 | + // name of the item twice. | |
| 309 | + | |
| 310 | + if (b != model.isSelected()) { | |
| 311 | + getModel().setSelected(b); | |
| 312 | + } | |
| 313 | + } | |
| 314 | + | |
| 315 | + /** | |
| 316 | + * Returns true if the menu's popup window is visible. | |
| 317 | + * | |
| 318 | + * @return true if the menu is visible, else false | |
| 319 | + */ | |
| 320 | + public boolean isPopupMenuVisible() { | |
| 321 | + ensurePopupMenuCreated(); | |
| 322 | + return popupMenu.isVisible(); | |
| 323 | + } | |
| 324 | + | |
| 325 | + /** | |
| 326 | + * Sets the visibility of the menu's popup. If the menu is | |
| 327 | + * not enabled, this method will have no effect. | |
| 328 | + * | |
| 329 | + * @param b a boolean value -- true to make the menu visible, | |
| 330 | + * false to hide it | |
| 331 | + * @beaninfo | |
| 332 | + * description: The popup menu's visibility | |
| 333 | + * expert: true | |
| 334 | + * hidden: true | |
| 335 | + */ | |
| 336 | + public void setPopupMenuVisible(boolean b) { | |
| 337 | + if (DEBUG) { | |
| 338 | + System.out.println("in JMenu.setPopupMenuVisible " + b); | |
| 339 | + // Thread.dumpStack(); | |
| 340 | + } | |
| 341 | + | |
| 342 | + boolean isVisible = isPopupMenuVisible(); | |
| 343 | + if (b != isVisible && (isEnabled() || !b)) { | |
| 344 | + ensurePopupMenuCreated(); | |
| 345 | + if ((b==true) && isShowing()) { | |
| 346 | + // Set location of popupMenu (pulldown or pullright) | |
| 347 | + Point p = getCustomMenuLocation(); | |
| 348 | + if (p == null) { | |
| 349 | + p = getPopupMenuOrigin(); | |
| 350 | + } | |
| 351 | + getPopupMenu().show(this, p.x, p.y); | |
| 352 | + } else { | |
| 353 | + getPopupMenu().setVisible(false); | |
| 354 | + } | |
| 355 | + } | |
| 356 | + } | |
| 357 | + | |
| 358 | + /** | |
| 359 | + * Computes the origin for the <code>JMenu</code>'s popup menu. | |
| 360 | + * This method uses Look and Feel properties named | |
| 361 | + * <code>Menu.menuPopupOffsetX</code>, | |
| 362 | + * <code>Menu.menuPopupOffsetY</code>, | |
| 363 | + * <code>Menu.submenuPopupOffsetX</code>, and | |
| 364 | + * <code>Menu.submenuPopupOffsetY</code> | |
| 365 | + * to adjust the exact location of popup. | |
| 366 | + * | |
| 367 | + * @return a <code>Point</code> in the coordinate space of the | |
| 368 | + * menu which should be used as the origin | |
| 369 | + * of the <code>JMenu</code>'s popup menu | |
| 370 | + * | |
| 371 | + * @since 1.3 | |
| 372 | + */ | |
| 373 | + protected Point getPopupMenuOrigin() { | |
| 374 | + int x; | |
| 375 | + int y; | |
| 376 | + JPopupMenu pm = getPopupMenu(); | |
| 377 | + // Figure out the sizes needed to caclulate the menu position | |
| 378 | + Dimension s = getSize(); | |
| 379 | + Dimension pmSize = pm.getSize(); | |
| 380 | + // For the first time the menu is popped up, | |
| 381 | + // the size has not yet been initiated | |
| 382 | + if (pmSize.width==0) { | |
| 383 | + pmSize = pm.getPreferredSize(); | |
| 384 | + } | |
| 385 | + Point position = getLocationOnScreen(); | |
| 386 | + Toolkit toolkit = Toolkit.getDefaultToolkit(); | |
| 387 | + GraphicsConfiguration gc = getGraphicsConfiguration(); | |
| 388 | + Rectangle screenBounds = new Rectangle(toolkit.getScreenSize()); | |
| 389 | + GraphicsEnvironment ge = | |
| 390 | + GraphicsEnvironment.getLocalGraphicsEnvironment(); | |
| 391 | + GraphicsDevice[] gd = ge.getScreenDevices(); | |
| 392 | + for(int i = 0; i < gd.length; i++) { | |
| 393 | + if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) { | |
| 394 | + GraphicsConfiguration dgc = | |
| 395 | + gd[i].getDefaultConfiguration(); | |
| 396 | + if(dgc.getBounds().contains(position)) { | |
| 397 | + gc = dgc; | |
| 398 | + break; | |
| 399 | + } | |
| 400 | + } | |
| 401 | + } | |
| 402 | + | |
| 403 | + | |
| 404 | + if (gc != null) { | |
| 405 | + screenBounds = gc.getBounds(); | |
| 406 | + // take screen insets (e.g. taskbar) into account | |
| 407 | + Insets screenInsets = toolkit.getScreenInsets(gc); | |
| 408 | + | |
| 409 | + screenBounds.width -= | |
| 410 | + Math.abs(screenInsets.left + screenInsets.right); | |
| 411 | + screenBounds.height -= | |
| 412 | + Math.abs(screenInsets.top + screenInsets.bottom); | |
| 413 | + position.x -= Math.abs(screenInsets.left); | |
| 414 | + position.y -= Math.abs(screenInsets.top); | |
| 415 | + } | |
| 416 | + | |
| 417 | + Container parent = getParent(); | |
| 418 | + if (parent instanceof JPopupMenu) { | |
| 419 | + // We are a submenu (pull-right) | |
| 420 | + int xOffset = UIManager.getInt("Menu.submenuPopupOffsetX"); | |
| 421 | + int yOffset = UIManager.getInt("Menu.submenuPopupOffsetY"); | |
| 422 | + | |
| 423 | + if( SwingUtilities.isLeftToRight(this) ) { | |
| 424 | + // First determine x: | |
| 425 | + x = s.width + xOffset; // Prefer placement to the right | |
| 426 | + if (position.x + x + pmSize.width >= screenBounds.width | |
| 427 | + + screenBounds.x && | |
| 428 | + // popup doesn't fit - place it wherever there's more room | |
| 429 | + screenBounds.width - s.width < 2*(position.x | |
| 430 | + - screenBounds.x)) { | |
| 431 | + | |
| 432 | + x = 0 - xOffset - pmSize.width; | |
| 433 | + } | |
| 434 | + } else { | |
| 435 | + // First determine x: | |
| 436 | + x = 0 - xOffset - pmSize.width; // Prefer placement to the left | |
| 437 | + if (position.x + x < screenBounds.x && | |
| 438 | + // popup doesn't fit - place it wherever there's more room | |
| 439 | + screenBounds.width - s.width > 2*(position.x - | |
| 440 | + screenBounds.x)) { | |
| 441 | + | |
| 442 | + x = s.width + xOffset; | |
| 443 | + } | |
| 444 | + } | |
| 445 | + // Then the y: | |
| 446 | + y = yOffset; // Prefer dropping down | |
| 447 | + if (position.y + y + pmSize.height >= screenBounds.height | |
| 448 | + + screenBounds.y && | |
| 449 | + // popup doesn't fit - place it wherever there's more room | |
| 450 | + screenBounds.height - s.height < 2*(position.y | |
| 451 | + - screenBounds.y)) { | |
| 452 | + | |
| 453 | + y = s.height - yOffset - pmSize.height; | |
| 454 | + } | |
| 455 | + } else { | |
| 456 | + // We are a toplevel menu (pull-down) | |
| 457 | + int xOffset = UIManager.getInt("Menu.menuPopupOffsetX"); | |
| 458 | + int yOffset = UIManager.getInt("Menu.menuPopupOffsetY"); | |
| 459 | + | |
| 460 | + if( SwingUtilities.isLeftToRight(this) ) { | |
| 461 | + // First determine the x: | |
| 462 | + x = xOffset; // Extend to the right | |
| 463 | + if (position.x + x + pmSize.width >= screenBounds.width | |
| 464 | + + screenBounds.x && | |
| 465 | + // popup doesn't fit - place it wherever there's more room | |
| 466 | + screenBounds.width - s.width < 2*(position.x | |
| 467 | + - screenBounds.x)) { | |
| 468 | + | |
| 469 | + x = s.width - xOffset - pmSize.width; | |
| 470 | + } | |
| 471 | + } else { | |
| 472 | + // First determine the x: | |
| 473 | + x = s.width - xOffset - pmSize.width; // Extend to the left | |
| 474 | + if (position.x + x < screenBounds.x && | |
| 475 | + // popup doesn't fit - place it wherever there's more room | |
| 476 | + screenBounds.width - s.width > 2*(position.x | |
| 477 | + - screenBounds.x)) { | |
| 478 | + | |
| 479 | + x = xOffset; | |
| 480 | + } | |
| 481 | + } | |
| 482 | + // Then the y: | |
| 483 | + y = s.height + yOffset; // Prefer dropping down | |
| 484 | + if (position.y + y + pmSize.height >= screenBounds.height && | |
| 485 | + // popup doesn't fit - place it wherever there's more room | |
| 486 | + screenBounds.height - s.height < 2*(position.y | |
| 487 | + - screenBounds.y)) { | |
| 488 | + | |
| 489 | + y = 0 - yOffset - pmSize.height; // Otherwise drop 'up' | |
| 490 | + } | |
| 491 | + } | |
| 492 | + return new Point(x,y); | |
| 493 | + } | |
| 494 | + | |
| 495 | + | |
| 496 | + /** | |
| 497 | + * Returns the suggested delay, in milliseconds, before submenus | |
| 498 | + * are popped up or down. | |
| 499 | + * Each look and feel (L&F) may determine its own policy for | |
| 500 | + * observing the <code>delay</code> property. | |
| 501 | + * In most cases, the delay is not observed for top level menus | |
| 502 | + * or while dragging. The default for <code>delay</code> is 0. | |
| 503 | + * This method is a property of the look and feel code and is used | |
| 504 | + * to manage the idiosyncracies of the various UI implementations. | |
| 505 | + * | |
| 506 | + * | |
| 507 | + * @return the <code>delay</code> property | |
| 508 | + */ | |
| 509 | + public int getDelay() { | |
| 510 | + return delay; | |
| 511 | + } | |
| 512 | + | |
| 513 | + /** | |
| 514 | + * Sets the suggested delay before the menu's <code>PopupMenu</code> | |
| 515 | + * is popped up or down. Each look and feel (L&F) may determine | |
| 516 | + * it's own policy for observing the delay property. In most cases, | |
| 517 | + * the delay is not observed for top level menus or while dragging. | |
| 518 | + * This method is a property of the look and feel code and is used | |
| 519 | + * to manage the idiosyncracies of the various UI implementations. | |
| 520 | + * | |
| 521 | + * @param d the number of milliseconds to delay | |
| 522 | + * @exception IllegalArgumentException if <code>d</code> | |
| 523 | + * is less than 0 | |
| 524 | + * @beaninfo | |
| 525 | + * description: The delay between menu selection and making the popup menu visible | |
| 526 | + * expert: true | |
| 527 | + */ | |
| 528 | + public void setDelay(int d) { | |
| 529 | + if (d < 0) | |
| 530 | + throw new IllegalArgumentException("Delay must be a positive integer"); | |
| 531 | + | |
| 532 | + delay = d; | |
| 533 | + } | |
| 534 | + | |
| 535 | + /** | |
| 536 | + * The window-closing listener for the popup. | |
| 537 | + * | |
| 538 | + * @see WinListener | |
| 539 | + */ | |
| 540 | + protected WinListener popupListener; | |
| 541 | + | |
| 542 | + private void ensurePopupMenuCreated() { | |
| 543 | + if (popupMenu == null) { | |
| 544 | + final JMenu thisMenu = this; | |
| 545 | + this.popupMenu = new JPopupMenu(); | |
| 546 | + popupMenu.setInvoker(this); | |
| 547 | + popupListener = createWinListener(popupMenu); | |
| 548 | + } | |
| 549 | + } | |
| 550 | + | |
| 551 | + /* | |
| 552 | + * Return the customized location of the popup component. | |
| 553 | + */ | |
| 554 | + private Point getCustomMenuLocation() { | |
| 555 | + return customMenuLocation; | |
| 556 | + } | |
| 557 | + | |
| 558 | + /** | |
| 559 | + * Sets the location of the popup component. | |
| 560 | + * | |
| 561 | + * @param x the x coordinate of the popup's new position | |
| 562 | + * @param y the y coordinate of the popup's new position | |
| 563 | + */ | |
| 564 | + public void setMenuLocation(int x, int y) { | |
| 565 | + customMenuLocation = new Point(x, y); | |
| 566 | + if (popupMenu != null) | |
| 567 | + popupMenu.setLocation(x, y); | |
| 568 | + } | |
| 569 | + | |
| 570 | + /** | |
| 571 | + * Appends a menu item to the end of this menu. | |
| 572 | + * Returns the menu item added. | |
| 573 | + * | |
| 574 | + * @param menuItem the <code>JMenuitem</code> to be added | |
| 575 | + * @return the <code>JMenuItem</code> added | |
| 576 | + */ | |
| 577 | + public JMenuItem add(JMenuItem menuItem) { | |
| 578 | + ensurePopupMenuCreated(); | |
| 579 | + return popupMenu.add(menuItem); | |
| 580 | + } | |
| 581 | + | |
| 582 | + /** | |
| 583 | + * Appends a component to the end of this menu. | |
| 584 | + * Returns the component added. | |
| 585 | + * | |
| 586 | + * @param c the <code>Component</code> to add | |
| 587 | + * @return the <code>Component</code> added | |
| 588 | + */ | |
| 589 | + public Component add(Component c) { | |
| 590 | + ensurePopupMenuCreated(); | |
| 591 | + popupMenu.add(c); | |
| 592 | + return c; | |
| 593 | + } | |
| 594 | + | |
| 595 | + /** | |
| 596 | + * Adds the specified component to this container at the given | |
| 597 | + * position. If <code>index</code> equals -1, the component will | |
| 598 | + * be appended to the end. | |
| 599 | + * @param c the <code>Component</code> to add | |
| 600 | + * @param index the position at which to insert the component | |
| 601 | + * @return the <code>Component</code> added | |
| 602 | + * @see #remove | |
| 603 | + * @see java.awt.Container#add(Component, int) | |
| 604 | + */ | |
| 605 | + public Component add(Component c, int index) { | |
| 606 | + ensurePopupMenuCreated(); | |
| 607 | + popupMenu.add(c, index); | |
| 608 | + return c; | |
| 609 | + } | |
| 610 | + | |
| 611 | + /** | |
| 612 | + * Creates a new menu item with the specified text and appends | |
| 613 | + * it to the end of this menu. | |
| 614 | + * | |
| 615 | + * @param s the string for the menu item to be added | |
| 616 | + */ | |
| 617 | + public JMenuItem add(String s) { | |
| 618 | + return add(new JMenuItem(s)); | |
| 619 | + } | |
| 620 | + | |
| 621 | + /** | |
| 622 | + * Creates a new menu item attached to the specified | |
| 623 | + * <code>Action</code> object and appends it to the end of this menu. | |
| 624 | + * | |
| 625 | + * @param a the <code>Action</code> for the menu item to be added | |
| 626 | + * @see Action | |
| 627 | + */ | |
| 628 | + public JMenuItem add(Action a) { | |
| 629 | + JMenuItem mi = createActionComponent(a); | |
| 630 | + mi.setAction(a); | |
| 631 | + add(mi); | |
| 632 | + return mi; | |
| 633 | + } | |
| 634 | + | |
| 635 | + /** | |
| 636 | + * Factory method which creates the <code>JMenuItem</code> for | |
| 637 | + * <code>Action</code>s added to the <code>JMenu</code>. | |
| 638 | + * | |
| 639 | + * @param a the <code>Action</code> for the menu item to be added | |
| 640 | + * @return the new menu item | |
| 641 | + * @see Action | |
| 642 | + * | |
| 643 | + * @since 1.3 | |
| 644 | + */ | |
| 645 | + protected JMenuItem createActionComponent(Action a) { | |
| 646 | + JMenuItem mi = new JMenuItem() { | |
| 647 | + protected PropertyChangeListener createActionPropertyChangeListener(Action a) { | |
| 648 | + PropertyChangeListener pcl = createActionChangeListener(this); | |
| 649 | + if (pcl == null) { | |
| 650 | + pcl = super.createActionPropertyChangeListener(a); | |
| 651 | + } | |
| 652 | + return pcl; | |
| 653 | + } | |
| 654 | + }; | |
| 655 | + mi.setHorizontalTextPosition(JButton.TRAILING); | |
| 656 | + mi.setVerticalTextPosition(JButton.CENTER); | |
| 657 | + return mi; | |
| 658 | + } | |
| 659 | + | |
| 660 | + /** | |
| 661 | + * Returns a properly configured <code>PropertyChangeListener</code> | |
| 662 | + * which updates the control as changes to the <code>Action</code> occur. | |
| 663 | + */ | |
| 664 | + protected PropertyChangeListener createActionChangeListener(JMenuItem b) { | |
| 665 | + return b.createActionPropertyChangeListener0(b.getAction()); | |
| 666 | + } | |
| 667 | + | |
| 668 | + /** | |
| 669 | + * Appends a new separator to the end of the menu. | |
| 670 | + */ | |
| 671 | + public void addSeparator() | |
| 672 | + { | |
| 673 | + ensurePopupMenuCreated(); | |
| 674 | + popupMenu.addSeparator(); | |
| 675 | + } | |
| 676 | + | |
| 677 | + /** | |
| 678 | + * Inserts a new menu item with the specified text at a | |
| 679 | + * given position. | |
| 680 | + * | |
| 681 | + * @param s the text for the menu item to add | |
| 682 | + * @param pos an integer specifying the position at which to add the | |
| 683 | + * new menu item | |
| 684 | + * @exception IllegalArgumentException when the value of | |
| 685 | + * <code>pos</code> < 0 | |
| 686 | + */ | |
| 687 | + public void insert(String s, int pos) { | |
| 688 | + if (pos < 0) { | |
| 689 | + throw new IllegalArgumentException("index less than zero."); | |
| 690 | + } | |
| 691 | + | |
| 692 | + ensurePopupMenuCreated(); | |
| 693 | + popupMenu.insert(new JMenuItem(s), pos); | |
| 694 | + } | |
| 695 | + | |
| 696 | + /** | |
| 697 | + * Inserts the specified <code>JMenuitem</code> at a given position. | |
| 698 | + * | |
| 699 | + * @param mi the <code>JMenuitem</code> to add | |
| 700 | + * @param pos an integer specifying the position at which to add the | |
| 701 | + * new <code>JMenuitem</code> | |
| 702 | + * @return the new menu item | |
| 703 | + * @exception IllegalArgumentException if the value of | |
| 704 | + * <code>pos</code> < 0 | |
| 705 | + */ | |
| 706 | + public JMenuItem insert(JMenuItem mi, int pos) { | |
| 707 | + if (pos < 0) { | |
| 708 | + throw new IllegalArgumentException("index less than zero."); | |
| 709 | + } | |
| 710 | + ensurePopupMenuCreated(); | |
| 711 | + popupMenu.insert(mi, pos); | |
| 712 | + return mi; | |
| 713 | + } | |
| 714 | + | |
| 715 | + /** | |
| 716 | + * Inserts a new menu item attached to the specified <code>Action</code> | |
| 717 | + * object at a given position. | |
| 718 | + * | |
| 719 | + * @param a the <code>Action</code> object for the menu item to add | |
| 720 | + * @param pos an integer specifying the position at which to add the | |
| 721 | + * new menu item | |
| 722 | + * @exception IllegalArgumentException if the value of | |
| 723 | + * <code>pos</code> < 0 | |
| 724 | + */ | |
| 725 | + public JMenuItem insert(Action a, int pos) { | |
| 726 | + if (pos < 0) { | |
| 727 | + throw new IllegalArgumentException("index less than zero."); | |
| 728 | + } | |
| 729 | + | |
| 730 | + ensurePopupMenuCreated(); | |
| 731 | + JMenuItem mi = new JMenuItem(a); | |
| 732 | + mi.setHorizontalTextPosition(JButton.TRAILING); | |
| 733 | + mi.setVerticalTextPosition(JButton.CENTER); | |
| 734 | + popupMenu.insert(mi, pos); | |
| 735 | + return mi; | |
| 736 | + } | |
| 737 | + | |
| 738 | + /** | |
| 739 | + * Inserts a separator at the specified position. | |
| 740 | + * | |
| 741 | + * @param index an integer specifying the position at which to | |
| 742 | + * insert the menu separator | |
| 743 | + * @exception IllegalArgumentException if the value of | |
| 744 | + * <code>index</code> < 0 | |
| 745 | + */ | |
| 746 | + public void insertSeparator(int index) { | |
| 747 | + if (index < 0) { | |
| 748 | + throw new IllegalArgumentException("index less than zero."); | |
| 749 | + } | |
| 750 | + | |
| 751 | + ensurePopupMenuCreated(); | |
| 752 | + popupMenu.insert( new JPopupMenu.Separator(), index ); | |
| 753 | + } | |
| 754 | + | |
| 755 | + /** | |
| 756 | + * Returns the <code>JMenuItem</code> at the specified position. | |
| 757 | + * If the component at <code>pos</code> is not a menu item, | |
| 758 | + * <code>null</code> is returned. | |
| 759 | + * This method is included for AWT compatibility. | |
| 760 | + * | |
| 761 | + * @param pos an integer specifying the position | |
| 762 | + * @exception IllegalArgumentException if the value of | |
| 763 | + * <code>pos</code> < 0 | |
| 764 | + * @return the menu item at the specified position; or <code>null</code> | |
| 765 | + * if the item as the specified position is not a menu item | |
| 766 | + */ | |
| 767 | + public JMenuItem getItem(int pos) { | |
| 768 | + if (pos < 0) { | |
| 769 | + throw new IllegalArgumentException("index less than zero."); | |
| 770 | + } | |
| 771 | + | |
| 772 | + Component c = getMenuComponent(pos); | |
| 773 | + if (c instanceof JMenuItem) { | |
| 774 | + JMenuItem mi = (JMenuItem) c; | |
| 775 | + return mi; | |
| 776 | + } | |
| 777 | + | |
| 778 | + // 4173633 | |
| 779 | + return null; | |
| 780 | + } | |
| 781 | + | |
| 782 | + /** | |
| 783 | + * Returns the number of items on the menu, including separators. | |
| 784 | + * This method is included for AWT compatibility. | |
| 785 | + * | |
| 786 | + * @return an integer equal to the number of items on the menu | |
| 787 | + * @see #getMenuComponentCount | |
| 788 | + */ | |
| 789 | + public int getItemCount() { | |
| 790 | + return getMenuComponentCount(); | |
| 791 | + } | |
| 792 | + | |
| 793 | + /** | |
| 794 | + * Returns true if the menu can be torn off. This method is not | |
| 795 | + * yet implemented. | |
| 796 | + * | |
| 797 | + * @return true if the menu can be torn off, else false | |
| 798 | + * @exception Error if invoked -- this method is not yet implemented | |
| 799 | + */ | |
| 800 | + public boolean isTearOff() { | |
| 801 | + throw new Error("boolean isTearOff() {} not yet implemented"); | |
| 802 | + } | |
| 803 | + | |
| 804 | + /** | |
| 805 | + * Removes the specified menu item from this menu. If there is no | |
| 806 | + * popup menu, this method will have no effect. | |
| 807 | + * | |
| 808 | + * @param item the <code>JMenuItem</code> to be removed from the menu | |
| 809 | + */ | |
| 810 | + public void remove(JMenuItem item) { | |
| 811 | + if (popupMenu != null) | |
| 812 | + popupMenu.remove(item); | |
| 813 | + } | |
| 814 | + | |
| 815 | + /** | |
| 816 | + * Removes the menu item at the specified index from this menu. | |
| 817 | + * | |
| 818 | + * @param pos the position of the item to be removed | |
| 819 | + * @exception IllegalArgumentException if the value of | |
| 820 | + * <code>pos</code> < 0, or if <code>pos</code> | |
| 821 | + * is greater than the number of menu items | |
| 822 | + */ | |
| 823 | + public void remove(int pos) { | |
| 824 | + if (pos < 0) { | |
| 825 | + throw new IllegalArgumentException("index less than zero."); | |
| 826 | + } | |
| 827 | + if (pos > getItemCount()) { | |
| 828 | + throw new IllegalArgumentException("index greater than the number of items."); | |
| 829 | + } | |
| 830 | + if (popupMenu != null) | |
| 831 | + popupMenu.remove(pos); | |
| 832 | + } | |
| 833 | + | |
| 834 | + /** | |
| 835 | + * Removes the component <code>c</code> from this menu. | |
| 836 | + * | |
| 837 | + * @param c the component to be removed | |
| 838 | + */ | |
| 839 | + public void remove(Component c) { | |
| 840 | + if (popupMenu != null) | |
| 841 | + popupMenu.remove(c); | |
| 842 | + } | |
| 843 | + | |
| 844 | + /** | |
| 845 | + * Removes all menu items from this menu. | |
| 846 | + */ | |
| 847 | + public void removeAll() { | |
| 848 | + if (popupMenu != null) | |
| 849 | + popupMenu.removeAll(); | |
| 850 | + } | |
| 851 | + | |
| 852 | + /** | |
| 853 | + * Returns the number of components on the menu. | |
| 854 | + * | |
| 855 | + * @return an integer containing the number of components on the menu | |
| 856 | + */ | |
| 857 | + public int getMenuComponentCount() { | |
| 858 | + int componentCount = 0; | |
| 859 | + if (popupMenu != null) | |
| 860 | + componentCount = popupMenu.getComponentCount(); | |
| 861 | + return componentCount; | |
| 862 | + } | |
| 863 | + | |
| 864 | + /** | |
| 865 | + * Returns the component at position <code>n</code>. | |
| 866 | + * | |
| 867 | + * @param n the position of the component to be returned | |
| 868 | + * @return the component requested, or <code>null</code> | |
| 869 | + * if there is no popup menu | |
| 870 | + * | |
| 871 | + */ | |
| 872 | + public Component getMenuComponent(int n) { | |
| 873 | + if (popupMenu != null) | |
| 874 | + return popupMenu.getComponent(n); | |
| 875 | + | |
| 876 | + return null; | |
| 877 | + } | |
| 878 | + | |
| 879 | + /** | |
| 880 | + * Returns an array of <code>Component</code>s of the menu's | |
| 881 | + * subcomponents. Note that this returns all <code>Component</code>s | |
| 882 | + * in the popup menu, including separators. | |
| 883 | + * | |
| 884 | + * @return an array of <code>Component</code>s or an empty array | |
| 885 | + * if there is no popup menu | |
| 886 | + */ | |
| 887 | + public Component[] getMenuComponents() { | |
| 888 | + if (popupMenu != null) | |
| 889 | + return popupMenu.getComponents(); | |
| 890 | + | |
| 891 | + return new Component[0]; | |
| 892 | + } | |
| 893 | + | |
| 894 | + /** | |
| 895 | + * Returns true if the menu is a 'top-level menu', that is, if it is | |
| 896 | + * the direct child of a menubar. | |
| 897 | + * | |
| 898 | + * @return true if the menu is activated from the menu bar; | |
| 899 | + * false if the menu is activated from a menu item | |
| 900 | + * on another menu | |
| 901 | + */ | |
| 902 | + public boolean isTopLevelMenu() { | |
| 903 | + return getParent() instanceof JMenuBar; | |
| 904 | + | |
| 905 | + } | |
| 906 | + | |
| 907 | + /** | |
| 908 | + * Returns true if the specified component exists in the | |
| 909 | + * submenu hierarchy. | |
| 910 | + * | |
| 911 | + * @param c the <code>Component</code> to be tested | |
| 912 | + * @return true if the <code>Component</code> exists, false otherwise | |
| 913 | + */ | |
| 914 | + public boolean isMenuComponent(Component c) { | |
| 915 | + // Are we in the MenuItem part of the menu | |
| 916 | + if (c == this) | |
| 917 | + return true; | |
| 918 | + // Are we in the PopupMenu? | |
| 919 | + if (c instanceof JPopupMenu) { | |
| 920 | + JPopupMenu comp = (JPopupMenu) c; | |
| 921 | + if (comp == this.getPopupMenu()) | |
| 922 | + return true; | |
| 923 | + } | |
| 924 | + // Are we in a Component on the PopupMenu | |
| 925 | + int ncomponents = this.getMenuComponentCount(); | |
| 926 | + Component[] component = this.getMenuComponents(); | |
| 927 | + for (int i = 0 ; i < ncomponents ; i++) { | |
| 928 | + Component comp = component[i]; | |
| 929 | + // Are we in the current component? | |
| 930 | + if (comp == c) | |
| 931 | + return true; | |
| 932 | + // Hmmm, what about Non-menu containers? | |
| 933 | + | |
| 934 | + // Recursive call for the Menu case | |
| 935 | + if (comp instanceof JMenu) { | |
| 936 | + JMenu subMenu = (JMenu) comp; | |
| 937 | + if (subMenu.isMenuComponent(c)) | |
| 938 | + return true; | |
| 939 | + } | |
| 940 | + } | |
| 941 | + return false; | |
| 942 | + } | |
| 943 | + | |
| 944 | + | |
| 945 | + /* | |
| 946 | + * Returns a point in the coordinate space of this menu's popupmenu | |
| 947 | + * which corresponds to the point <code>p</code> in the menu's | |
| 948 | + * coordinate space. | |
| 949 | + * | |
| 950 | + * @param p the point to be translated | |
| 951 | + * @return the point in the coordinate space of this menu's popupmenu | |
| 952 | + */ | |
| 953 | + private Point translateToPopupMenu(Point p) { | |
| 954 | + return translateToPopupMenu(p.x, p.y); | |
| 955 | + } | |
| 956 | + | |
| 957 | + /* | |
| 958 | + * Returns a point in the coordinate space of this menu's popupmenu | |
| 959 | + * which corresponds to the point (x,y) in the menu's coordinate space. | |
| 960 | + * | |
| 961 | + * @param x the x coordinate of the point to be translated | |
| 962 | + * @param y the y coordinate of the point to be translated | |
| 963 | + * @return the point in the coordinate space of this menu's popupmenu | |
| 964 | + */ | |
| 965 | + private Point translateToPopupMenu(int x, int y) { | |
| 966 | + int newX; | |
| 967 | + int newY; | |
| 968 | + | |
| 969 | + if (getParent() instanceof JPopupMenu) { | |
| 970 | + newX = x - getSize().width; | |
| 971 | + newY = y; | |
| 972 | + } else { | |
| 973 | + newX = x; | |
| 974 | + newY = y - getSize().height; | |
| 975 | + } | |
| 976 | + | |
| 977 | + return new Point(newX, newY); | |
| 978 | + } | |
| 979 | + | |
| 980 | + /** | |
| 981 | + * Returns the popupmenu associated with this menu. If there is | |
| 982 | + * no popupmenu, it will create one. | |
| 983 | + */ | |
| 984 | + public JPopupMenu getPopupMenu() { | |
| 985 | + ensurePopupMenuCreated(); | |
| 986 | + return popupMenu; | |
| 987 | + } | |
| 988 | + | |
| 989 | + /** | |
| 990 | + * Adds a listener for menu events. | |
| 991 | + * | |
| 992 | + * @param l the listener to be added | |
| 993 | + */ | |
| 994 | + public void addMenuListener(MenuListener l) { | |
| 995 | + listenerList.add(MenuListener.class, l); | |
| 996 | + } | |
| 997 | + | |
| 998 | + /** | |
| 999 | + * Removes a listener for menu events. | |
| 1000 | + * | |
| 1001 | + * @param l the listener to be removed | |
| 1002 | + */ | |
| 1003 | + public void removeMenuListener(MenuListener l) { | |
| 1004 | + listenerList.remove(MenuListener.class, l); | |
| 1005 | + } | |
| 1006 | + | |
| 1007 | + /** | |
| 1008 | + * Returns an array of all the <code>MenuListener</code>s added | |
| 1009 | + * to this JMenu with addMenuListener(). | |
| 1010 | + * | |
| 1011 | + * @return all of the <code>MenuListener</code>s added or an empty | |
| 1012 | + * array if no listeners have been added | |
| 1013 | + * @since 1.4 | |
| 1014 | + */ | |
| 1015 | + public MenuListener[] getMenuListeners() { | |
| 1016 | + return listenerList.getListeners(MenuListener.class); | |
| 1017 | + } | |
| 1018 | + | |
| 1019 | + /** | |
| 1020 | + * Notifies all listeners that have registered interest for | |
| 1021 | + * notification on this event type. The event instance | |
| 1022 | + * is created lazily. | |
| 1023 | + * | |
| 1024 | + * @exception Error if there is a <code>null</code> listener | |
| 1025 | + * @see EventListenerList | |
| 1026 | + */ | |
| 1027 | + protected void fireMenuSelected() { | |
| 1028 | + if (DEBUG) { | |
| 1029 | + System.out.println("In JMenu.fireMenuSelected"); | |
| 1030 | + } | |
| 1031 | + // Guaranteed to return a non-null array | |
| 1032 | + Object[] listeners = listenerList.getListenerList(); | |
| 1033 | + // Process the listeners last to first, notifying | |
| 1034 | + // those that are interested in this event | |
| 1035 | + for (int i = listeners.length-2; i>=0; i-=2) { | |
| 1036 | + if (listeners[i]==MenuListener.class) { | |
| 1037 | + if (listeners[i+1]== null) { | |
| 1038 | + throw new Error(getText() +" has a NULL Listener!! " + i); | |
| 1039 | + } else { | |
| 1040 | + // Lazily create the event: | |
| 1041 | + if (menuEvent == null) | |
| 1042 | + menuEvent = new MenuEvent(this); | |
| 1043 | + ((MenuListener)listeners[i+1]).menuSelected(menuEvent); | |
| 1044 | + } | |
| 1045 | + } | |
| 1046 | + } | |
| 1047 | + } | |
| 1048 | + | |
| 1049 | + /** | |
| 1050 | + * Notifies all listeners that have registered interest for | |
| 1051 | + * notification on this event type. The event instance | |
| 1052 | + * is created lazily. | |
| 1053 | + * | |
| 1054 | + * @exception Error if there is a <code>null</code> listener | |
| 1055 | + * @see EventListenerList | |
| 1056 | + */ | |
| 1057 | + protected void fireMenuDeselected() { | |
| 1058 | + if (DEBUG) { | |
| 1059 | + System.out.println("In JMenu.fireMenuDeselected"); | |
| 1060 | + } | |
| 1061 | + // Guaranteed to return a non-null array | |
| 1062 | + Object[] listeners = listenerList.getListenerList(); | |
| 1063 | + // Process the listeners last to first, notifying | |
| 1064 | + // those that are interested in this event | |
| 1065 | + for (int i = listeners.length-2; i>=0; i-=2) { | |
| 1066 | + if (listeners[i]==MenuListener.class) { | |
| 1067 | + if (listeners[i+1]== null) { | |
| 1068 | + throw new Error(getText() +" has a NULL Listener!! " + i); | |
| 1069 | + } else { | |
| 1070 | + // Lazily create the event: | |
| 1071 | + if (menuEvent == null) | |
| 1072 | + menuEvent = new MenuEvent(this); | |
| 1073 | + ((MenuListener)listeners[i+1]).menuDeselected(menuEvent); | |
| 1074 | + } | |
| 1075 | + } | |
| 1076 | + } | |
| 1077 | + } | |
| 1078 | + | |
| 1079 | + /** | |
| 1080 | + * Notifies all listeners that have registered interest for | |
| 1081 | + * notification on this event type. The event instance | |
| 1082 | + * is created lazily. | |
| 1083 | + * | |
| 1084 | + * @exception Error if there is a <code>null</code> listener | |
| 1085 | + * @see EventListenerList | |
| 1086 | + */ | |
| 1087 | + protected void fireMenuCanceled() { | |
| 1088 | + if (DEBUG) { | |
| 1089 | + System.out.println("In JMenu.fireMenuCanceled"); | |
| 1090 | + } | |
| 1091 | + // Guaranteed to return a non-null array | |
| 1092 | + Object[] listeners = listenerList.getListenerList(); | |
| 1093 | + // Process the listeners last to first, notifying | |
| 1094 | + // those that are interested in this event | |
| 1095 | + for (int i = listeners.length-2; i>=0; i-=2) { | |
| 1096 | + if (listeners[i]==MenuListener.class) { | |
| 1097 | + if (listeners[i+1]== null) { | |
| 1098 | + throw new Error(getText() +" has a NULL Listener!! " | |
| 1099 | + + i); | |
| 1100 | + } else { | |
| 1101 | + // Lazily create the event: | |
| 1102 | + if (menuEvent == null) | |
| 1103 | + menuEvent = new MenuEvent(this); | |
| 1104 | + ((MenuListener)listeners[i+1]).menuCanceled(menuEvent); | |
| 1105 | + } | |
| 1106 | + } | |
| 1107 | + } | |
| 1108 | + } | |
| 1109 | + | |
| 1110 | + // Overriden to do nothing, JMenu doesn't support an accelerator | |
| 1111 | + void configureAcceleratorFromAction(Action a) { | |
| 1112 | + } | |
| 1113 | + | |
| 1114 | + class MenuChangeListener implements ChangeListener, Serializable { | |
| 1115 | + boolean isSelected = false; | |
| 1116 | + public void stateChanged(ChangeEvent e) { | |
| 1117 | + ButtonModel model = (ButtonModel) e.getSource(); | |
| 1118 | + boolean modelSelected = model.isSelected(); | |
| 1119 | + | |
| 1120 | + if (modelSelected != isSelected) { | |
| 1121 | + if (modelSelected == true) { | |
| 1122 | + fireMenuSelected(); | |
| 1123 | + } else { | |
| 1124 | + fireMenuDeselected(); | |
| 1125 | + } | |
| 1126 | + isSelected = modelSelected; | |
| 1127 | + } | |
| 1128 | + } | |
| 1129 | + } | |
| 1130 | + | |
| 1131 | + private ChangeListener createMenuChangeListener() { | |
| 1132 | + return new MenuChangeListener(); | |
| 1133 | + } | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + /** | |
| 1137 | + * Creates a window-closing listener for the popup. | |
| 1138 | + * | |
| 1139 | + * @param p the <code>JPopupMenu</code> | |
| 1140 | + * @return the new window-closing listener | |
| 1141 | + * | |
| 1142 | + * @see WinListener | |
| 1143 | + */ | |
| 1144 | + protected WinListener createWinListener(JPopupMenu p) { | |
| 1145 | + return new WinListener(p); | |
| 1146 | + } | |
| 1147 | + | |
| 1148 | + /** | |
| 1149 | + * A listener class that watches for a popup window closing. | |
| 1150 | + * When the popup is closing, the listener deselects the menu. | |
| 1151 | + * <p> | |
| 1152 | + * <strong>Warning:</strong> | |
| 1153 | + * Serialized objects of this class will not be compatible with | |
| 1154 | + * future Swing releases. The current serialization support is | |
| 1155 | + * appropriate for short term storage or RMI between applications running | |
| 1156 | + * the same version of Swing. As of 1.4, support for long term storage | |
| 1157 | + * of all JavaBeans<sup><font size="-2">TM</font></sup> | |
| 1158 | + * has been added to the <code>java.beans</code> package. | |
| 1159 | + * Please see {@link java.beans.XMLEncoder}. | |
| 1160 | + */ | |
| 1161 | + protected class WinListener extends WindowAdapter implements Serializable { | |
| 1162 | + JPopupMenu popupMenu; | |
| 1163 | + /** | |
| 1164 | + * Create the window listener for the specified popup. | |
| 1165 | + * @since 1.4 | |
| 1166 | + */ | |
| 1167 | + public WinListener(JPopupMenu p) { | |
| 1168 | + this.popupMenu = p; | |
| 1169 | + } | |
| 1170 | + /** | |
| 1171 | + * Deselect the menu when the popup is closed from outside. | |
| 1172 | + */ | |
| 1173 | + public void windowClosing(WindowEvent e) { | |
| 1174 | + setSelected(false); | |
| 1175 | + } | |
| 1176 | + } | |
| 1177 | + | |
| 1178 | + /** | |
| 1179 | + * Messaged when the menubar selection changes to activate or | |
| 1180 | + * deactivate this menu. | |
| 1181 | + * Overrides <code>JMenuItem.menuSelectionChanged</code>. | |
| 1182 | + * | |
| 1183 | + * @param isIncluded true if this menu is active, false if | |
| 1184 | + * it is not | |
| 1185 | + */ | |
| 1186 | + public void menuSelectionChanged(boolean isIncluded) { | |
| 1187 | + if (DEBUG) { | |
| 1188 | + System.out.println("In JMenu.menuSelectionChanged to " + isIncluded); | |
| 1189 | + } | |
| 1190 | + setSelected(isIncluded); | |
| 1191 | + } | |
| 1192 | + | |
| 1193 | + /** | |
| 1194 | + * Returns an array of <code>MenuElement</code>s containing the submenu | |
| 1195 | + * for this menu component. If popup menu is <code>null</code> returns | |
| 1196 | + * an empty array. This method is required to conform to the | |
| 1197 | + * <code>MenuElement</code> interface. Note that since | |
| 1198 | + * <code>JSeparator</code>s do not conform to the <code>MenuElement</code> | |
| 1199 | + * interface, this array will only contain <code>JMenuItem</code>s. | |
| 1200 | + * | |
| 1201 | + * @return an array of <code>MenuElement</code> objects | |
| 1202 | + */ | |
| 1203 | + public MenuElement[] getSubElements() { | |
| 1204 | + if(popupMenu == null) | |
| 1205 | + return new MenuElement[0]; | |
| 1206 | + else { | |
| 1207 | + MenuElement result[] = new MenuElement[1]; | |
| 1208 | + result[0] = popupMenu; | |
| 1209 | + return result; | |
| 1210 | + } | |
| 1211 | + } | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + // implements javax.swing.MenuElement | |
| 1215 | + /** | |
| 1216 | + * Returns the <code>java.awt.Component</code> used to | |
| 1217 | + * paint this <code>MenuElement</code>. | |
| 1218 | + * The returned component is used to convert events and detect if | |
| 1219 | + * an event is inside a menu component. | |
| 1220 | + */ | |
| 1221 | + public Component getComponent() { | |
| 1222 | + return this; | |
| 1223 | + } | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + /** | |
| 1227 | + * Sets the <code>ComponentOrientation</code> property of this menu | |
| 1228 | + * and all components contained within it. This includes all | |
| 1229 | + * components returned by {@link #getMenuComponents getMenuComponents}. | |
| 1230 | + * | |
| 1231 | + * @param o the new component orientation of this menu and | |
| 1232 | + * the components contained within it. | |
| 1233 | + * @exception NullPointerException if <code>orientation</code> is null. | |
| 1234 | + * @see java.awt.Component#setComponentOrientation | |
| 1235 | + * @see java.awt.Component#getComponentOrientation | |
| 1236 | + * @since 1.4 | |
| 1237 | + */ | |
| 1238 | + public void applyComponentOrientation(ComponentOrientation o) { | |
| 1239 | + super.applyComponentOrientation(o); | |
| 1240 | + | |
| 1241 | + if ( popupMenu != null ) { | |
| 1242 | + int ncomponents = getMenuComponentCount(); | |
| 1243 | + for (int i = 0 ; i < ncomponents ; ++i) { | |
| 1244 | + getMenuComponent(i).applyComponentOrientation(o); | |
| 1245 | + } | |
| 1246 | + popupMenu.setComponentOrientation(o); | |
| 1247 | + } | |
| 1248 | + } | |
| 1249 | + | |
| 1250 | + public void setComponentOrientation(ComponentOrientation o) { | |
| 1251 | + super.setComponentOrientation(o); | |
| 1252 | + if ( popupMenu != null ) { | |
| 1253 | + popupMenu.setComponentOrientation(o); | |
| 1254 | + } | |
| 1255 | + } | |
| 1256 | + | |
| 1257 | + /** | |
| 1258 | + * <code>setAccelerator</code> is not defined for <code>JMenu</code>. | |
| 1259 | + * Use <code>setMnemonic</code> instead. | |
| 1260 | + * @param keyStroke the keystroke combination which will invoke | |
| 1261 | + * the <code>JMenuItem</code>'s actionlisteners | |
| 1262 | + * without navigating the menu hierarchy | |
| 1263 | + * @exception Error if invoked -- this method is not defined for JMenu. | |
| 1264 | + * Use <code>setMnemonic</code> instead | |
| 1265 | + * | |
| 1266 | + * @beaninfo | |
| 1267 | + * description: The keystroke combination which will invoke the JMenuItem's | |
| 1268 | + * actionlisteners without navigating the menu hierarchy | |
| 1269 | + * hidden: true | |
| 1270 | + */ | |
| 1271 | + public void setAccelerator(KeyStroke keyStroke) { | |
| 1272 | + throw new Error("setAccelerator() is not defined for JMenu. Use setMnemonic() instead."); | |
| 1273 | + } | |
| 1274 | + | |
| 1275 | + /** | |
| 1276 | + * Processes key stroke events such as mnemonics and accelerators. | |
| 1277 | + * | |
| 1278 | + * @param evt the key event to be processed | |
| 1279 | + */ | |
| 1280 | + protected void processKeyEvent(KeyEvent evt) { | |
| 1281 | + MenuSelectionManager.defaultManager().processKeyEvent(evt); | |
| 1282 | + if (evt.isConsumed()) | |
| 1283 | + return; | |
| 1284 | + | |
| 1285 | + super.processKeyEvent(evt); | |
| 1286 | + } | |
| 1287 | + | |
| 1288 | + /** | |
| 1289 | + * Programmatically performs a "click". This overrides the method | |
| 1290 | + * <code>AbstractButton.doClick</code> in order to make the menu pop up. | |
| 1291 | + * @param pressTime indicates the number of milliseconds the | |
| 1292 | + * button was pressed for | |
| 1293 | + */ | |
| 1294 | + public void doClick(int pressTime) { | |
| 1295 | + MenuElement me[] = buildMenuElementArray(this); | |
| 1296 | + MenuSelectionManager.defaultManager().setSelectedPath(me); | |
| 1297 | + } | |
| 1298 | + | |
| 1299 | + /* | |
| 1300 | + * Build an array of menu elements - from <code>PopupMenu</code> to | |
| 1301 | + * the root <code>JMenuBar</code>. | |
| 1302 | + * @param leaf the leaf node from which to start building up the array | |
| 1303 | + * @return the array of menu items | |
| 1304 | + */ | |
| 1305 | + private MenuElement[] buildMenuElementArray(JMenu leaf) { | |
| 1306 | + Vector<MenuElement> elements = new Vector<MenuElement>(); | |
| 1307 | + Component current = leaf.getPopupMenu(); | |
| 1308 | + JPopupMenu pop; | |
| 1309 | + JMenu menu; | |
| 1310 | + JMenuBar bar; | |
| 1311 | + | |
| 1312 | + while (true) { | |
| 1313 | + if (current instanceof JPopupMenu) { | |
| 1314 | + pop = (JPopupMenu) current; | |
| 1315 | + elements.insertElementAt(pop, 0); | |
| 1316 | + current = pop.getInvoker(); | |
| 1317 | + } else if (current instanceof JMenu) { | |
| 1318 | + menu = (JMenu) current; | |
| 1319 | + elements.insertElementAt(menu, 0); | |
| 1320 | + current = menu.getParent(); | |
| 1321 | + } else if (current instanceof JMenuBar) { | |
| 1322 | + bar = (JMenuBar) current; | |
| 1323 | + elements.insertElementAt(bar, 0); | |
| 1324 | + MenuElement me[] = new MenuElement[elements.size()]; | |
| 1325 | + elements.copyInto(me); | |
| 1326 | + return me; | |
| 1327 | + } | |
| 1328 | + } | |
| 1329 | + } | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + /** | |
| 1333 | + * See <code>readObject</code> and <code>writeObject</code> in | |
| 1334 | + * <code>JComponent</code> for more | |
| 1335 | + * information about serialization in Swing. | |
| 1336 | + */ | |
| 1337 | + private void writeObject(ObjectOutputStream s) throws IOException { | |
| 1338 | + s.defaultWriteObject(); | |
| 1339 | + if (getUIClassID().equals(uiClassID)) { | |
| 1340 | + byte count = JComponent.getWriteObjCounter(this); | |
| 1341 | + JComponent.setWriteObjCounter(this, --count); | |
| 1342 | + if (count == 0 && ui != null) { | |
| 1343 | + ui.installUI(this); | |
| 1344 | + } | |
| 1345 | + } | |
| 1346 | + } | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + /** | |
| 1350 | + * Returns a string representation of this <code>JMenu</code>. This | |
| 1351 | + * method is intended to be used only for debugging purposes, and the | |
| 1352 | + * content and format of the returned string may vary between | |
| 1353 | + * implementations. The returned string may be empty but may not | |
| 1354 | + * be <code>null</code>. | |
| 1355 | + * | |
| 1356 | + * @return a string representation of this JMenu. | |
| 1357 | + */ | |
| 1358 | + protected String paramString() { | |
| 1359 | + return super.paramString(); | |
| 1360 | + } | |
| 1361 | + | |
| 1362 | + | |
| 1363 | +///////////////// | |
| 1364 | +// Accessibility support | |
| 1365 | +//////////////// | |
| 1366 | + | |
| 1367 | + /** | |
| 1368 | + * Gets the AccessibleContext associated with this JMenu. | |
| 1369 | + * For JMenus, the AccessibleContext takes the form of an | |
| 1370 | + * AccessibleJMenu. | |
| 1371 | + * A new AccessibleJMenu instance is created if necessary. | |
| 1372 | + * | |
| 1373 | + * @return an AccessibleJMenu that serves as the | |
| 1374 | + * AccessibleContext of this JMenu | |
| 1375 | + */ | |
| 1376 | + public AccessibleContext getAccessibleContext() { | |
| 1377 | + if (accessibleContext == null) { | |
| 1378 | + accessibleContext = new AccessibleJMenu(); | |
| 1379 | + } | |
| 1380 | + return accessibleContext; | |
| 1381 | + } | |
| 1382 | + | |
| 1383 | + /** | |
| 1384 | + * This class implements accessibility support for the | |
| 1385 | + * <code>JMenu</code> class. It provides an implementation of the | |
| 1386 | + * Java Accessibility API appropriate to menu user-interface elements. | |
| 1387 | + * <p> | |
| 1388 | + * <strong>Warning:</strong> | |
| 1389 | + * Serialized objects of this class will not be compatible with | |
| 1390 | + * future Swing releases. The current serialization support is | |
| 1391 | + * appropriate for short term storage or RMI between applications running | |
| 1392 | + * the same version of Swing. As of 1.4, support for long term storage | |
| 1393 | + * of all JavaBeans<sup><font size="-2">TM</font></sup> | |
| 1394 | + * has been added to the <code>java.beans</code> package. | |
| 1395 | + * Please see {@link java.beans.XMLEncoder}. | |
| 1396 | + */ | |
| 1397 | + protected class AccessibleJMenu extends AccessibleJMenuItem | |
| 1398 | + implements AccessibleSelection { | |
| 1399 | + | |
| 1400 | + /** | |
| 1401 | + * Returns the number of accessible children in the object. If all | |
| 1402 | + * of the children of this object implement Accessible, than this | |
| 1403 | + * method should return the number of children of this object. | |
| 1404 | + * | |
| 1405 | + * @return the number of accessible children in the object. | |
| 1406 | + */ | |
| 1407 | + public int getAccessibleChildrenCount() { | |
| 1408 | + Component[] children = getMenuComponents(); | |
| 1409 | + int count = 0; | |
| 1410 | + for (Component child : children) { | |
| 1411 | + if (child instanceof Accessible) { | |
| 1412 | + count++; | |
| 1413 | + } | |
| 1414 | + } | |
| 1415 | + return count; | |
| 1416 | + } | |
| 1417 | + | |
| 1418 | + /** | |
| 1419 | + * Returns the nth Accessible child of the object. | |
| 1420 | + * | |
| 1421 | + * @param i zero-based index of child | |
| 1422 | + * @return the nth Accessible child of the object | |
| 1423 | + */ | |
| 1424 | + public Accessible getAccessibleChild(int i) { | |
| 1425 | + Component[] children = getMenuComponents(); | |
| 1426 | + int count = 0; | |
| 1427 | + for (Component child : children) { | |
| 1428 | + if (child instanceof Accessible) { | |
| 1429 | + if (count == i) { | |
| 1430 | + if (child instanceof JComponent) { | |
| 1431 | + // FIXME: [[[WDW - probably should set this when | |
| 1432 | + // the component is added to the menu. I tried | |
| 1433 | + // to do this in most cases, but the separators | |
| 1434 | + // added by addSeparator are hard to get to.]]] | |
| 1435 | + AccessibleContext ac = child.getAccessibleContext(); | |
| 1436 | + ac.setAccessibleParent(JMenu.this); | |
| 1437 | + } | |
| 1438 | + return (Accessible) child; | |
| 1439 | + } else { | |
| 1440 | + count++; | |
| 1441 | + } | |
| 1442 | + } | |
| 1443 | + } | |
| 1444 | + return null; | |
| 1445 | + } | |
| 1446 | + | |
| 1447 | + /** | |
| 1448 | + * Get the role of this object. | |
| 1449 | + * | |
| 1450 | + * @return an instance of AccessibleRole describing the role of the | |
| 1451 | + * object | |
| 1452 | + * @see AccessibleRole | |
| 1453 | + */ | |
| 1454 | + public AccessibleRole getAccessibleRole() { | |
| 1455 | + return AccessibleRole.MENU; | |
| 1456 | + } | |
| 1457 | + | |
| 1458 | + /** | |
| 1459 | + * Get the AccessibleSelection associated with this object. In the | |
| 1460 | + * implementation of the Java Accessibility API for this class, | |
| 1461 | + * return this object, which is responsible for implementing the | |
| 1462 | + * AccessibleSelection interface on behalf of itself. | |
| 1463 | + * | |
| 1464 | + * @return this object | |
| 1465 | + */ | |
| 1466 | + public AccessibleSelection getAccessibleSelection() { | |
| 1467 | + return this; | |
| 1468 | + } | |
| 1469 | + | |
| 1470 | + /** | |
| 1471 | + * Returns 1 if a sub-menu is currently selected in this menu. | |
| 1472 | + * | |
| 1473 | + * @return 1 if a menu is currently selected, else 0 | |
| 1474 | + */ | |
| 1475 | + public int getAccessibleSelectionCount() { | |
| 1476 | + MenuElement me[] = | |
| 1477 | + MenuSelectionManager.defaultManager().getSelectedPath(); | |
| 1478 | + if (me != null) { | |
| 1479 | + for (int i = 0; i < me.length; i++) { | |
| 1480 | + if (me[i] == JMenu.this) { // this menu is selected | |
| 1481 | + if (i+1 < me.length) { | |
| 1482 | + return 1; | |
| 1483 | + } | |
| 1484 | + } | |
| 1485 | + } | |
| 1486 | + } | |
| 1487 | + return 0; | |
| 1488 | + } | |
| 1489 | + | |
| 1490 | + /** | |
| 1491 | + * Returns the currently selected sub-menu if one is selected, | |
| 1492 | + * otherwise null (there can only be one selection, and it can | |
| 1493 | + * only be a sub-menu, as otherwise menu items don't remain | |
| 1494 | + * selected). | |
| 1495 | + */ | |
| 1496 | + public Accessible getAccessibleSelection(int i) { | |
| 1497 | + // if i is a sub-menu & popped, return it | |
| 1498 | + if (i < 0 || i >= getItemCount()) { | |
| 1499 | + return null; | |
| 1500 | + } | |
| 1501 | + MenuElement me[] = | |
| 1502 | + MenuSelectionManager.defaultManager().getSelectedPath(); | |
| 1503 | + if (me != null) { | |
| 1504 | + for (int j = 0; j < me.length; j++) { | |
| 1505 | + if (me[j] == JMenu.this) { // this menu is selected | |
| 1506 | + // so find the next JMenuItem in the MenuElement | |
| 1507 | + // array, and return it! | |
| 1508 | + while (++j < me.length) { | |
| 1509 | + if (me[j] instanceof JMenuItem) { | |
| 1510 | + return (Accessible) me[j]; | |
| 1511 | + } | |
| 1512 | + } | |
| 1513 | + } | |
| 1514 | + } | |
| 1515 | + } | |
| 1516 | + return null; | |
| 1517 | + } | |
| 1518 | + | |
| 1519 | + /** | |
| 1520 | + * Returns true if the current child of this object is selected | |
| 1521 | + * (that is, if this child is a popped-up submenu). | |
| 1522 | + * | |
| 1523 | + * @param i the zero-based index of the child in this Accessible | |
| 1524 | + * object. | |
| 1525 | + * @see AccessibleContext#getAccessibleChild | |
| 1526 | + */ | |
| 1527 | + public boolean isAccessibleChildSelected(int i) { | |
| 1528 | + // if i is a sub-menu and is pop-ed up, return true, else false | |
| 1529 | + MenuElement me[] = | |
| 1530 | + MenuSelectionManager.defaultManager().getSelectedPath(); | |
| 1531 | + if (me != null) { | |
| 1532 | + JMenuItem mi = JMenu.this.getItem(i); | |
| 1533 | + for (int j = 0; j < me.length; j++) { | |
| 1534 | + if (me[j] == mi) { | |
| 1535 | + return true; | |
| 1536 | + } | |
| 1537 | + } | |
| 1538 | + } | |
| 1539 | + return false; | |
| 1540 | + } | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + /** | |
| 1544 | + * Selects the <code>i</code>th menu in the menu. | |
| 1545 | + * If that item is a submenu, | |
| 1546 | + * it will pop up in response. If a different item is already | |
| 1547 | + * popped up, this will force it to close. If this is a sub-menu | |
| 1548 | + * that is already popped up (selected), this method has no | |
| 1549 | + * effect. | |
| 1550 | + * | |
| 1551 | + * @param i the index of the item to be selected | |
| 1552 | + * @see #getAccessibleStateSet | |
| 1553 | + */ | |
| 1554 | + public void addAccessibleSelection(int i) { | |
| 1555 | + if (i < 0 || i >= getItemCount()) { | |
| 1556 | + return; | |
| 1557 | + } | |
| 1558 | + JMenuItem mi = getItem(i); | |
| 1559 | + if (mi != null) { | |
| 1560 | + if (mi instanceof JMenu) { | |
| 1561 | + MenuElement me[] = buildMenuElementArray((JMenu) mi); | |
| 1562 | + MenuSelectionManager.defaultManager().setSelectedPath(me); | |
| 1563 | + } else { | |
| 1564 | + MenuSelectionManager.defaultManager().setSelectedPath(null); | |
| 1565 | + } | |
| 1566 | + } | |
| 1567 | + } | |
| 1568 | + | |
| 1569 | + /** | |
| 1570 | + * Removes the nth item from the selection. In general, menus | |
| 1571 | + * can only have one item within them selected at a time | |
| 1572 | + * (e.g. one sub-menu popped open). | |
| 1573 | + * | |
| 1574 | + * @param i the zero-based index of the selected item | |
| 1575 | + */ | |
| 1576 | + public void removeAccessibleSelection(int i) { | |
| 1577 | + if (i < 0 || i >= getItemCount()) { | |
| 1578 | + return; | |
| 1579 | + } | |
| 1580 | + JMenuItem mi = getItem(i); | |
| 1581 | + if (mi != null && mi instanceof JMenu) { | |
| 1582 | + if (mi.isSelected()) { | |
| 1583 | + MenuElement old[] = | |
| 1584 | + MenuSelectionManager.defaultManager().getSelectedPath(); | |
| 1585 | + MenuElement me[] = new MenuElement[old.length-2]; | |
| 1586 | + for (int j = 0; j < old.length -2; j++) { | |
| 1587 | + me[j] = old[j]; | |
| 1588 | + } | |
| 1589 | + MenuSelectionManager.defaultManager().setSelectedPath(me); | |
| 1590 | + } | |
| 1591 | + } | |
| 1592 | + } | |
| 1593 | + | |
| 1594 | + /** | |
| 1595 | + * Clears the selection in the object, so that nothing in the | |
| 1596 | + * object is selected. This will close any open sub-menu. | |
| 1597 | + */ | |
| 1598 | + public void clearAccessibleSelection() { | |
| 1599 | + // if this menu is selected, reset selection to only go | |
| 1600 | + // to this menu; else do nothing | |
| 1601 | + MenuElement old[] = | |
| 1602 | + MenuSelectionManager.defaultManager().getSelectedPath(); | |
| 1603 | + if (old != null) { | |
| 1604 | + for (int j = 0; j < old.length; j++) { | |
| 1605 | + if (old[j] == JMenu.this) { // menu is in the selection! | |
| 1606 | + MenuElement me[] = new MenuElement[j+1]; | |
| 1607 | + System.arraycopy(old, 0, me, 0, j); | |
| 1608 | + me[j] = JMenu.this.getPopupMenu(); | |
| 1609 | + MenuSelectionManager.defaultManager().setSelectedPath(me); | |
| 1610 | + } | |
| 1611 | + } | |
| 1612 | + } | |
| 1613 | + } | |
| 1614 | + | |
| 1615 | + /** | |
| 1616 | + * Normally causes every selected item in the object to be selected | |
| 1617 | + * if the object supports multiple selections. This method | |
| 1618 | + * makes no sense in a menu bar, and so does nothing. | |
| 1619 | + */ | |
| 1620 | + public void selectAllAccessibleSelection() { | |
| 1621 | + } | |
| 1622 | + } // inner class AccessibleJMenu | |
| 1623 | + | |
| 1624 | +} | |