jpopupmenu.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,075 行 · 第 1/2 页

JAVA
1,075
字号
  public boolean isVisible()  {    return visible;  }  /**   * Sets visibility property of this popup menu. If the property is   * set to true then popup menu will be dispayed and popup menu will   * hide itself if visible property is set to false.   *   * @param visible true if popup menu will become visible and false otherwise.   */  public void setVisible(boolean visible)  {    if (visible == isVisible())      return;    boolean old = isVisible();    this.visible = visible;    if (old != isVisible())      {	firePropertyChange("visible", old, isVisible());	if (visible)	  {	    firePopupMenuWillBecomeVisible();	    Container rootContainer = (Container) SwingUtilities.getRoot(invoker);	    boolean fit = true;	    Dimension size;	    // Determine the size of the popup menu	    if (this.getSize().width == 0 && this.getSize().width == 0)	      size = this.getPreferredSize();	    else	      size = this.getSize();	    if ((size.width > (rootContainer.getWidth() - popupLocation.x))	        || (size.height > (rootContainer.getHeight() - popupLocation.y)))	      fit = false;	    if (lightWeightPopupEnabled && fit)	      popup = new LightWeightPopup(this);	    else	      {		if (fit)		  popup = new MediumWeightPopup(this);		else		  popup = new HeavyWeightPopup(this);	      }	    if (popup instanceof LightWeightPopup	        || popup instanceof MediumWeightPopup)	      {		JLayeredPane layeredPane;		layeredPane = SwingUtilities.getRootPane(invoker)		                            .getLayeredPane();		Point p = new Point(popupLocation.x, popupLocation.y);		SwingUtilities.convertPointFromScreen(p, layeredPane);		popup.show(p.x, p.y, size.width, size.height);  	      }	    else	      {		// Subtract insets of the top-level container if popup menu's		// top-left corner is inside it.		Insets insets = rootContainer.getInsets();		popup.show(popupLocation.x - insets.left,		           popupLocation.y - insets.top, size.width,		           size.height);	      }	  }	else	  {	    firePopupMenuWillBecomeInvisible();	    popup.hide();	  }      }  }  /**   * Sets location of the popup menu.   *   * @param x X coordinate of the popup menu's location   * @param y Y coordinate of the popup menu's location   */  public void setLocation(int x, int y)  {    if (popupLocation == null)      popupLocation = new Point();    popupLocation.x = x;    popupLocation.y = y;  }  /**   * Returns popup menu's invoker.   *   * @return popup menu's invoker   */  public Component getInvoker()  {    return invoker;  }  /**   * Sets popup menu's invoker.   *   * @param component The new invoker of this popup menu   */  public void setInvoker(Component component)  {    invoker = component;  }  /**   * This method displays JPopupMenu on the screen at the specified   * location. Note that x and y coordinates given to this method   * should be expressed in terms of the popup menus' invoker.   *   * @param component Invoker for this popup menu   * @param x x-coordinate of the popup menu relative to the specified invoker   * @param y y-coordiate of the popup menu relative to the specified invoker   */  public void show(Component component, int x, int y)  {    setInvoker(component);    Point p = new Point(x, y);    SwingUtilities.convertPointToScreen(p, component);    setLocation(p.x, p.y);    setVisible(true);  }  /**   * Returns component located at the specified index in the popup menu   *   * @param index index of the component to return   *   * @return component located at the specified index in the popup menu   *   * @deprecated Replaced by getComponent(int)   */  public Component getComponentAtIndex(int index)  {    return getComponent(index);  }  /**   * Returns index of the specified component in the popup menu   *   * @param component Component to look for   *   * @return index of the specified component in the popup menu   */  public int getComponentIndex(Component component)  {    Component[] items = getComponents();    for (int i = 0; i < items.length; i++)      {	if (items[i].equals(component))	  return i;      }    return -1;  }  /**   * Sets size of the popup   *   * @param size Dimensions representing new size of the popup menu   */  public void setPopupSize(Dimension size)  {    super.setSize(size);  }  /**   * Sets size of the popup menu   *   * @param width width for the new size   * @param height height for the new size   */  public void setPopupSize(int width, int height)  {    super.setSize(width, height);  }  /**   * Selects specified component in this popup menu.   *   * @param selected component to select   */  public void setSelected(Component selected)  {    int index = getComponentIndex(selected);    selectionModel.setSelectedIndex(index);  }  /**   * Checks if this popup menu paints its border.   *   * @return true if this popup menu paints its border and false otherwise.   */  public boolean isBorderPainted()  {    return borderPainted;  }  /**   * Sets if the border of the popup menu should be   * painter or not.   *   * @param painted true if the border should be painted and false otherwise   */  public void setBorderPainted(boolean painted)  {    borderPainted = painted;  }  /**   * Returns margin for this popup menu.   *   * @return margin for this popup menu.   */  public Insets getMargin()  {    return margin;  }  /**   * A string that describes this JPopupMenu. Normally only used   * for debugging.   *   * @return A string describing this JMenuItem   */  protected String paramString()  {    StringBuffer sb = new StringBuffer();    sb.append(super.paramString());    sb.append(",label=");    if (getLabel() != null)      sb.append(getLabel());    sb.append(",lightWeightPopupEnabled=").append(isLightWeightPopupEnabled());    sb.append(",margin=");    if (getMargin() != null)      sb.append(margin);    sb.append(",paintBorder=").append(isBorderPainted());    return sb.toString();  }  /**  * Process mouse events forwarded from MenuSelectionManager. This method   * doesn't do anything. It is here to conform to the MenuElement interface.  *  * @param event event forwarded from MenuSelectionManager  * @param path path to the menu element from which event was generated  * @param manager MenuSelectionManager for the current menu hierarchy  */  public void processMouseEvent(MouseEvent event, MenuElement[] path,                                MenuSelectionManager manager)  {    // Empty Implementation. This method is needed for the implementation    // of MenuElement interface  }  /**   * Process key events forwarded from MenuSelectionManager. This method   * doesn't do anything. It is here to conform to the MenuElement interface.   *   * @param event event forwarded from MenuSelectionManager   * @param path path to the menu element from which event was generated   * @param manager MenuSelectionManager for the current menu hierarchy   *   */  public void processKeyEvent(KeyEvent event, MenuElement[] path,                              MenuSelectionManager manager)  {    // Empty Implementation. This method is needed for the implementation    // of MenuElement interface  }  /**   * Method of MenuElement Interface. It is invoked when   * popupMenu's selection has changed   *   * @param changed true if this popupMenu is part of current menu   * hierarchy and false otherwise.   */  public void menuSelectionChanged(boolean changed)  {    if (! changed)      setVisible(false);  }  /**   * Return subcomonents of this popup menu. This method returns only   * components that implement the <code>MenuElement</code> interface.   *   * @return array of menu items belonging to this popup menu   */  public MenuElement[] getSubElements()  {    Component[] items = getComponents();    ArrayList subElements = new ArrayList();    for (int i = 0; i < items.length; i++)      if (items[i] instanceof MenuElement)	subElements.add(items[i]);    return (MenuElement[])      subElements.toArray(new MenuElement[subElements.size()]);  }  /**   * Method of the MenuElement interface. Returns reference to itself.   *   * @return Returns reference to itself   */  public Component getComponent()  {    return this;  }  /**   * Checks if observing mouse event should trigger popup   * menu to show on the screen.   *   * @param event MouseEvent to check   *   * @return true if the observing mouse event is popup trigger and false otherwise   */  public boolean isPopupTrigger(MouseEvent event)  {    return ((PopupMenuUI) getUI()).isPopupTrigger(event);  }  /**   * DOCUMENT ME!   *   * @return DOCUMENT ME!   */  public AccessibleContext getAccessibleContext()  {    if (accessibleContext == null)      accessibleContext = new AccessibleJPopupMenu();    return accessibleContext;  }  /**   * This interface is used to display menu items of the JPopupMenu   */  private interface Popup  {    /**     * Displays container on the screen     *     * @param x x-coordinate of popup menu's location on the screen     * @param y y-coordinate of popup menu's location on the screen     * @param width width of the container that is used to display menu     * item's for popup menu     * @param height height of the container that is used to display menu     * item's for popup menu     */    void show(int x, int y, int width, int height);    /**     * Hides container used to display popup menu item's from the screen     */    void hide();  }  /**   * This class represents Popup menu that uses light weight container   * to display its contents.   */  private class LightWeightPopup extends Container implements Popup  {    /**     * Creates a new LightWeightPopup menu     *     * @param c Container containing menu items     */    private Component c;    public LightWeightPopup(Container c)    {      this.c = c;    }    /**     * Displayes lightweight container with menu items to the screen     *     * @param x x-coordinate of lightweight container on the screen     * @param y y-coordinate of lightweight container on the screen     * @param width width of the lightweight container     * @param height height of the lightweight container     */    public void show(int x, int y, int width, int height)    {      JLayeredPane layeredPane;      layeredPane = SwingUtilities.getRootPane(invoker).getLayeredPane();      c.setBounds(x, y, width, height);      layeredPane.add(c, JLayeredPane.POPUP_LAYER, 0);    }    /**     * Hides lightweight container from the screen     */    public void hide()    {      // FIXME: Right now the lightweight container is removed from JLayered      // pane. It is probably would be better in order to improve performance      // to make the container invisible instead of removing it everytime.      JLayeredPane layeredPane;      layeredPane = SwingUtilities.getRootPane(invoker).getLayeredPane();      int index = layeredPane.getIndexOf(c);      layeredPane.remove(index);    }  }  /**   * MediumWeightPopup is an AWT Panel with JPopupMenu's menu items.   * It is used to display JPopupMenu's menu items on the screen   */  private class MediumWeightPopup extends Panel implements Popup  {    /**     * Creates a new MediumWeightPopup object.     *     * @param c Container with JPopupMenu's menu items     */    public MediumWeightPopup(Container c)    {      this.add(c);    }    /**     * Displays AWT Panel with its components on the screen     *     * @param x x-coordinate of the upper-left corner of the panel's     * @param y y-coordinate of the upper-left corner of the panel's     * @param width width of the panel     * @param height height of the panel     */    public void show(int x, int y, int width, int height)    {      JLayeredPane layeredPane;      layeredPane = SwingUtilities.getRootPane(invoker).getLayeredPane();      layeredPane.add(this, JLayeredPane.POPUP_LAYER, 0);      this.setBounds(x, y, width, height);    }    /**     * Hides This panel from the screen     */    public void hide()    {      // FIXME: Right now the lightweight container is removed from JLayered      // pane. It is probably would be better in order to improve performance      // to make the container invisible instead of removing it everytime.      JLayeredPane layeredPane;      layeredPane = SwingUtilities.getRootPane(invoker).getLayeredPane();      int index = layeredPane.getIndexOf(this);      layeredPane.remove(index);    }  }  /**   * HeavyWeightPopup is JWindow that is used to display JPopupMenu menu item's   * on the screen   */  private class HeavyWeightPopup extends JWindow implements Popup  {    /**     * Creates a new HeavyWeightPopup object.     *     * @param c Container containing menu items     */    public HeavyWeightPopup(Container c)    {      this.setContentPane(c);    }    /**     * Displays JWindow container JPopupMenu's menu items to the screen     *     * @param x x-coordinate of JWindow containing menu items     * @param y y-coordinate of JWindow containing menu items     * @param width width of the JWindow     * @param height height of the JWindow     */    public void show(int x, int y, int width, int height)    {      this.setBounds(x, y, width, height);      this.show();    }  }  /**   * This is the separator that can be used in popup menu.   */  public static class Separator extends JSeparator  {    public Separator()    {    }    public String getUIClassID()    {      return "PopupMenuSeparatorUI";    }  }  protected class AccessibleJPopupMenu extends AccessibleJComponent  {    private static final long serialVersionUID = 7423261328879849768L;    protected AccessibleJPopupMenu()    {    }    public AccessibleRole getAccessibleRole()    {      return AccessibleRole.POPUP_MENU;    }  }  /* This class resizes popup menu and repaints popup menu appropriately if one   of item's action has changed */  protected class ActionChangeListener implements PropertyChangeListener  {    public void propertyChange(PropertyChangeEvent evt)    {      JPopupMenu.this.revalidate();      JPopupMenu.this.repaint();    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?