GNU Classpath (0.20) | |
Frames | No Frames |
1: /* MenuBar.java -- An AWT menu bar class 2: Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 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: 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: /* 64: * Static Variables 65: */ 66: 67: // Serialization Constant 68: private static final long serialVersionUID = -4930327919388951260L; 69: 70: /*************************************************************************/ 71: 72: /* 73: * Instance Variables 74: */ 75: 76: /** 77: * @serial The menu used for providing help information 78: */ 79: private Menu helpMenu; 80: 81: /** 82: * @serial The menus contained in this menu bar. 83: */ 84: private Vector menus = new Vector(); 85: 86: /** 87: * The accessible context for this component. 88: * 89: * @see #getAccessibleContext() 90: * @serial ignored. 91: */ 92: private transient AccessibleContext accessibleContext; 93: 94: /*************************************************************************/ 95: 96: /* 97: * Constructors 98: */ 99: 100: /** 101: * Initializes a new instance of <code>MenuBar</code>. 102: * 103: * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true. 104: */ 105: public 106: MenuBar() 107: { 108: if (GraphicsEnvironment.isHeadless()) 109: throw new HeadlessException (); 110: } 111: 112: /*************************************************************************/ 113: 114: /* 115: * Instance Methods 116: */ 117: 118: /** 119: * Returns the help menu for this menu bar. This may be <code>null</code>. 120: * 121: * @return The help menu for this menu bar. 122: */ 123: public Menu 124: getHelpMenu() 125: { 126: return(helpMenu); 127: } 128: 129: /*************************************************************************/ 130: 131: /** 132: * Sets the help menu for this menu bar. 133: * 134: * @param menu The new help menu for this menu bar. 135: */ 136: public synchronized void 137: setHelpMenu(Menu menu) 138: { 139: if (helpMenu != null) 140: { 141: helpMenu.removeNotify (); 142: helpMenu.parent = null; 143: } 144: helpMenu = menu; 145: 146: if (menu.parent != null) 147: menu.parent.remove (menu); 148: menu.parent = this; 149: 150: MenuBarPeer peer = (MenuBarPeer) getPeer (); 151: if (peer != null) 152: { 153: menu.addNotify(); 154: peer.addHelpMenu (menu); 155: } 156: } 157: 158: /*************************************************************************/ 159: 160: /** Add a menu to this MenuBar. If the menu has already has a 161: * parent, it is first removed from its old parent before being 162: * added. 163: * 164: * @param menu The menu to add. 165: * 166: * @return The menu that was added. 167: */ 168: public synchronized Menu 169: add(Menu menu) 170: { 171: if (menu.parent != null) 172: menu.parent.remove (menu); 173: 174: menu.parent = this; 175: menus.addElement(menu); 176: 177: if (peer != null) 178: { 179: menu.addNotify(); 180: } 181: 182: return(menu); 183: } 184: 185: /*************************************************************************/ 186: 187: /** 188: * Removes the menu at the specified index. 189: * 190: * @param index The index of the menu to remove from the menu bar. 191: */ 192: public synchronized void 193: remove(int index) 194: { 195: Menu m = (Menu) menus.get (index); 196: menus.remove (index); 197: m.removeNotify (); 198: m.parent = null; 199: 200: if (peer != null) 201: { 202: MenuBarPeer mp = (MenuBarPeer) peer; 203: mp.delMenu (index); 204: } 205: } 206: 207: /*************************************************************************/ 208: 209: /** 210: * Removes the specified menu from the menu bar. 211: * 212: * @param menu The menu to remove from the menu bar. 213: */ 214: public void 215: remove(MenuComponent menu) 216: { 217: int index = menus.indexOf(menu); 218: if (index == -1) 219: return; 220: 221: remove(index); 222: } 223: 224: /*************************************************************************/ 225: 226: /** 227: * Returns the number of elements in this menu bar. 228: * 229: * @return The number of elements in the menu bar. 230: */ 231: public int 232: getMenuCount() 233: { 234: return countMenus (); 235: } 236: 237: /*************************************************************************/ 238: 239: /** 240: * Returns the number of elements in this menu bar. 241: * 242: * @return The number of elements in the menu bar. 243: * 244: * @deprecated This method is deprecated in favor of <code>getMenuCount()</code>. 245: */ 246: public int 247: countMenus() 248: { 249: return menus.size () + (getHelpMenu () == null ? 0 : 1); 250: } 251: 252: /*************************************************************************/ 253: 254: /** 255: * Returns the menu at the specified index. 256: * 257: * @param index the index of the menu 258: * 259: * @return The requested menu. 260: * 261: * @exception ArrayIndexOutOfBoundsException If the index is not valid. 262: */ 263: public Menu 264: getMenu(int index) 265: { 266: return((Menu)menus.elementAt(index)); 267: } 268: 269: /*************************************************************************/ 270: 271: /** 272: * Creates this object's native peer. 273: */ 274: public void 275: addNotify() 276: { 277: if (getPeer() == null) 278: setPeer((MenuComponentPeer)getToolkit().createMenuBar(this)); 279: Enumeration e = menus.elements(); 280: while (e.hasMoreElements()) 281: { 282: Menu mi = (Menu)e.nextElement(); 283: mi.addNotify(); 284: } 285: if (helpMenu != null) 286: { 287: helpMenu.addNotify(); 288: ((MenuBarPeer) peer).addHelpMenu(helpMenu); 289: } 290: } 291: 292: /*************************************************************************/ 293: 294: /** 295: * Destroys this object's native peer. 296: */ 297: public void 298: removeNotify() 299: { 300: Enumeration e = menus.elements(); 301: while (e.hasMoreElements()) 302: { 303: Menu mi = (Menu) e.nextElement(); 304: mi.removeNotify(); 305: } 306: super.removeNotify(); 307: } 308: 309: /*************************************************************************/ 310: 311: /** 312: * Returns a list of all shortcuts for the menus in this menu bar. 313: * 314: * @return A list of all shortcuts for the menus in this menu bar. 315: */ 316: public synchronized Enumeration 317: shortcuts() 318: { 319: Vector shortcuts = new Vector(); 320: Enumeration e = menus.elements(); 321: 322: while (e.hasMoreElements()) 323: { 324: Menu menu = (Menu)e.nextElement(); 325: if (menu.getShortcut() != null) 326: shortcuts.addElement(menu.getShortcut()); 327: } 328: 329: return(shortcuts.elements()); 330: } 331: 332: /*************************************************************************/ 333: 334: /** 335: * Returns the menu item for the specified shortcut, or <code>null</code> 336: * if no such item exists. 337: * 338: * @param shortcut The shortcut to return the menu item for. 339: * 340: * @return The menu item for the specified shortcut. 341: */ 342: public MenuItem 343: getShortcutMenuItem(MenuShortcut shortcut) 344: { 345: Enumeration e = menus.elements(); 346: 347: while (e.hasMoreElements()) 348: { 349: Menu menu = (Menu)e.nextElement(); 350: MenuShortcut s = menu.getShortcut(); 351: if ((s != null) && (s.equals(shortcut))) 352: return(menu); 353: } 354: 355: return(null); 356: } 357: 358: /*************************************************************************/ 359: 360: /** 361: * Deletes the specified menu shortcut. 362: * 363: * @param shortcut The shortcut to delete. 364: */ 365: public void 366: deleteShortcut(MenuShortcut shortcut) 367: { 368: MenuItem it; 369: // This is a slow implementation, but it probably doesn't matter. 370: while ((it = getShortcutMenuItem (shortcut)) != null) 371: it.deleteShortcut (); 372: } 373: 374: /** 375: * Gets the AccessibleContext associated with this <code>MenuBar</code>. 376: * The context is created, if necessary. 377: * 378: * @return the associated context 379: */ 380: public AccessibleContext getAccessibleContext() 381: { 382: /* Create the context if this is the first request */ 383: if (accessibleContext == null) 384: accessibleContext = new AccessibleAWTMenuBar(); 385: return accessibleContext; 386: } 387: 388: /** 389: * This class provides accessibility support for AWT menu bars. 390: * 391: * @author Andrew John Hughes (gnu_andrew@member.fsf.org) 392: */ 393: protected class AccessibleAWTMenuBar 394: extends AccessibleAWTMenuComponent 395: { 396: 397: /** 398: * Compatible with JDK 1.4.2 revision 5 399: */ 400: private static final long serialVersionUID = -8577604491830083815L; 401: 402: /** 403: * This is the default constructor, which simply calls the default 404: * constructor of the superclass. 405: */ 406: protected AccessibleAWTMenuBar() 407: { 408: super(); 409: } 410: 411: /** 412: * Returns the accessible role relating to the menu bar. 413: * 414: * @return <code>AccessibleRole.MENU_BAR</code>. 415: */ 416: public AccessibleRole getAccessibleRole() 417: { 418: return AccessibleRole.MENU_BAR; 419: } 420: 421: } // class AccessibleAWTMenuBar 422: 423: } // class MenuBar
GNU Classpath (0.20) |