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
67 // Serialization Constant
68 private static final long serialVersionUID
= -21757335363267194L;
70 /*************************************************************************/
77 * @serial The name of the action command generated by this item.
78 * This is package-private to avoid an accessor method.
83 * @serial Indicates whether or not this menu item is enabled.
84 * This is package-private to avoid an accessor method.
86 boolean enabled
= true;
89 * @serial The mask of events that are enabled for this menu item.
94 * @serial This menu item's label
95 * This is package-private to avoid an accessor method.
100 * @serial The shortcut for this menu item, if any
102 private MenuShortcut shortcut
;
104 // The list of action listeners for this menu item.
105 private transient ActionListener action_listeners
;
107 protected class AccessibleAWTMenuItem
108 extends MenuComponent
.AccessibleAWTMenuComponent
109 implements AccessibleAction
, AccessibleValue
111 private static final long serialVersionUID
= -217847831945965825L;
114 public AccessibleAWTMenuItem()
121 public String
getAccessibleName()
126 public AccessibleAction
getAccessibleAction()
131 public AccessibleRole
getAccessibleRole()
133 return AccessibleRole
.MENU_ITEM
;
137 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
139 public int getAccessibleActionCount()
145 * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
147 public String
getAccessibleActionDescription(int i
)
156 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
158 public boolean doAccessibleAction(int i
)
162 processActionEvent(new ActionEvent(this, ActionEvent
.ACTION_PERFORMED
, actionCommand
));
166 public AccessibleValue
getAccessibleValue()
172 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
174 public Number
getCurrentAccessibleValue()
176 return (enabled
) ?
new Integer(1) : new Integer(0);
180 * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
182 public boolean setCurrentAccessibleValue(Number number
)
184 boolean result
= (number
.intValue() != 0);
185 // this. is required by javac 1.3, otherwise it is confused with
186 // MenuItem.this.setEnabled.
187 this.setEnabled(result
);
192 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
194 public Number
getMinimumAccessibleValue()
196 return new Integer(0);
200 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
202 public Number
getMaximumAccessibleValue()
204 return new Integer(0);
210 /*************************************************************************/
217 * Initializes a new instance of <code>MenuItem</code> with no label
225 /*************************************************************************/
228 * Initializes a new instance of <code>MenuItem</code> with the specified
229 * label and no shortcut.
231 * @param label The label for this menu item.
234 MenuItem(String label
)
239 /*************************************************************************/
242 * Initializes a new instance of <code>MenuItem</code> with the specified
243 * label and shortcut.
245 * @param label The label for this menu item.
246 * @param shortcut The shortcut for this menu item.
249 MenuItem(String label
, MenuShortcut shortcut
)
252 this.shortcut
= shortcut
;
255 /*************************************************************************/
262 * Returns the label for this menu item, which may be <code>null</code>.
264 * @return The label for this menu item.
272 /*************************************************************************/
275 * This method sets the label for this menu to the specified value.
277 * @param label The new label for this menu item.
279 public synchronized void
280 setLabel(String label
)
285 MenuItemPeer mp
= (MenuItemPeer
) peer
;
290 /*************************************************************************/
293 * Tests whether or not this menu item is enabled.
295 * @return <code>true</code> if this menu item is enabled, <code>false</code>
304 /*************************************************************************/
307 * Sets the enabled status of this menu item.
309 * @param enabled <code>true</code> to enable this menu item,
310 * <code>false</code> otherwise.
312 public synchronized void
313 setEnabled(boolean enabled
)
318 /*************************************************************************/
321 * Sets the enabled status of this menu item.
323 * @param enabled <code>true</code> to enable this menu item,
324 * <code>false</code> otherwise.
326 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
329 enable(boolean enabled
)
337 /*************************************************************************/
340 * Enables this menu item.
342 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
352 ((MenuItemPeer
) peer
).setEnabled (true);
355 /*************************************************************************/
358 * Disables this menu item.
360 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
368 this.enabled
= false;
370 ((MenuItemPeer
) peer
).setEnabled (false);
373 /*************************************************************************/
376 * Returns the shortcut for this menu item, which may be <code>null</code>.
378 * @return The shortcut for this menu item.
386 /*************************************************************************/
389 * Sets the shortcut for this menu item to the specified value. This
390 * must be done before the native peer is created.
392 * @param shortcut The new shortcut for this menu item.
395 setShortcut(MenuShortcut shortcut
)
397 this.shortcut
= shortcut
;
400 /*************************************************************************/
403 * Deletes the shortcut for this menu item if one exists. This must be
404 * done before the native peer is created.
412 /*************************************************************************/
415 * Returns the name of the action command in the action events
416 * generated by this menu item.
418 * @return The action command name
423 if (actionCommand
== null)
426 return actionCommand
;
429 /*************************************************************************/
432 * Sets the name of the action command in the action events generated by
435 * @param actionCommand The new action command name.
438 setActionCommand(String actionCommand
)
440 this.actionCommand
= actionCommand
;
443 /*************************************************************************/
446 * Enables the specified events. This is done automatically when a
447 * listener is added and does not normally need to be done by
450 * @param events The events to enable, which should be the bit masks
451 * from <code>AWTEvent</code>.
454 enableEvents(long events
)
457 // TODO: see comment in Component.enableEvents().
460 /*************************************************************************/
463 * Disables the specified events.
465 * @param events The events to enable, which should be the bit masks
466 * from <code>AWTEvent</code>.
469 disableEvents(long events
)
471 eventMask
&= ~events
;
474 /*************************************************************************/
477 * Creates the native peer for this object.
483 peer
= getToolkit ().createMenuItem (this);
486 /*************************************************************************/
489 * Adds the specified listener to the list of registered action listeners
490 * for this component.
492 * @param listener The listener to add.
494 public synchronized void
495 addActionListener(ActionListener listener
)
497 action_listeners
= AWTEventMulticaster
.add(action_listeners
, listener
);
499 enableEvents(AWTEvent
.ACTION_EVENT_MASK
);
502 public synchronized void
503 removeActionListener(ActionListener l
)
505 action_listeners
= AWTEventMulticaster
.remove(action_listeners
, l
);
508 public synchronized ActionListener
[] getActionListeners()
510 return (ActionListener
[])
511 AWTEventMulticaster
.getListeners(action_listeners
,
512 ActionListener
.class);
515 /** Returns all registered EventListers of the given listenerType.
516 * listenerType must be a subclass of EventListener, or a
517 * ClassClassException is thrown.
520 public EventListener
[] getListeners(Class listenerType
)
522 if (listenerType
== ActionListener
.class)
523 return getActionListeners();
524 return (EventListener
[]) Array
.newInstance(listenerType
, 0);
527 /*************************************************************************/
530 dispatchEventImpl(AWTEvent e
)
532 if (e
.id
<= ActionEvent
.ACTION_LAST
533 && e
.id
>= ActionEvent
.ACTION_FIRST
534 && (action_listeners
!= null
535 || (eventMask
& AWTEvent
.ACTION_EVENT_MASK
) != 0))
538 // Send the event to the parent menu if it has not yet been
540 if (!e
.isConsumed ())
541 ((Menu
) getParent ()).processEvent (e
);
545 * Processes the specified event by calling <code>processActionEvent()</code>
546 * if it is an instance of <code>ActionEvent</code>.
548 * @param event The event to process.
551 processEvent(AWTEvent event
)
553 if (event
instanceof ActionEvent
)
554 processActionEvent((ActionEvent
)event
);
557 /*************************************************************************/
560 * Processes the specified event by dispatching it to any registered listeners.
562 * @param event The event to process.
565 processActionEvent(ActionEvent event
)
567 if (action_listeners
!= null)
569 event
.setSource(this);
570 action_listeners
.actionPerformed(event
);
574 /*************************************************************************/
577 * Returns a debugging string for this object.
579 * @return A debugging string for this object.
584 return ("label=" + label
+ ",enabled=" + enabled
+
585 ",actionCommand=" + actionCommand
+ "," + super.paramString());
589 * Gets the AccessibleContext associated with this <code>MenuItem</code>.
590 * The context is created, if necessary.
592 * @return the associated context
594 public AccessibleContext
getAccessibleContext()
596 /* Create the context if this is the first request */
597 if (accessibleContext
== null)
598 accessibleContext
= new AccessibleAWTMenuItem();
599 return accessibleContext
;