2003-12-04 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / libjava / java / awt / MenuItem.java
blob02c4d0d099988279a2acc6fe2b58eb3a1fcb0b6d
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)
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 /**
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.
60 * Static Variables
63 // Serialization Constant
64 private static final long serialVersionUID = -21757335363267194L;
66 /*************************************************************************/
69 * Instance Variables
72 /**
73 * @serial The name of the action command generated by this item.
75 private String actionCommand;
77 /**
78 * @serial Indicates whether or not this menu item is enabled.
80 private boolean enabled;
82 /**
83 * @serial The mask of events that are enabled for this menu item.
85 long eventMask;
87 /**
88 * @serial This menu item's label
90 private String label;
92 /**
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 /*************************************************************************/
103 * Constructors
107 * Initializes a new instance of <code>MenuItem</code> with no label
108 * and no shortcut.
110 public
111 MenuItem()
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.
123 public
124 MenuItem(String label)
126 this.label = 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.
138 public
139 MenuItem(String label, MenuShortcut shortcut)
141 this.label = label;
142 this.shortcut = shortcut;
145 /*************************************************************************/
148 * Instance Methods
152 * Returns the label for this menu item, which may be <code>null</code>.
154 * @return The label for this menu item.
156 public String
157 getLabel()
159 return(label);
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)
172 this.label = label;
173 if (peer != null)
175 MenuItemPeer mp = (MenuItemPeer) peer;
176 mp.setLabel (label);
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>
186 * otherwise.
188 public boolean
189 isEnabled()
191 return(enabled);
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)
206 return;
208 this.enabled = enabled;
209 if (peer != null)
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>.
226 public void
227 enable(boolean enabled)
229 setEnabled(enabled);
232 /*************************************************************************/
235 * Enables this menu item.
237 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
239 public void
240 enable()
242 setEnabled(true);
245 /*************************************************************************/
248 * Disables this menu item.
250 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
252 public void
253 disable()
255 setEnabled(false);
258 /*************************************************************************/
261 * Returns the shortcut for this menu item, which may be <code>null</code>.
263 * @return The shortcut for this menu item.
265 public MenuShortcut
266 getShortcut()
268 return(shortcut);
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.
279 public void
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.
291 public void
292 deleteShortcut()
294 shortcut = null;
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
305 public String
306 getActionCommand()
308 return(actionCommand);
311 /*************************************************************************/
314 * Sets the name of the action command in the action events generated by
315 * this menu item.
317 * @param actionCommand The new action command name.
319 public void
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
330 * application code.
332 * @param events The events to enable, which should be the bit masks
333 * from <code>AWTEvent</code>.
335 protected final void
336 enableEvents(long events)
338 eventMask |= 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>.
350 protected final void
351 disableEvents(long events)
353 eventMask &= ~events;
356 /*************************************************************************/
359 * Creates the native peer for this object.
361 public void
362 addNotify()
364 if (peer != null)
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.
400 * @since 1.3
402 public EventListener[] getListeners(Class listenerType)
404 if (listenerType == ActionListener.class)
405 return getActionListeners();
406 return (EventListener[]) Array.newInstance(listenerType, 0);
409 /*************************************************************************/
411 void
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))
418 processEvent(e);
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.
427 protected void
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.
441 protected void
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.
455 public String
456 paramString()
458 return ("label=" + label + ",enabled=" + enabled +
459 ",actionCommand=" + actionCommand + "," + super.paramString());
462 // Accessibility API not yet implemented.
463 // public AccessibleContext getAccessibleContext()
465 } // class MenuItem