2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / awt / Menu.java
blob52244b9a1c46a7663cb2b4dd4f716373ff4cd01b
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)
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.MenuPeer;
42 import java.io.Serializable;
43 import java.util.Vector;
45 /**
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
54 * Static Variables
57 // Serialization Constant
58 private static final long serialVersionUID = -8809584163345499784L;
60 /*************************************************************************/
63 * Instance Variables
66 /**
67 * @serial The actual items in the menu
69 private Vector items = new Vector();
71 /**
72 * @serial Flag indicating whether or not this menu is a tear off
74 private boolean isTearOff;
76 /**
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 /*************************************************************************/
89 * Constructors
92 /**
93 * Initializes a new instance of <code>Menu</code> with no label and that
94 * is not a tearoff;
96 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
98 public
99 Menu()
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.
113 public
114 Menu(String label)
116 this(label, false);
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.
131 public
132 Menu(String label, boolean isTearOff)
134 super(label);
136 this.isTearOff = isTearOff;
138 if (label.equals("Help"))
139 isHelpMenu = true;
141 if (GraphicsEnvironment.isHeadless())
142 throw new HeadlessException ();
145 /*************************************************************************/
148 * Instance Methods
152 * Tests whether or not this menu is a tearoff.
154 * @return <code>true</code> if this menu is a tearoff, <code>false</code>
155 * otherwise.
157 public boolean
158 isTearOff()
160 return(isTearOff);
163 /*************************************************************************/
166 * Returns the number of items in this menu.
168 * @return The number of items in this menu.
170 public int
171 getItemCount()
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.
197 public MenuItem
198 getItem(int index)
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.
213 public MenuItem
214 add(MenuItem item)
216 items.addElement(item);
217 if (item.parent != null)
219 item.parent.remove(item);
221 item.parent = this;
223 if (peer != null)
225 MenuPeer mp = (MenuPeer) peer;
226 mp.addItem(item);
229 return item;
232 /*************************************************************************/
235 * Add an item with the specified label to this menu.
237 * @param label The label of the menu item to add.
239 public void
240 add(String label)
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.
253 * XXX: FIXME
255 * @exception IllegalArgumentException If the index is less than zero.
256 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
258 public void
259 insert(MenuItem item, int index)
261 if (index < 0)
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.
268 // if (mp != null)
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.
283 public void
284 insert(String label, int index)
286 insert(new MenuItem(label), index);
289 /*************************************************************************/
292 * Adds a separator bar at the current menu location.
294 public void
295 addSeparator()
297 add(separator);
300 /*************************************************************************/
303 * Inserts a separator bar at the specified index value.
305 * @param index The index at which to insert a separator bar.
307 * XXX: FIXME
309 * @exception IllegalArgumentException If the index is less than zero.
310 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
312 public void
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
328 remove(int index)
330 items.removeElementAt(index);
332 MenuPeer mp = (MenuPeer)getPeer();
333 if (mp != null)
334 mp.delItem(index);
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.
345 public void
346 remove(MenuComponent item)
348 int index = items.indexOf(item);
349 if (index == -1)
350 return;
352 remove(index);
355 /*************************************************************************/
358 * Removes all the elements from this menu.
360 public synchronized void
361 removeAll()
363 int count = getItemCount();
364 for(int i = 0; i < count; i++)
366 // We must always remove item 0.
367 remove(0);
371 /*************************************************************************/
374 * Creates the native peer for this object.
376 public void
377 addNotify()
379 if (peer != null)
380 peer = getToolkit().createMenu(this);
381 super.addNotify ();
384 /*************************************************************************/
387 * Destroys the native peer for this object.
389 public void
390 removeNotify()
392 super.removeNotify();
395 /*************************************************************************/
398 * Returns a debugging string for this menu.
400 * @return A debugging string for this menu.
402 public String
403 paramString()
405 return (",isTearOff=" + isTearOff + ",isHelpMenu=" + isHelpMenu
406 + super.paramString());
409 // Accessibility API not yet implemented.
410 // public AccessibleContext getAccessibleContext()
412 } // class Menu