]>
Commit | Line | Data |
---|---|---|
bda14505 | 1 | /* Menu.java -- A Java AWT Menu |
2c20a171 | 2 | Copyright (C) 1999, 2002, 2004 Free Software Foundation, Inc. |
fd164b17 | 3 | |
bda14505 TT |
4 | This file is part of GNU Classpath. |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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. | |
20 | ||
98c3a44f MW |
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. | |
25 | ||
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. */ | |
fd164b17 | 37 | |
fd164b17 PB |
38 | |
39 | package java.awt; | |
40 | ||
bda14505 | 41 | import java.awt.peer.MenuPeer; |
bda14505 | 42 | import java.io.Serializable; |
c7a136d3 | 43 | import java.util.Vector; |
e300e74f | 44 | import java.util.Enumeration; |
bda14505 TT |
45 | |
46 | /** | |
47 | * This class represents a pull down or tear off menu in Java's AWT. | |
48 | * | |
49 | * @author Aaron M. Renn (arenn@urbanophile.com) | |
50 | */ | |
51 | public class Menu extends MenuItem implements MenuContainer, Serializable | |
52 | { | |
53 | ||
54 | /* | |
55 | * Static Variables | |
56 | */ | |
57 | ||
58 | // Serialization Constant | |
59 | private static final long serialVersionUID = -8809584163345499784L; | |
60 | ||
61 | /*************************************************************************/ | |
62 | ||
63 | /* | |
64 | * Instance Variables | |
65 | */ | |
66 | ||
67 | /** | |
68 | * @serial The actual items in the menu | |
69 | */ | |
70 | private Vector items = new Vector(); | |
71 | ||
72 | /** | |
73 | * @serial Flag indicating whether or not this menu is a tear off | |
74 | */ | |
75 | private boolean isTearOff; | |
76 | ||
77 | /** | |
78 | * @serial Indicates whether or not this is a help menu. | |
79 | */ | |
80 | private boolean isHelpMenu; | |
81 | ||
82 | // From the serialization spec. FIXME: what should it be? | |
83 | private int menuSerializedDataVersion; | |
84 | ||
85 | static final MenuItem separator = new MenuItem("-"); | |
86 | ||
87 | /*************************************************************************/ | |
88 | ||
89 | /* | |
90 | * Constructors | |
91 | */ | |
92 | ||
93 | /** | |
94 | * Initializes a new instance of <code>Menu</code> with no label and that | |
95 | * is not a tearoff; | |
62d2eed6 TT |
96 | * |
97 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true. | |
bda14505 TT |
98 | */ |
99 | public | |
100 | Menu() | |
101 | { | |
102 | } | |
103 | ||
104 | /*************************************************************************/ | |
105 | ||
106 | /** | |
107 | * Initializes a new instance of <code>Menu</code> that is not a tearoff and | |
108 | * that has the specified label. | |
109 | * | |
110 | * @param label The menu label. | |
62d2eed6 TT |
111 | * |
112 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true. | |
bda14505 TT |
113 | */ |
114 | public | |
115 | Menu(String label) | |
116 | { | |
117 | this(label, false); | |
118 | } | |
119 | ||
120 | /*************************************************************************/ | |
c7a136d3 | 121 | |
bda14505 TT |
122 | /** |
123 | * Initializes a new instance of <code>Menu</code> with the specified | |
124 | * label and tearoff status. | |
125 | * | |
126 | * @param label The label for this menu | |
127 | * @param isTearOff <code>true</code> if this menu is a tear off menu, | |
128 | * <code>false</code> otherwise. | |
62d2eed6 TT |
129 | * |
130 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true. | |
bda14505 TT |
131 | */ |
132 | public | |
133 | Menu(String label, boolean isTearOff) | |
134 | { | |
135 | super(label); | |
136 | ||
137 | this.isTearOff = isTearOff; | |
138 | ||
139 | if (label.equals("Help")) | |
140 | isHelpMenu = true; | |
62d2eed6 TT |
141 | |
142 | if (GraphicsEnvironment.isHeadless()) | |
143 | throw new HeadlessException (); | |
fd164b17 | 144 | } |
bda14505 TT |
145 | |
146 | /*************************************************************************/ | |
147 | ||
148 | /* | |
149 | * Instance Methods | |
150 | */ | |
151 | ||
152 | /** | |
153 | * Tests whether or not this menu is a tearoff. | |
154 | * | |
155 | * @return <code>true</code> if this menu is a tearoff, <code>false</code> | |
156 | * otherwise. | |
157 | */ | |
158 | public boolean | |
159 | isTearOff() | |
160 | { | |
161 | return(isTearOff); | |
162 | } | |
163 | ||
164 | /*************************************************************************/ | |
165 | ||
166 | /** | |
167 | * Returns the number of items in this menu. | |
168 | * | |
169 | * @return The number of items in this menu. | |
170 | */ | |
171 | public int | |
172 | getItemCount() | |
173 | { | |
174 | return(items.size()); | |
175 | } | |
30df932c MK |
176 | |
177 | /** | |
178 | * Returns the number of items in this menu. | |
179 | * | |
180 | * @return The number of items in this menu. | |
181 | * | |
182 | * @deprecated As of JDK 1.1, replaced by getItemCount(). | |
183 | */ | |
184 | public int countItems () | |
185 | { | |
186 | return getItemCount (); | |
187 | } | |
bda14505 TT |
188 | |
189 | /*************************************************************************/ | |
190 | ||
bda14505 TT |
191 | /** |
192 | * Returns the item at the specified index. | |
193 | * | |
194 | * @return The item at the specified index. | |
195 | * | |
196 | * @exception ArrayIndexOutOfBoundsException If the index value is not valid. | |
197 | */ | |
198 | public MenuItem | |
199 | getItem(int index) | |
200 | { | |
201 | return((MenuItem)items.elementAt(index)); | |
202 | } | |
203 | ||
204 | /*************************************************************************/ | |
205 | ||
206 | /** | |
207 | * Adds the specified item to this menu. If it was previously part of | |
208 | * another menu, it is first removed from that menu. | |
209 | * | |
210 | * @param item The new item to add. | |
211 | * | |
212 | * @return The item that was added. | |
213 | */ | |
214 | public MenuItem | |
215 | add(MenuItem item) | |
216 | { | |
217 | items.addElement(item); | |
218 | if (item.parent != null) | |
219 | { | |
220 | item.parent.remove(item); | |
221 | } | |
222 | item.parent = this; | |
223 | ||
224 | if (peer != null) | |
225 | { | |
226 | MenuPeer mp = (MenuPeer) peer; | |
227 | mp.addItem(item); | |
228 | } | |
229 | ||
230 | return item; | |
231 | } | |
232 | ||
233 | /*************************************************************************/ | |
234 | ||
235 | /** | |
236 | * Add an item with the specified label to this menu. | |
237 | * | |
238 | * @param label The label of the menu item to add. | |
239 | */ | |
240 | public void | |
241 | add(String label) | |
242 | { | |
243 | add(new MenuItem(label)); | |
244 | } | |
245 | ||
246 | /*************************************************************************/ | |
247 | ||
248 | /** | |
249 | * Inserts the specified menu item into this menu at the specified index. | |
250 | * | |
251 | * @param item The menu item to add. | |
252 | * @param index The index of the menu item. | |
253 | * | |
254 | * XXX: FIXME | |
255 | * | |
256 | * @exception IllegalArgumentException If the index is less than zero. | |
257 | * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid. | |
258 | */ | |
259 | public void | |
260 | insert(MenuItem item, int index) | |
261 | { | |
262 | if (index < 0) | |
263 | throw new IllegalArgumentException("Index is less than zero"); | |
264 | ||
265 | items.insertElementAt(item, index); | |
266 | ||
267 | MenuPeer mp = (MenuPeer)getPeer(); | |
268 | // FIXME: Need to add a peer method here. | |
269 | // if (mp != null) | |
270 | // mp.insertItem(item, index); | |
271 | } | |
272 | ||
273 | /*************************************************************************/ | |
274 | ||
275 | /** | |
276 | * Inserts an item with the specified label into this menu at the specified index. | |
277 | * | |
278 | * @param label The label of the item to add. | |
279 | * @param index The index of the menu item. | |
280 | * | |
281 | * @exception IllegalArgumentException If the index is less than zero. | |
282 | * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid. | |
283 | */ | |
284 | public void | |
285 | insert(String label, int index) | |
286 | { | |
287 | insert(new MenuItem(label), index); | |
288 | } | |
289 | ||
290 | /*************************************************************************/ | |
291 | ||
292 | /** | |
293 | * Adds a separator bar at the current menu location. | |
294 | */ | |
295 | public void | |
296 | addSeparator() | |
297 | { | |
2c20a171 KH |
298 | if (peer != null) |
299 | ((MenuPeer) peer).addSeparator(); | |
bda14505 TT |
300 | } |
301 | ||
302 | /*************************************************************************/ | |
303 | ||
304 | /** | |
305 | * Inserts a separator bar at the specified index value. | |
306 | * | |
307 | * @param index The index at which to insert a separator bar. | |
308 | * | |
309 | * XXX: FIXME | |
310 | * | |
311 | * @exception IllegalArgumentException If the index is less than zero. | |
312 | * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid. | |
313 | */ | |
314 | public void | |
315 | insertSeparator(int index) | |
316 | { | |
317 | insert(separator, index); | |
318 | } | |
319 | ||
320 | /*************************************************************************/ | |
321 | ||
322 | /** | |
323 | * Deletes the item at the specified index from this menu. | |
324 | * | |
325 | * @param index The index of the item to remove. | |
326 | * | |
327 | * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid. | |
328 | */ | |
329 | public synchronized void | |
330 | remove(int index) | |
331 | { | |
332 | items.removeElementAt(index); | |
333 | ||
334 | MenuPeer mp = (MenuPeer)getPeer(); | |
335 | if (mp != null) | |
336 | mp.delItem(index); | |
337 | } | |
338 | ||
339 | /*************************************************************************/ | |
340 | ||
341 | /** | |
342 | * Removes the specifed item from the menu. If the specified component | |
343 | * does not exist, this method does nothing. // FIXME: Right? | |
344 | * | |
345 | * @param item The component to remove. | |
346 | */ | |
347 | public void | |
348 | remove(MenuComponent item) | |
349 | { | |
350 | int index = items.indexOf(item); | |
351 | if (index == -1) | |
352 | return; | |
353 | ||
354 | remove(index); | |
355 | } | |
356 | ||
357 | /*************************************************************************/ | |
358 | ||
359 | /** | |
360 | * Removes all the elements from this menu. | |
361 | */ | |
362 | public synchronized void | |
363 | removeAll() | |
364 | { | |
365 | int count = getItemCount(); | |
366 | for(int i = 0; i < count; i++) | |
367 | { | |
368 | // We must always remove item 0. | |
369 | remove(0); | |
370 | } | |
371 | } | |
372 | ||
373 | /*************************************************************************/ | |
374 | ||
375 | /** | |
376 | * Creates the native peer for this object. | |
377 | */ | |
378 | public void | |
379 | addNotify() | |
380 | { | |
2c20a171 | 381 | if (peer == null) |
bda14505 | 382 | peer = getToolkit().createMenu(this); |
e300e74f | 383 | Enumeration e = items.elements(); |
2c20a171 KH |
384 | while (e.hasMoreElements()) |
385 | { | |
386 | MenuItem mi = (MenuItem)e.nextElement(); | |
387 | mi.addNotify(); | |
388 | } | |
bda14505 TT |
389 | super.addNotify (); |
390 | } | |
391 | ||
392 | /*************************************************************************/ | |
393 | ||
394 | /** | |
395 | * Destroys the native peer for this object. | |
396 | */ | |
397 | public void | |
398 | removeNotify() | |
399 | { | |
e300e74f KH |
400 | Enumeration e = items.elements(); |
401 | while (e.hasMoreElements()) | |
402 | { | |
403 | MenuItem mi = (MenuItem) e.nextElement(); | |
404 | mi.removeNotify(); | |
405 | } | |
bda14505 TT |
406 | super.removeNotify(); |
407 | } | |
408 | ||
409 | /*************************************************************************/ | |
410 | ||
411 | /** | |
412 | * Returns a debugging string for this menu. | |
413 | * | |
414 | * @return A debugging string for this menu. | |
415 | */ | |
416 | public String | |
417 | paramString() | |
418 | { | |
419 | return (",isTearOff=" + isTearOff + ",isHelpMenu=" + isHelpMenu | |
420 | + super.paramString()); | |
421 | } | |
422 | ||
423 | // Accessibility API not yet implemented. | |
424 | // public AccessibleContext getAccessibleContext() | |
425 | ||
426 | } // class Menu |