1 /* MenuItem.java -- An item in a menu
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 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
.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
;
49 * This class represents an item in a menu.
51 * @author Aaron M. Renn (arenn@urbanophile.com)
53 public class MenuItem
extends MenuComponent
54 implements Serializable
57 // FIXME: The enabled event mask is not used at this time.
63 // Serialization Constant
64 private static final long serialVersionUID
= -21757335363267194L;
66 /*************************************************************************/
73 * @serial The name of the action command generated by this item.
75 private String actionCommand
;
78 * @serial Indicates whether or not this menu item is enabled.
80 private boolean enabled
;
83 * @serial The mask of events that are enabled for this menu item.
88 * @serial This menu item's label
93 * @serial The shortcut for this menu item, if any
95 private MenuShortcut shortcut
;
97 // The list of action listeners for this menu item.
98 private transient ActionListener action_listeners
;
100 /*************************************************************************/
107 * Initializes a new instance of <code>MenuItem</code> with no label
115 /*************************************************************************/
118 * Initializes a new instance of <code>MenuItem</code> with the specified
119 * label and no shortcut.
121 * @param label The label for this menu item.
124 MenuItem(String label
)
129 /*************************************************************************/
132 * Initializes a new instance of <code>MenuItem</code> with the specified
133 * label and shortcut.
135 * @param label The label for this menu item.
136 * @param shortcut The shortcut for this menu item.
139 MenuItem(String label
, MenuShortcut shortcut
)
142 this.shortcut
= shortcut
;
145 /*************************************************************************/
152 * Returns the label for this menu item, which may be <code>null</code>.
154 * @return The label for this menu item.
162 /*************************************************************************/
165 * This method sets the label for this menu to the specified value.
167 * @param label The new label for this menu item.
169 public synchronized void
170 setLabel(String label
)
175 MenuItemPeer mp
= (MenuItemPeer
) peer
;
180 /*************************************************************************/
183 * Tests whether or not this menu item is enabled.
185 * @return <code>true</code> if this menu item is enabled, <code>false</code>
194 /*************************************************************************/
197 * Sets the enabled status of this menu item.
199 * @param enabled <code>true</code> to enable this menu item,
200 * <code>false</code> otherwise.
202 public synchronized void
203 setEnabled(boolean enabled
)
205 if (enabled
== this.enabled
)
208 this.enabled
= enabled
;
211 MenuItemPeer mp
= (MenuItemPeer
) peer
;
212 mp
.setEnabled (enabled
);
216 /*************************************************************************/
219 * Sets the enabled status of this menu item.
221 * @param enabled <code>true</code> to enable this menu item,
222 * <code>false</code> otherwise.
224 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
227 enable(boolean enabled
)
232 /*************************************************************************/
235 * Enables this menu item.
237 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
245 /*************************************************************************/
248 * Disables this menu item.
250 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
258 /*************************************************************************/
261 * Returns the shortcut for this menu item, which may be <code>null</code>.
263 * @return The shortcut for this menu item.
271 /*************************************************************************/
274 * Sets the shortcut for this menu item to the specified value. This
275 * must be done before the native peer is created.
277 * @param shortcut The new shortcut for this menu item.
280 setShortcut(MenuShortcut shortcut
)
282 this.shortcut
= shortcut
;
285 /*************************************************************************/
288 * Deletes the shortcut for this menu item if one exists. This must be
289 * done before the native peer is created.
297 /*************************************************************************/
300 * Returns the name of the action command in the action events
301 * generated by this menu item.
303 * @return The action command name
308 return(actionCommand
);
311 /*************************************************************************/
314 * Sets the name of the action command in the action events generated by
317 * @param actionCommand The new action command name.
320 setActionCommand(String actionCommand
)
322 this.actionCommand
= actionCommand
;
325 /*************************************************************************/
328 * Enables the specified events. This is done automatically when a
329 * listener is added and does not normally need to be done by
332 * @param events The events to enable, which should be the bit masks
333 * from <code>AWTEvent</code>.
336 enableEvents(long events
)
339 // TODO: see comment in Component.enableEvents().
342 /*************************************************************************/
345 * Disables the specified events.
347 * @param events The events to enable, which should be the bit masks
348 * from <code>AWTEvent</code>.
351 disableEvents(long events
)
353 eventMask
&= ~events
;
356 /*************************************************************************/
359 * Creates the native peer for this object.
365 peer
= getToolkit ().createMenuItem (this);
368 /*************************************************************************/
371 * Adds the specified listener to the list of registered action listeners
372 * for this component.
374 * @param listener The listener to add.
376 public synchronized void
377 addActionListener(ActionListener listener
)
379 action_listeners
= AWTEventMulticaster
.add(action_listeners
, listener
);
381 enableEvents(AWTEvent
.ACTION_EVENT_MASK
);
384 public synchronized void
385 removeActionListener(ActionListener l
)
387 action_listeners
= AWTEventMulticaster
.remove(action_listeners
, l
);
390 public synchronized ActionListener
[] getActionListeners()
392 return (ActionListener
[])
393 AWTEventMulticaster
.getListeners(action_listeners
,
394 ActionListener
.class);
397 /** Returns all registered EventListers of the given listenerType.
398 * listenerType must be a subclass of EventListener, or a
399 * ClassClassException is thrown.
402 public EventListener
[] getListeners(Class listenerType
)
404 if (listenerType
== ActionListener
.class)
405 return getActionListeners();
406 return (EventListener
[]) Array
.newInstance(listenerType
, 0);
409 /*************************************************************************/
412 dispatchEventImpl(AWTEvent e
)
414 if (e
.id
<= ActionEvent
.ACTION_LAST
415 && e
.id
>= ActionEvent
.ACTION_FIRST
416 && (action_listeners
!= null
417 || (eventMask
& AWTEvent
.ACTION_EVENT_MASK
) != 0))
422 * Processes the specified event by calling <code>processActionEvent()</code>
423 * if it is an instance of <code>ActionEvent</code>.
425 * @param event The event to process.
428 processEvent(AWTEvent event
)
430 if (event
instanceof ActionEvent
)
431 processActionEvent((ActionEvent
)event
);
434 /*************************************************************************/
437 * Processes the specified event by dispatching it to any registered listeners.
439 * @param event The event to process.
442 processActionEvent(ActionEvent event
)
444 if (action_listeners
!= null)
445 action_listeners
.actionPerformed(event
);
448 /*************************************************************************/
451 * Returns a debugging string for this object.
453 * @return A debugging string for this object.
458 return ("label=" + label
+ ",enabled=" + enabled
+
459 ",actionCommand=" + actionCommand
+ "," + super.paramString());
462 // Accessibility API not yet implemented.
463 // public AccessibleContext getAccessibleContext()