1 /* Menu.java -- A Java AWT Menu
2 Copyright (C) 1999, 2002, 2004, 2006 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
41 import java
.awt
.peer
.MenuPeer
;
42 import java
.io
.Serializable
;
43 import java
.util
.Enumeration
;
44 import java
.util
.Vector
;
46 import javax
.accessibility
.AccessibleContext
;
47 import javax
.accessibility
.AccessibleRole
;
50 * This class represents a pull down or tear off menu in Java's AWT.
52 * @author Aaron M. Renn (arenn@urbanophile.com)
54 public class Menu
extends MenuItem
implements MenuContainer
, Serializable
61 // Serialization Constant
62 private static final long serialVersionUID
= -8809584163345499784L;
64 /*************************************************************************/
71 * @serial The actual items in the menu
73 private Vector items
= new Vector();
76 * @serial Flag indicating whether or not this menu is a tear off
78 private boolean tearOff
;
81 * @serial Indicates whether or not this is a help menu.
83 private boolean isHelpMenu
;
86 * @serial Unused in this implementation, but present in Sun's
87 * serialization spec. Value obtained via reflection.
89 private int menuSerializedDataVersion
= 1;
91 static final transient String separatorLabel
= "-";
93 /*************************************************************************/
100 * Initializes a new instance of <code>Menu</code> with no label and that
103 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
110 /*************************************************************************/
113 * Initializes a new instance of <code>Menu</code> that is not a tearoff and
114 * that has the specified label.
116 * @param label The menu label.
118 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
126 /*************************************************************************/
129 * Initializes a new instance of <code>Menu</code> with the specified
130 * label and tearoff status.
132 * @param label The label for this menu
133 * @param isTearOff <code>true</code> if this menu is a tear off menu,
134 * <code>false</code> otherwise.
136 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
139 Menu(String label
, boolean isTearOff
)
145 if (label
.equals("Help"))
148 if (GraphicsEnvironment
.isHeadless())
149 throw new HeadlessException ();
152 /*************************************************************************/
159 * Tests whether or not this menu is a tearoff.
161 * @return <code>true</code> if this menu is a tearoff, <code>false</code>
170 /*************************************************************************/
173 * Returns the number of items in this menu.
175 * @return The number of items in this menu.
180 return countItems ();
184 * Returns the number of items in this menu.
186 * @return The number of items in this menu.
188 * @deprecated As of JDK 1.1, replaced by getItemCount().
190 public int countItems ()
192 return items
.size ();
195 /*************************************************************************/
198 * Returns the item at the specified index.
200 * @return The item at the specified index.
202 * @exception ArrayIndexOutOfBoundsException If the index value is not valid.
207 return((MenuItem
)items
.elementAt(index
));
210 /*************************************************************************/
213 * Adds the specified item to this menu. If it was previously part of
214 * another menu, it is first removed from that menu.
216 * @param item The new item to add.
218 * @return The item that was added.
223 MenuContainer parent
= item
.getParent();
227 items
.addElement(item
);
228 item
.setParent(this);
233 MenuPeer mp
= (MenuPeer
) peer
;
240 /*************************************************************************/
243 * Add an item with the specified label to this menu.
245 * @param label The label of the menu item to add.
250 add(new MenuItem(label
));
253 /*************************************************************************/
256 * Inserts the specified menu item into this menu at the specified index.
258 * @param item The menu item to add.
259 * @param index The index of the menu item.
261 * @exception IllegalArgumentException If the index is less than zero.
262 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
265 insert(MenuItem item
, int index
)
268 throw new IllegalArgumentException("Index is less than zero");
270 int count
= getItemCount ();
276 MenuContainer parent
= item
.getParent();
280 items
.insertElementAt(item
, index
);
281 item
.setParent(this);
283 MenuPeer peer
= (MenuPeer
) getPeer();
287 for (int i
= count
- 1; i
>= index
; i
--)
293 for (int i
= index
; i
< count
; i
++)
294 peer
.addItem((MenuItem
) items
.elementAt (i
));
299 /*************************************************************************/
302 * Inserts an item with the specified label into this menu at the specified index.
304 * @param label The label of the item to add.
305 * @param index The index of the menu item.
307 * @exception IllegalArgumentException If the index is less than zero.
308 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
311 insert(String label
, int index
)
313 insert(new MenuItem(label
), index
);
316 /*************************************************************************/
319 * Adds a separator bar at the current menu location.
324 add(new MenuItem(separatorLabel
));
327 /*************************************************************************/
330 * Inserts a separator bar at the specified index value.
332 * @param index The index at which to insert a separator bar.
334 * @exception IllegalArgumentException If the index is less than zero.
335 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
338 insertSeparator(int index
)
340 insert(new MenuItem(separatorLabel
), index
);
343 /*************************************************************************/
346 * Deletes the item at the specified index from this menu.
348 * @param index The index of the item to remove.
350 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
352 public synchronized void
355 MenuItem item
= (MenuItem
) items
.remove(index
);
357 MenuPeer mp
= (MenuPeer
) getPeer();
363 item
.setParent(null);
366 /*************************************************************************/
369 * Removes the specifed item from the menu. If the specified component
370 * does not exist, this method does nothing.
372 * @param item The component to remove.
375 remove(MenuComponent item
)
377 int index
= items
.indexOf(item
);
384 /*************************************************************************/
387 * Removes all the elements from this menu.
389 public synchronized void
392 int count
= getItemCount();
393 for(int i
= 0; i
< count
; i
++)
395 // We must always remove item 0.
400 /*************************************************************************/
403 * Creates the native peer for this object.
408 MenuPeer peer
= (MenuPeer
) getPeer();
411 peer
= getToolkit().createMenu(this);
415 Enumeration e
= items
.elements();
416 while (e
.hasMoreElements())
418 MenuItem mi
= (MenuItem
)e
.nextElement();
426 /*************************************************************************/
429 * Destroys the native peer for this object.
434 Enumeration e
= items
.elements();
435 while (e
.hasMoreElements())
437 MenuItem mi
= (MenuItem
) e
.nextElement();
440 super.removeNotify();
443 /*************************************************************************/
446 * Returns a debugging string for this menu.
448 * @return A debugging string for this menu.
453 return (",tearOff=" + tearOff
+ ",isHelpMenu=" + isHelpMenu
454 + super.paramString());
458 * Basic Accessibility class for Menu. Details get provided in derived
461 protected class AccessibleAWTMenu
extends AccessibleAWTMenuItem
463 private static final long serialVersionUID
= 5228160894980069094L;
465 protected AccessibleAWTMenu()
469 public AccessibleRole
getAccessibleRole()
471 return AccessibleRole
.MENU
;
476 * Gets the AccessibleContext associated with this <code>Menu</code>.
477 * The context is created, if necessary.
479 * @return the associated context
481 public AccessibleContext
getAccessibleContext()
483 /* Create the context if this is the first request */
484 if (accessibleContext
== null)
485 accessibleContext
= new AccessibleAWTMenu();
486 return accessibleContext
;