| 1 | /* MenuItem.java -- An item in a menu
|
|---|
| 2 | Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt;
|
|---|
| 40 |
|
|---|
| 41 | import java.awt.peer.MenuItemPeer;
|
|---|
| 42 | import java.awt.peer.MenuComponentPeer;
|
|---|
| 43 | import java.awt.event.ActionEvent;
|
|---|
| 44 | import java.awt.event.ActionListener;
|
|---|
| 45 | import java.lang.reflect.Array;
|
|---|
| 46 | import java.util.EventListener;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * This class represents an item in a menu.
|
|---|
| 50 | *
|
|---|
| 51 | * @author Aaron M. Renn ([email protected])
|
|---|
| 52 | */
|
|---|
| 53 | public class MenuItem extends MenuComponent implements java.io.Serializable
|
|---|
| 54 | {
|
|---|
| 55 |
|
|---|
| 56 | // FIXME: The enabled event mask is not used at this time.
|
|---|
| 57 |
|
|---|
| 58 | /*
|
|---|
| 59 | * Static Variables
|
|---|
| 60 | */
|
|---|
| 61 |
|
|---|
| 62 | // Serialization Constant
|
|---|
| 63 | private static final long serialVersionUID = -21757335363267194L;
|
|---|
| 64 |
|
|---|
| 65 | /*************************************************************************/
|
|---|
| 66 |
|
|---|
| 67 | /*
|
|---|
| 68 | * Instance Variables
|
|---|
| 69 | */
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * @serial The name of the action command generated by this item.
|
|---|
| 73 | */
|
|---|
| 74 | private String actionCommand;
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * @serial Indicates whether or not this menu item is enabled.
|
|---|
| 78 | */
|
|---|
| 79 | private boolean enabled;
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * @serial The mask of events that are enabled for this menu item.
|
|---|
| 83 | */
|
|---|
| 84 | long eventMask;
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | * @serial This menu item's label
|
|---|
| 88 | */
|
|---|
| 89 | private String label;
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * @serial The shortcut for this menu item, if any
|
|---|
| 93 | */
|
|---|
| 94 | private MenuShortcut shortcut;
|
|---|
| 95 |
|
|---|
| 96 | // The list of action listeners for this menu item.
|
|---|
| 97 | private transient ActionListener action_listeners;
|
|---|
| 98 |
|
|---|
| 99 | /*************************************************************************/
|
|---|
| 100 |
|
|---|
| 101 | /*
|
|---|
| 102 | * Constructors
|
|---|
| 103 | */
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * Initializes a new instance of <code>MenuItem</code> with no label
|
|---|
| 107 | * and no shortcut.
|
|---|
| 108 | */
|
|---|
| 109 | public
|
|---|
| 110 | MenuItem()
|
|---|
| 111 | {
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /*************************************************************************/
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Initializes a new instance of <code>MenuItem</code> with the specified
|
|---|
| 118 | * label and no shortcut.
|
|---|
| 119 | *
|
|---|
| 120 | * @param label The label for this menu item.
|
|---|
| 121 | */
|
|---|
| 122 | public
|
|---|
| 123 | MenuItem(String label)
|
|---|
| 124 | {
|
|---|
| 125 | this.label = label;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /*************************************************************************/
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * Initializes a new instance of <code>MenuItem</code> with the specified
|
|---|
| 132 | * label and shortcut.
|
|---|
| 133 | *
|
|---|
| 134 | * @param label The label for this menu item.
|
|---|
| 135 | * @param shortcut The shortcut for this menu item.
|
|---|
| 136 | */
|
|---|
| 137 | public
|
|---|
| 138 | MenuItem(String label, MenuShortcut shortcut)
|
|---|
| 139 | {
|
|---|
| 140 | this.label = label;
|
|---|
| 141 | this.shortcut = shortcut;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /*************************************************************************/
|
|---|
| 145 |
|
|---|
| 146 | /*
|
|---|
| 147 | * Instance Methods
|
|---|
| 148 | */
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * Returns the label for this menu item, which may be <code>null</code>.
|
|---|
| 152 | *
|
|---|
| 153 | * @return The label for this menu item.
|
|---|
| 154 | */
|
|---|
| 155 | public String
|
|---|
| 156 | getLabel()
|
|---|
| 157 | {
|
|---|
| 158 | return(label);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /*************************************************************************/
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * This method sets the label for this menu to the specified value.
|
|---|
| 165 | *
|
|---|
| 166 | * @param label The new label for this menu item.
|
|---|
| 167 | */
|
|---|
| 168 | public synchronized void
|
|---|
| 169 | setLabel(String label)
|
|---|
| 170 | {
|
|---|
| 171 | this.label = label;
|
|---|
| 172 | if (peer != null)
|
|---|
| 173 | {
|
|---|
| 174 | MenuItemPeer mp = (MenuItemPeer) peer;
|
|---|
| 175 | mp.setLabel (label);
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /*************************************************************************/
|
|---|
| 180 |
|
|---|
| 181 | /**
|
|---|
| 182 | * Tests whether or not this menu item is enabled.
|
|---|
| 183 | *
|
|---|
| 184 | * @return <code>true</code> if this menu item is enabled, <code>false</code>
|
|---|
| 185 | * otherwise.
|
|---|
| 186 | */
|
|---|
| 187 | public boolean
|
|---|
| 188 | isEnabled()
|
|---|
| 189 | {
|
|---|
| 190 | return(enabled);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /*************************************************************************/
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Sets the enabled status of this menu item.
|
|---|
| 197 | *
|
|---|
| 198 | * @param enabled <code>true</code> to enable this menu item,
|
|---|
| 199 | * <code>false</code> otherwise.
|
|---|
| 200 | */
|
|---|
| 201 | public synchronized void
|
|---|
| 202 | setEnabled(boolean enabled)
|
|---|
| 203 | {
|
|---|
| 204 | if (enabled == this.enabled)
|
|---|
| 205 | return;
|
|---|
| 206 |
|
|---|
| 207 | this.enabled = enabled;
|
|---|
| 208 | if (peer != null)
|
|---|
| 209 | {
|
|---|
| 210 | MenuItemPeer mp = (MenuItemPeer) peer;
|
|---|
| 211 | mp.setEnabled (enabled);
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | /*************************************************************************/
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Sets the enabled status of this menu item.
|
|---|
| 219 | *
|
|---|
| 220 | * @param enabled <code>true</code> to enable this menu item,
|
|---|
| 221 | * <code>false</code> otherwise.
|
|---|
| 222 | *
|
|---|
| 223 | * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
|
|---|
| 224 | */
|
|---|
| 225 | public void
|
|---|
| 226 | enable(boolean enabled)
|
|---|
| 227 | {
|
|---|
| 228 | setEnabled(enabled);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /*************************************************************************/
|
|---|
| 232 |
|
|---|
| 233 | /**
|
|---|
| 234 | * Enables this menu item.
|
|---|
| 235 | *
|
|---|
| 236 | * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
|
|---|
| 237 | */
|
|---|
| 238 | public void
|
|---|
| 239 | enable()
|
|---|
| 240 | {
|
|---|
| 241 | setEnabled(true);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /*************************************************************************/
|
|---|
| 245 |
|
|---|
| 246 | /**
|
|---|
| 247 | * Disables this menu item.
|
|---|
| 248 | *
|
|---|
| 249 | * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
|
|---|
| 250 | */
|
|---|
| 251 | public void
|
|---|
| 252 | disable()
|
|---|
| 253 | {
|
|---|
| 254 | setEnabled(false);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /*************************************************************************/
|
|---|
| 258 |
|
|---|
| 259 | /**
|
|---|
| 260 | * Returns the shortcut for this menu item, which may be <code>null</code>.
|
|---|
| 261 | *
|
|---|
| 262 | * @return The shortcut for this menu item.
|
|---|
| 263 | */
|
|---|
| 264 | public MenuShortcut
|
|---|
| 265 | getShortcut()
|
|---|
| 266 | {
|
|---|
| 267 | return(shortcut);
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /*************************************************************************/
|
|---|
| 271 |
|
|---|
| 272 | /**
|
|---|
| 273 | * Sets the shortcut for this menu item to the specified value. This
|
|---|
| 274 | * must be done before the native peer is created.
|
|---|
| 275 | *
|
|---|
| 276 | * @param shortcut The new shortcut for this menu item.
|
|---|
| 277 | */
|
|---|
| 278 | public void
|
|---|
| 279 | setShortcut(MenuShortcut shortcut)
|
|---|
| 280 | {
|
|---|
| 281 | this.shortcut = shortcut;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /*************************************************************************/
|
|---|
| 285 |
|
|---|
| 286 | /**
|
|---|
| 287 | * Deletes the shortcut for this menu item if one exists. This must be
|
|---|
| 288 | * done before the native peer is created.
|
|---|
| 289 | */
|
|---|
| 290 | public void
|
|---|
| 291 | deleteShortcut()
|
|---|
| 292 | {
|
|---|
| 293 | shortcut = null;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /*************************************************************************/
|
|---|
| 297 |
|
|---|
| 298 | /**
|
|---|
| 299 | * Returns the name of the action command in the action events
|
|---|
| 300 | * generated by this menu item.
|
|---|
| 301 | *
|
|---|
| 302 | * @return The action command name
|
|---|
| 303 | */
|
|---|
| 304 | public String
|
|---|
| 305 | getActionCommand()
|
|---|
| 306 | {
|
|---|
| 307 | return(actionCommand);
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | /*************************************************************************/
|
|---|
| 311 |
|
|---|
| 312 | /**
|
|---|
| 313 | * Sets the name of the action command in the action events generated by
|
|---|
| 314 | * this menu item.
|
|---|
| 315 | *
|
|---|
| 316 | * @param actionCommand The new action command name.
|
|---|
| 317 | */
|
|---|
| 318 | public void
|
|---|
| 319 | setActionCommand(String actionCommand)
|
|---|
| 320 | {
|
|---|
| 321 | this.actionCommand = actionCommand;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | /*************************************************************************/
|
|---|
| 325 |
|
|---|
| 326 | /**
|
|---|
| 327 | * Enables the specified events. This is done automatically when a
|
|---|
| 328 | * listener is added and does not normally need to be done by
|
|---|
| 329 | * application code.
|
|---|
| 330 | *
|
|---|
| 331 | * @param events The events to enable, which should be the bit masks
|
|---|
| 332 | * from <code>AWTEvent</code>.
|
|---|
| 333 | */
|
|---|
| 334 | protected final void
|
|---|
| 335 | enableEvents(long events)
|
|---|
| 336 | {
|
|---|
| 337 | eventMask |= events;
|
|---|
| 338 | // TODO: see comment in Component.enableEvents().
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | /*************************************************************************/
|
|---|
| 342 |
|
|---|
| 343 | /**
|
|---|
| 344 | * Disables the specified events.
|
|---|
| 345 | *
|
|---|
| 346 | * @param events The events to enable, which should be the bit masks
|
|---|
| 347 | * from <code>AWTEvent</code>.
|
|---|
| 348 | */
|
|---|
| 349 | protected final void
|
|---|
| 350 | disableEvents(long events)
|
|---|
| 351 | {
|
|---|
| 352 | eventMask &= ~events;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | /*************************************************************************/
|
|---|
| 356 |
|
|---|
| 357 | /**
|
|---|
| 358 | * Creates the native peer for this object.
|
|---|
| 359 | */
|
|---|
| 360 | public void
|
|---|
| 361 | addNotify()
|
|---|
| 362 | {
|
|---|
| 363 | if (peer != null)
|
|---|
| 364 | peer = getToolkit ().createMenuItem (this);
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | /*************************************************************************/
|
|---|
| 368 |
|
|---|
| 369 | /**
|
|---|
| 370 | * Adds the specified listener to the list of registered action listeners
|
|---|
| 371 | * for this component.
|
|---|
| 372 | *
|
|---|
| 373 | * @param listener The listener to add.
|
|---|
| 374 | */
|
|---|
| 375 | public synchronized void
|
|---|
| 376 | addActionListener(ActionListener listener)
|
|---|
| 377 | {
|
|---|
| 378 | action_listeners = AWTEventMulticaster.add(action_listeners, listener);
|
|---|
| 379 |
|
|---|
| 380 | enableEvents(AWTEvent.ACTION_EVENT_MASK);
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | public synchronized void
|
|---|
| 384 | removeActionListener(ActionListener l)
|
|---|
| 385 | {
|
|---|
| 386 | action_listeners = AWTEventMulticaster.remove(action_listeners, l);
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | public synchronized ActionListener[] getActionListeners()
|
|---|
| 390 | {
|
|---|
| 391 | return (ActionListener[])
|
|---|
| 392 | AWTEventMulticaster.getListeners(action_listeners,
|
|---|
| 393 | ActionListener.class);
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | /** Returns all registered EventListers of the given listenerType.
|
|---|
| 397 | * listenerType must be a subclass of EventListener, or a
|
|---|
| 398 | * ClassClassException is thrown.
|
|---|
| 399 | * @since 1.3
|
|---|
| 400 | */
|
|---|
| 401 | public EventListener[] getListeners(Class listenerType)
|
|---|
| 402 | {
|
|---|
| 403 | if (listenerType == ActionListener.class)
|
|---|
| 404 | return getActionListeners();
|
|---|
| 405 | return (EventListener[]) Array.newInstance(listenerType, 0);
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | /*************************************************************************/
|
|---|
| 409 |
|
|---|
| 410 | void
|
|---|
| 411 | dispatchEventImpl(AWTEvent e)
|
|---|
| 412 | {
|
|---|
| 413 | if (e.id <= ActionEvent.ACTION_LAST
|
|---|
| 414 | && e.id >= ActionEvent.ACTION_FIRST
|
|---|
| 415 | && (action_listeners != null
|
|---|
| 416 | || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
|
|---|
| 417 | processEvent(e);
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | /**
|
|---|
| 421 | * Processes the specified event by calling <code>processActionEvent()</code>
|
|---|
| 422 | * if it is an instance of <code>ActionEvent</code>.
|
|---|
| 423 | *
|
|---|
| 424 | * @param event The event to process.
|
|---|
| 425 | */
|
|---|
| 426 | protected void
|
|---|
| 427 | processEvent(AWTEvent event)
|
|---|
| 428 | {
|
|---|
| 429 | if (event instanceof ActionEvent)
|
|---|
| 430 | processActionEvent((ActionEvent)event);
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | /*************************************************************************/
|
|---|
| 434 |
|
|---|
| 435 | /**
|
|---|
| 436 | * Processes the specified event by dispatching it to any registered listeners.
|
|---|
| 437 | *
|
|---|
| 438 | * @param event The event to process.
|
|---|
| 439 | */
|
|---|
| 440 | protected void
|
|---|
| 441 | processActionEvent(ActionEvent event)
|
|---|
| 442 | {
|
|---|
| 443 | if (action_listeners != null)
|
|---|
| 444 | action_listeners.actionPerformed(event);
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | /*************************************************************************/
|
|---|
| 448 |
|
|---|
| 449 | /**
|
|---|
| 450 | * Returns a debugging string for this object.
|
|---|
| 451 | *
|
|---|
| 452 | * @return A debugging string for this object.
|
|---|
| 453 | */
|
|---|
| 454 | public String
|
|---|
| 455 | paramString()
|
|---|
| 456 | {
|
|---|
| 457 | return ("label=" + label + ",enabled=" + enabled +
|
|---|
| 458 | ",actionCommand=" + actionCommand + "," + super.paramString());
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | // Accessibility API not yet implemented.
|
|---|
| 462 | // public AccessibleContext getAccessibleContext()
|
|---|
| 463 |
|
|---|
| 464 | } // class MenuItem
|
|---|