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
58 * The number used to generate the name returned by getName.
60 private static transient long next_menu_number;
62 // Serialization Constant
63 private static final long serialVersionUID = -8809584163345499784L;
66 * @serial The actual items in the menu
68 private Vector items = new Vector();
71 * @serial Flag indicating whether or not this menu is a tear off
73 private boolean tearOff;
76 * @serial Indicates whether or not this is a help menu.
78 private boolean isHelpMenu;
81 * @serial Unused in this implementation, but present in Sun's
82 * serialization spec. Value obtained via reflection.
84 private int menuSerializedDataVersion = 1;
86 static final transient String separatorLabel = "-";
89 * Initializes a new instance of <code>Menu</code> with no label and that
92 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
99 * Initializes a new instance of <code>Menu</code> that is not a tearoff and
100 * that has the specified label.
102 * @param label The menu label.
104 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
106 public Menu(String label)
112 * Initializes a new instance of <code>Menu</code> with the specified
113 * label and tearoff status.
115 * @param label The label for this menu
116 * @param isTearOff <code>true</code> if this menu is a tear off menu,
117 * <code>false</code> otherwise.
119 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
121 public Menu(String label, boolean isTearOff)
127 if (label.equals("Help"))
130 if (GraphicsEnvironment.isHeadless())
131 throw new HeadlessException();
135 * Tests whether or not this menu is a tearoff.
137 * @return <code>true</code> if this menu is a tearoff, <code>false</code>
140 public boolean isTearOff()
146 * Returns the number of items in this menu.
148 * @return The number of items in this menu.
150 public int getItemCount()
156 * Returns the number of items in this menu.
158 * @return The number of items in this menu.
160 * @deprecated As of JDK 1.1, replaced by getItemCount().
162 public int countItems()
168 * Returns the item at the specified index.
170 * @param index the item index.
172 * @return The item at the specified index.
174 * @exception ArrayIndexOutOfBoundsException If the index value is not valid.
176 public MenuItem getItem(int index)
178 return((MenuItem) items.elementAt(index));
182 * Adds the specified item to this menu. If it was previously part of
183 * another menu, it is first removed from that menu.
185 * @param item The new item to add.
187 * @return The item that was added.
189 public MenuItem add(MenuItem item)
191 MenuContainer parent = item.getParent();
195 items.addElement(item);
196 item.setParent(this);
201 MenuPeer mp = (MenuPeer) peer;
209 * Add an item with the specified label to this menu.
211 * @param label The label of the menu item to add.
213 public void add(String label)
215 add(new MenuItem(label));
219 * Inserts the specified menu item into this menu at the specified index. If
220 * the index is greater than or equal to the number of items already in the
221 * menu, the new item is added as the last item in the menu.
223 * @param item The menu item to add (<code>null</code> not permitted).
224 * @param index The index of the menu item (>= 0).
226 * @throws IllegalArgumentException if the index is less than zero.
227 * @throws NullPointerException if <code>item</code> is <code>null</code>.
229 public void insert(MenuItem item, int index)
232 throw new IllegalArgumentException("Index is less than zero");
234 int count = getItemCount();
240 MenuContainer parent = item.getParent();
244 items.insertElementAt(item, index);
245 item.setParent(this);
247 MenuPeer peer = (MenuPeer) getPeer();
251 for (int i = count - 1; i >= index; i--)
257 // bear in mind that count is the number of items *before* the new
259 for (int i = index + 1; i <= count; i++)
260 peer.addItem((MenuItem) items.elementAt(i));
266 * Inserts an item with the specified label into this menu at the specified
267 * index. If the index is greater than or equal to the number of items
268 * already in the menu, the new item is added as the last item in the menu.
270 * @param label The label of the item to add.
271 * @param index The index of the menu item (>= 0).
273 * @throws IllegalArgumentException If the index is less than zero.
275 public void insert(String label, int index)
277 insert(new MenuItem(label), index);
281 * Adds a separator bar at the current menu location.
283 public void addSeparator()
285 add(new MenuItem(separatorLabel));
289 * Inserts a separator bar at the specified index value.
291 * @param index The index at which to insert a separator bar.
293 * @exception IllegalArgumentException If the index is less than zero.
294 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
296 public void insertSeparator(int index)
298 insert(new MenuItem(separatorLabel), index);
302 * Deletes the item at the specified index from this menu.
304 * @param index The index of the item to remove.
306 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
308 public synchronized void remove(int index)
310 MenuItem item = (MenuItem) items.remove(index);
312 MenuPeer mp = (MenuPeer) getPeer();
318 item.setParent(null);
322 * Removes the specifed item from the menu. If the specified component
323 * does not exist, this method does nothing.
325 * @param item The component to remove.
327 public void remove(MenuComponent item)
329 int index = items.indexOf(item);
337 * Removes all the elements from this menu.
339 public synchronized void removeAll()
341 int count = getItemCount();
342 for(int i = 0; i < count; i++)
344 // We must always remove item 0.
350 * Creates the native peer for this object.
352 public void addNotify()
354 MenuPeer peer = (MenuPeer) getPeer();
357 peer = getToolkit().createMenu(this);
361 Enumeration e = items.elements();
362 while (e.hasMoreElements())
364 MenuItem mi = (MenuItem)e.nextElement();
373 * Destroys the native peer for this object.
375 public void removeNotify()
377 Enumeration e = items.elements();
378 while (e.hasMoreElements())
380 MenuItem mi = (MenuItem) e.nextElement();
383 super.removeNotify();
387 * Returns a debugging string for this menu.
389 * @return A debugging string for this menu.
391 public String paramString()
393 return (",tearOff=" + tearOff + ",isHelpMenu=" + isHelpMenu
394 + super.paramString());
398 * Basic Accessibility class for Menu. Details get provided in derived
401 protected class AccessibleAWTMenu extends AccessibleAWTMenuItem
403 private static final long serialVersionUID = 5228160894980069094L;
405 protected AccessibleAWTMenu()
409 public AccessibleRole getAccessibleRole()
411 return AccessibleRole.MENU;
416 * Gets the AccessibleContext associated with this <code>Menu</code>.
417 * The context is created, if necessary.
419 * @return the associated context
421 public AccessibleContext getAccessibleContext()
423 /* Create the context if this is the first request */
424 if (accessibleContext == null)
425 accessibleContext = new AccessibleAWTMenu();
426 return accessibleContext;
430 * Generate a unique name for this <code>Menu</code>.
432 * @return A unique name for this <code>Menu</code>.
434 String generateName()
436 return "menu" + getUniqueLong();
439 private static synchronized long getUniqueLong()
441 return next_menu_number++;