1 /* MenuItem.java -- An item in a menu
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.peer.MenuItemPeer;
44 import java.io.Serializable;
45 import java.lang.reflect.Array;
46 import java.util.EventListener;
48 import javax.accessibility.Accessible;
49 import javax.accessibility.AccessibleAction;
50 import javax.accessibility.AccessibleContext;
51 import javax.accessibility.AccessibleRole;
52 import javax.accessibility.AccessibleValue;
55 * This class represents an item in a menu.
57 * @author Aaron M. Renn (arenn@urbanophile.com)
59 public class MenuItem extends MenuComponent
60 implements Serializable, Accessible
69 * The number used to generate the name returned by getName.
71 private static transient long next_menuitem_number;
73 // Serialization Constant
74 private static final long serialVersionUID = - 21757335363267194L;
76 /*************************************************************************/
83 * @serial The name of the action command generated by this item.
84 * This is package-private to avoid an accessor method.
89 * @serial Indicates whether or not this menu item is enabled.
90 * This is package-private to avoid an accessor method.
92 boolean enabled = true;
95 * @serial The mask of events that are enabled for this menu item.
100 * @serial This menu item's label
101 * This is package-private to avoid an accessor method.
106 * @serial The shortcut for this menu item, if any
108 private MenuShortcut shortcut;
110 // The list of action listeners for this menu item.
111 private transient ActionListener action_listeners;
113 protected class AccessibleAWTMenuItem
114 extends MenuComponent.AccessibleAWTMenuComponent
115 implements AccessibleAction, AccessibleValue
117 private static final long serialVersionUID = -217847831945965825L;
120 protected AccessibleAWTMenuItem()
127 public String getAccessibleName()
132 public AccessibleAction getAccessibleAction()
137 public AccessibleRole getAccessibleRole()
139 return AccessibleRole.MENU_ITEM;
143 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
145 public int getAccessibleActionCount()
151 * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
153 public String getAccessibleActionDescription(int i)
162 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
164 public boolean doAccessibleAction(int i)
168 processActionEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
172 public AccessibleValue getAccessibleValue()
178 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
180 public Number getCurrentAccessibleValue()
182 return (enabled) ? new Integer(1) : new Integer(0);
186 * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
188 public boolean setCurrentAccessibleValue(Number number)
190 boolean result = (number.intValue() != 0);
191 // this. is required by javac 1.3, otherwise it is confused with
192 // MenuItem.this.setEnabled.
193 this.setEnabled(result);
198 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
200 public Number getMinimumAccessibleValue()
202 return new Integer(0);
206 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
208 public Number getMaximumAccessibleValue()
210 return new Integer(0);
216 /*************************************************************************/
223 * Initializes a new instance of <code>MenuItem</code> with no label
231 /*************************************************************************/
234 * Initializes a new instance of <code>MenuItem</code> with the specified
235 * label and no shortcut.
237 * @param label The label for this menu item.
240 MenuItem(String label)
245 /*************************************************************************/
248 * Initializes a new instance of <code>MenuItem</code> with the specified
249 * label and shortcut.
251 * @param label The label for this menu item.
252 * @param shortcut The shortcut for this menu item.
255 MenuItem(String label, MenuShortcut shortcut)
258 this.shortcut = shortcut;
261 /*************************************************************************/
268 * Returns the label for this menu item, which may be <code>null</code>.
270 * @return The label for this menu item.
278 /*************************************************************************/
281 * This method sets the label for this menu to the specified value.
283 * @param label The new label for this menu item.
285 public synchronized void
286 setLabel(String label)
291 MenuItemPeer mp = (MenuItemPeer) peer;
296 /*************************************************************************/
299 * Tests whether or not this menu item is enabled.
301 * @return <code>true</code> if this menu item is enabled, <code>false</code>
310 /*************************************************************************/
313 * Sets the enabled status of this menu item.
315 * @param enabled <code>true</code> to enable this menu item,
316 * <code>false</code> otherwise.
318 public synchronized void
319 setEnabled(boolean enabled)
324 /*************************************************************************/
327 * Sets the enabled status of this menu item.
329 * @param enabled <code>true</code> to enable this menu item,
330 * <code>false</code> otherwise.
332 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
335 enable(boolean enabled)
343 /*************************************************************************/
346 * Enables this menu item.
348 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
358 ((MenuItemPeer) peer).setEnabled (true);
361 /*************************************************************************/
364 * Disables this menu item.
366 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
374 this.enabled = false;
376 ((MenuItemPeer) peer).setEnabled (false);
379 /*************************************************************************/
382 * Returns the shortcut for this menu item, which may be <code>null</code>.
384 * @return The shortcut for this menu item.
392 /*************************************************************************/
395 * Sets the shortcut for this menu item to the specified value. This
396 * must be done before the native peer is created.
398 * @param shortcut The new shortcut for this menu item.
401 setShortcut(MenuShortcut shortcut)
403 this.shortcut = shortcut;
406 /*************************************************************************/
409 * Deletes the shortcut for this menu item if one exists. This must be
410 * done before the native peer is created.
418 /*************************************************************************/
421 * Returns the name of the action command in the action events
422 * generated by this menu item.
424 * @return The action command name
429 if (actionCommand == null)
432 return actionCommand;
435 /*************************************************************************/
438 * Sets the name of the action command in the action events generated by
441 * @param actionCommand The new action command name.
444 setActionCommand(String actionCommand)
446 this.actionCommand = actionCommand;
449 /*************************************************************************/
452 * Enables the specified events. This is done automatically when a
453 * listener is added and does not normally need to be done by
456 * @param events The events to enable, which should be the bit masks
457 * from <code>AWTEvent</code>.
460 enableEvents(long events)
463 // TODO: see comment in Component.enableEvents().
466 /*************************************************************************/
469 * Disables the specified events.
471 * @param events The events to enable, which should be the bit masks
472 * from <code>AWTEvent</code>.
475 disableEvents(long events)
477 eventMask &= ~events;
480 /*************************************************************************/
483 * Creates the native peer for this object.
489 peer = getToolkit ().createMenuItem (this);
492 /*************************************************************************/
495 * Adds the specified listener to the list of registered action listeners
496 * for this component.
498 * @param listener The listener to add.
500 public synchronized void
501 addActionListener(ActionListener listener)
503 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
505 enableEvents(AWTEvent.ACTION_EVENT_MASK);
508 public synchronized void
509 removeActionListener(ActionListener l)
511 action_listeners = AWTEventMulticaster.remove(action_listeners, l);
514 public synchronized ActionListener[] getActionListeners()
516 return (ActionListener[])
517 AWTEventMulticaster.getListeners(action_listeners,
518 ActionListener.class);
521 /** Returns all registered EventListers of the given listenerType.
522 * listenerType must be a subclass of EventListener, or a
523 * ClassClassException is thrown.
526 public <T extends EventListener> T[] getListeners(Class<T> listenerType)
528 if (listenerType == ActionListener.class)
529 return (T[]) getActionListeners();
530 return (T[]) Array.newInstance(listenerType, 0);
533 /*************************************************************************/
536 dispatchEventImpl(AWTEvent e)
538 if (e.id <= ActionEvent.ACTION_LAST
539 && e.id >= ActionEvent.ACTION_FIRST
540 && (action_listeners != null
541 || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
544 // Send the event to the parent menu if it has not yet been
546 if (!e.isConsumed ())
547 ((Menu) getParent ()).processEvent (e);
551 * Processes the specified event by calling <code>processActionEvent()</code>
552 * if it is an instance of <code>ActionEvent</code>.
554 * @param event The event to process.
557 processEvent(AWTEvent event)
559 if (event instanceof ActionEvent)
560 processActionEvent((ActionEvent)event);
563 /*************************************************************************/
566 * Processes the specified event by dispatching it to any registered listeners.
568 * @param event The event to process.
571 processActionEvent(ActionEvent event)
573 if (action_listeners != null)
575 event.setSource(this);
576 action_listeners.actionPerformed(event);
580 /*************************************************************************/
583 * Returns a debugging string for this object.
585 * @return A debugging string for this object.
590 return ("label=" + label + ",enabled=" + enabled +
591 ",actionCommand=" + actionCommand + "," + super.paramString());
595 * Gets the AccessibleContext associated with this <code>MenuItem</code>.
596 * The context is created, if necessary.
598 * @return the associated context
600 public AccessibleContext getAccessibleContext()
602 /* Create the context if this is the first request */
603 if (accessibleContext == null)
604 accessibleContext = new AccessibleAWTMenuItem();
605 return accessibleContext;
609 * Generate a unique name for this <code>MenuItem</code>.
611 * @return A unique name for this <code>MenuItem</code>.
613 String generateName()
615 return "menuitem" + getUniqueLong();
618 private static synchronized long getUniqueLong()
620 return next_menuitem_number++;