1 /* Menu.java -- A Java AWT Menu
2 Copyright (C) 1999, 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)
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
.awt
.peer
.MenuItemPeer
;
43 import java
.awt
.peer
.MenuComponentPeer
;
44 import java
.io
.Serializable
;
45 import java
.util
.Vector
;
46 import java
.util
.Enumeration
;
49 * This class represents a pull down or tear off menu in Java's AWT.
51 * @author Aaron M. Renn (arenn@urbanophile.com)
53 public class Menu
extends MenuItem
implements MenuContainer
, Serializable
60 // Serialization Constant
61 private static final long serialVersionUID
= -8809584163345499784L;
63 /*************************************************************************/
70 * @serial The actual items in the menu
72 private Vector items
= new Vector();
75 * @serial Flag indicating whether or not this menu is a tear off
77 private boolean isTearOff
;
80 * @serial Indicates whether or not this is a help menu.
82 private boolean isHelpMenu
;
84 // From the serialization spec. FIXME: what should it be?
85 private int menuSerializedDataVersion
;
87 static final MenuItem separator
= new MenuItem("-");
89 /*************************************************************************/
96 * Initializes a new instance of <code>Menu</code> with no label and that
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.
118 /*************************************************************************/
121 * Initializes a new instance of <code>Menu</code> with the specified
122 * label and tearoff status.
124 * @param label The label for this menu
125 * @param isTearOff <code>true</code> if this menu is a tear off menu,
126 * <code>false</code> otherwise.
129 Menu(String label
, boolean isTearOff
)
133 this.isTearOff
= isTearOff
;
135 if (label
.equals("Help"))
139 /*************************************************************************/
146 * Tests whether or not this menu is a tearoff.
148 * @return <code>true</code> if this menu is a tearoff, <code>false</code>
157 /*************************************************************************/
160 * Returns the number of items in this menu.
162 * @return The number of items in this menu.
167 return(items
.size());
170 /*************************************************************************/
173 * Returns the number of items in this menu.
175 * @return The number of items in this menu.
177 * @deprecated This method is deprecated in favor of <code>getItemCount()</code>.
182 return(items
.size());
185 /*************************************************************************/
188 * Returns the item at the specified index.
190 * @return The item at the specified index.
192 * @exception ArrayIndexOutOfBoundsException If the index value is not valid.
197 return((MenuItem
)items
.elementAt(index
));
200 /*************************************************************************/
203 * Adds the specified item to this menu. If it was previously part of
204 * another menu, it is first removed from that menu.
206 * @param item The new item to add.
208 * @return The item that was added.
213 items
.addElement(item
);
214 if (item
.parent
!= null)
216 item
.parent
.remove(item
);
222 MenuPeer mp
= (MenuPeer
) peer
;
229 /*************************************************************************/
232 * Add an item with the specified label to this menu.
234 * @param label The label of the menu item to add.
239 add(new MenuItem(label
));
242 /*************************************************************************/
245 * Inserts the specified menu item into this menu at the specified index.
247 * @param item The menu item to add.
248 * @param index The index of the menu item.
252 * @exception IllegalArgumentException If the index is less than zero.
253 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
256 insert(MenuItem item
, int index
)
259 throw new IllegalArgumentException("Index is less than zero");
261 items
.insertElementAt(item
, index
);
263 MenuPeer mp
= (MenuPeer
)getPeer();
264 // FIXME: Need to add a peer method here.
266 // mp.insertItem(item, index);
269 /*************************************************************************/
272 * Inserts an item with the specified label into this menu at the specified index.
274 * @param label The label of the item to add.
275 * @param index The index of the menu item.
277 * @exception IllegalArgumentException If the index is less than zero.
278 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
281 insert(String label
, int index
)
283 insert(new MenuItem(label
), index
);
286 /*************************************************************************/
289 * Adds a separator bar at the current menu location.
297 /*************************************************************************/
300 * Inserts a separator bar at the specified index value.
302 * @param index The index at which to insert a separator bar.
306 * @exception IllegalArgumentException If the index is less than zero.
307 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
310 insertSeparator(int index
)
312 insert(separator
, index
);
315 /*************************************************************************/
318 * Deletes the item at the specified index from this menu.
320 * @param index The index of the item to remove.
322 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
324 public synchronized void
327 items
.removeElementAt(index
);
329 MenuPeer mp
= (MenuPeer
)getPeer();
334 /*************************************************************************/
337 * Removes the specifed item from the menu. If the specified component
338 * does not exist, this method does nothing. // FIXME: Right?
340 * @param item The component to remove.
343 remove(MenuComponent item
)
345 int index
= items
.indexOf(item
);
352 /*************************************************************************/
355 * Removes all the elements from this menu.
357 public synchronized void
360 int count
= getItemCount();
361 for(int i
= 0; i
< count
; i
++)
363 // We must always remove item 0.
368 /*************************************************************************/
371 * Creates the native peer for this object.
377 peer
= getToolkit().createMenu(this);
381 /*************************************************************************/
384 * Destroys the native peer for this object.
389 super.removeNotify();
392 /*************************************************************************/
395 * Returns a debugging string for this menu.
397 * @return A debugging string for this menu.
402 return (",isTearOff=" + isTearOff
+ ",isHelpMenu=" + isHelpMenu
403 + super.paramString());
406 // Accessibility API not yet implemented.
407 // public AccessibleContext getAccessibleContext()