source: trunk/gcc/libjava/java/awt/MenuBar.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 8.0 KB
Line 
1/* MenuBar.java -- An AWT menu bar class
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.peer.MenuBarPeer;
42import java.awt.peer.MenuComponentPeer;
43
44import java.io.Serializable;
45import java.util.Enumeration;
46import java.util.Vector;
47
48/**
49 * This class implements a menu bar in the AWT system.
50 *
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Tom Tromey <tromey@redhat.com>
53 */
54public class MenuBar extends MenuComponent
55 implements MenuContainer, Serializable
56{
57
58/*
59 * Static Variables
60 */
61
62// Serialization Constant
63private static final long serialVersionUID = -4930327919388951260L;
64
65/*************************************************************************/
66
67/*
68 * Instance Variables
69 */
70
71/**
72 * @serial The menu used for providing help information
73 */
74private Menu helpMenu;
75
76/**
77 * @serial The menus contained in this menu bar.
78 */
79private Vector menus = new Vector();
80
81/*************************************************************************/
82
83/*
84 * Constructors
85 */
86
87/**
88 * Initializes a new instance of <code>MenuBar</code>.
89 *
90 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
91 */
92public
93MenuBar()
94{
95 if (GraphicsEnvironment.isHeadless())
96 throw new HeadlessException ();
97}
98
99/*************************************************************************/
100
101/*
102 * Instance Methods
103 */
104
105/**
106 * Returns the help menu for this menu bar. This may be <code>null</code>.
107 *
108 * @return The help menu for this menu bar.
109 */
110public Menu
111getHelpMenu()
112{
113 return(helpMenu);
114}
115
116/*************************************************************************/
117
118/**
119 * Sets the help menu for this menu bar.
120 *
121 * @param helpMenu The new help menu for this menu bar.
122 */
123public synchronized void
124setHelpMenu(Menu menu)
125{
126 if (helpMenu != null)
127 {
128 helpMenu.removeNotify ();
129 helpMenu.parent = null;
130 }
131
132 if (menu.parent != null)
133 menu.parent.remove (menu);
134 if (menu.parent != null)
135 menu.parent.remove (menu);
136 menu.parent = this;
137
138 if (peer != null)
139 {
140 MenuBarPeer mp = (MenuBarPeer) peer;
141 mp.addHelpMenu (menu);
142 }
143}
144
145/*************************************************************************/
146
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.
150 *
151 * @param menu The menu to add.
152 *
153 * @return The menu that was added.
154 */
155public synchronized Menu
156add(Menu menu)
157{
158 if (menu.parent != null)
159 menu.parent.remove (menu);
160
161 menu.parent = this;
162 menus.addElement(menu);
163
164 if (peer != null)
165 {
166 MenuBarPeer mp = (MenuBarPeer) peer;
167 mp.addMenu (menu);
168 }
169
170 return(menu);
171}
172
173/*************************************************************************/
174
175/**
176 * Removes the menu at the specified index.
177 *
178 * @param index The index of the menu to remove from the menu bar.
179 */
180public synchronized void
181remove(int index)
182{
183 Menu m = (Menu) menus.get (index);
184 menus.remove (index);
185 m.removeNotify ();
186 m.parent = null;
187
188 if (peer != null)
189 {
190 MenuBarPeer mp = (MenuBarPeer) peer;
191 mp.delMenu (index);
192 }
193}
194
195/*************************************************************************/
196
197/**
198 * Removes the specified menu from the menu bar.
199 *
200 * @param menu The menu to remove from the menu bar.
201 */
202public void
203remove(MenuComponent menu)
204{
205 int index = menus.indexOf(menu);
206 if (index == -1)
207 return;
208
209 remove(index);
210}
211
212/*************************************************************************/
213
214/**
215 * Returns the number of elements in this menu bar.
216 *
217 * @return The number of elements in the menu bar.
218 */
219public int
220getMenuCount()
221{
222 // FIXME: How does the help menu fit in here?
223 return(menus.size());
224}
225
226/*************************************************************************/
227
228/**
229 * Returns the number of elements in this menu bar.
230 *
231 * @return The number of elements in the menu bar.
232 *
233 * @deprecated This method is deprecated in favor of <code>getMenuCount()</code>.
234 */
235public int
236countMenus()
237{
238 return(getMenuCount());
239}
240
241/*************************************************************************/
242
243/**
244 * Returns the menu at the specified index.
245 *
246 * @return The requested menu.
247 *
248 * @exception ArrayIndexOutOfBoundsException If the index is not valid.
249 */
250public Menu
251getMenu(int index)
252{
253 return((Menu)menus.elementAt(index));
254}
255
256/*************************************************************************/
257
258/**
259 * Creates this object's native peer.
260 */
261public void
262addNotify()
263{
264 if (getPeer() == null)
265 setPeer((MenuComponentPeer)getToolkit().createMenuBar(this));
266}
267
268/*************************************************************************/
269
270/**
271 * Destroys this object's native peer.
272 */
273public void
274removeNotify()
275{
276 super.removeNotify();
277}
278
279/*************************************************************************/
280
281/**
282 * Returns a list of all shortcuts for the menus in this menu bar.
283 *
284 * @return A list of all shortcuts for the menus in this menu bar.
285 */
286public synchronized Enumeration
287shortcuts()
288{
289 Vector shortcuts = new Vector();
290 Enumeration e = menus.elements();
291
292 while (e.hasMoreElements())
293 {
294 Menu menu = (Menu)e.nextElement();
295 if (menu.getShortcut() != null)
296 shortcuts.addElement(menu.getShortcut());
297 }
298
299 return(shortcuts.elements());
300}
301
302/*************************************************************************/
303
304/**
305 * Returns the menu item for the specified shortcut, or <code>null</code>
306 * if no such item exists.
307 *
308 * @param shortcut The shortcut to return the menu item for.
309 *
310 * @return The menu item for the specified shortcut.
311 */
312public MenuItem
313getShortcutMenuItem(MenuShortcut shortcut)
314{
315 Enumeration e = menus.elements();
316
317 while (e.hasMoreElements())
318 {
319 Menu menu = (Menu)e.nextElement();
320 MenuShortcut s = menu.getShortcut();
321 if ((s != null) && (s.equals(shortcut)))
322 return(menu);
323 }
324
325 return(null);
326}
327
328/*************************************************************************/
329
330/**
331 * Deletes the specified menu shortcut.
332 *
333 * @param shortcut The shortcut to delete.
334 */
335public void
336deleteShortcut(MenuShortcut shortcut)
337{
338 MenuItem it;
339 // This is a slow implementation, but it probably doesn't matter.
340 while ((it = getShortcutMenuItem (shortcut)) != null)
341 it.deleteShortcut ();
342}
343
344} // class MenuBar
Note: See TracBrowser for help on using the repository browser.