| 1 | /* MenuBar.java -- An AWT menu bar class
|
|---|
| 2 | Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 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 |
|
|---|
| 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. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt;
|
|---|
| 40 |
|
|---|
| 41 | import java.awt.peer.MenuBarPeer;
|
|---|
| 42 | import java.awt.peer.MenuComponentPeer;
|
|---|
| 43 |
|
|---|
| 44 | import java.io.Serializable;
|
|---|
| 45 | import java.util.Enumeration;
|
|---|
| 46 | import java.util.Vector;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * This class implements a menu bar in the AWT system.
|
|---|
| 50 | *
|
|---|
| 51 | * @author Aaron M. Renn ([email protected])
|
|---|
| 52 | * @author Tom Tromey <[email protected]>
|
|---|
| 53 | */
|
|---|
| 54 | public class MenuBar extends MenuComponent
|
|---|
| 55 | implements MenuContainer, Serializable
|
|---|
| 56 | {
|
|---|
| 57 |
|
|---|
| 58 | /*
|
|---|
| 59 | * Static Variables
|
|---|
| 60 | */
|
|---|
| 61 |
|
|---|
| 62 | // Serialization Constant
|
|---|
| 63 | private static final long serialVersionUID = -4930327919388951260L;
|
|---|
| 64 |
|
|---|
| 65 | /*************************************************************************/
|
|---|
| 66 |
|
|---|
| 67 | /*
|
|---|
| 68 | * Instance Variables
|
|---|
| 69 | */
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * @serial The menu used for providing help information
|
|---|
| 73 | */
|
|---|
| 74 | private Menu helpMenu;
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * @serial The menus contained in this menu bar.
|
|---|
| 78 | */
|
|---|
| 79 | private Vector menus = new Vector();
|
|---|
| 80 |
|
|---|
| 81 | /*************************************************************************/
|
|---|
| 82 |
|
|---|
| 83 | /*
|
|---|
| 84 | * Constructors
|
|---|
| 85 | */
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * Initializes a new instance of <code>MenuBar</code>.
|
|---|
| 89 | */
|
|---|
| 90 | public
|
|---|
| 91 | MenuBar()
|
|---|
| 92 | {
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /*************************************************************************/
|
|---|
| 96 |
|
|---|
| 97 | /*
|
|---|
| 98 | * Instance Methods
|
|---|
| 99 | */
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Returns the help menu for this menu bar. This may be <code>null</code>.
|
|---|
| 103 | *
|
|---|
| 104 | * @return The help menu for this menu bar.
|
|---|
| 105 | */
|
|---|
| 106 | public Menu
|
|---|
| 107 | getHelpMenu()
|
|---|
| 108 | {
|
|---|
| 109 | return(helpMenu);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /*************************************************************************/
|
|---|
| 113 |
|
|---|
| 114 | /**
|
|---|
| 115 | * Sets the help menu for this menu bar.
|
|---|
| 116 | *
|
|---|
| 117 | * @param helpMenu The new help menu for this menu bar.
|
|---|
| 118 | */
|
|---|
| 119 | public synchronized void
|
|---|
| 120 | setHelpMenu(Menu menu)
|
|---|
| 121 | {
|
|---|
| 122 | if (helpMenu != null)
|
|---|
| 123 | {
|
|---|
| 124 | helpMenu.removeNotify ();
|
|---|
| 125 | helpMenu.parent = null;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | if (menu.parent != null)
|
|---|
| 129 | menu.parent.remove (menu);
|
|---|
| 130 | if (menu.parent != null)
|
|---|
| 131 | menu.parent.remove (menu);
|
|---|
| 132 | menu.parent = this;
|
|---|
| 133 |
|
|---|
| 134 | if (peer != null)
|
|---|
| 135 | {
|
|---|
| 136 | MenuBarPeer mp = (MenuBarPeer) peer;
|
|---|
| 137 | mp.addHelpMenu (menu);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /*************************************************************************/
|
|---|
| 142 |
|
|---|
| 143 | /** Add a menu to this MenuBar. If the menu has already has a
|
|---|
| 144 | * parent, it is first removed from its old parent before being
|
|---|
| 145 | * added.
|
|---|
| 146 | *
|
|---|
| 147 | * @param menu The menu to add.
|
|---|
| 148 | *
|
|---|
| 149 | * @return The menu that was added.
|
|---|
| 150 | */
|
|---|
| 151 | public synchronized Menu
|
|---|
| 152 | add(Menu menu)
|
|---|
| 153 | {
|
|---|
| 154 | if (menu.parent != null)
|
|---|
| 155 | menu.parent.remove (menu);
|
|---|
| 156 |
|
|---|
| 157 | menu.parent = this;
|
|---|
| 158 | menus.addElement(menu);
|
|---|
| 159 |
|
|---|
| 160 | if (peer != null)
|
|---|
| 161 | {
|
|---|
| 162 | MenuBarPeer mp = (MenuBarPeer) peer;
|
|---|
| 163 | mp.addMenu (menu);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | return(menu);
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /*************************************************************************/
|
|---|
| 170 |
|
|---|
| 171 | /**
|
|---|
| 172 | * Removes the menu at the specified index.
|
|---|
| 173 | *
|
|---|
| 174 | * @param index The index of the menu to remove from the menu bar.
|
|---|
| 175 | */
|
|---|
| 176 | public synchronized void
|
|---|
| 177 | remove(int index)
|
|---|
| 178 | {
|
|---|
| 179 | Menu m = (Menu) menus.get (index);
|
|---|
| 180 | menus.remove (index);
|
|---|
| 181 | m.removeNotify ();
|
|---|
| 182 | m.parent = null;
|
|---|
| 183 |
|
|---|
| 184 | if (peer != null)
|
|---|
| 185 | {
|
|---|
| 186 | MenuBarPeer mp = (MenuBarPeer) peer;
|
|---|
| 187 | mp.delMenu (index);
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /*************************************************************************/
|
|---|
| 192 |
|
|---|
| 193 | /**
|
|---|
| 194 | * Removes the specified menu from the menu bar.
|
|---|
| 195 | *
|
|---|
| 196 | * @param menu The menu to remove from the menu bar.
|
|---|
| 197 | */
|
|---|
| 198 | public void
|
|---|
| 199 | remove(MenuComponent menu)
|
|---|
| 200 | {
|
|---|
| 201 | int index = menus.indexOf(menu);
|
|---|
| 202 | if (index == -1)
|
|---|
| 203 | return;
|
|---|
| 204 |
|
|---|
| 205 | remove(index);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /*************************************************************************/
|
|---|
| 209 |
|
|---|
| 210 | /**
|
|---|
| 211 | * Returns the number of elements in this menu bar.
|
|---|
| 212 | *
|
|---|
| 213 | * @return The number of elements in the menu bar.
|
|---|
| 214 | */
|
|---|
| 215 | public int
|
|---|
| 216 | getMenuCount()
|
|---|
| 217 | {
|
|---|
| 218 | // FIXME: How does the help menu fit in here?
|
|---|
| 219 | return(menus.size());
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | /*************************************************************************/
|
|---|
| 223 |
|
|---|
| 224 | /**
|
|---|
| 225 | * Returns the number of elements in this menu bar.
|
|---|
| 226 | *
|
|---|
| 227 | * @return The number of elements in the menu bar.
|
|---|
| 228 | *
|
|---|
| 229 | * @deprecated This method is deprecated in favor of <code>getMenuCount()</code>.
|
|---|
| 230 | */
|
|---|
| 231 | public int
|
|---|
| 232 | countMenus()
|
|---|
| 233 | {
|
|---|
| 234 | return(getMenuCount());
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /*************************************************************************/
|
|---|
| 238 |
|
|---|
| 239 | /**
|
|---|
| 240 | * Returns the menu at the specified index.
|
|---|
| 241 | *
|
|---|
| 242 | * @return The requested menu.
|
|---|
| 243 | *
|
|---|
| 244 | * @exception ArrayIndexOutOfBoundsException If the index is not valid.
|
|---|
| 245 | */
|
|---|
| 246 | public Menu
|
|---|
| 247 | getMenu(int index)
|
|---|
| 248 | {
|
|---|
| 249 | return((Menu)menus.elementAt(index));
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | /*************************************************************************/
|
|---|
| 253 |
|
|---|
| 254 | /**
|
|---|
| 255 | * Creates this object's native peer.
|
|---|
| 256 | */
|
|---|
| 257 | public void
|
|---|
| 258 | addNotify()
|
|---|
| 259 | {
|
|---|
| 260 | if (getPeer() == null)
|
|---|
| 261 | setPeer((MenuComponentPeer)getToolkit().createMenuBar(this));
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | /*************************************************************************/
|
|---|
| 265 |
|
|---|
| 266 | /**
|
|---|
| 267 | * Destroys this object's native peer.
|
|---|
| 268 | */
|
|---|
| 269 | public void
|
|---|
| 270 | removeNotify()
|
|---|
| 271 | {
|
|---|
| 272 | super.removeNotify();
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /*************************************************************************/
|
|---|
| 276 |
|
|---|
| 277 | /**
|
|---|
| 278 | * Returns a list of all shortcuts for the menus in this menu bar.
|
|---|
| 279 | *
|
|---|
| 280 | * @return A list of all shortcuts for the menus in this menu bar.
|
|---|
| 281 | */
|
|---|
| 282 | public synchronized Enumeration
|
|---|
| 283 | shortcuts()
|
|---|
| 284 | {
|
|---|
| 285 | Vector shortcuts = new Vector();
|
|---|
| 286 | Enumeration e = menus.elements();
|
|---|
| 287 |
|
|---|
| 288 | while (e.hasMoreElements())
|
|---|
| 289 | {
|
|---|
| 290 | Menu menu = (Menu)e.nextElement();
|
|---|
| 291 | if (menu.getShortcut() != null)
|
|---|
| 292 | shortcuts.addElement(menu.getShortcut());
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | return(shortcuts.elements());
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | /*************************************************************************/
|
|---|
| 299 |
|
|---|
| 300 | /**
|
|---|
| 301 | * Returns the menu item for the specified shortcut, or <code>null</code>
|
|---|
| 302 | * if no such item exists.
|
|---|
| 303 | *
|
|---|
| 304 | * @param shortcut The shortcut to return the menu item for.
|
|---|
| 305 | *
|
|---|
| 306 | * @return The menu item for the specified shortcut.
|
|---|
| 307 | */
|
|---|
| 308 | public MenuItem
|
|---|
| 309 | getShortcutMenuItem(MenuShortcut shortcut)
|
|---|
| 310 | {
|
|---|
| 311 | Enumeration e = menus.elements();
|
|---|
| 312 |
|
|---|
| 313 | while (e.hasMoreElements())
|
|---|
| 314 | {
|
|---|
| 315 | Menu menu = (Menu)e.nextElement();
|
|---|
| 316 | MenuShortcut s = menu.getShortcut();
|
|---|
| 317 | if ((s != null) && (s.equals(shortcut)))
|
|---|
| 318 | return(menu);
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | return(null);
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | /*************************************************************************/
|
|---|
| 325 |
|
|---|
| 326 | /**
|
|---|
| 327 | * Deletes the specified menu shortcut.
|
|---|
| 328 | *
|
|---|
| 329 | * @param shortcut The shortcut to delete.
|
|---|
| 330 | */
|
|---|
| 331 | public void
|
|---|
| 332 | deleteShortcut(MenuShortcut shortcut)
|
|---|
| 333 | {
|
|---|
| 334 | MenuItem it;
|
|---|
| 335 | // This is a slow implementation, but it probably doesn't matter.
|
|---|
| 336 | while ((it = getShortcutMenuItem (shortcut)) != null)
|
|---|
| 337 | it.deleteShortcut ();
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | } // class MenuBar
|
|---|