source: trunk/gcc/libjava/java/awt/Menu.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 9.9 KB
Line 
1/* Menu.java -- A Java AWT Menu
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.peer.MenuPeer;
42import java.awt.peer.MenuItemPeer;
43import java.awt.peer.MenuComponentPeer;
44import java.io.Serializable;
45import java.util.Vector;
46import java.util.Enumeration;
47
48/**
49 * This class represents a pull down or tear off menu in Java's AWT.
50 *
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 */
53public class Menu extends MenuItem implements MenuContainer, Serializable
54{
55
56/*
57 * Static Variables
58 */
59
60// Serialization Constant
61private static final long serialVersionUID = -8809584163345499784L;
62
63/*************************************************************************/
64
65/*
66 * Instance Variables
67 */
68
69/**
70 * @serial The actual items in the menu
71 */
72private Vector items = new Vector();
73
74/**
75 * @serial Flag indicating whether or not this menu is a tear off
76 */
77private boolean isTearOff;
78
79/**
80 * @serial Indicates whether or not this is a help menu.
81 */
82private boolean isHelpMenu;
83
84// From the serialization spec. FIXME: what should it be?
85private int menuSerializedDataVersion;
86
87static final MenuItem separator = new MenuItem("-");
88
89/*************************************************************************/
90
91/*
92 * Constructors
93 */
94
95/**
96 * Initializes a new instance of <code>Menu</code> with no label and that
97 * is not a tearoff;
98 *
99 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
100 */
101public
102Menu()
103{
104}
105
106/*************************************************************************/
107
108/**
109 * Initializes a new instance of <code>Menu</code> that is not a tearoff and
110 * that has the specified label.
111 *
112 * @param label The menu label.
113 *
114 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
115 */
116public
117Menu(String label)
118{
119 this(label, false);
120}
121
122/*************************************************************************/
123
124/**
125 * Initializes a new instance of <code>Menu</code> with the specified
126 * label and tearoff status.
127 *
128 * @param label The label for this menu
129 * @param isTearOff <code>true</code> if this menu is a tear off menu,
130 * <code>false</code> otherwise.
131 *
132 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
133 */
134public
135Menu(String label, boolean isTearOff)
136{
137 super(label);
138
139 this.isTearOff = isTearOff;
140
141 if (label.equals("Help"))
142 isHelpMenu = true;
143
144 if (GraphicsEnvironment.isHeadless())
145 throw new HeadlessException ();
146}
147
148/*************************************************************************/
149
150/*
151 * Instance Methods
152 */
153
154/**
155 * Tests whether or not this menu is a tearoff.
156 *
157 * @return <code>true</code> if this menu is a tearoff, <code>false</code>
158 * otherwise.
159 */
160public boolean
161isTearOff()
162{
163 return(isTearOff);
164}
165
166/*************************************************************************/
167
168/**
169 * Returns the number of items in this menu.
170 *
171 * @return The number of items in this menu.
172 */
173public int
174getItemCount()
175{
176 return(items.size());
177}
178
179/*************************************************************************/
180
181/**
182 * Returns the number of items in this menu.
183 *
184 * @return The number of items in this menu.
185 *
186 * @deprecated This method is deprecated in favor of <code>getItemCount()</code>.
187 */
188public int
189count()
190{
191 return(items.size());
192}
193
194/*************************************************************************/
195
196/**
197 * Returns the item at the specified index.
198 *
199 * @return The item at the specified index.
200 *
201 * @exception ArrayIndexOutOfBoundsException If the index value is not valid.
202 */
203public MenuItem
204getItem(int index)
205{
206 return((MenuItem)items.elementAt(index));
207}
208
209/*************************************************************************/
210
211/**
212 * Adds the specified item to this menu. If it was previously part of
213 * another menu, it is first removed from that menu.
214 *
215 * @param item The new item to add.
216 *
217 * @return The item that was added.
218 */
219public MenuItem
220add(MenuItem item)
221{
222 items.addElement(item);
223 if (item.parent != null)
224 {
225 item.parent.remove(item);
226 }
227 item.parent = this;
228
229 if (peer != null)
230 {
231 MenuPeer mp = (MenuPeer) peer;
232 mp.addItem(item);
233 }
234
235 return item;
236}
237
238/*************************************************************************/
239
240/**
241 * Add an item with the specified label to this menu.
242 *
243 * @param label The label of the menu item to add.
244 */
245public void
246add(String label)
247{
248 add(new MenuItem(label));
249}
250
251/*************************************************************************/
252
253/**
254 * Inserts the specified menu item into this menu at the specified index.
255 *
256 * @param item The menu item to add.
257 * @param index The index of the menu item.
258 *
259 * XXX: FIXME
260 *
261 * @exception IllegalArgumentException If the index is less than zero.
262 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
263 */
264public void
265insert(MenuItem item, int index)
266{
267 if (index < 0)
268 throw new IllegalArgumentException("Index is less than zero");
269
270 items.insertElementAt(item, index);
271
272 MenuPeer mp = (MenuPeer)getPeer();
273 // FIXME: Need to add a peer method here.
274// if (mp != null)
275// mp.insertItem(item, index);
276}
277
278/*************************************************************************/
279
280/**
281 * Inserts an item with the specified label into this menu at the specified index.
282 *
283 * @param label The label of the item to add.
284 * @param index The index of the menu item.
285 *
286 * @exception IllegalArgumentException If the index is less than zero.
287 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
288 */
289public void
290insert(String label, int index)
291{
292 insert(new MenuItem(label), index);
293}
294
295/*************************************************************************/
296
297/**
298 * Adds a separator bar at the current menu location.
299 */
300public void
301addSeparator()
302{
303 add(separator);
304}
305
306/*************************************************************************/
307
308/**
309 * Inserts a separator bar at the specified index value.
310 *
311 * @param index The index at which to insert a separator bar.
312 *
313 * XXX: FIXME
314 *
315 * @exception IllegalArgumentException If the index is less than zero.
316 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
317 */
318public void
319insertSeparator(int index)
320{
321 insert(separator, index);
322}
323
324/*************************************************************************/
325
326/**
327 * Deletes the item at the specified index from this menu.
328 *
329 * @param index The index of the item to remove.
330 *
331 * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
332 */
333public synchronized void
334remove(int index)
335{
336 items.removeElementAt(index);
337
338 MenuPeer mp = (MenuPeer)getPeer();
339 if (mp != null)
340 mp.delItem(index);
341}
342
343/*************************************************************************/
344
345/**
346 * Removes the specifed item from the menu. If the specified component
347 * does not exist, this method does nothing. // FIXME: Right?
348 *
349 * @param item The component to remove.
350 */
351public void
352remove(MenuComponent item)
353{
354 int index = items.indexOf(item);
355 if (index == -1)
356 return;
357
358 remove(index);
359}
360
361/*************************************************************************/
362
363/**
364 * Removes all the elements from this menu.
365 */
366public synchronized void
367removeAll()
368{
369 int count = getItemCount();
370 for(int i = 0; i < count; i++)
371 {
372 // We must always remove item 0.
373 remove(0);
374 }
375}
376
377/*************************************************************************/
378
379/**
380 * Creates the native peer for this object.
381 */
382public void
383addNotify()
384{
385 if (peer != null)
386 peer = getToolkit().createMenu(this);
387 super.addNotify ();
388}
389
390/*************************************************************************/
391
392/**
393 * Destroys the native peer for this object.
394 */
395public void
396removeNotify()
397{
398 super.removeNotify();
399}
400
401/*************************************************************************/
402
403/**
404 * Returns a debugging string for this menu.
405 *
406 * @return A debugging string for this menu.
407 */
408public String
409paramString()
410{
411 return (",isTearOff=" + isTearOff + ",isHelpMenu=" + isHelpMenu
412 + super.paramString());
413}
414
415// Accessibility API not yet implemented.
416// public AccessibleContext getAccessibleContext()
417
418} // class Menu
Note: See TracBrowser for help on using the repository browser.