1 /* Menu.java -- A Java AWT Menu
2 Copyright (C) 1999, 2002, 2004 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)
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
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
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. */
41 import java
.awt
.peer
.MenuPeer
;
42 import java
.io
.Serializable
;
43 import java
.util
.Vector
;
44 import java
.util
.Enumeration
;
47 * This class represents a pull down or tear off menu in Java's AWT.
49 * @author Aaron M. Renn (arenn@urbanophile.com)
51 public class Menu
extends MenuItem
implements MenuContainer
, Serializable
58 // Serialization Constant
59 private static final long serialVersionUID
= -8809584163345499784L;
61 /*************************************************************************/
68 * @serial The actual items in the menu
70 private Vector items
= new Vector();
73 * @serial Flag indicating whether or not this menu is a tear off
75 private boolean isTearOff
;
78 * @serial Indicates whether or not this is a help menu.
80 private boolean isHelpMenu
;
82 // From the serialization spec. FIXME: what should it be?
83 private int menuSerializedDataVersion
;
85 static final MenuItem separator
= new MenuItem("-");
87 /*************************************************************************/
94 * Initializes a new instance of <code>Menu</code> with no label and that
97 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
104 /*************************************************************************/
107 * Initializes a new instance of <code>Menu</code> that is not a tearoff and
108 * that has the specified label.
110 * @param label The menu label.
112 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
120 /*************************************************************************/
123 * Initializes a new instance of <code>Menu</code> with the specified
124 * label and tearoff status.
126 * @param label The label for this menu
127 * @param isTearOff <code>true</code> if this menu is a tear off menu,
128 * <code>false</code> otherwise.
130 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
133 Menu(String label
, boolean isTearOff
)
137 this.isTearOff
= isTearOff
;
139 if (label
.equals("Help"))
142 if (GraphicsEnvironment
.isHeadless())
143 throw new HeadlessException ();
146 /*************************************************************************/
153 * Tests whether or not this menu is a tearoff.
155 * @return <code>true</code> if this menu is a tearoff, <code>false</code>
164 /*************************************************************************/
167 * Returns the number of items in this menu.
169 * @return The number of items in this menu.
174 return(items
.size());
178 * Returns the number of items in this menu.
180 * @return The number of items in this menu.
182 * @deprecated As of JDK 1.1, replaced by getItemCount().
184 public int countItems ()
186 return getItemCount ();
189 /*************************************************************************/
192 * Returns the item at the specified index.
194 * @return The item at the specified index.
196 * @exception ArrayIndexOutOfBoundsException If the index value is not valid.
201 return((MenuItem
)items
.elementAt(index
));
204 /*************************************************************************/
207 * Adds the specified item to this menu. If it was previously part of
208 * another menu, it is first removed from that menu.
210 * @param item The new item to add.
212 * @return The item that was added.
217 items
.addElement(item
);
218 if (item
.parent
!= null)
220 item
.parent
.remove(item
);
226 MenuPeer mp
= (MenuPeer
) peer
;
233 /*************************************************************************/
236 * Add an item with the specified label to this menu.
238 * @param label The label of the menu item to add.
243 add(new MenuItem(label
));
246 /*************************************************************************/
249 * Inserts the specified menu item into this menu at the specified index.
251 * @param item The menu item to add.
252 * @param index The index of the menu item.
256 * @exception IllegalArgumentException If the index is less than zero.
257 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
260 insert(MenuItem item
, int index
)
263 throw new IllegalArgumentException("Index is less than zero");
265 items
.insertElementAt(item
, index
);
267 MenuPeer mp
= (MenuPeer
)getPeer();
268 // FIXME: Need to add a peer method here.
270 // mp.insertItem(item, index);
273 /*************************************************************************/
276 * Inserts an item with the specified label into this menu at the specified index.
278 * @param label The label of the item to add.
279 * @param index The index of the menu item.
281 * @exception IllegalArgumentException If the index is less than zero.
282 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
285 insert(String label
, int index
)
287 insert(new MenuItem(label
), index
);
290 /*************************************************************************/
293 * Adds a separator bar at the current menu location.
299 ((MenuPeer
) peer
).addSeparator();
302 /*************************************************************************/
305 * Inserts a separator bar at the specified index value.
307 * @param index The index at which to insert a separator bar.
311 * @exception IllegalArgumentException If the index is less than zero.
312 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
315 insertSeparator(int index
)
317 insert(separator
, index
);
320 /*************************************************************************/
323 * Deletes the item at the specified index from this menu.
325 * @param index The index of the item to remove.
327 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
329 public synchronized void
332 items
.removeElementAt(index
);
334 MenuPeer mp
= (MenuPeer
)getPeer();
339 /*************************************************************************/
342 * Removes the specifed item from the menu. If the specified component
343 * does not exist, this method does nothing. // FIXME: Right?
345 * @param item The component to remove.
348 remove(MenuComponent item
)
350 int index
= items
.indexOf(item
);
357 /*************************************************************************/
360 * Removes all the elements from this menu.
362 public synchronized void
365 int count
= getItemCount();
366 for(int i
= 0; i
< count
; i
++)
368 // We must always remove item 0.
373 /*************************************************************************/
376 * Creates the native peer for this object.
382 peer
= getToolkit().createMenu(this);
383 Enumeration e
= items
.elements();
384 while (e
.hasMoreElements())
386 MenuItem mi
= (MenuItem
)e
.nextElement();
392 /*************************************************************************/
395 * Destroys the native peer for this object.
400 Enumeration e
= items
.elements();
401 while (e
.hasMoreElements())
403 MenuItem mi
= (MenuItem
) e
.nextElement();
406 super.removeNotify();
409 /*************************************************************************/
412 * Returns a debugging string for this menu.
414 * @return A debugging string for this menu.
419 return (",isTearOff=" + isTearOff
+ ",isHelpMenu=" + isHelpMenu
420 + super.paramString());
423 // Accessibility API not yet implemented.
424 // public AccessibleContext getAccessibleContext()