1 /* MenuItem.java -- An item in a menu
2 Copyright (C) 1999, 2000, 2001, 2002 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., 59 Temple Place, Suite 330, 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.peer.MenuItemPeer;
42 import java.awt.peer.MenuComponentPeer;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.util.EventListener;
48 * This class represents an item in a menu.
50 * @author Aaron M. Renn (arenn@urbanophile.com)
52 public class MenuItem extends MenuComponent implements java.io.Serializable
55 // FIXME: The enabled event mask is not used at this time.
61 // Serialization Constant
62 private static final long serialVersionUID = -21757335363267194L;
64 /*************************************************************************/
71 * @serial The name of the action command generated by this item.
73 private String actionCommand;
76 * @serial Indicates whether or not this menu item is enabled.
78 private boolean enabled;
81 * @serial The mask of events that are enabled for this menu item.
86 * @serial This menu item's label
91 * @serial The shortcut for this menu item, if any
93 private MenuShortcut shortcut;
95 // The list of action listeners for this menu item.
96 private transient ActionListener action_listeners;
98 /*************************************************************************/
105 * Initializes a new instance of <code>MenuItem</code> with no label
113 /*************************************************************************/
116 * Initializes a new instance of <code>MenuItem</code> with the specified
117 * label and no shortcut.
119 * @param label The label for this menu item.
122 MenuItem(String label)
127 /*************************************************************************/
130 * Initializes a new instance of <code>MenuItem</code> with the specified
131 * label and shortcut.
133 * @param label The label for this menu item.
134 * @param shortcut The shortcut for this menu item.
137 MenuItem(String label, MenuShortcut shortcut)
140 this.shortcut = shortcut;
143 /*************************************************************************/
150 * Returns the label for this menu item, which may be <code>null</code>.
152 * @return The label for this menu item.
160 /*************************************************************************/
163 * This method sets the label for this menu to the specified value.
165 * @param label The new label for this menu item.
167 public synchronized void
168 setLabel(String label)
173 MenuItemPeer mp = (MenuItemPeer) peer;
178 /*************************************************************************/
181 * Tests whether or not this menu item is enabled.
183 * @return <code>true</code> if this menu item is enabled, <code>false</code>
192 /*************************************************************************/
195 * Sets the enabled status of this menu item.
197 * @param enabled <code>true</code> to enable this menu item,
198 * <code>false</code> otherwise.
200 public synchronized void
201 setEnabled(boolean enabled)
203 if (enabled == this.enabled)
206 this.enabled = enabled;
209 MenuItemPeer mp = (MenuItemPeer) peer;
210 mp.setEnabled (enabled);
214 /*************************************************************************/
217 * Sets the enabled status of this menu item.
219 * @param enabled <code>true</code> to enable this menu item,
220 * <code>false</code> otherwise.
222 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
225 enable(boolean enabled)
230 /*************************************************************************/
233 * Enables this menu item.
235 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
243 /*************************************************************************/
246 * Disables this menu item.
248 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
256 /*************************************************************************/
259 * Returns the shortcut for this menu item, which may be <code>null</code>.
261 * @return The shortcut for this menu item.
269 /*************************************************************************/
272 * Sets the shortcut for this menu item to the specified value. This
273 * must be done before the native peer is created.
275 * @param shortcut The new shortcut for this menu item.
278 setShortcut(MenuShortcut shortcut)
280 this.shortcut = shortcut;
283 /*************************************************************************/
286 * Deletes the shortcut for this menu item if one exists. This must be
287 * done before the native peer is created.
295 /*************************************************************************/
298 * Returns the name of the action command in the action events
299 * generated by this menu item.
301 * @return The action command name
306 return(actionCommand);
309 /*************************************************************************/
312 * Sets the name of the action command in the action events generated by
315 * @param actionCommand The new action command name.
318 setActionCommand(String actionCommand)
320 this.actionCommand = actionCommand;
323 /*************************************************************************/
326 * Enables the specified events. This is done automatically when a
327 * listener is added and does not normally need to be done by
330 * @param events The events to enable, which should be the bit masks
331 * from <code>AWTEvent</code>.
334 enableEvents(long events)
337 // TODO: see comment in Component.enableEvents().
340 /*************************************************************************/
343 * Disables the specified events.
345 * @param events The events to enable, which should be the bit masks
346 * from <code>AWTEvent</code>.
349 disableEvents(long events)
351 eventMask &= ~events;
354 /*************************************************************************/
357 * Creates the native peer for this object.
363 peer = getToolkit ().createMenuItem (this);
366 /*************************************************************************/
369 * Adds the specified listener to the list of registered action listeners
370 * for this component.
372 * @param listener The listener to add.
374 public synchronized void
375 addActionListener(ActionListener listener)
377 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
379 enableEvents(AWTEvent.ACTION_EVENT_MASK);
382 public synchronized void
383 removeActionListener(ActionListener l)
385 action_listeners = AWTEventMulticaster.remove(action_listeners, l);
388 /** Returns all registered EventListers of the given listenerType.
389 * listenerType must be a subclass of EventListener, or a
390 * ClassClassException is thrown.
393 public EventListener[]
394 getListeners(Class listenerType)
396 if (listenerType == ActionListener.class)
397 return Component.getListenersImpl(listenerType, action_listeners);
399 return Component.getListenersImpl(listenerType, null);
402 /*************************************************************************/
405 dispatchEventImpl(AWTEvent e)
407 if (e.id <= ActionEvent.ACTION_LAST
408 && e.id >= ActionEvent.ACTION_FIRST
409 && (action_listeners != null
410 || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
415 * Processes the specified event by calling <code>processActionEvent()</code>
416 * if it is an instance of <code>ActionEvent</code>.
418 * @param event The event to process.
421 processEvent(AWTEvent event)
423 if (event instanceof ActionEvent)
424 processActionEvent((ActionEvent)event);
427 /*************************************************************************/
430 * Processes the specified event by dispatching it to any registered listeners.
432 * @param event The event to process.
435 processActionEvent(ActionEvent event)
437 if (action_listeners != null)
438 action_listeners.actionPerformed(event);
441 /*************************************************************************/
444 * Returns a debugging string for this object.
446 * @return A debugging string for this object.
451 return ("label=" + label + ",enabled=" + enabled +
452 ",actionCommand=" + actionCommand + "," + super.paramString());
455 // Accessibility API not yet implemented.
456 // public AccessibleContext getAccessibleContext()