GNU Classpath (0.91) | |
Frames | No Frames |
1: /* MenuBar.java -- An AWT menu bar class 2: Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006 3: Free Software Foundation, Inc. 4: 5: This file is part of GNU Classpath. 6: 7: GNU Classpath is free software; you can redistribute it and/or modify 8: it under the terms of the GNU General Public License as published by 9: the Free Software Foundation; either version 2, or (at your option) 10: any later version. 11: 12: GNU Classpath is distributed in the hope that it will be useful, but 13: WITHOUT ANY WARRANTY; without even the implied warranty of 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15: General Public License for more details. 16: 17: You should have received a copy of the GNU General Public License 18: along with GNU Classpath; see the file COPYING. If not, write to the 19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20: 02110-1301 USA. 21: 22: Linking this library statically or dynamically with other modules is 23: making a combined work based on this library. Thus, the terms and 24: conditions of the GNU General Public License cover the whole 25: combination. 26: 27: As a special exception, the copyright holders of this library give you 28: permission to link this library with independent modules to produce an 29: executable, regardless of the license terms of these independent 30: modules, and to copy and distribute the resulting executable under 31: terms of your choice, provided that you also meet, for each linked 32: independent module, the terms and conditions of the license of that 33: module. An independent module is a module which is not derived from 34: or based on this library. If you modify this library, you may extend 35: this exception to your version of the library, but you are not 36: obligated to do so. If you do not wish to do so, delete this 37: exception statement from your version. */ 38: 39: 40: package java.awt; 41: 42: import java.awt.peer.MenuBarPeer; 43: 44: import java.io.Serializable; 45: import java.util.Enumeration; 46: import java.util.Vector; 47: 48: import javax.accessibility.Accessible; 49: import javax.accessibility.AccessibleContext; 50: import javax.accessibility.AccessibleRole; 51: 52: /** 53: * This class implements a menu bar in the AWT system. 54: * 55: * @author Aaron M. Renn (arenn@urbanophile.com) 56: * @author Tom Tromey (tromey@redhat.com) 57: * @author Andrew John Hughes (gnu_andrew@member.fsf.org) 58: */ 59: public class MenuBar extends MenuComponent 60: implements MenuContainer, Serializable, Accessible 61: { 62: 63: //Serialization Constant 64: private static final long serialVersionUID = -4930327919388951260L; 65: 66: /** 67: * @serial The menu used for providing help information 68: */ 69: private Menu helpMenu; 70: 71: /** 72: * @serial The menus contained in this menu bar. 73: */ 74: private Vector menus = new Vector(); 75: 76: /** 77: * Initializes a new instance of <code>MenuBar</code>. 78: * 79: * @throws HeadlessException if GraphicsEnvironment.isHeadless() is true 80: */ 81: public MenuBar() 82: { 83: if (GraphicsEnvironment.isHeadless()) 84: throw new HeadlessException(); 85: } 86: 87: /** 88: * Returns the help menu for this menu bar. This may be <code>null</code>. 89: * 90: * @return the help menu for this menu bar 91: */ 92: public Menu getHelpMenu() 93: { 94: return helpMenu; 95: } 96: 97: /** 98: * Sets the help menu for this menu bar. 99: * 100: * @param menu the new help menu for this menu bar 101: */ 102: public synchronized void setHelpMenu(Menu menu) 103: { 104: MenuBarPeer myPeer = (MenuBarPeer) getPeer (); 105: 106: if (helpMenu != null) 107: { 108: if (myPeer != null) 109: helpMenu.removeNotify(); 110: helpMenu.setParent(null); 111: } 112: helpMenu = menu; 113: 114: MenuContainer parent = menu.getParent(); 115: if (parent != null) 116: parent.remove(menu); 117: menu.setParent(this); 118: 119: if (myPeer != null) 120: { 121: menu.addNotify(); 122: myPeer.addHelpMenu(menu); 123: } 124: } 125: 126: /** 127: * Add a menu to this MenuBar. If the menu has already has a 128: * parent, it is first removed from its old parent before being 129: * added. 130: * 131: * @param menu the menu to add 132: * 133: * @return the menu that was added 134: */ 135: public synchronized Menu add(Menu menu) 136: { 137: MenuBarPeer myPeer = (MenuBarPeer) getPeer (); 138: 139: MenuContainer parent = menu.getParent(); 140: if (parent != null) 141: parent.remove(menu); 142: 143: menus.addElement(menu); 144: menu.setParent(this); 145: 146: if (myPeer != null) 147: { 148: menu.addNotify(); 149: myPeer.addMenu(menu); 150: } 151: return menu; 152: } 153: 154: /** 155: * Removes the menu at the specified index. 156: * 157: * @param index the index of the menu to remove from the menu bar 158: */ 159: public synchronized void remove(int index) 160: { 161: Menu m = (Menu) menus.remove(index); 162: MenuBarPeer mp = (MenuBarPeer) getPeer(); 163: 164: if (mp != null) 165: m.removeNotify(); 166: 167: m.setParent(null); 168: 169: if (mp != null) 170: mp.delMenu(index); 171: } 172: 173: /** 174: * Removes the specified menu from the menu bar. 175: * 176: * @param menu the menu to remove from the menu bar 177: */ 178: public void remove(MenuComponent menu) 179: { 180: int index = menus.indexOf(menu); 181: if (index == -1) 182: return; 183: 184: remove(index); 185: } 186: 187: /** 188: * Returns the number of elements in this menu bar. 189: * 190: * @return the number of elements in the menu bar 191: */ 192: public int getMenuCount() 193: { 194: return countMenus(); 195: } 196: 197: /** 198: * Returns the number of elements in this menu bar. 199: * 200: * @return the number of elements in the menu bar 201: * 202: * @deprecated This method is deprecated in favor of 203: * <code>getMenuCount()</code>. 204: */ 205: public int countMenus() 206: { 207: return menus.size() + (getHelpMenu() == null ? 0 : 1); 208: } 209: 210: /** 211: * Returns the menu at the specified index. 212: * 213: * @param index the index of the menu 214: * 215: * @return the requested menu 216: * 217: * @throws ArrayIndexOutOfBoundsException if the index is not valid 218: */ 219: public Menu getMenu(int index) 220: { 221: return (Menu) menus.elementAt(index); 222: } 223: 224: /** 225: * Creates this object's native peer. 226: */ 227: public void addNotify() 228: { 229: MenuBarPeer peer = (MenuBarPeer) getPeer(); 230: if (peer == null) 231: { 232: peer = getToolkit().createMenuBar(this); 233: setPeer(peer); 234: } 235: 236: Enumeration e = menus.elements(); 237: while (e.hasMoreElements()) 238: { 239: Menu mi = (Menu)e.nextElement(); 240: mi.addNotify(); 241: peer.addMenu(mi); 242: } 243: 244: if (helpMenu != null) 245: { 246: helpMenu.addNotify(); 247: peer.addHelpMenu(helpMenu); 248: } 249: } 250: 251: /** 252: * Destroys this object's native peer. 253: */ 254: public void removeNotify() 255: { 256: Enumeration e = menus.elements(); 257: while (e.hasMoreElements()) 258: { 259: Menu mi = (Menu) e.nextElement(); 260: mi.removeNotify(); 261: } 262: super.removeNotify(); 263: } 264: 265: /** 266: * Returns a list of all shortcuts for the menus in this menu bar. 267: * 268: * @return a list of all shortcuts for the menus in this menu bar 269: */ 270: public synchronized Enumeration shortcuts() 271: { 272: Vector shortcuts = new Vector(); 273: Enumeration e = menus.elements(); 274: 275: while (e.hasMoreElements()) 276: { 277: Menu menu = (Menu)e.nextElement(); 278: if (menu.getShortcut() != null) 279: shortcuts.addElement(menu.getShortcut()); 280: } 281: 282: return shortcuts.elements(); 283: } 284: 285: /** 286: * Returns the menu item for the specified shortcut, or <code>null</code> 287: * if no such item exists. 288: * 289: * @param shortcut the shortcut to return the menu item for 290: * 291: * @return the menu item for the specified shortcut 292: */ 293: public MenuItem getShortcutMenuItem(MenuShortcut shortcut) 294: { 295: Enumeration e = menus.elements(); 296: 297: while (e.hasMoreElements()) 298: { 299: Menu menu = (Menu) e.nextElement(); 300: MenuShortcut s = menu.getShortcut(); 301: if ((s != null) && s.equals(shortcut)) 302: return menu; 303: } 304: 305: return null; 306: } 307: 308: /** 309: * Deletes the specified menu shortcut. 310: * 311: * @param shortcut the shortcut to delete 312: */ 313: public void deleteShortcut(MenuShortcut shortcut) 314: { 315: MenuItem it; 316: // This is a slow implementation, but it probably doesn't matter. 317: while ((it = getShortcutMenuItem (shortcut)) != null) 318: it.deleteShortcut(); 319: } 320: 321: /** 322: * Gets the AccessibleContext associated with this <code>MenuBar</code>. 323: * The context is created, if necessary. 324: * 325: * @return the associated context 326: */ 327: public AccessibleContext getAccessibleContext() 328: { 329: // Create the context if this is the first request. 330: if (accessibleContext == null) 331: accessibleContext = new AccessibleAWTMenuBar(); 332: return accessibleContext; 333: } 334: 335: /** 336: * This class provides accessibility support for AWT menu bars. 337: * 338: * @author Andrew John Hughes (gnu_andrew@member.fsf.org) 339: */ 340: protected class AccessibleAWTMenuBar 341: extends AccessibleAWTMenuComponent 342: { 343: 344: /** 345: * Compatible with JDK 1.4.2 revision 5 346: */ 347: private static final long serialVersionUID = -8577604491830083815L; 348: 349: /** 350: * This is the default constructor, which simply calls the default 351: * constructor of the superclass. 352: */ 353: protected AccessibleAWTMenuBar() 354: { 355: super(); 356: } 357: 358: /** 359: * Returns the accessible role relating to the menu bar. 360: * 361: * @return <code>AccessibleRole.MENU_BAR</code> 362: */ 363: public AccessibleRole getAccessibleRole() 364: { 365: return AccessibleRole.MENU_BAR; 366: } 367: 368: } 369: 370: }
GNU Classpath (0.91) |