OSDN Git Service

2002-04-16 David S. Miller <davem@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / java / awt / MenuItem.java
1 /* MenuItem.java -- An item in a menu
2    Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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.
20
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.
25
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. */
37
38
39 package java.awt;
40
41 import java.awt.peer.MenuItemPeer;
42 import java.awt.peer.MenuComponentPeer;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.util.EventListener;
46
47 /**
48   * This class represents an item in a menu.
49   *
50   * @author Aaron M. Renn (arenn@urbanophile.com)
51   */
52 public 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
62 private 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   */
73 private String actionCommand;
74
75 /**
76   * @serial Indicates whether or not this menu item is enabled.
77   */
78 private boolean enabled;
79
80 /**
81   * @serial The mask of events that are enabled for this menu item.
82   */
83 long eventMask;
84
85 /**
86   * @serial This menu item's label
87   */
88 private String label;
89
90 /**
91   * @serial The shortcut for this menu item, if any
92   */
93 private MenuShortcut shortcut;
94
95 // The list of action listeners for this menu item.
96 private 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   */
108 public
109 MenuItem()
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   */
121 public 
122 MenuItem(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   */
136 public
137 MenuItem(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   */
154 public String
155 getLabel()
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   */
167 public synchronized void
168 setLabel(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   */
186 public boolean
187 isEnabled()
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   */
200 public synchronized void
201 setEnabled(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   */
224 public void
225 enable(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   */
237 public void
238 enable()
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   */
250 public void
251 disable()
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   */
263 public MenuShortcut
264 getShortcut()
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   */
277 public void
278 setShortcut(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   */
289 public void
290 deleteShortcut()
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   */
303 public String
304 getActionCommand()
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   */
317 public void
318 setActionCommand(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   */
333 protected final void
334 enableEvents(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   */
348 protected final void
349 disableEvents(long events)
350 {
351   eventMask &= ~events;
352 }
353
354 /*************************************************************************/
355
356 /**
357   * Creates the native peer for this object.
358   */
359 public void
360 addNotify()
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   */
374 public synchronized void
375 addActionListener(ActionListener listener)
376 {
377   action_listeners = AWTEventMulticaster.add(action_listeners, listener);
378
379   enableEvents(AWTEvent.ACTION_EVENT_MASK);
380 }
381
382 public synchronized void
383 removeActionListener(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  */
393 public EventListener[]
394 getListeners(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
404 void
405 dispatchEventImpl(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   */
420 protected void
421 processEvent(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   */
434 protected void
435 processActionEvent(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   */
448 public String
449 paramString()
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