1 /* Copyright (C) 1999, 2000 Free Software Foundation
3 This file is part of libjava.
5 This software is copyrighted work licensed under the terms of the
6 Libjava License. Please consult the file "LIBJAVA_LICENSE" for
11 import java.awt.peer.MenuBarPeer;
12 import java.util.Vector;
13 import java.util.Enumeration;
14 import java.util.NoSuchElementException;
16 /** This class implements a MenuBar, such as might appear across the
18 * @author Tom Tromey <tromey@redhat.com>
19 * @date December 25, 2000
21 public class MenuBar extends MenuComponent implements MenuContainer
23 /** Create a new MenuBar. */
26 menus = new Vector ();
29 /** Add a menu to this MenuBar. If the menu has already has a
30 * parent, it is first removed from its old parent before being
32 * @param menu The menu to add.
35 public synchronized Menu add (Menu menu)
37 if (menu.parent != null)
38 menu.parent.remove (menu);
45 MenuBarPeer mp = (MenuBarPeer) peer;
52 /** This creates the component's peer. */
53 public void addNotify ()
57 // This choice of toolkit seems unsatisfying, but I'm not sure
59 peer = Toolkit.getDefaultToolkit ().createMenuBar (this);
63 /** @deprecated Use getMenuCount() instead. */
64 public int countMenus ()
66 return getMenuCount ();
69 /** Delete a keyboard shortcut.
70 * @param shortcut The short cut which should be deleted from all
71 * menus on this MenuBar.
73 public void deleteShortcut (MenuShortcut shortcut)
76 // This is a slow implementation, but it probably doesn't matter.
77 while ((it = getShortcutMenuItem (shortcut)) != null)
81 /** Returns the current Help menu. */
82 public Menu getHelpMenu ()
87 /** Returns a menu from this object.
88 * @param index Index of menu to return.
90 public Menu getMenu (int index)
92 return (Menu) menus.get (index);
95 /** Returns the number of menus on this MenuBar. */
96 public int getMenuCount ()
101 /** Returns the menu item on this MenuBar with the specified
103 * @param shortcut Shortcut to look for
105 public MenuItem getShortcutMenuItem (MenuShortcut shortcut)
107 Enumeration m = new MenuEnumeration (this);
108 while (m.hasMoreElements ())
110 MenuItem item = (MenuItem) m.nextElement ();
111 if (item.getShortcut () == shortcut)
117 /** Remove a menu from the menu bar. If the menu is specified by
118 * component (and not index), and does not exist on the menu, then
119 * the method does nothing. If the removed menu has a peer, it is
121 * @param menu The menu to remove
122 * @param index The index of the menu to remove
124 public synchronized void remove (MenuComponent menu)
126 int s = menus.size ();
127 for (int i = 0; i < s; ++i)
129 if (menus.get (i) == menu)
137 public synchronized void remove (int index)
139 Menu m = (Menu) menus.get (index);
140 menus.remove (index);
146 MenuBarPeer mp = (MenuBarPeer) peer;
151 /** Set the Help menu for this MenuBar. If a Help menu already
152 * exists, it is first removed.
153 * @param menu The new Help menu.
155 public synchronized void setHelpMenu (Menu menu)
157 if (help_menu != null)
159 help_menu.removeNotify ();
160 help_menu.parent = null;
163 if (menu.parent != null)
164 menu.parent.remove (menu);
165 if (menu.parent != null)
166 menu.parent.remove (menu);
171 MenuBarPeer mp = (MenuBarPeer) peer;
172 mp.addHelpMenu (menu);
176 /** Returns an Enumeration which lists the keyboard shortcuts
177 * associated with menu items on this MenuBar.
179 public synchronized Enumeration shortcuts ()
181 return new ShortcutEnumeration (new MenuEnumeration (this));
184 // Iterate over the items of a menu.
185 private static class MenuEnumeration implements Enumeration
187 // Enumerate over the menu's items.
189 // Enumerate over a submenu.
191 // Menubar so we can keep track of help menu too.
194 MenuEnumeration (Menu m)
198 main = m.items.elements ();
201 MenuEnumeration (MenuBar mb)
205 main = mb.menus.elements ();
208 public boolean hasMoreElements ()
212 r = sub.hasMoreElements ();
214 r = main.hasMoreElements ();
215 if (! r && menubar != null)
217 if (menubar.help_menu != null)
219 main = new MenuEnumeration (menubar.help_menu);
220 r = main.hasMoreElements ();
227 public Object nextElement () throws NoSuchElementException
231 if (! sub.hasMoreElements ())
234 return sub.nextElement ();
236 if (! main.hasMoreElements () && menubar != null
237 && menubar.help_menu != null)
239 main = new MenuEnumeration (menubar.help_menu);
243 Object r = main.nextElement ();
244 if (r instanceof Menu)
246 sub = new MenuEnumeration ((Menu) r);
255 // This is an enumeration that shadows another enumeration and
256 // returns the shortcut for each item returned. I wonder if we're
257 // only supposed to return unique shortcuts? If so then we could
258 // keep a hash table here and remove duplicates.
259 private static class ShortcutEnumeration implements Enumeration
263 ShortcutEnumeration (Enumeration back)
268 public boolean hasMoreElements ()
270 return back.hasMoreElements ();
273 public Object nextElement () throws NoSuchElementException
277 MenuItem item = (MenuItem) back.nextElement ();
278 if (item.getShortcut () != null)
279 return item.getShortcut ();
284 // We use Vector because it makes enumerating easier than ArrayList