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:     invalidate();
 296:   }
 297: 
 298:   /**
 299:    * This method returns a name to identify which look and feel class will be
 300:    * the UI delegate for the menuItem.
 301:    *
 302:    * @return The Look and Feel classID. "PopupMenuUI"
 303:    */
 304:   public String getUIClassID()
 305:   {
 306:     return "PopupMenuUI";
 307:   }
 308: 
 309:   /**
 310:    * Returns selectionModel used by this popup menu to keep
 311:    * track of the selection.
 312:    *
 313:    * @return popup menu's selection model
 314:    */
 315:   public SingleSelectionModel getSelectionModel()
 316:   {
 317:     return selectionModel;
 318:   }
 319: 
 320:   /**
 321:    * Sets selection model for this popup menu
 322:    *
 323:    * @param model new selection model of this popup menu
 324:    */
 325:   public void setSelectionModel(SingleSelectionModel model)
 326:   {
 327:     selectionModel = model;
 328:   }
 329: 
 330:   /**
 331:    * Creates new menu item associated with a given action.
 332:    *
 333:    * @param action Action used to create new menu item
 334:    *
 335:    * @return new created menu item associated with a given action.
 336:    */
 337:   protected JMenuItem createActionComponent(Action action)
 338:   {
 339:     return new JMenuItem(action);
 340:   }
 341: 
 342:   /**
 343:    * Creates PropertyChangeListener that listens to PropertyChangeEvents
 344:    * occuring in the Action associated with given menu item in this popup menu.
 345:    *
 346:    * @param item MenuItem
 347:    *
 348:    * @return The PropertyChangeListener
 349:    */
 350:   protected PropertyChangeListener createActionChangeListener(JMenuItem item)
 351:   {
 352:     return new ActionChangeListener();
 353:   }
 354: 
 355:   /**
 356:    * Returns true if this popup menu will display its menu item in
 357:    * a lightweight container and false otherwise.
 358:    *
 359:    * @return true if this popup menu will display its menu items
 360:    * in a lightweight container and false otherwise.
 361:    */
 362:   public boolean isLightWeightPopupEnabled()
 363:   {
 364:     return lightWeightPopupEnabled;
 365:   }
 366: 
 367:   /**
 368:    * DOCUMENT ME!
 369:    *
 370:    * @param enabled DOCUMENT ME!
 371:    */
 372:   public void setLightWeightPopupEnabled(boolean enabled)
 373:   {
 374:     lightWeightPopupEnabled = enabled;
 375:   }
 376: 
 377:   /**
 378:    * Returns label for this popup menu
 379:    *
 380:    * @return label for this popup menu
 381:    */
 382:   public String getLabel()
 383:   {
 384:     return label;
 385:   }
 386: 
 387:   /**
 388:    * Sets label for this popup menu. This method fires PropertyChangeEvent
 389:    * when the label property is changed. Please note that most
 390:    * of the Look & Feel will ignore this property.
 391:    *
 392:    * @param label label for this popup menu
 393:    */
 394:   public void setLabel(String label)
 395:   {
 396:     if (label != this.label)
 397:       {
 398:     String oldLabel = this.label;
 399:     this.label = label;
 400:     firePropertyChange("label", oldLabel, label);
 401:       }
 402:   }
 403: 
 404:   /**
 405:    * Adds separator to this popup menu
 406:    */
 407:   public void addSeparator()
 408:   {
 409:     // insert separator at the end of the list of menu items    
 410:     this.insert(new Separator(), -1);
 411:   }
 412: 
 413:   /**
 414:    * Adds popupMenuListener to listen for PopupMenuEvents fired
 415:    * by the JPopupMenu
 416:    *
 417:    * @param listener PopupMenuListener to add to JPopupMenu
 418:    */
 419:   public void addPopupMenuListener(PopupMenuListener listener)
 420:   {
 421:     listenerList.add(PopupMenuListener.class, listener);
 422:   }
 423: 
 424:   /**
 425:    * Removes PopupMenuListener from JPopupMenu's list of listeners
 426:    *
 427:    * @param listener PopupMenuListener which needs to be removed
 428:    */
 429:   public void removePopupMenuListener(PopupMenuListener listener)
 430:   {
 431:     listenerList.remove(PopupMenuListener.class, listener);
 432:   }
 433: 
 434:   /**
 435:    * Returns array of PopupMenuListeners that are listening to JPopupMenu
 436:    *
 437:    * @return Array of PopupMenuListeners that are listening to JPopupMenu
 438:    */
 439:   public PopupMenuListener[] getPopupMenuListeners()
 440:   {
 441:     return ((PopupMenuListener[]) listenerList.getListeners(PopupMenuListener.class));
 442:   }
 443: 
 444:   /**
 445:    * This method calls popupMenuWillBecomeVisible() of popup menu's
 446:    * PopupMenuListeners. This method is invoked just before popup menu
 447:    * will appear on the screen.
 448:    */
 449:   protected void firePopupMenuWillBecomeVisible()
 450:   {
 451:     EventListener[] ll = listenerList.getListeners(PopupMenuListener.class);
 452: 
 453:     for (int i = 0; i < ll.length; i++)
 454:       ((PopupMenuListener) ll[i]).popupMenuWillBecomeVisible(new PopupMenuEvent(this));
 455:   }
 456: 
 457:   /**
 458:    * This method calls popupMenuWillBecomeInvisible() of popup
 459:    * menu's PopupMenuListeners. This method is invoked just before popup
 460:    * menu will disappear from the screen
 461:    */
 462:   protected void firePopupMenuWillBecomeInvisible()
 463:   {
 464:     EventListener[] ll = listenerList.getListeners(PopupMenuListener.class);
 465: 
 466:     for (int i = 0; i < ll.length; i++)
 467:       ((PopupMenuListener) ll[i]).popupMenuWillBecomeInvisible(new PopupMenuEvent(this));
 468:   }
 469: 
 470:   /**
 471:    * This method calls popupMenuCanceled() of popup menu's PopupMenuListeners.
 472:    * This method is invoked just before popup menu is cancelled. This happens
 473:    * when popup menu is closed without selecting any of its menu items. This
 474:    * usually happens when the top-level window is resized or moved.
 475:    */
 476:   protected void firePopupMenuCanceled()
 477:   {
 478:     EventListener[] ll = listenerList.getListeners(PopupMenuListener.class);
 479: 
 480:     for (int i = 0; i < ll.length; i++)
 481:       ((PopupMenuListener) ll[i]).popupMenuCanceled(new PopupMenuEvent(this));
 482:   }
 483: 
 484:   /**
 485:    * This methods sets popup menu's size to its' preferred size. If the
 486:    * popup menu's size is previously set it will be ignored.
 487:    */
 488:   public void pack()
 489:   {
 490:     // Hook up this call so that it gets executed on the event thread in order
 491:     // to avoid synchronization problems when calling the layout manager.
 492:     if (! SwingUtilities.isEventDispatchThread())
 493:       {
 494:         SwingUtilities.invokeLater(new Runnable()
 495:           {
 496:             public void run()
 497:             {
 498:               show();
 499:             }
 500:           });
 501:       }
 502: 
 503:     setSize(getPreferredSize());
 504:   }
 505: 
 506:   /**
 507:    * Return visibility of the popup menu
 508:    *
 509:    * @return true if popup menu is visible on the screen and false otherwise.
 510:    */
 511:   public boolean isVisible()
 512:   {
 513:     return visible;
 514:   }
 515: 
 516:   /**
 517:    * Sets visibility property of this popup menu. If the property is
 518:    * set to true then popup menu will be dispayed and popup menu will
 519:    * hide itself if visible property is set to false.
 520:    *
 521:    * @param visible true if popup menu will become visible and false otherwise.
 522:    */
 523:   public void setVisible(final boolean visible)
 524:   {
 525:     // Hook up this call so that it gets executed on the event thread in order
 526:     // to avoid synchronization problems when calling the layout manager.
 527:     if (! SwingUtilities.isEventDispatchThread())
 528:       {
 529:         SwingUtilities.invokeLater(new Runnable()
 530:           {
 531:             public void run()
 532:             {
 533:               setVisible(visible);
 534:             }
 535:           });
 536:       }
 537: 
 538:     if (visible == isVisible())
 539:       return;
 540: 
 541:     boolean old = isVisible();
 542:     this.visible = visible;
 543:     if (old != isVisible())
 544:       {
 545:         firePropertyChange("visible", old, isVisible());
 546:         if (visible)
 547:           {
 548:             firePopupMenuWillBecomeVisible();
 549: 
 550:             PopupFactory pf = PopupFactory.getSharedInstance();
 551:             pack();
 552:             popup = pf.getPopup(invoker, this, popupLocationX, popupLocationY);
 553:             popup.show();
 554:           }
 555:         else
 556:           {
 557:             firePopupMenuWillBecomeInvisible();
 558:             popup.hide();
 559:           }
 560:       }
 561:   }
 562: 
 563:   /**
 564:    * Sets location of the popup menu.
 565:    *
 566:    * @param x X coordinate of the popup menu's location
 567:    * @param y Y coordinate of the popup menu's location
 568:    */
 569:   public void setLocation(int x, int y)
 570:   {
 571:     popupLocationX = x;
 572:     popupLocationY = y;
 573:     // Handle the case when the popup is already showing. In this case we need
 574:     // to fetch a new popup from PopupFactory and use this. See the general
 575:     // contract of the PopupFactory.
 576:   }
 577: 
 578:   /**
 579:    * Returns popup menu's invoker.
 580:    *
 581:    * @return popup menu's invoker
 582:    */
 583:   public Component getInvoker()
 584:   {
 585:     return invoker;
 586:   }
 587: 
 588:   /**
 589:    * Sets popup menu's invoker.
 590:    *
 591:    * @param component The new invoker of this popup menu
 592:    */
 593:   public void setInvoker(Component component)
 594:   {
 595:     invoker = component;
 596:   }
 597: 
 598:   /**
 599:    * This method displays JPopupMenu on the screen at the specified
 600:    * location. Note that x and y coordinates given to this method
 601:    * should be expressed in terms of the popup menus' invoker.
 602:    *
 603:    * @param component Invoker for this popup menu
 604:    * @param x x-coordinate of the popup menu relative to the specified invoker
 605:    * @param y y-coordiate of the popup menu relative to the specified invoker
 606:    */
 607:   public void show(Component component, int x, int y)
 608:   {
 609:     if (component.isShowing())
 610:       {
 611:         setInvoker(component);
 612:         Point p = new Point(x, y);
 613:         SwingUtilities.convertPointToScreen(p, component);
 614:         setLocation(p.x, p.y);
 615:         setVisible(true);
 616:       }
 617:   }
 618: 
 619:   /**
 620:    * Returns component located at the specified index in the popup menu
 621:    *
 622:    * @param index index of the component to return
 623:    *
 624:    * @return component located at the specified index in the popup menu
 625:    *
 626:    * @deprecated Replaced by getComponent(int)
 627:    */
 628:   public Component getComponentAtIndex(int index)
 629:   {
 630:     return getComponent(index);
 631:   }
 632: 
 633:   /**
 634:    * Returns index of the specified component in the popup menu
 635:    *
 636:    * @param component Component to look for
 637:    *
 638:    * @return index of the specified component in the popup menu
 639:    */
 640:   public int getComponentIndex(Component component)
 641:   {
 642:     Component[] items = getComponents();
 643: 
 644:     for (int i = 0; i < items.length; i++)
 645:       {
 646:     if (items[i].equals(component))
 647:       return i;
 648:       }
 649: 
 650:     return -1;
 651:   }
 652: 
 653:   /**
 654:    * Sets size of the popup
 655:    *
 656:    * @param size Dimensions representing new size of the popup menu
 657:    */
 658:   public void setPopupSize(Dimension size)
 659:   {
 660:     super.setSize(size);
 661:   }
 662: 
 663:   /**
 664:    * Sets size of the popup menu
 665:    *
 666:    * @param width width for the new size
 667:    * @param height height for the new size
 668:    */
 669:   public void setPopupSize(int width, int height)
 670:   {
 671:     super.setSize(width, height);
 672:   }
 673: 
 674:   /**
 675:    * Selects specified component in this popup menu.
 676:    *
 677:    * @param selected component to select
 678:    */
 679:   public void setSelected(Component selected)
 680:   {
 681:     int index = getComponentIndex(selected);
 682:     selectionModel.setSelectedIndex(index);
 683:   }
 684: 
 685:   /**
 686:    * Checks if this popup menu paints its border.
 687:    *
 688:    * @return true if this popup menu paints its border and false otherwise.
 689:    */
 690:   public boolean isBorderPainted()
 691:   {
 692:     return borderPainted;
 693:   }
 694: 
 695:   /**
 696:    * Sets if the border of the popup menu should be
 697:    * painter or not.
 698:    *
 699:    * @param painted true if the border should be painted and false otherwise
 700:    */
 701:   public void setBorderPainted(boolean painted)
 702:   {
 703:     borderPainted = painted;
 704:   }
 705: 
 706:   /**
 707:    * Returns margin for this popup menu.
 708:    *
 709:    * @return margin for this popup menu.
 710:    */
 711:   public Insets getMargin()
 712:   {
 713:     return margin;
 714:   }
 715: 
 716:   /**
 717:    * A string that describes this JPopupMenu. Normally only used
 718:    * for debugging.
 719:    *
 720:    * @return A string describing this JMenuItem
 721:    */
 722:   protected String paramString()
 723:   {
 724:     StringBuffer sb = new StringBuffer();
 725:     sb.append(super.paramString());
 726:     sb.append(",label=");
 727:     if (getLabel() != null)
 728:       sb.append(getLabel());
 729:     sb.append(",lightWeightPopupEnabled=").append(isLightWeightPopupEnabled());
 730:     sb.append(",margin=");
 731:     if (getMargin() != null)
 732:       sb.append(margin);
 733:     sb.append(",paintBorder=").append(isBorderPainted());
 734:     return sb.toString();
 735:   }
 736: 
 737:   /**
 738:   * Process mouse events forwarded from MenuSelectionManager. This method 
 739:   * doesn't do anything. It is here to conform to the MenuElement interface.
 740:   *
 741:   * @param event event forwarded from MenuSelectionManager
 742:   * @param path path to the menu element from which event was generated
 743:   * @param manager MenuSelectionManager for the current menu hierarchy
 744:   */
 745:   public void processMouseEvent(MouseEvent event, MenuElement[] path,
 746:                                 MenuSelectionManager manager)
 747:   {
 748:     // Empty Implementation. This method is needed for the implementation
 749:     // of MenuElement interface
 750:   }
 751: 
 752:   /**
 753:    * Process key 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:    */
 761:   public void processKeyEvent(KeyEvent event, MenuElement[] path,
 762:                               MenuSelectionManager manager)
 763:   {
 764:     // Empty Implementation. This method is needed for the implementation
 765:     // of MenuElement interface
 766:   }
 767: 
 768:   /**
 769:    * Method of MenuElement Interface. It is invoked when
 770:    * popupMenu's selection has changed
 771:    *
 772:    * @param changed true if this popupMenu is part of current menu
 773:    * hierarchy and false otherwise.
 774:    */
 775:   public void menuSelectionChanged(boolean changed)
 776:   {
 777:     if (! changed)
 778:       setVisible(false);
 779:   }
 780: 
 781:   /**
 782:    * Return subcomonents of this popup menu. This method returns only
 783:    * components that implement the <code>MenuElement</code> interface.
 784:    *
 785:    * @return array of menu items belonging to this popup menu
 786:    */
 787:   public MenuElement[] getSubElements()
 788:   {
 789:     Component[] items = getComponents();
 790:     ArrayList subElements = new ArrayList();
 791: 
 792:     for (int i = 0; i < items.length; i++)
 793:       if (items[i] instanceof MenuElement)
 794:     subElements.add(items[i]);
 795: 
 796:     return (MenuElement[])
 797:       subElements.toArray(new MenuElement[subElements.size()]);
 798:   }
 799: 
 800:   /**
 801:    * Method of the MenuElement interface. Returns reference to itself.
 802:    *
 803:    * @return Returns reference to itself
 804:    */
 805:   public Component getComponent()
 806:   {
 807:     return this;
 808:   }
 809: 
 810:   /**
 811:    * Checks if observing mouse event should trigger popup
 812:    * menu to show on the screen.
 813:    *
 814:    * @param event MouseEvent to check
 815:    *
 816:    * @return true if the observing mouse event is popup trigger and false otherwise
 817:    */
 818:   public boolean isPopupTrigger(MouseEvent event)
 819:   {
 820:     return ((PopupMenuUI) getUI()).isPopupTrigger(event);
 821:   }
 822: 
 823:   /**
 824:    * DOCUMENT ME!
 825:    *
 826:    * @return DOCUMENT ME!
 827:    */
 828:   public AccessibleContext getAccessibleContext()
 829:   {
 830:     if (accessibleContext == null)
 831:       accessibleContext = new AccessibleJPopupMenu();
 832: 
 833:     return accessibleContext;
 834:   }
 835: 
 836:   /**
 837:    * This is the separator that can be used in popup menu.
 838:    */
 839:   public static class Separator extends JSeparator
 840:   {
 841:     public Separator()
 842:     {
 843:       super();
 844:     }
 845: 
 846:     public String getUIClassID()
 847:     {
 848:       return "PopupMenuSeparatorUI";
 849:     }
 850:   }
 851: 
 852:   protected class AccessibleJPopupMenu extends AccessibleJComponent
 853:   {
 854:     private static final long serialVersionUID = 7423261328879849768L;
 855: 
 856:     protected AccessibleJPopupMenu()
 857:     {
 858:       // Nothing to do here.
 859:     }
 860: 
 861:     public AccessibleRole getAccessibleRole()
 862:     {
 863:       return AccessibleRole.POPUP_MENU;
 864:     }
 865:   }
 866: 
 867:   /* This class resizes popup menu and repaints popup menu appropriately if one
 868:    of item's action has changed */
 869:   protected class ActionChangeListener implements PropertyChangeListener
 870:   {
 871:     public void propertyChange(PropertyChangeEvent evt)
 872:     {
 873:       // We used to have a revalidate() and repaint() call here. However I think
 874:       // this is not needed. Instead, a new Popup has to be fetched from the
 875:       // PopupFactory and used here.
 876:     }
 877:   }
 878: }