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 * @serial The menu used for providing help information
69 private Menu helpMenu
;
72 * @serial The menus contained in this menu bar.
74 private Vector menus
= new Vector();
77 * Initializes a new instance of <code>MenuBar</code>.
79 * @throws HeadlessException if GraphicsEnvironment.isHeadless() is true
83 if (GraphicsEnvironment
.isHeadless())
84 throw new HeadlessException();
88 * Returns the help menu for this menu bar. This may be <code>null</code>.
90 * @return the help menu for this menu bar
92 public Menu
getHelpMenu()
98 * Sets the help menu for this menu bar.
100 * @param menu the new help menu for this menu bar
102 public synchronized void setHelpMenu(Menu menu
)
104 MenuBarPeer myPeer
= (MenuBarPeer
) getPeer ();
106 if (helpMenu
!= null)
109 helpMenu
.removeNotify();
110 helpMenu
.setParent(null);
114 MenuContainer parent
= menu
.getParent();
117 menu
.setParent(this);
122 myPeer
.addHelpMenu(menu
);
127 * Add a menu to this MenuBar. If the menu has already has a
128 * parent, it is first removed from its old parent before being
131 * @param menu the menu to add
133 * @return the menu that was added
135 public synchronized Menu
add(Menu menu
)
137 MenuBarPeer myPeer
= (MenuBarPeer
) getPeer ();
139 MenuContainer parent
= menu
.getParent();
143 menus
.addElement(menu
);
144 menu
.setParent(this);
149 myPeer
.addMenu(menu
);
155 * Removes the menu at the specified index.
157 * @param index the index of the menu to remove from the menu bar
159 public synchronized void remove(int index
)
161 Menu m
= (Menu
) menus
.remove(index
);
162 MenuBarPeer mp
= (MenuBarPeer
) getPeer();
174 * Removes the specified menu from the menu bar.
176 * @param menu the menu to remove from the menu bar
178 public void remove(MenuComponent menu
)
180 int index
= menus
.indexOf(menu
);
188 * Returns the number of elements in this menu bar.
190 * @return the number of elements in the menu bar
192 public int getMenuCount()
198 * Returns the number of elements in this menu bar.
200 * @return the number of elements in the menu bar
202 * @deprecated This method is deprecated in favor of
203 * <code>getMenuCount()</code>.
205 public int countMenus()
207 return menus
.size() + (getHelpMenu() == null ?
0 : 1);
211 * Returns the menu at the specified index.
213 * @param index the index of the menu
215 * @return the requested menu
217 * @throws ArrayIndexOutOfBoundsException if the index is not valid
219 public Menu
getMenu(int index
)
221 return (Menu
) menus
.elementAt(index
);
225 * Creates this object's native peer.
227 public void addNotify()
229 MenuBarPeer peer
= (MenuBarPeer
) getPeer();
232 peer
= getToolkit().createMenuBar(this);
236 Enumeration e
= menus
.elements();
237 while (e
.hasMoreElements())
239 Menu mi
= (Menu
)e
.nextElement();
244 if (helpMenu
!= null)
246 helpMenu
.addNotify();
247 peer
.addHelpMenu(helpMenu
);
252 * Destroys this object's native peer.
254 public void removeNotify()
256 Enumeration e
= menus
.elements();
257 while (e
.hasMoreElements())
259 Menu mi
= (Menu
) e
.nextElement();
262 super.removeNotify();
266 * Returns a list of all shortcuts for the menus in this menu bar.
268 * @return a list of all shortcuts for the menus in this menu bar
270 public synchronized Enumeration
shortcuts()
272 Vector shortcuts
= new Vector();
273 Enumeration e
= menus
.elements();
275 while (e
.hasMoreElements())
277 Menu menu
= (Menu
)e
.nextElement();
278 if (menu
.getShortcut() != null)
279 shortcuts
.addElement(menu
.getShortcut());
282 return shortcuts
.elements();
286 * Returns the menu item for the specified shortcut, or <code>null</code>
287 * if no such item exists.
289 * @param shortcut the shortcut to return the menu item for
291 * @return the menu item for the specified shortcut
293 public MenuItem
getShortcutMenuItem(MenuShortcut shortcut
)
295 Enumeration e
= menus
.elements();
297 while (e
.hasMoreElements())
299 Menu menu
= (Menu
) e
.nextElement();
300 MenuShortcut s
= menu
.getShortcut();
301 if ((s
!= null) && s
.equals(shortcut
))
309 * Deletes the specified menu shortcut.
311 * @param shortcut the shortcut to delete
313 public void deleteShortcut(MenuShortcut shortcut
)
316 // This is a slow implementation, but it probably doesn't matter.
317 while ((it
= getShortcutMenuItem (shortcut
)) != null)
322 * Gets the AccessibleContext associated with this <code>MenuBar</code>.
323 * The context is created, if necessary.
325 * @return the associated context
327 public AccessibleContext
getAccessibleContext()
329 // Create the context if this is the first request.
330 if (accessibleContext
== null)
331 accessibleContext
= new AccessibleAWTMenuBar();
332 return accessibleContext
;
336 * This class provides accessibility support for AWT menu bars.
338 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
340 protected class AccessibleAWTMenuBar
341 extends AccessibleAWTMenuComponent
345 * Compatible with JDK 1.4.2 revision 5
347 private static final long serialVersionUID
= -8577604491830083815L;
350 * This is the default constructor, which simply calls the default
351 * constructor of the superclass.
353 protected AccessibleAWTMenuBar()
359 * Returns the accessible role relating to the menu bar.
361 * @return <code>AccessibleRole.MENU_BAR</code>
363 public AccessibleRole
getAccessibleRole()
365 return AccessibleRole
.MENU_BAR
;