2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / awt / MenuBar.java
blobb9ddef9aa1b0653be2be2f29523eb60a358e631e
1 /* MenuBar.java -- An AWT menu bar class
2 Copyright (C) 1999, 2000, 2001, 2002 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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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
24 combination.
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. */
39 package java.awt;
41 import java.awt.peer.MenuBarPeer;
42 import java.awt.peer.MenuComponentPeer;
44 import java.io.Serializable;
45 import java.util.Enumeration;
46 import java.util.Vector;
48 /**
49 * This class implements a menu bar in the AWT system.
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Tom Tromey <tromey@redhat.com>
54 public class MenuBar extends MenuComponent
55 implements MenuContainer, Serializable
59 * Static Variables
62 // Serialization Constant
63 private static final long serialVersionUID = -4930327919388951260L;
65 /*************************************************************************/
68 * Instance Variables
71 /**
72 * @serial The menu used for providing help information
74 private Menu helpMenu;
76 /**
77 * @serial The menus contained in this menu bar.
79 private Vector menus = new Vector();
81 /*************************************************************************/
84 * Constructors
87 /**
88 * Initializes a new instance of <code>MenuBar</code>.
90 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
92 public
93 MenuBar()
95 if (GraphicsEnvironment.isHeadless())
96 throw new HeadlessException ();
99 /*************************************************************************/
102 * Instance Methods
106 * Returns the help menu for this menu bar. This may be <code>null</code>.
108 * @return The help menu for this menu bar.
110 public Menu
111 getHelpMenu()
113 return(helpMenu);
116 /*************************************************************************/
119 * Sets the help menu for this menu bar.
121 * @param helpMenu The new help menu for this menu bar.
123 public synchronized void
124 setHelpMenu(Menu menu)
126 if (helpMenu != null)
128 helpMenu.removeNotify ();
129 helpMenu.parent = null;
132 if (menu.parent != null)
133 menu.parent.remove (menu);
134 if (menu.parent != null)
135 menu.parent.remove (menu);
136 menu.parent = this;
138 if (peer != null)
140 MenuBarPeer mp = (MenuBarPeer) peer;
141 mp.addHelpMenu (menu);
145 /*************************************************************************/
147 /** Add a menu to this MenuBar. If the menu has already has a
148 * parent, it is first removed from its old parent before being
149 * added.
151 * @param menu The menu to add.
153 * @return The menu that was added.
155 public synchronized Menu
156 add(Menu menu)
158 if (menu.parent != null)
159 menu.parent.remove (menu);
161 menu.parent = this;
162 menus.addElement(menu);
164 if (peer != null)
166 MenuBarPeer mp = (MenuBarPeer) peer;
167 mp.addMenu (menu);
170 return(menu);
173 /*************************************************************************/
176 * Removes the menu at the specified index.
178 * @param index The index of the menu to remove from the menu bar.
180 public synchronized void
181 remove(int index)
183 Menu m = (Menu) menus.get (index);
184 menus.remove (index);
185 m.removeNotify ();
186 m.parent = null;
188 if (peer != null)
190 MenuBarPeer mp = (MenuBarPeer) peer;
191 mp.delMenu (index);
195 /*************************************************************************/
198 * Removes the specified menu from the menu bar.
200 * @param menu The menu to remove from the menu bar.
202 public void
203 remove(MenuComponent menu)
205 int index = menus.indexOf(menu);
206 if (index == -1)
207 return;
209 remove(index);
212 /*************************************************************************/
215 * Returns the number of elements in this menu bar.
217 * @return The number of elements in the menu bar.
219 public int
220 getMenuCount()
222 // FIXME: How does the help menu fit in here?
223 return(menus.size());
226 /*************************************************************************/
229 * Returns the number of elements in this menu bar.
231 * @return The number of elements in the menu bar.
233 * @deprecated This method is deprecated in favor of <code>getMenuCount()</code>.
235 public int
236 countMenus()
238 return(getMenuCount());
241 /*************************************************************************/
244 * Returns the menu at the specified index.
246 * @return The requested menu.
248 * @exception ArrayIndexOutOfBoundsException If the index is not valid.
250 public Menu
251 getMenu(int index)
253 return((Menu)menus.elementAt(index));
256 /*************************************************************************/
259 * Creates this object's native peer.
261 public void
262 addNotify()
264 if (getPeer() == null)
265 setPeer((MenuComponentPeer)getToolkit().createMenuBar(this));
268 /*************************************************************************/
271 * Destroys this object's native peer.
273 public void
274 removeNotify()
276 super.removeNotify();
279 /*************************************************************************/
282 * Returns a list of all shortcuts for the menus in this menu bar.
284 * @return A list of all shortcuts for the menus in this menu bar.
286 public synchronized Enumeration
287 shortcuts()
289 Vector shortcuts = new Vector();
290 Enumeration e = menus.elements();
292 while (e.hasMoreElements())
294 Menu menu = (Menu)e.nextElement();
295 if (menu.getShortcut() != null)
296 shortcuts.addElement(menu.getShortcut());
299 return(shortcuts.elements());
302 /*************************************************************************/
305 * Returns the menu item for the specified shortcut, or <code>null</code>
306 * if no such item exists.
308 * @param shortcut The shortcut to return the menu item for.
310 * @return The menu item for the specified shortcut.
312 public MenuItem
313 getShortcutMenuItem(MenuShortcut shortcut)
315 Enumeration e = menus.elements();
317 while (e.hasMoreElements())
319 Menu menu = (Menu)e.nextElement();
320 MenuShortcut s = menu.getShortcut();
321 if ((s != null) && (s.equals(shortcut)))
322 return(menu);
325 return(null);
328 /*************************************************************************/
331 * Deletes the specified menu shortcut.
333 * @param shortcut The shortcut to delete.
335 public void
336 deleteShortcut(MenuShortcut shortcut)
338 MenuItem it;
339 // This is a slow implementation, but it probably doesn't matter.
340 while ((it = getShortcutMenuItem (shortcut)) != null)
341 it.deleteShortcut ();
344 } // class MenuBar