source: trunk/gcc/libjava/java/awt/MenuItem.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 11.1 KB
Line 
1/* MenuItem.java -- An item in a menu
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.peer.MenuItemPeer;
42import java.awt.peer.MenuComponentPeer;
43import java.awt.event.ActionEvent;
44import java.awt.event.ActionListener;
45import java.lang.reflect.Array;
46import java.util.EventListener;
47
48/**
49 * This class represents an item in a menu.
50 *
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 */
53public class MenuItem extends MenuComponent implements java.io.Serializable
54{
55
56// FIXME: The enabled event mask is not used at this time.
57
58/*
59 * Static Variables
60 */
61
62// Serialization Constant
63private static final long serialVersionUID = -21757335363267194L;
64
65/*************************************************************************/
66
67/*
68 * Instance Variables
69 */
70
71/**
72 * @serial The name of the action command generated by this item.
73 */
74private String actionCommand;
75
76/**
77 * @serial Indicates whether or not this menu item is enabled.
78 */
79private boolean enabled;
80
81/**
82 * @serial The mask of events that are enabled for this menu item.
83 */
84long eventMask;
85
86/**
87 * @serial This menu item's label
88 */
89private String label;
90
91/**
92 * @serial The shortcut for this menu item, if any
93 */
94private MenuShortcut shortcut;
95
96// The list of action listeners for this menu item.
97private transient ActionListener action_listeners;
98
99/*************************************************************************/
100
101/*
102 * Constructors
103 */
104
105/**
106 * Initializes a new instance of <code>MenuItem</code> with no label
107 * and no shortcut.
108 */
109public
110MenuItem()
111{
112}
113
114/*************************************************************************/
115
116/**
117 * Initializes a new instance of <code>MenuItem</code> with the specified
118 * label and no shortcut.
119 *
120 * @param label The label for this menu item.
121 */
122public
123MenuItem(String label)
124{
125 this.label = label;
126}
127
128/*************************************************************************/
129
130/**
131 * Initializes a new instance of <code>MenuItem</code> with the specified
132 * label and shortcut.
133 *
134 * @param label The label for this menu item.
135 * @param shortcut The shortcut for this menu item.
136 */
137public
138MenuItem(String label, MenuShortcut shortcut)
139{
140 this.label = label;
141 this.shortcut = shortcut;
142}
143
144/*************************************************************************/
145
146/*
147 * Instance Methods
148 */
149
150/**
151 * Returns the label for this menu item, which may be <code>null</code>.
152 *
153 * @return The label for this menu item.
154 */
155public String
156getLabel()
157{
158 return(label);
159}
160
161/*************************************************************************/
162
163/**
164 * This method sets the label for this menu to the specified value.
165 *
166 * @param label The new label for this menu item.
167 */
168public synchronized void
169setLabel(String label)
170{
171 this.label = label;
172 if (peer != null)
173 {
174 MenuItemPeer mp = (MenuItemPeer) peer;
175 mp.setLabel (label);
176 }
177}
178
179/*************************************************************************/
180
181/**
182 * Tests whether or not this menu item is enabled.
183 *
184 * @return <code>true</code> if this menu item is enabled, <code>false</code>
185 * otherwise.
186 */
187public boolean
188isEnabled()
189{
190 return(enabled);
191}
192
193/*************************************************************************/
194
195/**
196 * Sets the enabled status of this menu item.
197 *
198 * @param enabled <code>true</code> to enable this menu item,
199 * <code>false</code> otherwise.
200 */
201public synchronized void
202setEnabled(boolean enabled)
203{
204 if (enabled == this.enabled)
205 return;
206
207 this.enabled = enabled;
208 if (peer != null)
209 {
210 MenuItemPeer mp = (MenuItemPeer) peer;
211 mp.setEnabled (enabled);
212 }
213}
214
215/*************************************************************************/
216
217/**
218 * Sets the enabled status of this menu item.
219 *
220 * @param enabled <code>true</code> to enable this menu item,
221 * <code>false</code> otherwise.
222 *
223 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
224 */
225public void
226enable(boolean enabled)
227{
228 setEnabled(enabled);
229}
230
231/*************************************************************************/
232
233/**
234 * Enables this menu item.
235 *
236 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
237 */
238public void
239enable()
240{
241 setEnabled(true);
242}
243
244/*************************************************************************/
245
246/**
247 * Disables this menu item.
248 *
249 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
250 */
251public void
252disable()
253{
254 setEnabled(false);
255}
256
257/*************************************************************************/
258
259/**
260 * Returns the shortcut for this menu item, which may be <code>null</code>.
261 *
262 * @return The shortcut for this menu item.
263 */
264public MenuShortcut
265getShortcut()
266{
267 return(shortcut);
268}
269
270/*************************************************************************/
271
272/**
273 * Sets the shortcut for this menu item to the specified value. This
274 * must be done before the native peer is created.
275 *
276 * @param shortcut The new shortcut for this menu item.
277 */
278public void
279setShortcut(MenuShortcut shortcut)
280{
281 this.shortcut = shortcut;
282}
283
284/*************************************************************************/
285
286/**
287 * Deletes the shortcut for this menu item if one exists. This must be
288 * done before the native peer is created.
289 */
290public void
291deleteShortcut()
292{
293 shortcut = null;
294}
295
296/*************************************************************************/
297
298/**
299 * Returns the name of the action command in the action events
300 * generated by this menu item.
301 *
302 * @return The action command name
303 */
304public String
305getActionCommand()
306{
307 return(actionCommand);
308}
309
310/*************************************************************************/
311
312/**
313 * Sets the name of the action command in the action events generated by
314 * this menu item.
315 *
316 * @param actionCommand The new action command name.
317 */
318public void
319setActionCommand(String actionCommand)
320{
321 this.actionCommand = actionCommand;
322}
323
324/*************************************************************************/
325
326/**
327 * Enables the specified events. This is done automatically when a
328 * listener is added and does not normally need to be done by
329 * application code.
330 *
331 * @param events The events to enable, which should be the bit masks
332 * from <code>AWTEvent</code>.
333 */
334protected final void
335enableEvents(long events)
336{
337 eventMask |= events;
338 // TODO: see comment in Component.enableEvents().
339}
340
341/*************************************************************************/
342
343/**
344 * Disables the specified events.
345 *
346 * @param events The events to enable, which should be the bit masks
347 * from <code>AWTEvent</code>.
348 */
349protected final void
350disableEvents(long events)
351{
352 eventMask &= ~events;
353}
354
355/*************************************************************************/
356
357/**
358 * Creates the native peer for this object.
359 */
360public void
361addNotify()
362{
363 if (peer != null)
364 peer = getToolkit ().createMenuItem (this);
365}
366
367/*************************************************************************/
368
369/**
370 * Adds the specified listener to the list of registered action listeners
371 * for this component.
372 *
373 * @param listener The listener to add.
374 */
375public synchronized void
376addActionListener(ActionListener listener)
377{
378 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
379
380 enableEvents(AWTEvent.ACTION_EVENT_MASK);
381}
382
383public synchronized void
384removeActionListener(ActionListener l)
385{
386 action_listeners = AWTEventMulticaster.remove(action_listeners, l);
387}
388
389 public synchronized ActionListener[] getActionListeners()
390 {
391 return (ActionListener[])
392 AWTEventMulticaster.getListeners(action_listeners,
393 ActionListener.class);
394 }
395
396/** Returns all registered EventListers of the given listenerType.
397 * listenerType must be a subclass of EventListener, or a
398 * ClassClassException is thrown.
399 * @since 1.3
400 */
401 public EventListener[] getListeners(Class listenerType)
402 {
403 if (listenerType == ActionListener.class)
404 return getActionListeners();
405 return (EventListener[]) Array.newInstance(listenerType, 0);
406 }
407
408/*************************************************************************/
409
410void
411dispatchEventImpl(AWTEvent e)
412{
413 if (e.id <= ActionEvent.ACTION_LAST
414 && e.id >= ActionEvent.ACTION_FIRST
415 && (action_listeners != null
416 || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
417 processEvent(e);
418}
419
420/**
421 * Processes the specified event by calling <code>processActionEvent()</code>
422 * if it is an instance of <code>ActionEvent</code>.
423 *
424 * @param event The event to process.
425 */
426protected void
427processEvent(AWTEvent event)
428{
429 if (event instanceof ActionEvent)
430 processActionEvent((ActionEvent)event);
431}
432
433/*************************************************************************/
434
435/**
436 * Processes the specified event by dispatching it to any registered listeners.
437 *
438 * @param event The event to process.
439 */
440protected void
441processActionEvent(ActionEvent event)
442{
443 if (action_listeners != null)
444 action_listeners.actionPerformed(event);
445}
446
447/*************************************************************************/
448
449/**
450 * Returns a debugging string for this object.
451 *
452 * @return A debugging string for this object.
453 */
454public String
455paramString()
456{
457 return ("label=" + label + ",enabled=" + enabled +
458 ",actionCommand=" + actionCommand + "," + super.paramString());
459}
460
461// Accessibility API not yet implemented.
462// public AccessibleContext getAccessibleContext()
463
464} // class MenuItem
Note: See TracBrowser for help on using the repository browser.