示例#1
0
    /**
     * Paints the border for the specified component with the specified position and size.
     *
     * @param c the component for which this border is being painted
     * @param g the paint graphics
     * @param x the x position of the painted border
     * @param y the y position of the painted border
     * @param width the width of the painted border
     * @param height the height of the painted border
     */
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
      if (!(c instanceof JPopupMenu)) {
        return;
      }

      Font origFont = g.getFont();
      Color origColor = g.getColor();
      JPopupMenu popup = (JPopupMenu) c;

      String title = popup.getLabel();
      if (title == null) {
        return;
      }

      g.setFont(font);

      FontMetrics fm = SwingUtilities2.getFontMetrics(popup, g, font);
      int fontHeight = fm.getHeight();
      int descent = fm.getDescent();
      int ascent = fm.getAscent();
      Point textLoc = new Point();
      int stringWidth = SwingUtilities2.stringWidth(popup, fm, title);

      textLoc.y = y + ascent + TEXT_SPACING;
      textLoc.x = x + ((width - stringWidth) / 2);

      g.setColor(background);
      g.fillRect(
          textLoc.x - TEXT_SPACING,
          textLoc.y - (fontHeight - descent),
          stringWidth + (2 * TEXT_SPACING),
          fontHeight - descent);
      g.setColor(foreground);
      SwingUtilities2.drawString(popup, g, title, textLoc.x, textLoc.y);

      MotifGraphicsUtils.drawGroove(
          g, x, textLoc.y + TEXT_SPACING, width, GROOVE_HEIGHT, shadowColor, highlightColor);

      g.setFont(origFont);
      g.setColor(origColor);
    }
示例#2
0
  /**
   * Displays this menu's popup menu if the specified value is true; hides the menu if it is false.
   */
  public void setPopupMenuVisible(boolean visible_) {
    _popupMenuVisible = visible_;
    if (!visible_) {
      _popup.hide();
      return;
    }

    if (_popup == null) _popup = new JPopupMenu(_menuItems);

    Point p;
    if (!this.isTopLevelMenu()) {

      /* If this menu is a submenu (i.e. it is not a direct
       * child of the menubar), check if there is enough
       * space on the right hand side of the parent menu.
       * If there is not enough space, position it on the
       * left of the parent menu.
       */
      JMenu parentmenu = (JMenu) getParentMenu();
      JPopupMenu parentpopup = parentmenu.getPopupMenu();
      p = parentpopup.getLocation();

      int verticalOffset = parentpopup.getComponentIndex(this);
      _popup.setInvoker(parentpopup);
      int parentwidth = parentpopup.getSize().width;
      Toolkit term = Toolkit.getDefaultToolkit();
      if (p.x + parentwidth + _popup.getWidth() < term.getScreenColumns()) {

        _popup.setLocation(p.addOffset(parentwidth - 1, verticalOffset));
      } else {
        _popup.setLocation(p.addOffset(-_popup.getWidth() + 1, verticalOffset));
      }
    } else {
      JMenuBar parentMenuBar = (JMenuBar) getParent();
      p = parentMenuBar.getPopupMenuLocation(this);
      _popup.setInvoker(parentMenuBar);
      _popup.setLocation(p);
    }
    _popup.show();
  }