KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > MenuItem


1 /*
2  * @(#)MenuItem.java 1.88 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 import java.awt.peer.MenuItemPeer;
10 import java.awt.event.*;
11 import java.util.EventListener JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.io.ObjectInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import javax.accessibility.*;
16
17
18 /**
19  * All items in a menu must belong to the class
20  * <code>MenuItem</code>, or one of its subclasses.
21  * <p>
22  * The default <code>MenuItem</code> object embodies
23  * a simple labeled menu item.
24  * <p>
25  * This picture of a menu bar shows five menu items:
26  * <IMG SRC="doc-files/MenuBar-1.gif" alt="The following text describes this graphic."
27  * ALIGN=CENTER HSPACE=10 VSPACE=7>
28  * <br CLEAR=LEFT>
29  * The first two items are simple menu items, labeled
30  * <code>"Basic"</code> and <code>"Simple"</code>.
31  * Following these two items is a separator, which is itself
32  * a menu item, created with the label <code>"-"</code>.
33  * Next is an instance of <code>CheckboxMenuItem</code>
34  * labeled <code>"Check"</code>. The final menu item is a
35  * submenu labeled <code>"More&nbsp;Examples"</code>,
36  * and this submenu is an instance of <code>Menu</code>.
37  * <p>
38  * When a menu item is selected, AWT sends an action event to
39  * the menu item. Since the event is an
40  * instance of <code>ActionEvent</code>, the <code>processEvent</code>
41  * method examines the event and passes it along to
42  * <code>processActionEvent</code>. The latter method redirects the
43  * event to any <code>ActionListener</code> objects that have
44  * registered an interest in action events generated by this
45  * menu item.
46  * <P>
47  * Note that the subclass <code>Menu</code> overrides this behavior and
48  * does not send any event to the frame until one of its subitems is
49  * selected.
50  *
51  * @version 1.88, 05/18/04
52  * @author Sami Shaio
53  */

54 public class MenuItem extends MenuComponent JavaDoc implements Accessible {
55
56     static {
57         /* ensure that the necessary native libraries are loaded */
58     Toolkit.loadLibraries();
59         if (!GraphicsEnvironment.isHeadless()) {
60             initIDs();
61         }
62     }
63
64     /**
65      * A value to indicate whether a menu item is enabled
66      * or not. If it is enabled, <code>enabled</code> will
67      * be set to true. Else <code>enabled</code> will
68      * be set to false.
69      *
70      * @serial
71      * @see #isEnabled()
72      * @see #setEnabled(boolean)
73      */

74     boolean enabled = true;
75
76     /**
77      * <code>label</code> is the label of a menu item.
78      * It can be any string.
79      *
80      * @serial
81      * @see #getLabel()
82      * @see #setLabel(String)
83      */

84     String JavaDoc label;
85
86     /**
87      * This field indicates the command tha has been issued
88      * by a particular menu item.
89      * By default the <code>actionCommand</code>
90      * is the label of the menu item, unless it has been
91      * set using setActionCommand.
92      *
93      * @serial
94      * @see #setActionCommand(String)
95      * @see #getActionCommand()
96      */

97     String JavaDoc actionCommand;
98
99     /**
100      * The eventMask is ONLY set by subclasses via enableEvents.
101      * The mask should NOT be set when listeners are registered
102      * so that we can distinguish the difference between when
103      * listeners request events and subclasses request them.
104      *
105      * @serial
106      */

107     long eventMask;
108
109     transient ActionListener actionListener;
110
111     /**
112      * A sequence of key stokes that ia associated with
113      * a menu item.
114      * Note :in 1.1.2 you must use setActionCommand()
115      * on a menu item in order for its shortcut to
116      * work.
117      *
118      * @serial
119      * @see #getShortcut()
120      * @see #setShortcut(MenuShortcut)
121      * @see #deleteShortcut()
122      */

123     private MenuShortcut JavaDoc shortcut = null;
124
125     private static final String JavaDoc base = "menuitem";
126     private static int nameCounter = 0;
127
128     /*
129      * JDK 1.1 serialVersionUID
130      */

131     private static final long serialVersionUID = -21757335363267194L;
132
133     /**
134      * Constructs a new MenuItem with an empty label and no keyboard
135      * shortcut.
136      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
137      * returns true.
138      * @see java.awt.GraphicsEnvironment#isHeadless
139      * @since JDK1.1
140      */

141     public MenuItem() throws HeadlessException JavaDoc {
142     this("", null);
143     }
144
145     /**
146      * Constructs a new MenuItem with the specified label
147      * and no keyboard shortcut. Note that use of "-" in
148      * a label is reserved to indicate a separator between
149      * menu items. By default, all menu items except for
150      * separators are enabled.
151      * @param label the label for this menu item.
152      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
153      * returns true.
154      * @see java.awt.GraphicsEnvironment#isHeadless
155      * @since JDK1.0
156      */

157     public MenuItem(String JavaDoc label) throws HeadlessException JavaDoc {
158     this(label, null);
159     }
160
161     /**
162      * Create a menu item with an associated keyboard shortcut.
163      * Note that use of "-" in a label is reserved to indicate
164      * a separator between menu items. By default, all menu
165      * items except for separators are enabled.
166      * @param label the label for this menu item.
167      * @param s the instance of <code>MenuShortcut</code>
168      * associated with this menu item.
169      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
170      * returns true.
171      * @see java.awt.GraphicsEnvironment#isHeadless
172      * @since JDK1.1
173      */

174     public MenuItem(String JavaDoc label, MenuShortcut JavaDoc s) throws HeadlessException JavaDoc {
175     this.label = label;
176         this.shortcut = s;
177     }
178
179     /**
180      * Construct a name for this MenuComponent. Called by getName() when
181      * the name is null.
182      */

183     String JavaDoc constructComponentName() {
184         synchronized (getClass()) {
185         return base + nameCounter++;
186     }
187     }
188
189     /**
190      * Creates the menu item's peer. The peer allows us to modify the
191      * appearance of the menu item without changing its functionality.
192      */

193     public void addNotify() {
194         synchronized (getTreeLock()) {
195         if (peer == null)
196             peer = Toolkit.getDefaultToolkit().createMenuItem(this);
197     }
198     }
199
200     /**
201      * Gets the label for this menu item.
202      * @return the label of this menu item, or <code>null</code>
203                        if this menu item has no label.
204      * @see java.awt.MenuItem#setLabel
205      * @since JDK1.0
206      */

207     public String JavaDoc getLabel() {
208     return label;
209     }
210
211     /**
212      * Sets the label for this menu item to the specified label.
213      * @param label the new label, or <code>null</code> for no label.
214      * @see java.awt.MenuItem#getLabel
215      * @since JDK1.0
216      */

217     public synchronized void setLabel(String JavaDoc label) {
218     this.label = label;
219     MenuItemPeer peer = (MenuItemPeer)this.peer;
220     if (peer != null) {
221         peer.setLabel(label);
222     }
223     }
224
225     /**
226      * Checks whether this menu item is enabled.
227      * @see java.awt.MenuItem#setEnabled
228      * @since JDK1.0
229      */

230     public boolean isEnabled() {
231     return enabled;
232     }
233
234     /**
235      * Sets whether or not this menu item can be chosen.
236      * @param b if <code>true</code>, enables this menu item;
237      * if <code>false</code>, disables it.
238      * @see java.awt.MenuItem#isEnabled
239      * @since JDK1.1
240      */

241     public synchronized void setEnabled(boolean b) {
242         enable(b);
243     }
244
245     /**
246      * @deprecated As of JDK version 1.1,
247      * replaced by <code>setEnabled(boolean)</code>.
248      */

249     @Deprecated JavaDoc
250     public synchronized void enable() {
251     enabled = true;
252     MenuItemPeer peer = (MenuItemPeer)this.peer;
253     if (peer != null) {
254         peer.enable();
255     }
256     }
257
258     /**
259      * @deprecated As of JDK version 1.1,
260      * replaced by <code>setEnabled(boolean)</code>.
261      */

262     @Deprecated JavaDoc
263     public void enable(boolean b) {
264         if (b) {
265         enable();
266     } else {
267         disable();
268     }
269     }
270
271     /**
272      * @deprecated As of JDK version 1.1,
273      * replaced by <code>setEnabled(boolean)</code>.
274      */

275     @Deprecated JavaDoc
276     public synchronized void disable() {
277     enabled = false;
278     MenuItemPeer peer = (MenuItemPeer)this.peer;
279     if (peer != null) {
280         peer.disable();
281     }
282     }
283
284     /**
285      * Get the <code>MenuShortcut</code> object associated with this
286      * menu item,
287      * @return the menu shortcut associated with this menu item,
288      * or <code>null</code> if none has been specified.
289      * @see java.awt.MenuItem#setShortcut
290      * @since JDK1.1
291      */

292     public MenuShortcut JavaDoc getShortcut() {
293         return shortcut;
294     }
295
296     /**
297      * Set the <code>MenuShortcut</code> object associated with this
298      * menu item. If a menu shortcut is already associated with
299      * this menu item, it is replaced.
300      * @param s the menu shortcut to associate
301      * with this menu item.
302      * @see java.awt.MenuItem#getShortcut
303      * @since JDK1.1
304      */

305     public void setShortcut(MenuShortcut JavaDoc s) {
306         shortcut = s;
307     MenuItemPeer peer = (MenuItemPeer)this.peer;
308     if (peer != null) {
309         peer.setLabel(label);
310     }
311     }
312
313     /**
314      * Delete any <code>MenuShortcut</code> object associated
315      * with this menu item.
316      * @since JDK1.1
317      */

318     public void deleteShortcut() {
319         shortcut = null;
320     MenuItemPeer peer = (MenuItemPeer)this.peer;
321     if (peer != null) {
322         peer.setLabel(label);
323     }
324     }
325
326     /*
327      * Delete a matching MenuShortcut associated with this MenuItem.
328      * Used when iterating Menus.
329      */

330     void deleteShortcut(MenuShortcut JavaDoc s) {
331         if (s.equals(shortcut)) {
332             shortcut = null;
333             MenuItemPeer peer = (MenuItemPeer)this.peer;
334             if (peer != null) {
335                 peer.setLabel(label);
336             }
337         }
338     }
339
340     /*
341      * The main goal of this method is to post an appropriate event
342      * to the event queue when menu shortcut is pressed. However,
343      * in subclasses this method may do more than just posting
344      * an event.
345      */

346     void doMenuEvent(long when, int modifiers) {
347         Toolkit.getEventQueue().postEvent(
348             new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
349                             getActionCommand(), when, modifiers));
350     }
351
352     /*
353      * Post an ActionEvent to the target (on
354      * keydown). Returns true if there is an associated
355      * shortcut.
356      */

357     boolean handleShortcut(KeyEvent e) {
358         MenuShortcut JavaDoc s = new MenuShortcut JavaDoc(e.getKeyCode(),
359                              (e.getModifiers() & InputEvent.SHIFT_MASK) > 0);
360         if (s.equals(shortcut) && enabled) {
361             // MenuShortcut match -- issue an event on keydown.
362
if (e.getID() == KeyEvent.KEY_PRESSED) {
363                 doMenuEvent(e.getWhen(), e.getModifiers());
364             } else {
365                 // silently eat key release.
366
}
367             return true;
368     }
369         return false;
370     }
371
372     MenuItem JavaDoc getShortcutMenuItem(MenuShortcut JavaDoc s) {
373         return (s.equals(shortcut)) ? this : null;
374     }
375
376     /**
377      * Enables event delivery to this menu item for events
378      * to be defined by the specified event mask parameter
379      * <p>
380      * Since event types are automatically enabled when a listener for
381      * that type is added to the menu item, this method only needs
382      * to be invoked by subclasses of <code>MenuItem</code> which desire to
383      * have the specified event types delivered to <code>processEvent</code>
384      * regardless of whether a listener is registered.
385      *
386      * @param eventsToEnable the event mask defining the event types
387      * @see java.awt.MenuItem#processEvent
388      * @see java.awt.MenuItem#disableEvents
389      * @see java.awt.Component#enableEvents
390      * @since JDK1.1
391      */

392     protected final void enableEvents(long eventsToEnable) {
393         eventMask |= eventsToEnable;
394     newEventsOnly = true;
395     }
396
397     /**
398      * Disables event delivery to this menu item for events
399      * defined by the specified event mask parameter.
400      *
401      * @param eventsToDisable the event mask defining the event types
402      * @see java.awt.MenuItem#processEvent
403      * @see java.awt.MenuItem#enableEvents
404      * @see java.awt.Component#disableEvents
405      * @since JDK1.1
406      */

407     protected final void disableEvents(long eventsToDisable) {
408         eventMask &= ~eventsToDisable;
409     }
410
411     /**
412      * Sets the command name of the action event that is fired
413      * by this menu item.
414      * <p>
415      * By default, the action command is set to the label of
416      * the menu item.
417      * @param command the action command to be set
418      * for this menu item.
419      * @see java.awt.MenuItem#getActionCommand
420      * @since JDK1.1
421      */

422     public void setActionCommand(String JavaDoc command) {
423         actionCommand = command;
424     }
425
426     /**
427      * Gets the command name of the action event that is fired
428      * by this menu item.
429      * @see java.awt.MenuItem#setActionCommand
430      * @since JDK1.1
431      */

432     public String JavaDoc getActionCommand() {
433         return getActionCommandImpl();
434     }
435
436     // This is final so it can be called on the Toolkit thread.
437
final String JavaDoc getActionCommandImpl() {
438         return (actionCommand == null? label : actionCommand);
439     }
440
441     /**
442      * Adds the specified action listener to receive action events
443      * from this menu item.
444      * If l is null, no exception is thrown and no action is performed.
445      *
446      * @param l the action listener.
447      * @see #removeActionListener
448      * @see #getActionListeners
449      * @see java.awt.event.ActionEvent
450      * @see java.awt.event.ActionListener
451      * @since JDK1.1
452      */

453     public synchronized void addActionListener(ActionListener l) {
454     if (l == null) {
455         return;
456     }
457     actionListener = AWTEventMulticaster.add(actionListener, l);
458         newEventsOnly = true;
459     }
460
461     /**
462      * Removes the specified action listener so it no longer receives
463      * action events from this menu item.
464      * If l is null, no exception is thrown and no action is performed.
465      *
466      * @param l the action listener.
467      * @see #addActionListener
468      * @see #getActionListeners
469      * @see java.awt.event.ActionEvent
470      * @see java.awt.event.ActionListener
471      * @since JDK1.1
472      */

473     public synchronized void removeActionListener(ActionListener l) {
474     if (l == null) {
475         return;
476     }
477     actionListener = AWTEventMulticaster.remove(actionListener, l);
478     }
479
480     /**
481      * Returns an array of all the action listeners
482      * registered on this menu item.
483      *
484      * @return all of this menu item's <code>ActionListener</code>s
485      * or an empty array if no action
486      * listeners are currently registered
487      *
488      * @see #addActionListener
489      * @see #removeActionListener
490      * @see java.awt.event.ActionEvent
491      * @see java.awt.event.ActionListener
492      * @since 1.4
493      */

494     public synchronized ActionListener[] getActionListeners() {
495         return (ActionListener[])(getListeners(ActionListener.class));
496     }
497
498     /**
499      * Returns an array of all the objects currently registered
500      * as <code><em>Foo</em>Listener</code>s
501      * upon this <code>MenuItem</code>.
502      * <code><em>Foo</em>Listener</code>s are registered using the
503      * <code>add<em>Foo</em>Listener</code> method.
504      *
505      * <p>
506      * You can specify the <code>listenerType</code> argument
507      * with a class literal, such as
508      * <code><em>Foo</em>Listener.class</code>.
509      * For example, you can query a
510      * <code>MenuItem</code> <code>m</code>
511      * for its action listeners with the following code:
512      *
513      * <pre>ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));</pre>
514      *
515      * If no such listeners exist, this method returns an empty array.
516      *
517      * @param listenerType the type of listeners requested; this parameter
518      * should specify an interface that descends from
519      * <code>java.util.EventListener</code>
520      * @return an array of all objects registered as
521      * <code><em>Foo</em>Listener</code>s on this menu item,
522      * or an empty array if no such
523      * listeners have been added
524      * @exception ClassCastException if <code>listenerType</code>
525      * doesn't specify a class or interface that implements
526      * <code>java.util.EventListener</code>
527      *
528      * @see #getActionListeners
529      * @since 1.3
530      */

531     public <T extends EventListener JavaDoc> T[] getListeners(Class JavaDoc<T> listenerType) {
532     EventListener JavaDoc l = null;
533     if (listenerType == ActionListener.class) {
534         l = actionListener;
535     }
536     return AWTEventMulticaster.getListeners(l, listenerType);
537     }
538
539     /**
540      * Processes events on this menu item. If the event is an
541      * instance of <code>ActionEvent</code>, it invokes
542      * <code>processActionEvent</code>, another method
543      * defined by <code>MenuItem</code>.
544      * <p>
545      * Currently, menu items only support action events.
546      * <p>Note that if the event parameter is <code>null</code>
547      * the behavior is unspecified and may result in an
548      * exception.
549      *
550      * @param e the event
551      * @see java.awt.MenuItem#processActionEvent
552      * @since JDK1.1
553      */

554     protected void processEvent(AWTEvent JavaDoc e) {
555         if (e instanceof ActionEvent) {
556             processActionEvent((ActionEvent)e);
557         }
558     }
559
560     // REMIND: remove when filtering is done at lower level
561
boolean eventEnabled(AWTEvent JavaDoc e) {
562         if (e.id == ActionEvent.ACTION_PERFORMED) {
563             if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
564                 actionListener != null) {
565                 return true;
566             }
567             return false;
568         }
569         return super.eventEnabled(e);
570     }
571
572     /**
573      * Processes action events occurring on this menu item,
574      * by dispatching them to any registered
575      * <code>ActionListener</code> objects.
576      * This method is not called unless action events are
577      * enabled for this component. Action events are enabled
578      * when one of the following occurs:
579      * <p><ul>
580      * <li>An <code>ActionListener</code> object is registered
581      * via <code>addActionListener</code>.
582      * <li>Action events are enabled via <code>enableEvents</code>.
583      * </ul>
584      * <p>Note that if the event parameter is <code>null</code>
585      * the behavior is unspecified and may result in an
586      * exception.
587      *
588      * @param e the action event
589      * @see java.awt.event.ActionEvent
590      * @see java.awt.event.ActionListener
591      * @see java.awt.MenuItem#enableEvents
592      * @since JDK1.1
593      */

594     protected void processActionEvent(ActionEvent e) {
595         ActionListener listener = actionListener;
596         if (listener != null) {
597             listener.actionPerformed(e);
598         }
599     }
600
601     /**
602      * Returns a string representing the state of this <code>MenuItem</code>.
603      * This method is intended to be used only for debugging purposes, and the
604      * content and format of the returned string may vary between
605      * implementations. The returned string may be empty but may not be
606      * <code>null</code>.
607      *
608      * @return the parameter string of this menu item
609      */

610     public String JavaDoc paramString() {
611         String JavaDoc str = ",label=" + label;
612         if (shortcut != null) {
613             str += ",shortcut=" + shortcut;
614         }
615         return super.paramString() + str;
616     }
617
618
619     /* Serialization support.
620      */

621
622     /**
623      * Menu item serialized data version.
624      *
625      * @serial
626      */

627     private int menuItemSerializedDataVersion = 1;
628
629     /**
630      * Writes default serializable fields to stream. Writes
631      * a list of serializable <code>ActionListeners</code>
632      * as optional data. The non-serializable listeners are
633      * detected and no attempt is made to serialize them.
634      *
635      * @param s the <code>ObjectOutputStream</code> to write
636      * @serialData <code>null</code> terminated sequence of 0
637      * or more pairs; the pair consists of a <code>String</code>
638      * and an <code>Object</code>; the <code>String</code>
639      * indicates the type of object and is one of the following:
640      * <code>actionListenerK</code> indicating an
641      * <code>ActionListener</code> object
642      *
643      * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
644      * @see #readObject(ObjectInputStream)
645      */

646     private void writeObject(ObjectOutputStream JavaDoc s)
647       throws IOException JavaDoc
648     {
649       s.defaultWriteObject();
650
651       AWTEventMulticaster.save(s, actionListenerK, actionListener);
652       s.writeObject(null);
653     }
654
655     /**
656      * Reads the <code>ObjectInputStream</code> and if it
657      * isn't <code>null</code> adds a listener to receive
658      * action events fired by the <code>Menu</code> Item.
659      * Unrecognized keys or values will be ignored.
660      *
661      * @param s the <code>ObjectInputStream</code> to read
662      * @exception HeadlessException if
663      * <code>GraphicsEnvironment.isHeadless</code> returns
664      * <code>true</code>
665      * @see #removeActionListener(actionListener)
666      * @see #addActionListener(actionListener)
667      * @see #writeObject(ObjectOutputStream)
668      */

669     private void readObject(ObjectInputStream JavaDoc s)
670       throws ClassNotFoundException JavaDoc, IOException JavaDoc, HeadlessException JavaDoc
671     {
672       // HeadlessException will be thrown from MenuComponent's readObject
673
s.defaultReadObject();
674
675       Object JavaDoc keyOrNull;
676       while(null != (keyOrNull = s.readObject())) {
677     String JavaDoc key = ((String JavaDoc)keyOrNull).intern();
678
679     if (actionListenerK == key)
680       addActionListener((ActionListener)(s.readObject()));
681
682     else // skip value for unrecognized key
683
s.readObject();
684       }
685     }
686
687     /**
688      * Initialize JNI field and method IDs
689      */

690     private static native void initIDs();
691
692
693 /////////////////
694
// Accessibility support
695
////////////////
696

697     /**
698      * Gets the AccessibleContext associated with this MenuItem.
699      * For menu items, the AccessibleContext takes the form of an
700      * AccessibleAWTMenuItem.
701      * A new AccessibleAWTMenuItem instance is created if necessary.
702      *
703      * @return an AccessibleAWTMenuItem that serves as the
704      * AccessibleContext of this MenuItem
705      */

706     public AccessibleContext getAccessibleContext() {
707         if (accessibleContext == null) {
708             accessibleContext = new AccessibleAWTMenuItem();
709         }
710         return accessibleContext;
711     }
712
713     /**
714      * Inner class of MenuItem used to provide default support for
715      * accessibility. This class is not meant to be used directly by
716      * application developers, but is instead meant only to be
717      * subclassed by menu component developers.
718      * <p>
719      * This class implements accessibility support for the
720      * <code>MenuItem</code> class. It provides an implementation of the
721      * Java Accessibility API appropriate to menu item user-interface elements.
722      */

723     protected class AccessibleAWTMenuItem extends AccessibleAWTMenuComponent
724         implements AccessibleAction, AccessibleValue
725     {
726         /*
727          * JDK 1.3 serialVersionUID
728          */

729         private static final long serialVersionUID = -217847831945965825L;
730
731         /**
732          * Get the accessible name of this object.
733          *
734          * @return the localized name of the object -- can be null if this
735          * object does not have a name
736          */

737         public String JavaDoc getAccessibleName() {
738             if (accessibleName != null) {
739                 return accessibleName;
740             } else {
741                 if (getLabel() == null) {
742                     return super.getAccessibleName();
743                 } else {
744                     return getLabel();
745                 }
746             }
747         }
748
749         /**
750          * Get the role of this object.
751          *
752          * @return an instance of AccessibleRole describing the role of the
753          * object
754          */

755         public AccessibleRole getAccessibleRole() {
756             return AccessibleRole.MENU_ITEM;
757         }
758
759         /**
760          * Get the AccessibleAction associated with this object. In the
761          * implementation of the Java Accessibility API for this class,
762      * return this object, which is responsible for implementing the
763          * AccessibleAction interface on behalf of itself.
764      *
765      * @return this object
766          */

767         public AccessibleAction getAccessibleAction() {
768             return this;
769         }
770
771         /**
772          * Get the AccessibleValue associated with this object. In the
773          * implementation of the Java Accessibility API for this class,
774      * return this object, which is responsible for implementing the
775          * AccessibleValue interface on behalf of itself.
776      *
777      * @return this object
778          */

779         public AccessibleValue getAccessibleValue() {
780             return this;
781         }
782
783         /**
784          * Returns the number of Actions available in this object. The
785          * default behavior of a menu item is to have one action.
786          *
787          * @return 1, the number of Actions in this object
788          */

789         public int getAccessibleActionCount() {
790             return 1;
791         }
792     
793         /**
794          * Return a description of the specified action of the object.
795          *
796          * @param i zero-based index of the actions
797          */

798         public String JavaDoc getAccessibleActionDescription(int i) {
799             if (i == 0) {
800                 // [[[PENDING: WDW -- need to provide a localized string]]]
801
return new String JavaDoc("click");
802             } else {
803                 return null;
804             }
805         }
806     
807         /**
808          * Perform the specified Action on the object
809          *
810          * @param i zero-based index of actions
811          * @return true if the action was performed; otherwise false.
812          */

813         public boolean doAccessibleAction(int i) {
814             if (i == 0) {
815                 // Simulate a button click
816
Toolkit.getEventQueue().postEvent(
817                         new ActionEvent(MenuItem.this,
818                                         ActionEvent.ACTION_PERFORMED,
819                                         MenuItem.this.getActionCommand(),
820                                         EventQueue.getMostRecentEventTime(),
821                                         0));
822                 return true;
823             } else {
824                 return false;
825             }
826         }
827
828         /**
829          * Get the value of this object as a Number.
830          *
831          * @return An Integer of 0 if this isn't selected or an Integer of 1 if
832          * this is selected.
833          * @see javax.swing.AbstractButton#isSelected()
834          */

835         public Number JavaDoc getCurrentAccessibleValue() {
836             return new Integer JavaDoc(0);
837         }
838
839         /**
840          * Set the value of this object as a Number.
841          *
842          * @return True if the value was set.
843          */

844         public boolean setCurrentAccessibleValue(Number JavaDoc n) {
845             return false;
846         }
847
848         /**
849          * Get the minimum value of this object as a Number.
850          *
851          * @return An Integer of 0.
852          */

853         public Number JavaDoc getMinimumAccessibleValue() {
854             return new Integer JavaDoc(0);
855         }
856
857         /**
858          * Get the maximum value of this object as a Number.
859          *
860          * @return An Integer of 0.
861          */

862         public Number JavaDoc getMaximumAccessibleValue() {
863             return new Integer JavaDoc(0);
864         }
865
866     } // class AccessibleAWTMenuItem
867

868 }
869
Popular Tags