* gcj.texi (Compatibility): Add Limitations and Extensions section.
[official-gcc.git] / libjava / java / awt / Menu.java
blob18d63be749077f989afc4fb3155cc7f522ca30ce
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.awt.peer.MenuItemPeer;
43 import java.awt.peer.MenuComponentPeer;
44 import java.io.Serializable;
45 import java.util.Vector;
46 import java.util.Enumeration;
48 /**
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
57 * Static Variables
60 // Serialization Constant
61 private static final long serialVersionUID = -8809584163345499784L;
63 /*************************************************************************/
66 * Instance Variables
69 /**
70 * @serial The actual items in the menu
72 private Vector items = new Vector();
74 /**
75 * @serial Flag indicating whether or not this menu is a tear off
77 private boolean isTearOff;
79 /**
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 /*************************************************************************/
92 * Constructors
95 /**
96 * Initializes a new instance of <code>Menu</code> with no label and that
97 * is not a tearoff;
99 public
100 Menu()
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 public
113 Menu(String label)
115 this(label, false);
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.
128 public
129 Menu(String label, boolean isTearOff)
131 super(label);
133 this.isTearOff = isTearOff;
135 if (label.equals("Help"))
136 isHelpMenu = true;
139 /*************************************************************************/
142 * Instance Methods
146 * Tests whether or not this menu is a tearoff.
148 * @return <code>true</code> if this menu is a tearoff, <code>false</code>
149 * otherwise.
151 public boolean
152 isTearOff()
154 return(isTearOff);
157 /*************************************************************************/
160 * Returns the number of items in this menu.
162 * @return The number of items in this menu.
164 public int
165 getItemCount()
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>.
179 public int
180 count()
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.
194 public MenuItem
195 getItem(int index)
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.
210 public MenuItem
211 add(MenuItem item)
213 items.addElement(item);
214 if (item.parent != null)
216 item.parent.remove(item);
218 item.parent = this;
220 if (peer != null)
222 MenuPeer mp = (MenuPeer) peer;
223 mp.addItem(item);
226 return item;
229 /*************************************************************************/
232 * Add an item with the specified label to this menu.
234 * @param label The label of the menu item to add.
236 public void
237 add(String label)
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.
250 * XXX: FIXME
252 * @exception IllegalArgumentException If the index is less than zero.
253 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
255 public void
256 insert(MenuItem item, int index)
258 if (index < 0)
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.
265 // if (mp != null)
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.
280 public void
281 insert(String label, int index)
283 insert(new MenuItem(label), index);
286 /*************************************************************************/
289 * Adds a separator bar at the current menu location.
291 public void
292 addSeparator()
294 add(separator);
297 /*************************************************************************/
300 * Inserts a separator bar at the specified index value.
302 * @param index The index at which to insert a separator bar.
304 * XXX: FIXME
306 * @exception IllegalArgumentException If the index is less than zero.
307 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
309 public void
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
325 remove(int index)
327 items.removeElementAt(index);
329 MenuPeer mp = (MenuPeer)getPeer();
330 if (mp != null)
331 mp.delItem(index);
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.
342 public void
343 remove(MenuComponent item)
345 int index = items.indexOf(item);
346 if (index == -1)
347 return;
349 remove(index);
352 /*************************************************************************/
355 * Removes all the elements from this menu.
357 public synchronized void
358 removeAll()
360 int count = getItemCount();
361 for(int i = 0; i < count; i++)
363 // We must always remove item 0.
364 remove(0);
368 /*************************************************************************/
371 * Creates the native peer for this object.
373 public void
374 addNotify()
376 if (peer != null)
377 peer = getToolkit().createMenu(this);
378 super.addNotify ();
381 /*************************************************************************/
384 * Destroys the native peer for this object.
386 public void
387 removeNotify()
389 super.removeNotify();
392 /*************************************************************************/
395 * Returns a debugging string for this menu.
397 * @return A debugging string for this menu.
399 public String
400 paramString()
402 return (",isTearOff=" + isTearOff + ",isHelpMenu=" + isHelpMenu
403 + super.paramString());
406 // Accessibility API not yet implemented.
407 // public AccessibleContext getAccessibleContext()
409 } // class Menu