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
.io
.Serializable
;
43 import java
.util
.Vector
;
46 * This class represents a pull down or tear off menu in Java's AWT.
48 * @author Aaron M. Renn (arenn@urbanophile.com)
50 public class Menu
extends MenuItem
implements MenuContainer
, Serializable
57 // Serialization Constant
58 private static final long serialVersionUID
= -8809584163345499784L;
60 /*************************************************************************/
67 * @serial The actual items in the menu
69 private Vector items
= new Vector();
72 * @serial Flag indicating whether or not this menu is a tear off
74 private boolean isTearOff
;
77 * @serial Indicates whether or not this is a help menu.
79 private boolean isHelpMenu
;
81 // From the serialization spec. FIXME: what should it be?
82 private int menuSerializedDataVersion
;
84 static final MenuItem separator
= new MenuItem("-");
86 /*************************************************************************/
93 * Initializes a new instance of <code>Menu</code> with no label and that
96 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
103 /*************************************************************************/
106 * Initializes a new instance of <code>Menu</code> that is not a tearoff and
107 * that has the specified label.
109 * @param label The menu label.
111 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
119 /*************************************************************************/
122 * Initializes a new instance of <code>Menu</code> with the specified
123 * label and tearoff status.
125 * @param label The label for this menu
126 * @param isTearOff <code>true</code> if this menu is a tear off menu,
127 * <code>false</code> otherwise.
129 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
132 Menu(String label
, boolean isTearOff
)
136 this.isTearOff
= isTearOff
;
138 if (label
.equals("Help"))
141 if (GraphicsEnvironment
.isHeadless())
142 throw new HeadlessException ();
145 /*************************************************************************/
152 * Tests whether or not this menu is a tearoff.
154 * @return <code>true</code> if this menu is a tearoff, <code>false</code>
163 /*************************************************************************/
166 * Returns the number of items in this menu.
168 * @return The number of items in this menu.
173 return(items
.size());
177 * Returns the number of items in this menu.
179 * @return The number of items in this menu.
181 * @deprecated As of JDK 1.1, replaced by getItemCount().
183 public int countItems ()
185 return getItemCount ();
188 /*************************************************************************/
191 * Returns the item at the specified index.
193 * @return The item at the specified index.
195 * @exception ArrayIndexOutOfBoundsException If the index value is not valid.
200 return((MenuItem
)items
.elementAt(index
));
203 /*************************************************************************/
206 * Adds the specified item to this menu. If it was previously part of
207 * another menu, it is first removed from that menu.
209 * @param item The new item to add.
211 * @return The item that was added.
216 items
.addElement(item
);
217 if (item
.parent
!= null)
219 item
.parent
.remove(item
);
225 MenuPeer mp
= (MenuPeer
) peer
;
232 /*************************************************************************/
235 * Add an item with the specified label to this menu.
237 * @param label The label of the menu item to add.
242 add(new MenuItem(label
));
245 /*************************************************************************/
248 * Inserts the specified menu item into this menu at the specified index.
250 * @param item The menu item to add.
251 * @param index The index of the menu item.
255 * @exception IllegalArgumentException If the index is less than zero.
256 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
259 insert(MenuItem item
, int index
)
262 throw new IllegalArgumentException("Index is less than zero");
264 items
.insertElementAt(item
, index
);
266 MenuPeer mp
= (MenuPeer
)getPeer();
267 // FIXME: Need to add a peer method here.
269 // mp.insertItem(item, index);
272 /*************************************************************************/
275 * Inserts an item with the specified label into this menu at the specified index.
277 * @param label The label of the item to add.
278 * @param index The index of the menu item.
280 * @exception IllegalArgumentException If the index is less than zero.
281 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
284 insert(String label
, int index
)
286 insert(new MenuItem(label
), index
);
289 /*************************************************************************/
292 * Adds a separator bar at the current menu location.
300 /*************************************************************************/
303 * Inserts a separator bar at the specified index value.
305 * @param index The index at which to insert a separator bar.
309 * @exception IllegalArgumentException If the index is less than zero.
310 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
313 insertSeparator(int index
)
315 insert(separator
, index
);
318 /*************************************************************************/
321 * Deletes the item at the specified index from this menu.
323 * @param index The index of the item to remove.
325 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
327 public synchronized void
330 items
.removeElementAt(index
);
332 MenuPeer mp
= (MenuPeer
)getPeer();
337 /*************************************************************************/
340 * Removes the specifed item from the menu. If the specified component
341 * does not exist, this method does nothing. // FIXME: Right?
343 * @param item The component to remove.
346 remove(MenuComponent item
)
348 int index
= items
.indexOf(item
);
355 /*************************************************************************/
358 * Removes all the elements from this menu.
360 public synchronized void
363 int count
= getItemCount();
364 for(int i
= 0; i
< count
; i
++)
366 // We must always remove item 0.
371 /*************************************************************************/
374 * Creates the native peer for this object.
380 peer
= getToolkit().createMenu(this);
384 /*************************************************************************/
387 * Destroys the native peer for this object.
392 super.removeNotify();
395 /*************************************************************************/
398 * Returns a debugging string for this menu.
400 * @return A debugging string for this menu.
405 return (",isTearOff=" + isTearOff
+ ",isHelpMenu=" + isHelpMenu
406 + super.paramString());
409 // Accessibility API not yet implemented.
410 // public AccessibleContext getAccessibleContext()