1 /* MenuBar.java -- An AWT menu bar class
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import java.awt.peer.MenuBarPeer;
44 import java.io.Serializable;
45 import java.util.Enumeration;
46 import java.util.Vector;
48 import javax.accessibility.Accessible;
49 import javax.accessibility.AccessibleContext;
50 import javax.accessibility.AccessibleRole;
53 * This class implements a menu bar in the AWT system.
55 * @author Aaron M. Renn (arenn@urbanophile.com)
56 * @author Tom Tromey (tromey@redhat.com)
57 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
59 public class MenuBar extends MenuComponent
60 implements MenuContainer, Serializable, Accessible
63 // Serialization Constant
64 private static final long serialVersionUID = -4930327919388951260L;
67 * The number used to generate the name returned by getName.
69 private static transient long next_menubar_number;
72 * @serial The menu used for providing help information
74 private Menu helpMenu;
77 * @serial The menus contained in this menu bar.
79 private Vector menus = new Vector();
82 * Initializes a new instance of <code>MenuBar</code>.
84 * @throws HeadlessException if GraphicsEnvironment.isHeadless() is true
88 if (GraphicsEnvironment.isHeadless())
89 throw new HeadlessException();
93 * Returns the help menu for this menu bar. This may be <code>null</code>.
95 * @return the help menu for this menu bar
97 public Menu getHelpMenu()
103 * Sets the help menu for this menu bar.
105 * @param menu the new help menu for this menu bar
107 public synchronized void setHelpMenu(Menu menu)
109 MenuBarPeer myPeer = (MenuBarPeer) getPeer ();
111 if (helpMenu != null)
114 helpMenu.removeNotify();
115 helpMenu.setParent(null);
119 MenuContainer parent = menu.getParent();
122 menu.setParent(this);
127 myPeer.addHelpMenu(menu);
132 * Add a menu to this MenuBar. If the menu has already has a
133 * parent, it is first removed from its old parent before being
136 * @param menu the menu to add
138 * @return the menu that was added
140 public synchronized Menu add(Menu menu)
142 MenuBarPeer myPeer = (MenuBarPeer) getPeer ();
144 MenuContainer parent = menu.getParent();
148 menus.addElement(menu);
149 menu.setParent(this);
154 myPeer.addMenu(menu);
160 * Removes the menu at the specified index.
162 * @param index the index of the menu to remove from the menu bar
164 public synchronized void remove(int index)
166 Menu m = (Menu) menus.remove(index);
167 MenuBarPeer mp = (MenuBarPeer) getPeer();
179 * Removes the specified menu from the menu bar.
181 * @param menu the menu to remove from the menu bar
183 public void remove(MenuComponent menu)
185 int index = menus.indexOf(menu);
193 * Returns the number of elements in this menu bar.
195 * @return the number of elements in the menu bar
197 public int getMenuCount()
203 * Returns the number of elements in this menu bar.
205 * @return the number of elements in the menu bar
207 * @deprecated This method is deprecated in favor of
208 * <code>getMenuCount()</code>.
210 public int countMenus()
212 return menus.size() + (getHelpMenu() == null ? 0 : 1);
216 * Returns the menu at the specified index.
218 * @param index the index of the menu
220 * @return the requested menu
222 * @throws ArrayIndexOutOfBoundsException if the index is not valid
224 public Menu getMenu(int index)
226 return (Menu) menus.elementAt(index);
230 * Creates this object's native peer.
232 public void addNotify()
234 MenuBarPeer peer = (MenuBarPeer) getPeer();
237 peer = getToolkit().createMenuBar(this);
241 Enumeration e = menus.elements();
242 while (e.hasMoreElements())
244 Menu mi = (Menu)e.nextElement();
249 if (helpMenu != null)
251 helpMenu.addNotify();
252 peer.addHelpMenu(helpMenu);
257 * Destroys this object's native peer.
259 public void removeNotify()
261 Enumeration e = menus.elements();
262 while (e.hasMoreElements())
264 Menu mi = (Menu) e.nextElement();
267 super.removeNotify();
271 * Returns a list of all shortcuts for the menus in this menu bar.
273 * @return a list of all shortcuts for the menus in this menu bar
275 public synchronized Enumeration<MenuShortcut> shortcuts()
277 Vector shortcuts = new Vector();
278 Enumeration e = menus.elements();
280 while (e.hasMoreElements())
282 Menu menu = (Menu)e.nextElement();
283 if (menu.getShortcut() != null)
284 shortcuts.addElement(menu.getShortcut());
287 return shortcuts.elements();
291 * Returns the menu item for the specified shortcut, or <code>null</code>
292 * if no such item exists.
294 * @param shortcut the shortcut to return the menu item for
296 * @return the menu item for the specified shortcut
298 public MenuItem getShortcutMenuItem(MenuShortcut shortcut)
300 Enumeration e = menus.elements();
302 while (e.hasMoreElements())
304 Menu menu = (Menu) e.nextElement();
305 MenuShortcut s = menu.getShortcut();
306 if ((s != null) && s.equals(shortcut))
314 * Deletes the specified menu shortcut.
316 * @param shortcut the shortcut to delete
318 public void deleteShortcut(MenuShortcut shortcut)
321 // This is a slow implementation, but it probably doesn't matter.
322 while ((it = getShortcutMenuItem (shortcut)) != null)
327 * Gets the AccessibleContext associated with this <code>MenuBar</code>.
328 * The context is created, if necessary.
330 * @return the associated context
332 public AccessibleContext getAccessibleContext()
334 // Create the context if this is the first request.
335 if (accessibleContext == null)
336 accessibleContext = new AccessibleAWTMenuBar();
337 return accessibleContext;
341 * Generate a unique name for this <code>MenuBar</code>.
343 * @return A unique name for this <code>MenuBar</code>.
345 String generateName()
347 return "menubar" + getUniqueLong();
350 private static synchronized long getUniqueLong()
352 return next_menubar_number++;
356 * This class provides accessibility support for AWT menu bars.
358 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
360 protected class AccessibleAWTMenuBar
361 extends AccessibleAWTMenuComponent
365 * Compatible with JDK 1.4.2 revision 5
367 private static final long serialVersionUID = -8577604491830083815L;
370 * This is the default constructor, which simply calls the default
371 * constructor of the superclass.
373 protected AccessibleAWTMenuBar()
379 * Returns the accessible role relating to the menu bar.
381 * @return <code>AccessibleRole.MENU_BAR</code>
383 public AccessibleRole getAccessibleRole()
385 return AccessibleRole.MENU_BAR;