Source for java.awt.Menu

   1: /* Menu.java -- A Java AWT Menu
   2:    Copyright (C) 1999, 2002, 2004, 2006 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.MenuPeer;
  42: import java.io.Serializable;
  43: import java.util.Enumeration;
  44: import java.util.Vector;
  45: 
  46: import javax.accessibility.AccessibleContext;
  47: import javax.accessibility.AccessibleRole;
  48: 
  49: /**
  50:   * This class represents a pull down or tear off menu in Java's AWT.
  51:   *
  52:   * @author Aaron M. Renn (arenn@urbanophile.com)
  53:   */
  54: public class Menu extends MenuItem implements MenuContainer, Serializable
  55: {
  56: 
  57: /*
  58:  * Static Variables
  59:  */
  60: 
  61: // Serialization Constant
  62: private static final long serialVersionUID = -8809584163345499784L;
  63: 
  64: /*************************************************************************/
  65: 
  66: /*
  67:  * Instance Variables
  68:  */
  69: 
  70: /**
  71:   * @serial The actual items in the menu
  72:   */
  73: private Vector items = new Vector();
  74: 
  75: /**
  76:   * @serial Flag indicating whether or not this menu is a tear off
  77:   */
  78: private boolean tearOff;
  79: 
  80: /**
  81:   * @serial Indicates whether or not this is a help menu.
  82:   */
  83: private boolean isHelpMenu;
  84: 
  85:   /*
  86:    * @serial Unused in this implementation, but present in Sun's
  87:    * serialization spec.  Value obtained via reflection.
  88:    */
  89:   private int menuSerializedDataVersion = 1;
  90: 
  91: static final transient String separatorLabel = "-";
  92: 
  93: /*************************************************************************/
  94: 
  95: /*
  96:  * Constructors
  97:  */
  98: 
  99: /**
 100:   * Initializes a new instance of <code>Menu</code> with no label and that
 101:   * is not a tearoff;
 102:   *
 103:   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
 104:   */
 105: public
 106: Menu()
 107: {
 108: }
 109: 
 110: /*************************************************************************/
 111: 
 112: /**
 113:   * Initializes a new instance of <code>Menu</code> that is not a tearoff and
 114:   * that has the specified label.
 115:   *
 116:   * @param label The menu label.
 117:   *
 118:   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
 119:   */
 120: public
 121: Menu(String label)
 122: {
 123:   this(label, false);
 124: }
 125: 
 126: /*************************************************************************/
 127: 
 128: /**
 129:   * Initializes a new instance of <code>Menu</code> with the specified
 130:   * label and tearoff status.
 131:   *
 132:   * @param label The label for this menu
 133:   * @param isTearOff <code>true</code> if this menu is a tear off menu,
 134:   * <code>false</code> otherwise.
 135:   *
 136:   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
 137:   */
 138: public
 139: Menu(String label, boolean isTearOff)
 140: {
 141:   super(label);
 142: 
 143:   tearOff = isTearOff;
 144: 
 145:   if (label.equals("Help"))
 146:     isHelpMenu = true;
 147: 
 148:   if (GraphicsEnvironment.isHeadless())
 149:     throw new HeadlessException ();
 150: }
 151: 
 152: /*************************************************************************/
 153: 
 154: /*
 155:  * Instance Methods
 156:  */
 157: 
 158: /**
 159:   * Tests whether or not this menu is a tearoff.
 160:   *
 161:   * @return <code>true</code> if this menu is a tearoff, <code>false</code>
 162:   * otherwise.
 163:   */
 164: public boolean
 165: isTearOff()
 166: {
 167:   return(tearOff);
 168: }
 169: 
 170: /*************************************************************************/
 171: 
 172: /**
 173:   * Returns the number of items in this menu.
 174:   *
 175:   * @return The number of items in this menu.
 176:   */
 177: public int
 178: getItemCount()
 179: {
 180:   return countItems ();
 181: }
 182: 
 183: /**
 184:  * Returns the number of items in this menu.
 185:  *
 186:  * @return The number of items in this menu.
 187:  *
 188:  * @deprecated As of JDK 1.1, replaced by getItemCount().
 189:  */
 190: public int countItems ()
 191: {
 192:   return items.size ();
 193: }
 194:  
 195: /*************************************************************************/
 196: 
 197: /**
 198:   * Returns the item at the specified index.
 199:   *
 200:   * @return The item at the specified index.
 201:   *
 202:   * @exception ArrayIndexOutOfBoundsException If the index value is not valid.
 203:   */
 204: public MenuItem
 205: getItem(int index)
 206: {
 207:   return((MenuItem)items.elementAt(index));
 208: }
 209: 
 210: /*************************************************************************/
 211: 
 212: /**
 213:   * Adds the specified item to this menu.  If it was previously part of
 214:   * another menu, it is first removed from that menu.
 215:   *
 216:   * @param item The new item to add.
 217:   *
 218:   * @return The item that was added.
 219:   */
 220: public MenuItem
 221: add(MenuItem item)
 222: {
 223:   MenuContainer parent = item.getParent();
 224:   if (parent != null)
 225:     parent.remove(item);
 226: 
 227:   items.addElement(item);
 228:   item.setParent(this);
 229: 
 230:   if (peer != null)
 231:     {
 232:       item.addNotify();
 233:       MenuPeer mp = (MenuPeer) peer;
 234:       mp.addItem(item);
 235:     }
 236: 
 237:   return item;
 238: }
 239: 
 240: /*************************************************************************/
 241: 
 242: /**
 243:   * Add an item with the specified label to this menu.
 244:   *
 245:   * @param label The label of the menu item to add.
 246:   */
 247: public void
 248: add(String label)
 249: {
 250:   add(new MenuItem(label));
 251: }
 252: 
 253: /*************************************************************************/
 254: 
 255: /**
 256:   * Inserts the specified menu item into this menu at the specified index.
 257:   *
 258:   * @param item The menu item to add.
 259:   * @param index The index of the menu item.
 260:   *
 261:   * @exception IllegalArgumentException If the index is less than zero.
 262:   * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
 263:   */
 264: public void
 265: insert(MenuItem item, int index)
 266: {
 267:   if (index < 0)
 268:     throw new IllegalArgumentException("Index is less than zero");
 269: 
 270:   int count = getItemCount ();
 271: 
 272:   if (index >= count)
 273:     add(item);
 274:   else
 275:     {
 276:       MenuContainer parent = item.getParent();
 277:       if (parent != null)
 278:     parent.remove(item);
 279:       
 280:       items.insertElementAt(item, index);
 281:       item.setParent(this);
 282: 
 283:       MenuPeer peer = (MenuPeer) getPeer();
 284:       if (peer == null)
 285:     return;
 286: 
 287:       for (int i = count - 1; i >= index; i--)
 288:         peer.delItem(i);
 289: 
 290:       item.addNotify();
 291:       peer.addItem(item);
 292: 
 293:       for (int i = index; i < count; i++)
 294:         peer.addItem((MenuItem) items.elementAt (i));
 295:     }
 296: 
 297: }
 298: 
 299: /*************************************************************************/
 300: 
 301: /**
 302:   * Inserts an item with the specified label into this menu at the specified index.
 303:   *
 304:   * @param label The label of the item to add.
 305:   * @param index The index of the menu item.
 306:   *
 307:   * @exception IllegalArgumentException If the index is less than zero.
 308:   * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
 309:   */
 310: public void
 311: insert(String label, int index)
 312: {
 313:   insert(new MenuItem(label), index);
 314: }
 315: 
 316: /*************************************************************************/
 317: 
 318: /**
 319:   * Adds a separator bar at the current menu location.
 320:   */
 321: public void
 322: addSeparator()
 323: {
 324:   add(new MenuItem(separatorLabel));
 325: }
 326: 
 327: /*************************************************************************/
 328: 
 329: /**
 330:   * Inserts a separator bar at the specified index value.
 331:   *
 332:   * @param index The index at which to insert a separator bar.
 333:   *
 334:   * @exception IllegalArgumentException If the index is less than zero.
 335:   * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
 336:   */
 337: public void
 338: insertSeparator(int index)
 339: {
 340:   insert(new MenuItem(separatorLabel), index);
 341: }
 342: 
 343: /*************************************************************************/
 344: 
 345: /**
 346:   * Deletes the item at the specified index from this menu.
 347:   *
 348:   * @param index The index of the item to remove.
 349:   * 
 350:   * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
 351:   */
 352: public synchronized void
 353: remove(int index)
 354: {
 355:   MenuItem item = (MenuItem) items.remove(index);
 356: 
 357:   MenuPeer mp = (MenuPeer) getPeer();
 358:   if (mp != null)
 359:     {
 360:       mp.delItem(index);
 361:       item.removeNotify();
 362:     }
 363:   item.setParent(null);
 364: }
 365: 
 366: /*************************************************************************/
 367: 
 368: /**
 369:   * Removes the specifed item from the menu.  If the specified component
 370:   * does not exist, this method does nothing.
 371:   *
 372:   * @param item The component to remove.
 373:   */
 374: public void
 375: remove(MenuComponent item)
 376: {
 377:   int index = items.indexOf(item);
 378:   if (index == -1)
 379:     return;
 380: 
 381:   remove(index);
 382: }
 383: 
 384: /*************************************************************************/
 385: 
 386: /**
 387:   * Removes all the elements from this menu.
 388:   */
 389: public synchronized void
 390: removeAll()
 391: {
 392:   int count = getItemCount();
 393:   for(int i = 0; i < count; i++)
 394:     {
 395:       // We must always remove item 0.
 396:       remove(0);
 397:     }
 398: }
 399: 
 400: /*************************************************************************/
 401: 
 402: /**
 403:   * Creates the native peer for this object.
 404:   */
 405: public void
 406: addNotify()
 407: {
 408:   MenuPeer peer = (MenuPeer) getPeer();
 409:   if (peer == null)
 410:     {
 411:       peer = getToolkit().createMenu(this);
 412:       setPeer(peer);
 413:     }
 414: 
 415:   Enumeration e = items.elements();
 416:   while (e.hasMoreElements())
 417:   {
 418:     MenuItem mi = (MenuItem)e.nextElement();
 419:     mi.addNotify();
 420:     peer.addItem(mi);
 421:   }
 422: 
 423:   super.addNotify ();
 424: }
 425: 
 426: /*************************************************************************/
 427: 
 428: /**
 429:   * Destroys the native peer for this object.
 430:   */
 431: public void
 432: removeNotify()
 433: {
 434:   Enumeration e = items.elements();
 435:   while (e.hasMoreElements())
 436:   {
 437:     MenuItem mi = (MenuItem) e.nextElement();
 438:     mi.removeNotify();
 439:   }
 440:   super.removeNotify();
 441: }
 442: 
 443: /*************************************************************************/
 444: 
 445: /**
 446:   * Returns a debugging string for this menu.
 447:   *
 448:   * @return A debugging string for this menu.
 449:   */
 450: public String
 451: paramString()
 452: {
 453:   return (",tearOff=" + tearOff + ",isHelpMenu=" + isHelpMenu
 454:       + super.paramString());
 455: }
 456: 
 457:   /**
 458:    * Basic Accessibility class for Menu.  Details get provided in derived
 459:    * classes.
 460:    */
 461:   protected class AccessibleAWTMenu extends AccessibleAWTMenuItem
 462:   {
 463:     private static final long serialVersionUID = 5228160894980069094L;
 464: 
 465:     protected AccessibleAWTMenu()
 466:     {
 467:     }
 468:     
 469:     public AccessibleRole getAccessibleRole()
 470:     {
 471:       return AccessibleRole.MENU;
 472:     }
 473:   }
 474: 
 475:   /**
 476:    * Gets the AccessibleContext associated with this <code>Menu</code>.
 477:    * The context is created, if necessary.
 478:    *
 479:    * @return the associated context
 480:    */
 481:   public AccessibleContext getAccessibleContext()
 482:   {
 483:     /* Create the context if this is the first request */
 484:     if (accessibleContext == null)
 485:       accessibleContext = new AccessibleAWTMenu();
 486:     return accessibleContext;
 487:   }
 488: 
 489: } // class Menu