Merge from mainline.
[official-gcc.git] / libjava / classpath / java / awt / MenuItem.java
bloba7ac79643be49e0252faf0786db608d03ab395dc
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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. */
39 package java.awt;
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;
54 /**
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
64 * Static Variables
67 // Serialization Constant
68 private static final long serialVersionUID = -21757335363267194L;
70 /*************************************************************************/
73 * Instance Variables
76 /**
77 * @serial The name of the action command generated by this item.
78 * This is package-private to avoid an accessor method.
80 String actionCommand;
82 /**
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;
88 /**
89 * @serial The mask of events that are enabled for this menu item.
91 long eventMask;
93 /**
94 * @serial This menu item's label
95 * This is package-private to avoid an accessor method.
97 String label = "";
99 /**
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;
113 /** Constructor */
114 protected AccessibleAWTMenuItem()
116 super();
121 public String getAccessibleName()
123 return label;
126 public AccessibleAction getAccessibleAction()
128 return this;
131 public AccessibleRole getAccessibleRole()
133 return AccessibleRole.MENU_ITEM;
136 /* (non-Javadoc)
137 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
139 public int getAccessibleActionCount()
141 return 1;
144 /* (non-Javadoc)
145 * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
147 public String getAccessibleActionDescription(int i)
149 if (i == 0)
150 return label;
151 else
152 return null;
155 /* (non-Javadoc)
156 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
158 public boolean doAccessibleAction(int i)
160 if (i != 0)
161 return false;
162 processActionEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
163 return true;
166 public AccessibleValue getAccessibleValue()
168 return this;
171 /* (non-Javadoc)
172 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
174 public Number getCurrentAccessibleValue()
176 return (enabled) ? new Integer(1) : new Integer(0);
179 /* (non-Javadoc)
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);
188 return result;
191 /* (non-Javadoc)
192 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
194 public Number getMinimumAccessibleValue()
196 return new Integer(0);
199 /* (non-Javadoc)
200 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
202 public Number getMaximumAccessibleValue()
204 return new Integer(0);
210 /*************************************************************************/
213 * Constructors
217 * Initializes a new instance of <code>MenuItem</code> with no label
218 * and no shortcut.
220 public
221 MenuItem()
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.
233 public
234 MenuItem(String label)
236 this.label = 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.
248 public
249 MenuItem(String label, MenuShortcut shortcut)
251 this.label = label;
252 this.shortcut = shortcut;
255 /*************************************************************************/
258 * Instance Methods
262 * Returns the label for this menu item, which may be <code>null</code>.
264 * @return The label for this menu item.
266 public String
267 getLabel()
269 return(label);
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)
282 this.label = label;
283 if (peer != null)
285 MenuItemPeer mp = (MenuItemPeer) peer;
286 mp.setLabel (label);
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>
296 * otherwise.
298 public boolean
299 isEnabled()
301 return(enabled);
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)
315 enable (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>.
328 public void
329 enable(boolean enabled)
331 if (enabled)
332 enable ();
333 else
334 disable ();
337 /*************************************************************************/
340 * Enables this menu item.
342 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
344 public void
345 enable()
347 if (enabled)
348 return;
350 this.enabled = true;
351 if (peer != null)
352 ((MenuItemPeer) peer).setEnabled (true);
355 /*************************************************************************/
358 * Disables this menu item.
360 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
362 public void
363 disable()
365 if (!enabled)
366 return;
368 this.enabled = false;
369 if (peer != null)
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.
380 public MenuShortcut
381 getShortcut()
383 return(shortcut);
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.
394 public void
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.
406 public void
407 deleteShortcut()
409 shortcut = null;
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
420 public String
421 getActionCommand()
423 if (actionCommand == null)
424 return label;
425 else
426 return actionCommand;
429 /*************************************************************************/
432 * Sets the name of the action command in the action events generated by
433 * this menu item.
435 * @param actionCommand The new action command name.
437 public void
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
448 * application code.
450 * @param events The events to enable, which should be the bit masks
451 * from <code>AWTEvent</code>.
453 protected final void
454 enableEvents(long events)
456 eventMask |= 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>.
468 protected final void
469 disableEvents(long events)
471 eventMask &= ~events;
474 /*************************************************************************/
477 * Creates the native peer for this object.
479 public void
480 addNotify()
482 if (peer == null)
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.
518 * @since 1.3
520 public EventListener[] getListeners(Class listenerType)
522 if (listenerType == ActionListener.class)
523 return getActionListeners();
524 return (EventListener[]) Array.newInstance(listenerType, 0);
527 /*************************************************************************/
529 void
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))
536 processEvent(e);
538 // Send the event to the parent menu if it has not yet been
539 // consumed.
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.
550 protected void
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.
564 protected void
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.
581 public String
582 paramString()
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;
602 } // class MenuItem