Merge from the pain train
[official-gcc.git] / libjava / java / awt / MenuItem.java
blob56082d36a9a8497212bb80f25ab27af34d47021b
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.
79 private String actionCommand;
81 /**
82 * @serial Indicates whether or not this menu item is enabled.
84 private boolean enabled = true;
86 /**
87 * @serial The mask of events that are enabled for this menu item.
89 long eventMask;
91 /**
92 * @serial This menu item's label
94 private String label;
96 /**
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
108 /** Constructor */
109 public AccessibleAWTMenuItem()
111 super();
116 public String getAccessibleName()
118 return label;
121 public AccessibleAction getAccessibleAction()
123 return this;
126 public AccessibleRole getAccessibleRole()
128 return AccessibleRole.MENU_ITEM;
131 /* (non-Javadoc)
132 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
134 public int getAccessibleActionCount()
136 return 1;
139 /* (non-Javadoc)
140 * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
142 public String getAccessibleActionDescription(int i)
144 if (i == 0)
145 return label;
146 else
147 return null;
150 /* (non-Javadoc)
151 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
153 public boolean doAccessibleAction(int i)
155 if (i != 0)
156 return false;
157 processActionEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
158 return true;
161 public AccessibleValue getAccessibleValue()
163 return this;
166 /* (non-Javadoc)
167 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
169 public Number getCurrentAccessibleValue()
171 return (enabled) ? new Integer(1) : new Integer(0);
174 /* (non-Javadoc)
175 * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
177 public boolean setCurrentAccessibleValue(Number number)
179 if (number.intValue() == 0)
181 setEnabled(false);
182 return false;
185 setEnabled(true);
186 return true;
189 /* (non-Javadoc)
190 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
192 public Number getMinimumAccessibleValue()
194 return new Integer(0);
197 /* (non-Javadoc)
198 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
200 public Number getMaximumAccessibleValue()
202 return new Integer(0);
208 /*************************************************************************/
211 * Constructors
215 * Initializes a new instance of <code>MenuItem</code> with no label
216 * and no shortcut.
218 public
219 MenuItem()
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.
231 public
232 MenuItem(String label)
234 this.label = 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.
246 public
247 MenuItem(String label, MenuShortcut shortcut)
249 this.label = label;
250 this.shortcut = shortcut;
253 /*************************************************************************/
256 * Instance Methods
260 * Returns the label for this menu item, which may be <code>null</code>.
262 * @return The label for this menu item.
264 public String
265 getLabel()
267 return(label);
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)
280 this.label = label;
281 if (peer != null)
283 MenuItemPeer mp = (MenuItemPeer) peer;
284 mp.setLabel (label);
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>
294 * otherwise.
296 public boolean
297 isEnabled()
299 return(enabled);
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)
313 enable (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>.
326 public void
327 enable(boolean enabled)
329 if (enabled)
330 enable ();
331 else
332 disable ();
335 /*************************************************************************/
338 * Enables this menu item.
340 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
342 public void
343 enable()
345 if (enabled)
346 return;
348 this.enabled = true;
349 if (peer != null)
350 ((MenuItemPeer) peer).setEnabled (true);
353 /*************************************************************************/
356 * Disables this menu item.
358 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
360 public void
361 disable()
363 if (!enabled)
364 return;
366 this.enabled = false;
367 if (peer != null)
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.
378 public MenuShortcut
379 getShortcut()
381 return(shortcut);
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.
392 public void
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.
404 public void
405 deleteShortcut()
407 shortcut = null;
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
418 public String
419 getActionCommand()
421 if (actionCommand == null)
422 return label;
423 else
424 return actionCommand;
427 /*************************************************************************/
430 * Sets the name of the action command in the action events generated by
431 * this menu item.
433 * @param actionCommand The new action command name.
435 public void
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
446 * application code.
448 * @param events The events to enable, which should be the bit masks
449 * from <code>AWTEvent</code>.
451 protected final void
452 enableEvents(long events)
454 eventMask |= 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>.
466 protected final void
467 disableEvents(long events)
469 eventMask &= ~events;
472 /*************************************************************************/
475 * Creates the native peer for this object.
477 public void
478 addNotify()
480 if (peer == null)
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.
516 * @since 1.3
518 public EventListener[] getListeners(Class listenerType)
520 if (listenerType == ActionListener.class)
521 return getActionListeners();
522 return (EventListener[]) Array.newInstance(listenerType, 0);
525 /*************************************************************************/
527 void
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))
534 processEvent(e);
536 // Send the event to the parent menu if it has not yet been
537 // consumed.
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.
548 protected void
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.
562 protected void
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.
579 public String
580 paramString()
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;
600 } // class MenuItem