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., 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
;
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
67 // Serialization Constant
68 private static final long serialVersionUID
= -21757335363267194L;
70 /*************************************************************************/
77 * @serial The name of the action command generated by this item.
79 private String actionCommand
;
82 * @serial Indicates whether or not this menu item is enabled.
84 private boolean enabled
= true;
87 * @serial The mask of events that are enabled for this menu item.
92 * @serial This menu item's label
97 * @serial The shortcut for this menu item, if any
99 private MenuShortcut shortcut
;
101 // The list of action listeners for this menu item.
102 private transient ActionListener action_listeners
;
104 protected class AccessibleAWTMenuItem
105 extends MenuComponent
.AccessibleAWTMenuComponent
106 implements AccessibleAction
, AccessibleValue
109 public AccessibleAWTMenuItem()
116 public String
getAccessibleName()
121 public AccessibleAction
getAccessibleAction()
126 public AccessibleRole
getAccessibleRole()
128 return AccessibleRole
.MENU_ITEM
;
132 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
134 public int getAccessibleActionCount()
140 * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
142 public String
getAccessibleActionDescription(int i
)
151 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
153 public boolean doAccessibleAction(int i
)
157 processActionEvent(new ActionEvent(this, ActionEvent
.ACTION_PERFORMED
, actionCommand
));
161 public AccessibleValue
getAccessibleValue()
167 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
169 public Number
getCurrentAccessibleValue()
171 return (enabled
) ?
new Integer(1) : new Integer(0);
175 * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
177 public boolean setCurrentAccessibleValue(Number number
)
179 if (number
.intValue() == 0)
190 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
192 public Number
getMinimumAccessibleValue()
194 return new Integer(0);
198 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
200 public Number
getMaximumAccessibleValue()
202 return new Integer(0);
208 /*************************************************************************/
215 * Initializes a new instance of <code>MenuItem</code> with no label
223 /*************************************************************************/
226 * Initializes a new instance of <code>MenuItem</code> with the specified
227 * label and no shortcut.
229 * @param label The label for this menu item.
232 MenuItem(String label
)
237 /*************************************************************************/
240 * Initializes a new instance of <code>MenuItem</code> with the specified
241 * label and shortcut.
243 * @param label The label for this menu item.
244 * @param shortcut The shortcut for this menu item.
247 MenuItem(String label
, MenuShortcut shortcut
)
250 this.shortcut
= shortcut
;
253 /*************************************************************************/
260 * Returns the label for this menu item, which may be <code>null</code>.
262 * @return The label for this menu item.
270 /*************************************************************************/
273 * This method sets the label for this menu to the specified value.
275 * @param label The new label for this menu item.
277 public synchronized void
278 setLabel(String label
)
283 MenuItemPeer mp
= (MenuItemPeer
) peer
;
288 /*************************************************************************/
291 * Tests whether or not this menu item is enabled.
293 * @return <code>true</code> if this menu item is enabled, <code>false</code>
302 /*************************************************************************/
305 * Sets the enabled status of this menu item.
307 * @param enabled <code>true</code> to enable this menu item,
308 * <code>false</code> otherwise.
310 public synchronized void
311 setEnabled(boolean enabled
)
316 /*************************************************************************/
319 * Sets the enabled status of this menu item.
321 * @param enabled <code>true</code> to enable this menu item,
322 * <code>false</code> otherwise.
324 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
327 enable(boolean enabled
)
335 /*************************************************************************/
338 * Enables this menu item.
340 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
350 ((MenuItemPeer
) peer
).setEnabled (true);
353 /*************************************************************************/
356 * Disables this menu item.
358 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
366 this.enabled
= false;
368 ((MenuItemPeer
) peer
).setEnabled (false);
371 /*************************************************************************/
374 * Returns the shortcut for this menu item, which may be <code>null</code>.
376 * @return The shortcut for this menu item.
384 /*************************************************************************/
387 * Sets the shortcut for this menu item to the specified value. This
388 * must be done before the native peer is created.
390 * @param shortcut The new shortcut for this menu item.
393 setShortcut(MenuShortcut shortcut
)
395 this.shortcut
= shortcut
;
398 /*************************************************************************/
401 * Deletes the shortcut for this menu item if one exists. This must be
402 * done before the native peer is created.
410 /*************************************************************************/
413 * Returns the name of the action command in the action events
414 * generated by this menu item.
416 * @return The action command name
421 if (actionCommand
== null)
424 return actionCommand
;
427 /*************************************************************************/
430 * Sets the name of the action command in the action events generated by
433 * @param actionCommand The new action command name.
436 setActionCommand(String actionCommand
)
438 this.actionCommand
= actionCommand
;
441 /*************************************************************************/
444 * Enables the specified events. This is done automatically when a
445 * listener is added and does not normally need to be done by
448 * @param events The events to enable, which should be the bit masks
449 * from <code>AWTEvent</code>.
452 enableEvents(long events
)
455 // TODO: see comment in Component.enableEvents().
458 /*************************************************************************/
461 * Disables the specified events.
463 * @param events The events to enable, which should be the bit masks
464 * from <code>AWTEvent</code>.
467 disableEvents(long events
)
469 eventMask
&= ~events
;
472 /*************************************************************************/
475 * Creates the native peer for this object.
481 peer
= getToolkit ().createMenuItem (this);
484 /*************************************************************************/
487 * Adds the specified listener to the list of registered action listeners
488 * for this component.
490 * @param listener The listener to add.
492 public synchronized void
493 addActionListener(ActionListener listener
)
495 action_listeners
= AWTEventMulticaster
.add(action_listeners
, listener
);
497 enableEvents(AWTEvent
.ACTION_EVENT_MASK
);
500 public synchronized void
501 removeActionListener(ActionListener l
)
503 action_listeners
= AWTEventMulticaster
.remove(action_listeners
, l
);
506 public synchronized ActionListener
[] getActionListeners()
508 return (ActionListener
[])
509 AWTEventMulticaster
.getListeners(action_listeners
,
510 ActionListener
.class);
513 /** Returns all registered EventListers of the given listenerType.
514 * listenerType must be a subclass of EventListener, or a
515 * ClassClassException is thrown.
518 public EventListener
[] getListeners(Class listenerType
)
520 if (listenerType
== ActionListener
.class)
521 return getActionListeners();
522 return (EventListener
[]) Array
.newInstance(listenerType
, 0);
525 /*************************************************************************/
528 dispatchEventImpl(AWTEvent e
)
530 if (e
.id
<= ActionEvent
.ACTION_LAST
531 && e
.id
>= ActionEvent
.ACTION_FIRST
532 && (action_listeners
!= null
533 || (eventMask
& AWTEvent
.ACTION_EVENT_MASK
) != 0))
536 // Send the event to the parent menu if it has not yet been
538 if (!e
.isConsumed ())
539 ((Menu
) getParent ()).processEvent (e
);
543 * Processes the specified event by calling <code>processActionEvent()</code>
544 * if it is an instance of <code>ActionEvent</code>.
546 * @param event The event to process.
549 processEvent(AWTEvent event
)
551 if (event
instanceof ActionEvent
)
552 processActionEvent((ActionEvent
)event
);
555 /*************************************************************************/
558 * Processes the specified event by dispatching it to any registered listeners.
560 * @param event The event to process.
563 processActionEvent(ActionEvent event
)
565 if (action_listeners
!= null)
567 event
.setSource(this);
568 action_listeners
.actionPerformed(event
);
572 /*************************************************************************/
575 * Returns a debugging string for this object.
577 * @return A debugging string for this object.
582 return ("label=" + label
+ ",enabled=" + enabled
+
583 ",actionCommand=" + actionCommand
+ "," + super.paramString());
587 * Gets the AccessibleContext associated with this <code>MenuItem</code>.
588 * The context is created, if necessary.
590 * @return the associated context
592 public AccessibleContext
getAccessibleContext()
594 /* Create the context if this is the first request */
595 if (accessibleContext
== null)
596 accessibleContext
= new AccessibleAWTMenuItem();
597 return accessibleContext
;