OSDN Git Service

* java/awt/MenuItem.java (paramString): Now protected.
[pf3gnuchains/gcc-fork.git] / libjava / java / awt / MenuBar.java
1 /* Copyright (C) 1999, 2000  Free Software Foundation
2
3    This file is part of libjava.
4
5 This software is copyrighted work licensed under the terms of the
6 Libjava License.  Please consult the file "LIBJAVA_LICENSE" for
7 details.  */
8
9 package java.awt;
10
11 import java.awt.peer.MenuBarPeer;
12 import java.util.Vector;
13 import java.util.Enumeration;
14 import java.util.NoSuchElementException;
15
16 /** This class implements a MenuBar, such as might appear across the
17  * top of a window.
18  * @author Tom Tromey <tromey@redhat.com>
19  * @date December 25, 2000
20  */
21 public class MenuBar extends MenuComponent implements MenuContainer
22 {
23   /** Create a new MenuBar.  */
24   public MenuBar ()
25   {
26     menus = new Vector ();
27   }
28
29   /** Add a menu to this MenuBar.  If the menu has already has a
30    * parent, it is first removed from its old parent before being
31    * added.
32    * @param menu The menu to add.
33    * @returns menu
34    */
35   public synchronized Menu add (Menu menu)
36   {
37     if (menu.parent != null)
38       menu.parent.remove (menu);
39
40     menu.parent = this;
41     menus.add (menu);
42
43     if (peer != null)
44       {
45         MenuBarPeer mp = (MenuBarPeer) peer;
46         mp.add (menu);
47       }
48
49     return menu;
50   }
51
52   /** This creates the component's peer.  */
53   public void addNotify ()
54   {
55     if (peer != null)
56       {
57         // This choice of toolkit seems unsatisfying, but I'm not sure
58         // what else to do.
59         peer = Toolkit.getDefaultToolkit ().createMenuBar (this);
60       }
61   }
62
63   /** @deprecated  Use getMenuCount() instead.  */
64   public int countMenus ()
65   {
66     return getMenuCount ();
67   }
68
69   /** Delete a keyboard shortcut.
70    * @param shortcut The short cut which should be deleted from all
71    *                 menus on this MenuBar.
72    */
73   public void deleteShortcut (MenuShortcut shortcut)
74   {
75     MenuItem it;
76     // This is a slow implementation, but it probably doesn't matter.
77     while ((it = getShortcutMenuItem (shortcut)) != null)
78       it.deleteShortcut ();
79   }
80
81   /** Returns the current Help menu.  */
82   public Menu getHelpMenu ()
83   {
84     return help_menu;
85   }
86
87   /** Returns a menu from this object.
88    * @param index Index of menu to return.
89    */
90   public Menu getMenu (int index)
91   {
92     return (Menu) menus.get (index);
93   }
94
95   /** Returns the number of menus on this MenuBar.  */
96   public int getMenuCount ()
97   {
98     return menus.size ();
99   }
100
101   /** Returns the menu item on this MenuBar with the specified
102    * shortcut.
103    * @param shortcut Shortcut to look for
104    */
105   public MenuItem getShortcutMenuItem (MenuShortcut shortcut)
106   {
107     Enumeration m = new MenuEnumeration (this);
108     while (m.hasMoreElements ())
109       {
110         MenuItem item = (MenuItem) m.nextElement ();
111         if (item.getShortcut () == shortcut)
112           return item;
113       }
114     return null;
115   }
116
117   /** Remove a menu from the menu bar.  If the menu is specified by
118    * component (and not index), and does not exist on the menu, then
119    * the method does nothing.  If the removed menu has a peer, it is
120    * destroyed.
121    * @param menu The menu to remove
122    * @param index The index of the menu to remove
123    */
124   public synchronized void remove (MenuComponent menu)
125   {
126     int s = menus.size ();
127     for (int i = 0; i < s; ++i)
128       {
129         if (menus.get (i) == menu)
130           {
131             remove (i);
132             break;
133           }
134       }
135   }
136
137   public synchronized void remove (int index)
138   {
139     Menu m = (Menu) menus.get (index);
140     menus.remove (index);
141     m.removeNotify ();
142     m.parent = null;
143
144     if (peer != null)
145       {
146         MenuBarPeer mp = (MenuBarPeer) peer;
147         mp.remove (index);
148       }
149   }
150
151   /** Set the Help menu for this MenuBar.  If a Help menu already
152    * exists, it is first removed.
153    * @param menu The new Help menu.
154    */
155   public synchronized void setHelpMenu (Menu menu)
156   {
157     if (help_menu != null)
158       {
159         help_menu.removeNotify ();
160         help_menu.parent = null;
161       }
162
163     if (menu.parent != null)
164       menu.parent.remove (menu);
165     if (menu.parent != null)
166       menu.parent.remove (menu);
167     menu.parent = this;
168
169     if (peer != null)
170       {
171         MenuBarPeer mp = (MenuBarPeer) peer;
172         mp.addHelpMenu (menu);
173       }
174   }
175
176   /** Returns an Enumeration which lists the keyboard shortcuts
177    * associated with menu items on this MenuBar.
178    */
179   public synchronized Enumeration shortcuts ()
180   {
181     return new ShortcutEnumeration (new MenuEnumeration (this));
182   }
183
184   // Iterate over the items of a menu.
185   private static class MenuEnumeration implements Enumeration
186   {
187     // Enumerate over the menu's items.
188     Enumeration main;
189     // Enumerate over a submenu.
190     Enumeration sub;
191     // Menubar so we can keep track of help menu too.
192     MenuBar menubar;
193
194     MenuEnumeration (Menu m)
195     {
196       sub = null;
197       menubar = null;
198       main = m.items.elements ();
199     }
200
201     MenuEnumeration (MenuBar mb)
202     {
203       sub = null;
204       menubar = mb;
205       main = mb.menus.elements ();
206     }
207
208     public boolean hasMoreElements ()
209     {
210       boolean r = false;
211       if (sub != null)
212         r = sub.hasMoreElements ();
213       if (! r)
214         r = main.hasMoreElements ();
215       if (! r && menubar != null)
216         {
217           if (menubar.help_menu != null)
218             {
219               main = new MenuEnumeration (menubar.help_menu);
220               r = main.hasMoreElements ();
221             }
222           menubar = null;
223         }
224       return r;
225     }
226
227     public Object nextElement () throws NoSuchElementException
228     {
229       while (true)
230         {
231           if (! sub.hasMoreElements ())
232             sub = null;
233           else
234             return sub.nextElement ();
235
236           if (! main.hasMoreElements () && menubar != null
237               && menubar.help_menu != null)
238             {
239               main = new MenuEnumeration (menubar.help_menu);
240               menubar = null;
241             }
242
243           Object r = main.nextElement ();
244           if (r instanceof Menu)
245             {
246               sub = new MenuEnumeration ((Menu) r);
247               continue;
248             }
249
250           return r;
251         }
252     }
253   }
254
255   // This is an enumeration that shadows another enumeration and
256   // returns the shortcut for each item returned.  I wonder if we're
257   // only supposed to return unique shortcuts?  If so then we could
258   // keep a hash table here and remove duplicates.
259   private static class ShortcutEnumeration implements Enumeration
260   {
261     Enumeration back;
262
263     ShortcutEnumeration (Enumeration back)
264     {
265       this.back = back;
266     }
267
268     public boolean hasMoreElements ()
269     {
270       return back.hasMoreElements ();
271     }
272
273     public Object nextElement () throws NoSuchElementException
274     {
275       while (true)
276         {
277           MenuItem item = (MenuItem) back.nextElement ();
278           if (item.getShortcut () != null)
279             return item.getShortcut ();
280         }
281     }
282   }
283
284   // We use Vector because it makes enumerating easier than ArrayList
285   // in this case.
286   Vector menus;
287   Menu help_menu;
288 }