source: trunk/src/gcc/libjava/java/awt/MenuItem.java@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.9 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.util.EventListener;
46
47/**
48 * This class represents an item in a menu.
49 *
50 * @author Aaron M. Renn ([email protected])
51 */
52public class MenuItem extends MenuComponent implements java.io.Serializable
53{
54
55// FIXME: The enabled event mask is not used at this time.
56
57/*
58 * Static Variables
59 */
60
61// Serialization Constant
62private static final long serialVersionUID = -21757335363267194L;
63
64/*************************************************************************/
65
66/*
67 * Instance Variables
68 */
69
70/**
71 * @serial The name of the action command generated by this item.
72 */
73private String actionCommand;
74
75/**
76 * @serial Indicates whether or not this menu item is enabled.
77 */
78private boolean enabled;
79
80/**
81 * @serial The mask of events that are enabled for this menu item.
82 */
83private long eventMask;
84
85/**
86 * @serial This menu item's label
87 */
88private String label;
89
90/**
91 * @serial The shortcut for this menu item, if any
92 */
93private MenuShortcut shortcut;
94
95// The list of action listeners for this menu item.
96private transient ActionListener action_listeners;
97
98/*************************************************************************/
99
100/*
101 * Constructors
102 */
103
104/**
105 * Initializes a new instance of <code>MenuItem</code> with no label
106 * and no shortcut.
107 */
108public
109MenuItem()
110{
111}
112
113/*************************************************************************/
114
115/**
116 * Initializes a new instance of <code>MenuItem</code> with the specified
117 * label and no shortcut.
118 *
119 * @param label The label for this menu item.
120 */
121public
122MenuItem(String label)
123{
124 this.label = label;
125}
126
127/*************************************************************************/
128
129/**
130 * Initializes a new instance of <code>MenuItem</code> with the specified
131 * label and shortcut.
132 *
133 * @param label The label for this menu item.
134 * @param shortcut The shortcut for this menu item.
135 */
136public
137MenuItem(String label, MenuShortcut shortcut)
138{
139 this.label = label;
140 this.shortcut = shortcut;
141}
142
143/*************************************************************************/
144
145/*
146 * Instance Methods
147 */
148
149/**
150 * Returns the label for this menu item, which may be <code>null</code>.
151 *
152 * @return The label for this menu item.
153 */
154public String
155getLabel()
156{
157 return(label);
158}
159
160/*************************************************************************/
161
162/**
163 * This method sets the label for this menu to the specified value.
164 *
165 * @param label The new label for this menu item.
166 */
167public synchronized void
168setLabel(String label)
169{
170 this.label = label;
171 if (peer != null)
172 {
173 MenuItemPeer mp = (MenuItemPeer) peer;
174 mp.setLabel (label);
175 }
176}
177
178/*************************************************************************/
179
180/**
181 * Tests whether or not this menu item is enabled.
182 *
183 * @return <code>true</code> if this menu item is enabled, <code>false</code>
184 * otherwise.
185 */
186public boolean
187isEnabled()
188{
189 return(enabled);
190}
191
192/*************************************************************************/
193
194/**
195 * Sets the enabled status of this menu item.
196 *
197 * @param enabled <code>true</code> to enable this menu item,
198 * <code>false</code> otherwise.
199 */
200public synchronized void
201setEnabled(boolean enabled)
202{
203 if (enabled == this.enabled)
204 return;
205
206 this.enabled = enabled;
207 if (peer != null)
208 {
209 MenuItemPeer mp = (MenuItemPeer) peer;
210 mp.setEnabled (enabled);
211 }
212}
213
214/*************************************************************************/
215
216/**
217 * Sets the enabled status of this menu item.
218 *
219 * @param enabled <code>true</code> to enable this menu item,
220 * <code>false</code> otherwise.
221 *
222 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
223 */
224public void
225enable(boolean enabled)
226{
227 setEnabled(enabled);
228}
229
230/*************************************************************************/
231
232/**
233 * Enables this menu item.
234 *
235 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
236 */
237public void
238enable()
239{
240 setEnabled(true);
241}
242
243/*************************************************************************/
244
245/**
246 * Disables this menu item.
247 *
248 * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
249 */
250public void
251disable()
252{
253 setEnabled(false);
254}
255
256/*************************************************************************/
257
258/**
259 * Returns the shortcut for this menu item, which may be <code>null</code>.
260 *
261 * @return The shortcut for this menu item.
262 */
263public MenuShortcut
264getShortcut()
265{
266 return(shortcut);
267}
268
269/*************************************************************************/
270
271/**
272 * Sets the shortcut for this menu item to the specified value. This
273 * must be done before the native peer is created.
274 *
275 * @param shortcut The new shortcut for this menu item.
276 */
277public void
278setShortcut(MenuShortcut shortcut)
279{
280 this.shortcut = shortcut;
281}
282
283/*************************************************************************/
284
285/**
286 * Deletes the shortcut for this menu item if one exists. This must be
287 * done before the native peer is created.
288 */
289public void
290deleteShortcut()
291{
292 shortcut = null;
293}
294
295/*************************************************************************/
296
297/**
298 * Returns the name of the action command in the action events
299 * generated by this menu item.
300 *
301 * @return The action command name
302 */
303public String
304getActionCommand()
305{
306 return(actionCommand);
307}
308
309/*************************************************************************/
310
311/**
312 * Sets the name of the action command in the action events generated by
313 * this menu item.
314 *
315 * @param actionCommand The new action command name.
316 */
317public void
318setActionCommand(String actionCommand)
319{
320 this.actionCommand = actionCommand;
321}
322
323/*************************************************************************/
324
325/**
326 * Enables the specified events. This is done automatically when a
327 * listener is added and does not normally need to be done by
328 * application code.
329 *
330 * @param events The events to enable, which should be the bit masks
331 * from <code>AWTEvent</code>.
332 */
333protected final void
334enableEvents(long events)
335{
336 eventMask |= events;
337 // TODO: see comment in Component.enableEvents().
338}
339
340/*************************************************************************/
341
342/**
343 * Disables the specified events.
344 *
345 * @param events The events to enable, which should be the bit masks
346 * from <code>AWTEvent</code>.
347 */
348protected final void
349disableEvents(long events)
350{
351 eventMask &= ~events;
352}
353
354/*************************************************************************/
355
356/**
357 * Creates the native peer for this object.
358 */
359public void
360addNotify()
361{
362 if (peer != null)
363 peer = getToolkit ().createMenuItem (this);
364}
365
366/*************************************************************************/
367
368/**
369 * Adds the specified listener to the list of registered action listeners
370 * for this component.
371 *
372 * @param listener The listener to add.
373 */
374public synchronized void
375addActionListener(ActionListener listener)
376{
377 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
378
379 enableEvents(AWTEvent.ACTION_EVENT_MASK);
380}
381
382public synchronized void
383removeActionListener(ActionListener l)
384{
385 action_listeners = AWTEventMulticaster.remove(action_listeners, l);
386}
387
388/** Returns all registered EventListers of the given listenerType.
389 * listenerType must be a subclass of EventListener, or a
390 * ClassClassException is thrown.
391 * @since 1.3
392 */
393public EventListener[]
394getListeners(Class listenerType)
395{
396 if (listenerType == ActionListener.class)
397 return Component.getListenersImpl(listenerType, action_listeners);
398 else
399 return Component.getListenersImpl(listenerType, null);
400}
401
402/*************************************************************************/
403
404void
405dispatchEventImpl(AWTEvent e)
406{
407 if (e.id <= ActionEvent.ACTION_LAST
408 && e.id >= ActionEvent.ACTION_FIRST
409 && (action_listeners != null
410 || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
411 processEvent(e);
412}
413
414/**
415 * Processes the specified event by calling <code>processActionEvent()</code>
416 * if it is an instance of <code>ActionEvent</code>.
417 *
418 * @param event The event to process.
419 */
420protected void
421processEvent(AWTEvent event)
422{
423 if (event instanceof ActionEvent)
424 processActionEvent((ActionEvent)event);
425}
426
427/*************************************************************************/
428
429/**
430 * Processes the specified event by dispatching it to any registered listeners.
431 *
432 * @param event The event to process.
433 */
434protected void
435processActionEvent(ActionEvent event)
436{
437 if (action_listeners != null)
438 action_listeners.actionPerformed(event);
439}
440
441/*************************************************************************/
442
443/**
444 * Returns a debugging string for this object.
445 *
446 * @return A debugging string for this object.
447 */
448public String
449paramString()
450{
451 return ("label=" + label + ",enabled=" + enabled +
452 ",actionCommand=" + actionCommand + "," + super.paramString());
453}
454
455// Accessibility API not yet implemented.
456// public AccessibleContext getAccessibleContext()
457
458} // class MenuItem
Note: See TracBrowser for help on using the repository browser.