Source for javax.swing.JPopupMenu

   1: /* JPopupMenu.java --
   2:    Copyright (C) 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 javax.swing;
  40: 
  41: import java.awt.Component;
  42: import java.awt.Dimension;
  43: import java.awt.Insets;
  44: import java.awt.Point;
  45: import java.awt.event.KeyEvent;
  46: import java.awt.event.MouseEvent;
  47: import java.beans.PropertyChangeEvent;
  48: import java.beans.PropertyChangeListener;
  49: import java.util.ArrayList;
  50: import java.util.EventListener;
  51: 
  52: import javax.accessibility.Accessible;
  53: import javax.accessibility.AccessibleContext;
  54: import javax.accessibility.AccessibleRole;
  55: import javax.swing.event.PopupMenuEvent;
  56: import javax.swing.event.PopupMenuListener;
  57: import javax.swing.plaf.PopupMenuUI;
  58: 
  59: /**
  60:  * JPopupMenu is a container that is used to display popup menu's menu
  61:  * items. By default JPopupMenu is a lightweight container, however if it
  62:  * is the case that JPopupMenu's bounds are outside of main window, then
  63:  * heawyweight container will be used to display menu items. It is also
  64:  * possible to change JPopupMenu's default  behavior and set JPopupMenu
  65:  * to always use heavyweight container.
  66:  *
  67:  * JPopupMenu can be displayed anywhere; it is a floating free popup menu.
  68:  * However before JPopupMenu is diplayed, its invoker property should be set.
  69:  * JPopupMenu's invoker is a component relative to which popup menu is
  70:  * displayed.
  71:  *
  72:  * JPopupMenu fires PopupMenuEvents to its registered listeners. Whenever
  73:  * JPopupMenu becomes visible on the screen then PopupMenuEvent indicating
  74:  * that popup menu became visible will be fired. In the case when
  75:  * JPopupMenu becomes invisible or cancelled without selection, then
  76:  * popupMenuBecomeInvisible() or popupMenuCancelled() methods of
  77:  * PopupMenuListeners will be invoked.
  78:  *
  79:  * JPopupMenu also fires PropertyChangeEvents when its bound properties 
  80:  * change.In addittion to inheritted bound properties, JPopupMenu has 
  81:  * 'visible' bound property. When JPopupMenu becomes visible/invisible on
  82:  * the screen it fires PropertyChangeEvents to its registered 
  83:  * PropertyChangeListeners.
  84:  */
  85: public class JPopupMenu extends JComponent implements Accessible, MenuElement
  86: {
  87:   private static final long serialVersionUID = -8336996630009646009L;
  88: 
  89:   /* indicates if popup's menu border should be painted*/
  90:   private boolean borderPainted = true;
  91: 
  92:   /** Flag indicating whether lightweight, mediumweight or heavyweight popup
  93:      is used to display menu items.
  94: 
  95:      These are the possible cases:
  96: 
  97:      1. if DefaultLightWeightPopupEnabled true
  98:          (i)  use lightweight container if popup feets inside top-level window
  99:          (ii) only use heavyweight container (JDialog) if popup doesn't fit.
 100: 
 101:      2. if DefaultLightWeightPopupEnabled false
 102:          (i) if popup fits, use awt.Panel (mediumWeight)
 103:          (ii) if popup doesn't fit, use JDialog (heavyWeight)
 104:   */
 105:   private static boolean DefaultLightWeightPopupEnabled = true;
 106: 
 107:   /* Component that invokes popup menu. */
 108:   transient Component invoker;
 109: 
 110:   /* Label for this popup menu. It is not used in most of the look and feel themes. */
 111:   private String label;
 112: 
 113:   /*Amount of space between menuItem's in JPopupMenu and JPopupMenu's border */
 114:   private Insets margin;
 115: 
 116:   /** Indicates whether ligthWeight container can be used to display popup
 117:      menu. This flag is the same as DefaultLightWeightPopupEnabled, but setting
 118:      this flag can change popup menu after creation of the object */
 119:   private boolean lightWeightPopupEnabled;
 120: 
 121:   /** SelectionModel that keeps track of menu selection. */
 122:   private SingleSelectionModel selectionModel;
 123: 
 124:   /* Popup that is used to display JPopupMenu */
 125:   private transient Popup popup;
 126: 
 127:   /**
 128:    * Location of the popup, X coordinate.
 129:    */
 130:   private int popupLocationX;
 131: 
 132:   /**
 133:    * Location of the popup, Y coordinate.
 134:    */
 135:   private int popupLocationY;
 136: 
 137:   /* Field indicating if popup menu is visible or not */
 138:   private boolean visible = false;
 139:   
 140:   /**
 141:    * Creates a new JPopupMenu object.
 142:    */
 143:   public JPopupMenu()
 144:   {
 145:     this(null);
 146:   }
 147: 
 148:   /**
 149:    * Creates a new JPopupMenu with specified label
 150:    *
 151:    * @param label Label for popup menu.
 152:    */
 153:   public JPopupMenu(String label)
 154:   {
 155:     lightWeightPopupEnabled = getDefaultLightWeightPopupEnabled();
 156:     setLabel(label);
 157:     setSelectionModel(new DefaultSingleSelectionModel());
 158:     super.setVisible(false);
 159:     updateUI();
 160:   }
 161: 
 162:   /**
 163:   * Adds given menu item to the popup menu
 164:   *
 165:   * @param item menu item to add to the popup menu
 166:   *
 167:   * @return menu item that was added to the popup menu
 168:   */
 169:   public JMenuItem add(JMenuItem item)
 170:   {
 171:     this.insert(item, -1);
 172:     return item;
 173:   }
 174: 
 175:   /**
 176:    * Constructs menu item with a specified label and adds it to
 177:    * popup menu
 178:    *
 179:    * @param text label for the menu item to be added
 180:    *
 181:    * @return constructed menu item that was added to the popup menu
 182:    */
 183:   public JMenuItem add(String text)
 184:   {
 185:     JMenuItem item = new JMenuItem(text);
 186:     return add(item);
 187:   }
 188: 
 189:   /**
 190:    * Constructs menu item associated with the specified action
 191:    * and adds it to the popup menu
 192:    *
 193:    * @param action Action for the new menu item
 194:    *
 195:    * @return menu item that was added to the menu
 196:    */
 197:   public JMenuItem add(Action action)
 198:   {
 199:     JMenuItem item = createActionComponent(action);
 200: 
 201:     if (action != null)
 202:       action.addPropertyChangeListener(createActionChangeListener(item));
 203: 
 204:     return add(item);
 205:   }
 206: 
 207:   /**
 208:    * Revomes component at the given index from the menu.
 209:    *
 210:    * @param index index of the component that will be removed in the menu
 211:    */
 212:   public void remove(int index)
 213:   {
 214:     super.remove(index);
 215:     revalidate();
 216:   }
 217: 
 218:   /**
 219:    * Create menu item associated with the given action
 220:    * and inserts it into the popup menu at the specified index
 221:    *
 222:    * @param action Action for the new menu item
 223:    * @param index index in the popup menu at which to insert new menu item.
 224:    */
 225:   public void insert(Action action, int index)
 226:   {
 227:     JMenuItem item = new JMenuItem(action);
 228:     this.insert(item, index);
 229:   }
 230: 
 231:   /**
 232:    * Insert given component to the popup menu at the
 233:    * specified index
 234:    *
 235:    * @param component Component to insert
 236:    * @param index Index at which to insert given component
 237:    */
 238:   public void insert(Component component, int index)
 239:   {
 240:     super.add(component, index);
 241:   }
 242: 
 243:   /**
 244:    * Returns flag indicating if newly created JPopupMenu will use
 245:    * heavyweight or lightweight container to display its menu items
 246:    *
 247:    * @return true if JPopupMenu will use lightweight container to display
 248:    * menu items by default, and false otherwise.
 249:    */
 250:   public static boolean getDefaultLightWeightPopupEnabled()
 251:   {
 252:     return DefaultLightWeightPopupEnabled;
 253:   }
 254: 
 255:   /**
 256:    * Sets whether JPopupMenu should use ligthWeight container to
 257:    * display it menu items by default
 258:    *
 259:    * @param enabled true if JPopupMenu should use lightweight container
 260:    * for displaying its menu items, and false otherwise.
 261:    */
 262:   public static void setDefaultLightWeightPopupEnabled(boolean enabled)
 263:   {
 264:     DefaultLightWeightPopupEnabled = enabled;
 265:   }
 266: 
 267:   /**
 268:    * This method returns the UI used to display the JPopupMenu.
 269:    *
 270:    * @return The UI used to display the JPopupMenu.
 271:    */
 272:   public PopupMenuUI getUI()
 273:   {
 274:     return (PopupMenuUI) ui;
 275:   }
 276: 
 277:   /**
 278:    * Set the "UI" property of the menu item, which is a look and feel class
 279:    * responsible for handling popupMenu's input events and painting it.
 280:    *
 281:    * @param ui The new "UI" property
 282:    */
 283:   public void setUI(PopupMenuUI ui)
 284:   {
 285:     super.setUI(ui);
 286:   }
 287: 
 288:   /**
 289:    * This method sets this menuItem's UI to the UIManager's default for the
 290:    * current look and feel.
 291:    */
 292:   public void updateUI()
 293:   {
 294:     setUI((PopupMenuUI) UIManager.getUI(this));
 295:   }
 296: 
 297:   /**
 298:    * This method returns a name to identify which look and feel class will be
 299:    * the UI delegate for the menuItem.
 300:    *
 301:    * @return The Look and Feel classID. "PopupMenuUI"
 302:    */
 303:   public String getUIClassID()
 304:   {
 305:     return "PopupMenuUI";
 306:   }
 307: 
 308:   /**
 309:    * Returns selectionModel used by this popup menu to keep
 310:    * track of the selection.
 311:    *
 312:    * @return popup menu's selection model
 313:    */
 314:   public SingleSelectionModel getSelectionModel()
 315:   {
 316:     return selectionModel;
 317:   }
 318: 
 319:   /**
 320:    * Sets selection model for this popup menu
 321:    *
 322:    * @param model new selection model of this popup menu
 323:    */
 324:   public void setSelectionModel(SingleSelectionModel model)
 325:   {
 326:     selectionModel = model;
 327:   }
 328: 
 329:   /**
 330:    * Creates new menu item associated with a given action.
 331:    *
 332:    * @param action Action used to create new menu item
 333:    *
 334:    * @return new created menu item associated with a given action.
 335:    */
 336:   protected JMenuItem createActionComponent(Action action)
 337:   {
 338:     return new JMenuItem(action);
 339:   }
 340: 
 341:   /**
 342:    * Creates PropertyChangeListener that listens to PropertyChangeEvents
 343:    * occuring in the Action associated with given menu item in this popup menu.
 344:    *
 345:    * @param item MenuItem
 346:    *
 347:    * @return The PropertyChangeListener
 348:    */
 349:   protected PropertyChangeListener createActionChangeListener(JMenuItem item)
 350:   {
 351:     return new ActionChangeListener();
 352:   }
 353: 
 354:   /**
 355:    * Returns true if this popup menu will display its menu item in
 356:    * a lightweight container and false otherwise.
 357:    *
 358:    * @return true if this popup menu will display its menu items
 359:    * in a lightweight container and false otherwise.
 360:    */
 361:   public boolean isLightWeightPopupEnabled()
 362:   {
 363:     return lightWeightPopupEnabled;
 364:   }
 365: 
 366:   /**
 367:    * DOCUMENT ME!
 368:    *
 369:    * @param enabled DOCUMENT ME!
 370:    */
 371:   public void setLightWeightPopupEnabled(boolean enabled)
 372:   {
 373:     lightWeightPopupEnabled = enabled;
 374:   }
 375: 
 376:   /**
 377:    * Returns label for this popup menu
 378:    *
 379:    * @return label for this popup menu
 380:    */
 381:   public String getLabel()
 382:   {
 383:     return label;
 384:   }
 385: 
 386:   /**
 387:    * Sets label for this popup menu. This method fires PropertyChangeEvent
 388:    * when the label property is changed. Please note that most
 389:    * of the Look & Feel will ignore this property.
 390:    *
 391:    * @param label label for this popup menu
 392:    */
 393:   public void setLabel(String label)
 394:   {
 395:     if (label != this.label)
 396:       {
 397:     String oldLabel = this.label;
 398:     this.label = label;
 399:     firePropertyChange("label", oldLabel, label);
 400:       }
 401:   }
 402: 
 403:   /**
 404:    * Adds separator to this popup menu
 405:    */
 406:   public void addSeparator()
 407:   {
 408:     // insert separator at the end of the list of menu items    
 409:     this.insert(new Separator(), -1);
 410:   }
 411: 
 412:   /**
 413:    * Adds popupMenuListener to listen for PopupMenuEvents fired
 414:    * by the JPopupMenu
 415:    *
 416:    * @param listener PopupMenuListener to add to JPopupMenu
 417:    */
 418:   public void addPopupMenuListener(PopupMenuListener listener)
 419:   {
 420:     listenerList.add(PopupMenuListener.class, listener);
 421:   }
 422: 
 423:   /**
 424:    * Removes PopupMenuListener from JPopupMenu's list of listeners
 425:    *
 426:    * @param listener PopupMenuListener which needs to be removed
 427:    */
 428:   public void removePopupMenuListener(PopupMenuListener listener)
 429:   {
 430:     listenerList.remove(PopupMenuListener.class, listener);
 431:   }
 432: 
 433:   /**
 434:    * Returns array of PopupMenuListeners that are listening to JPopupMenu
 435:    *
 436:    * @return Array of PopupMenuListeners that are listening to JPopupMenu
 437:    */
 438:   public PopupMenuListener[] getPopupMenuListeners()
 439:   {
 440:     return ((PopupMenuListener[]) listenerList.getListeners(PopupMenuListener.class));
 441:   }
 442: 
 443:   /**
 444:    * This method calls popupMenuWillBecomeVisible() of popup menu's
 445:    * PopupMenuListeners. This method is invoked just before popup menu
 446:    * will appear on the screen.
 447:    */
 448:   protected void firePopupMenuWillBecomeVisible()
 449:   {
 450:     EventListener[] ll = listenerList.getListeners(PopupMenuListener.class);
 451: 
 452:     for (int i = 0; i < ll.length; i++)
 453:       ((PopupMenuListener) ll[i]).popupMenuWillBecomeVisible(new PopupMenuEvent(this));
 454:   }
 455: 
 456:   /**
 457:    * This method calls popupMenuWillBecomeInvisible() of popup
 458:    * menu's PopupMenuListeners. This method is invoked just before popup
 459:    * menu will disappear from the screen
 460:    */
 461:   protected void firePopupMenuWillBecomeInvisible()
 462:   {
 463:     EventListener[] ll = listenerList.getListeners(PopupMenuListener.class);
 464: 
 465:     for (int i = 0; i < ll.length; i++)
 466:       ((PopupMenuListener) ll[i]).popupMenuWillBecomeInvisible(new PopupMenuEvent(this));
 467:   }
 468: 
 469:   /**
 470:    * This method calls popupMenuCanceled() of popup menu's PopupMenuListeners.
 471:    * This method is invoked just before popup menu is cancelled. This happens
 472:    * when popup menu is closed without selecting any of its menu items. This
 473:    * usually happens when the top-level window is resized or moved.
 474:    */
 475:   protected void firePopupMenuCanceled()
 476:   {
 477:     EventListener[] ll = listenerList.getListeners(PopupMenuListener.class);
 478: 
 479:     for (int i = 0; i < ll.length; i++)
 480:       ((PopupMenuListener) ll[i]).popupMenuCanceled(new PopupMenuEvent(this));
 481:   }
 482: 
 483:   /**
 484:    * This methods sets popup menu's size to its' preferred size. If the
 485:    * popup menu's size is previously set it will be ignored.
 486:    */
 487:   public void pack()
 488:   {
 489:     // Hook up this call so that it gets executed on the event thread in order
 490:     // to avoid synchronization problems when calling the layout manager.
 491:     if (! SwingUtilities.isEventDispatchThread())
 492:       {
 493:         SwingUtilities.invokeLater(new Runnable()
 494:           {
 495:             public void run()
 496:             {
 497:               show();
 498:             }
 499:           });
 500:       }
 501: 
 502:     setSize(getPreferredSize());
 503:   }
 504: 
 505:   /**
 506:    * Return visibility of the popup menu
 507:    *
 508:    * @return true if popup menu is visible on the screen and false otherwise.
 509:    */
 510:   public boolean isVisible()
 511:   {
 512:     return visible;
 513:   }
 514: 
 515:   /**
 516:    * Sets visibility property of this popup menu. If the property is
 517:    * set to true then popup menu will be dispayed and popup menu will
 518:    * hide itself if visible property is set to false.
 519:    *
 520:    * @param visible true if popup menu will become visible and false otherwise.
 521:    */
 522:   public void setVisible(final boolean visible)
 523:   {
 524:     // Hook up this call so that it gets executed on the event thread in order
 525:     // to avoid synchronization problems when calling the layout manager.
 526:     if (! SwingUtilities.isEventDispatchThread())
 527:       {
 528:         SwingUtilities.invokeLater(new Runnable()
 529:           {
 530:             public void run()
 531:             {
 532:               setVisible(visible);
 533:             }
 534:           });
 535:       }
 536: 
 537:     if (visible == isVisible())
 538:       return;
 539: 
 540:     boolean old = isVisible();
 541:     this.visible = visible;
 542:     if (old != isVisible())
 543:       {
 544:         if (visible)
 545:           {
 546:             if (invoker != null && !(invoker instanceof JMenu))
 547:               {
 548:                 MenuElement[] menuEls;
 549:                 if (getSubElements().length > 0)
 550:                   {
 551:                     menuEls = new MenuElement[2];
 552:                     menuEls[0] = this;
 553:                     menuEls[1] = getSubElements()[0];
 554:                 }
 555:                 else
 556:                   {
 557:                     menuEls = new MenuElement[1];
 558:                     menuEls[0] = this;
 559:                   }
 560:                 MenuSelectionManager.defaultManager().setSelectedPath(menuEls);
 561:               }
 562:             firePopupMenuWillBecomeVisible();
 563:             PopupFactory pf = PopupFactory.getSharedInstance();
 564:             pack();
 565:             popup = pf.getPopup(invoker, this, popupLocationX, popupLocationY);
 566:             popup.show();
 567:           }
 568:         else
 569:           {
 570:             getSelectionModel().clearSelection();
 571:             firePopupMenuWillBecomeInvisible();
 572:             popup.hide();
 573:           }
 574:         firePropertyChange("visible", old, isVisible());
 575:       }
 576:   }
 577: 
 578:   /**
 579:    * Sets location of the popup menu.
 580:    *
 581:    * @param x X coordinate of the popup menu's location
 582:    * @param y Y coordinate of the popup menu's location
 583:    */
 584:   public void setLocation(int x, int y)
 585:   {
 586:     popupLocationX = x;
 587:     popupLocationY = y;
 588:     // Handle the case when the popup is already showing. In this case we need
 589:     // to fetch a new popup from PopupFactory and use this. See the general
 590:     // contract of the PopupFactory.
 591:   }
 592: 
 593:   /**
 594:    * Returns popup menu's invoker.
 595:    *
 596:    * @return popup menu's invoker
 597:    */
 598:   public Component getInvoker()
 599:   {
 600:     return invoker;
 601:   }
 602: 
 603:   /**
 604:    * Sets popup menu's invoker.
 605:    *
 606:    * @param component The new invoker of this popup menu
 607:    */
 608:   public void setInvoker(Component component)
 609:   {
 610:     invoker = component;
 611:   }
 612: 
 613:   /**
 614:    * This method displays JPopupMenu on the screen at the specified
 615:    * location. Note that x and y coordinates given to this method
 616:    * should be expressed in terms of the popup menus' invoker.
 617:    *
 618:    * @param component Invoker for this popup menu
 619:    * @param x x-coordinate of the popup menu relative to the specified invoker
 620:    * @param y y-coordiate of the popup menu relative to the specified invoker
 621:    */
 622:   public void show(Component component, int x, int y)
 623:   {
 624:     if (component.isShowing())
 625:       {
 626:         setInvoker(component);
 627:         Point p = new Point(x, y);
 628:         SwingUtilities.convertPointToScreen(p, component);
 629:         setLocation(p.x, p.y);
 630:         setVisible(true);
 631:       }
 632:   }
 633: 
 634:   /**
 635:    * Returns component located at the specified index in the popup menu
 636:    *
 637:    * @param index index of the component to return
 638:    *
 639:    * @return component located at the specified index in the popup menu
 640:    *
 641:    * @deprecated Replaced by getComponent(int)
 642:    */
 643:   public Component getComponentAtIndex(int index)
 644:   {
 645:     return getComponent(index);
 646:   }
 647: 
 648:   /**
 649:    * Returns index of the specified component in the popup menu
 650:    *
 651:    * @param component Component to look for
 652:    *
 653:    * @return index of the specified component in the popup menu
 654:    */
 655:   public int getComponentIndex(Component component)
 656:   {
 657:     Component[] items = getComponents();
 658: 
 659:     for (int i = 0; i < items.length; i++)
 660:       {
 661:     if (items[i].equals(component))
 662:       return i;
 663:       }
 664: 
 665:     return -1;
 666:   }
 667: 
 668:   /**
 669:    * Sets size of the popup
 670:    *
 671:    * @param size Dimensions representing new size of the popup menu
 672:    */
 673:   public void setPopupSize(Dimension size)
 674:   {
 675:     super.setSize(size);
 676:   }
 677: 
 678:   /**
 679:    * Sets size of the popup menu
 680:    *
 681:    * @param width width for the new size
 682:    * @param height height for the new size
 683:    */
 684:   public void setPopupSize(int width, int height)
 685:   {
 686:     super.setSize(width, height);
 687:   }
 688: 
 689:   /**
 690:    * Selects specified component in this popup menu.
 691:    *
 692:    * @param selected component to select
 693:    */
 694:   public void setSelected(Component selected)
 695:   {
 696:     int index = getComponentIndex(selected);
 697:     selectionModel.setSelectedIndex(index);
 698:   }
 699: 
 700:   /**
 701:    * Checks if this popup menu paints its border.
 702:    *
 703:    * @return true if this popup menu paints its border and false otherwise.
 704:    */
 705:   public boolean isBorderPainted()
 706:   {
 707:     return borderPainted;
 708:   }
 709: 
 710:   /**
 711:    * Sets if the border of the popup menu should be
 712:    * painter or not.
 713:    *
 714:    * @param painted true if the border should be painted and false otherwise
 715:    */
 716:   public void setBorderPainted(boolean painted)
 717:   {
 718:     borderPainted = painted;
 719:   }
 720: 
 721:   /**
 722:    * Returns margin for this popup menu.
 723:    *
 724:    * @return margin for this popup menu.
 725:    */
 726:   public Insets getMargin()
 727:   {
 728:     return margin;
 729:   }
 730: 
 731:   /**
 732:    * A string that describes this JPopupMenu. Normally only used
 733:    * for debugging.
 734:    *
 735:    * @return A string describing this JMenuItem
 736:    */
 737:   protected String paramString()
 738:   {
 739:     StringBuffer sb = new StringBuffer();
 740:     sb.append(super.paramString());
 741:     sb.append(",label=");
 742:     if (getLabel() != null)
 743:       sb.append(getLabel());
 744:     sb.append(",lightWeightPopupEnabled=").append(isLightWeightPopupEnabled());
 745:     sb.append(",margin=");
 746:     if (getMargin() != null)
 747:       sb.append(margin);
 748:     sb.append(",paintBorder=").append(isBorderPainted());
 749:     return sb.toString();
 750:   }
 751: 
 752:   /**
 753:   * Process mouse events forwarded from MenuSelectionManager. This method 
 754:   * doesn't do anything. It is here to conform to the MenuElement interface.
 755:   *
 756:   * @param event event forwarded from MenuSelectionManager
 757:   * @param path path to the menu element from which event was generated
 758:   * @param manager MenuSelectionManager for the current menu hierarchy
 759:   */
 760:   public void processMouseEvent(MouseEvent event, MenuElement[] path,
 761:                                 MenuSelectionManager manager)
 762:   {
 763:     // Empty Implementation. This method is needed for the implementation
 764:     // of MenuElement interface
 765:   }
 766: 
 767:   /**
 768:    * Process key events forwarded from MenuSelectionManager. This method
 769:    * doesn't do anything. It is here to conform to the MenuElement interface.
 770:    *
 771:    * @param event event forwarded from MenuSelectionManager
 772:    * @param path path to the menu element from which event was generated
 773:    * @param manager MenuSelectionManager for the current menu hierarchy
 774:    *
 775:    */
 776:   public void processKeyEvent(KeyEvent event, MenuElement[] path,
 777:                               MenuSelectionManager manager)
 778:   {
 779:     // Empty Implementation. This method is needed for the implementation
 780:     // of MenuElement interface
 781:   }
 782: 
 783:   /**
 784:    * Method of MenuElement Interface. It is invoked when
 785:    * popupMenu's selection has changed
 786:    *
 787:    * @param changed true if this popupMenu is part of current menu
 788:    * hierarchy and false otherwise.
 789:    */
 790:   public void menuSelectionChanged(boolean changed)
 791:   {
 792:     if (! changed)
 793:       setVisible(false);
 794:   }
 795: 
 796:   /**
 797:    * Return subcomonents of this popup menu. This method returns only
 798:    * components that implement the <code>MenuElement</code> interface.
 799:    *
 800:    * @return array of menu items belonging to this popup menu
 801:    */
 802:   public MenuElement[] getSubElements()
 803:   {
 804:     Component[] items = getComponents();
 805:     ArrayList subElements = new ArrayList();
 806: 
 807:     for (int i = 0; i < items.length; i++)
 808:       if (items[i] instanceof MenuElement)
 809:     subElements.add(items[i]);
 810: 
 811:     return (MenuElement[])
 812:       subElements.toArray(new MenuElement[subElements.size()]);
 813:   }
 814: 
 815:   /**
 816:    * Method of the MenuElement interface. Returns reference to itself.
 817:    *
 818:    * @return Returns reference to itself
 819:    */
 820:   public Component getComponent()
 821:   {
 822:     return this;
 823:   }
 824: 
 825:   /**
 826:    * Checks if observing mouse event should trigger popup
 827:    * menu to show on the screen.
 828:    *
 829:    * @param event MouseEvent to check
 830:    *
 831:    * @return true if the observing mouse event is popup trigger and false otherwise
 832:    */
 833:   public boolean isPopupTrigger(MouseEvent event)
 834:   {
 835:     return ((PopupMenuUI) getUI()).isPopupTrigger(event);
 836:   }
 837: 
 838:   /**
 839:    * DOCUMENT ME!
 840:    *
 841:    * @return DOCUMENT ME!
 842:    */
 843:   public AccessibleContext getAccessibleContext()
 844:   {
 845:     if (accessibleContext == null)
 846:       accessibleContext = new AccessibleJPopupMenu();
 847: 
 848:     return accessibleContext;
 849:   }
 850: 
 851:   /**
 852:    * This is the separator that can be used in popup menu.
 853:    */
 854:   public static class Separator extends JSeparator
 855:   {
 856:     public Separator()
 857:     {
 858:       super();
 859:     }
 860: 
 861:     public String getUIClassID()
 862:     {
 863:       return "PopupMenuSeparatorUI";
 864:     }
 865:   }
 866: 
 867:   protected class AccessibleJPopupMenu extends AccessibleJComponent
 868:   {
 869:     private static final long serialVersionUID = 7423261328879849768L;
 870: 
 871:     protected AccessibleJPopupMenu()
 872:     {
 873:       // Nothing to do here.
 874:     }
 875: 
 876:     public AccessibleRole getAccessibleRole()
 877:     {
 878:       return AccessibleRole.POPUP_MENU;
 879:     }
 880:   }
 881: 
 882:   /* This class resizes popup menu and repaints popup menu appropriately if one
 883:    of item's action has changed */
 884:   private class ActionChangeListener implements PropertyChangeListener
 885:   {
 886:     public void propertyChange(PropertyChangeEvent evt)
 887:     {
 888:       // We used to have a revalidate() and repaint() call here. However I think
 889:       // this is not needed. Instead, a new Popup has to be fetched from the
 890:       // PopupFactory and used here.
 891:     }
 892:   }
 893: }