Esempio n. 1
0
  /** Returns the baseline for buttons. */
  private static int getButtonBaseline(AbstractButton button, int height) {
    FontMetrics fm = button.getFontMetrics(button.getFont());

    resetRects(button, height);

    String text = button.getText();
    if (text != null && text.startsWith("<html>")) {
      return -1;
    }
    // NOTE: that we use "a" here to make sure we get a valid value, if
    // we were to pass in an empty string or null we would not get
    // back the right thing.
    SwingUtilities.layoutCompoundLabel(
        button,
        fm,
        "a",
        button.getIcon(),
        button.getVerticalAlignment(),
        button.getHorizontalAlignment(),
        button.getVerticalTextPosition(),
        button.getHorizontalTextPosition(),
        viewRect,
        iconRect,
        textRect,
        text == null ? 0 : button.getIconTextGap());

    if (isAqua()) {
      return textRect.y + fm.getAscent() + 1;
    }
    return textRect.y + fm.getAscent();
  }
  public void paintComponent(Graphics g) {
    Icon icon = getIcon();
    FontMetrics fm = getFontMetrics(getFont());
    Rectangle viewRect = new Rectangle(getSize());
    JBInsets.removeFrom(viewRect, getInsets());

    Rectangle iconRect = new Rectangle();
    Rectangle textRect = new Rectangle();
    String text =
        SwingUtilities.layoutCompoundLabel(
            this,
            fm,
            getText(),
            icon,
            SwingConstants.CENTER,
            horizontalTextAlignment(),
            SwingConstants.CENTER,
            horizontalTextPosition(),
            viewRect,
            iconRect,
            textRect,
            iconTextSpace());
    ActionButtonLook look = ActionButtonLook.IDEA_LOOK;
    look.paintBackground(g, this);
    look.paintIconAt(g, this, icon, iconRect.x, iconRect.y);
    look.paintBorder(g, this);

    UISettings.setupAntialiasing(g);
    g.setColor(isButtonEnabled() ? getForeground() : getInactiveTextColor());
    SwingUtilities2.drawStringUnderlineCharAt(
        this, g, text, getMnemonicCharIndex(text), textRect.x, textRect.y + fm.getAscent());
  }
 /**
  * {@inheritDoc}
  *
  * <p>Paints a diagonal cross over the text if the comp is of type JLabel, does nothing otherwise.
  */
 @Override
 protected void doPaint(Graphics2D g, JComponent comp, int width, int height) {
   if (!(comp instanceof JLabel)) return;
   JLabel label = (JLabel) comp;
   Insets insets = label.getInsets(insetss);
   paintViewR.x = insets.left;
   paintViewR.y = insets.top;
   paintViewR.width = width - (insets.left + insets.right);
   paintViewR.height = height - (insets.top + insets.bottom);
   paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
   paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
   SwingUtilities.layoutCompoundLabel(
       label,
       label.getFontMetrics(label.getFont()),
       label.getText(),
       null,
       label.getVerticalAlignment(),
       label.getHorizontalAlignment(),
       label.getVerticalTextPosition(),
       label.getHorizontalTextPosition(),
       paintViewR,
       paintIconR,
       paintTextR,
       label.getIconTextGap());
   doPaint(g, paintTextR);
 }
Esempio n. 4
0
  private String layout(AbstractButton b, FontMetrics fm, int width, int height) {
    Insets i = b.getInsets();
    viewRect.x = i.left;
    viewRect.y = i.top;
    viewRect.width = width - (i.right + viewRect.x);
    viewRect.height = height - (i.bottom + viewRect.y);

    textRect.x = textRect.y = textRect.width = textRect.height = 0;
    iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;

    // layout the text and icon
    return SwingUtilities.layoutCompoundLabel(
        b,
        fm,
        b.getText(),
        b.getIcon(),
        b.getVerticalAlignment(),
        b.getHorizontalAlignment(),
        b.getVerticalTextPosition(),
        b.getHorizontalTextPosition(),
        viewRect,
        iconRect,
        textRect,
        b.getText() == null ? 0 : b.getIconTextGap());
  }
  protected void paintComponent(Graphics g) {
    g.setColor(GroupedElementsRenderer.POPUP_SEPARATOR_FOREGROUND);

    if (hasCaption()) {
      Rectangle viewR = new Rectangle(0, getVgap(), getWidth() - 1, getHeight() - getVgap() - 1);
      Rectangle iconR = new Rectangle();
      Rectangle textR = new Rectangle();
      String s =
          SwingUtilities.layoutCompoundLabel(
              g.getFontMetrics(),
              myCaption,
              null,
              CENTER,
              myCaptionCentered ? CENTER : LEFT,
              CENTER,
              myCaptionCentered ? CENTER : LEFT,
              viewR,
              iconR,
              textR,
              0);
      final int lineY = textR.y + textR.height / 2;
      if (s.equals(myCaption) && viewR.width - textR.width > 2 * getHgap()) {
        if (myCaptionCentered) {
          g.drawLine(0, lineY, textR.x - getHgap(), lineY);
        }
        g.drawLine(textR.x + textR.width + getHgap(), lineY, getWidth() - 1, lineY);
      }
      UIUtil.applyRenderingHints(g);
      g.setColor(GroupedElementsRenderer.POPUP_SEPARATOR_TEXT_FOREGROUND);
      g.drawString(s, textR.x, textR.y + g.getFontMetrics().getAscent());
    } else {
      g.drawLine(0, getVgap(), getWidth() - 1, getVgap());
    }
  }
Esempio n. 6
0
  /** Returns the dimensions required to display the icon and label. */
  private Dimension getContentSize(AbstractButton button) {
    Rectangle scratchIconRect = new Rectangle();
    Rectangle scratchTextRect = new Rectangle();
    Rectangle scratchViewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);

    FontMetrics fm = button.getFontMetrics(button.getFont());
    SwingUtilities.layoutCompoundLabel(
        fm,
        button.getText(),
        button.getIcon(),
        button.getVerticalAlignment(),
        button.getHorizontalAlignment(),
        button.getVerticalTextPosition(),
        button.getHorizontalTextPosition(),
        scratchViewRect,
        scratchIconRect,
        scratchTextRect,
        button.getIconTextGap());

    Insets textInsets = getTextPadding();
    scratchTextRect.y -= textInsets.top;
    scratchTextRect.x -= textInsets.left;
    scratchTextRect.width += textInsets.left + textInsets.right;
    scratchTextRect.height += textInsets.top + textInsets.bottom;

    Insets iconInsets = getIconPadding();
    scratchIconRect.y -= iconInsets.top;
    scratchIconRect.x -= iconInsets.left;
    scratchIconRect.width += iconInsets.left + iconInsets.right;
    scratchIconRect.height += iconInsets.top + iconInsets.bottom;

    Rectangle sum = getSum(new Rectangle[] {scratchIconRect, scratchTextRect});
    return new Dimension(sum.width, sum.height);
  }
  private boolean paintPlaceholderText(Graphics2D g) {
    CharSequence hintText = myEditor.getPlaceholder();
    EditorComponentImpl editorComponent = myEditor.getContentComponent();
    if (myDocument.getTextLength() > 0
        || hintText == null
        || hintText.length() == 0
        || KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == editorComponent
            && !myEditor.getShowPlaceholderWhenFocused()) {
      return false;
    }

    hintText =
        SwingUtilities.layoutCompoundLabel(
            g.getFontMetrics(),
            hintText.toString(),
            null,
            0,
            0,
            0,
            0,
            editorComponent.getBounds(),
            new Rectangle(),
            new Rectangle(),
            0);
    g.setColor(myEditor.getFoldingModel().getPlaceholderAttributes().getForegroundColor());
    g.setFont(myEditor.getColorsScheme().getFont(EditorFontType.PLAIN));
    g.drawString(hintText.toString(), 0, myView.getAscent());
    return true;
  }
  /**
   * Paint the component, which is an {@link AbstractButton}, according to its current state.
   *
   * @param g The graphics context to paint with
   * @param c The component to paint the state of
   */
  public void paint(Graphics g, JComponent c) {
    AbstractButton b = (AbstractButton) c;

    Rectangle tr = new Rectangle();
    Rectangle ir = new Rectangle();
    Rectangle vr = new Rectangle();

    Font f = c.getFont();

    g.setFont(f);

    if (b.isBorderPainted()) SwingUtilities.calculateInnerArea(b, vr);
    else vr = SwingUtilities.getLocalBounds(b);
    String text =
        SwingUtilities.layoutCompoundLabel(
            c,
            g.getFontMetrics(f),
            b.getText(),
            currentIcon(b),
            b.getVerticalAlignment(),
            b.getHorizontalAlignment(),
            b.getVerticalTextPosition(),
            b.getHorizontalTextPosition(),
            vr,
            ir,
            tr,
            b.getIconTextGap() + defaultTextShiftOffset);

    if ((b.getModel().isArmed() && b.getModel().isPressed()) || b.isSelected())
      paintButtonPressed(g, b);

    paintIcon(g, b, ir);
    if (text != null) paintText(g, b, tr, text);
    if (b.isFocusOwner() && b.isFocusPainted()) paintFocus(g, b, vr, tr, ir);
  }
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (UIUtil.isUnderAquaLookAndFeel()) {
      return;
    }

    switch (getState()) {
      case DONT_CARE:
        Icon icon = getIcon();
        if (icon == null) {
          icon = UIManager.getIcon("CheckBox.icon");
        }
        if (UIUtil.isUnderDarcula() || UIUtil.isUnderIntelliJLaF()) {
          icon = EmptyIcon.create(20, 18);
        }
        if (icon != null) {
          final Insets i = getInsets();
          final Rectangle r = getBounds();
          final Rectangle r1 = new Rectangle();
          r1.x = i.left;
          r1.y = i.top;
          r1.width = r.width - (i.right + r1.x);
          r1.height = r.height - (i.bottom + r1.y);

          final Rectangle r2 = new Rectangle();
          final Rectangle r3 = new Rectangle();
          SwingUtilities.layoutCompoundLabel(
              this,
              getFontMetrics(getFont()),
              getText(),
              icon,
              getVerticalAlignment(),
              getHorizontalAlignment(),
              getVerticalTextPosition(),
              getHorizontalTextPosition(),
              r1,
              r2,
              r3,
              getText() == null ? 0 : getIconTextGap());

          // selected table cell: do not paint white on white
          g.setColor(UIUtil.getTreeForeground());
          int height = r2.height / 10;
          int width = r2.width / 3;
          g.fillRect(
              r2.x + r2.width / 2 - width / 2, r2.y + r2.height / 2 - height / 2, width, height);
        }
        break;
      default:
        break;
    }
  }
  /** This method is taken from inside the source of JLabel (in the inner AccessibleJLabel class) */
  private Rectangle getTextRectangle() {
    final String text = getText();
    final Icon icon = (isEnabled()) ? getIcon() : getDisabledIcon();

    if ((icon == null) && (text == null)) {
      return null;
    }

    final Rectangle paintIconR = new Rectangle();
    final Rectangle paintTextR = new Rectangle();
    final Rectangle paintViewR = new Rectangle();

    Insets paintViewInsets = new Insets(0, 0, 0, 0);
    paintViewInsets = getInsets(paintViewInsets);

    paintViewR.x = paintViewInsets.left;
    paintViewR.y = paintViewInsets.top;
    paintViewR.width = getWidth() - (paintViewInsets.left + paintViewInsets.right);
    paintViewR.height = getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

    final Graphics g = getGraphics();
    if (g == null) {
      return null;
    }

    SwingUtilities.layoutCompoundLabel(
        this,
        g.getFontMetrics(),
        text,
        icon,
        getVerticalAlignment(),
        getHorizontalAlignment(),
        getVerticalTextPosition(),
        getHorizontalTextPosition(),
        paintViewR,
        paintIconR,
        paintTextR,
        getIconTextGap());

    final Rectangle returnValue = new Rectangle(paintTextR);
    returnValue.add(paintIconR);

    return returnValue;
  }
Esempio n. 11
0
    /**
     * Use a custom layout for the label (icon centered, text below icon).
     *
     * <p>Copy 'n' paste from the original source of BasicTabbedPaneUI.
     */
    protected void layoutLabel(
        int tabPlacement,
        FontMetrics metrics,
        int tabIndex,
        String title,
        Icon icon,
        Rectangle tabRect,
        Rectangle iconRect,
        Rectangle textRect,
        boolean isSelected) {
      textRect.x = 0;
      textRect.y = 0;
      textRect.width = 0;
      textRect.height = 0;
      iconRect.x = 0;
      iconRect.y = 0;
      iconRect.width = 0;
      iconRect.height = 0;

      // -- just this has to be changed to change the layout of the tabs --
      SwingUtilities.layoutCompoundLabel(
          tabPane,
          metrics,
          title,
          icon,
          SwingConstants.CENTER,
          SwingConstants.CENTER,
          SwingConstants.BOTTOM,
          SwingConstants.CENTER,
          tabRect,
          iconRect,
          textRect,
          textIconGap);

      int shiftX = getTabLabelShiftX(tabPlacement, tabIndex, isSelected);
      int shiftY = getTabLabelShiftY(tabPlacement, tabIndex, isSelected);

      iconRect.x += shiftX;
      iconRect.y += shiftY;

      textRect.x += shiftX;
      textRect.y += shiftY;
    }
 protected String truncateText(
     String text,
     Rectangle bounds,
     FontMetrics fm,
     Rectangle textR,
     Rectangle iconR,
     int maxWidth) {
   return SwingUtilities.layoutCompoundLabel(
       fm,
       text,
       null,
       SwingConstants.CENTER,
       SwingConstants.CENTER,
       SwingConstants.CENTER,
       SwingConstants.TRAILING,
       bounds,
       iconR,
       textR,
       0);
 }
Esempio n. 13
0
 /**
  * Performs label layout and returns clipped or full label text.
  *
  * @param label label to process
  * @param fm label font metrics
  * @param text label text
  * @param icon label icon
  * @param viewR rectangle limited by label insets
  * @param iconR icon rectangle dummy
  * @param textR text rectangle dummy
  * @return clipped or full label text
  */
 protected String layoutCL(
     final E label,
     final FontMetrics fm,
     final String text,
     final Icon icon,
     final Rectangle viewR,
     final Rectangle iconR,
     final Rectangle textR) {
   return SwingUtilities.layoutCompoundLabel(
       label,
       fm,
       text,
       icon,
       label.getVerticalAlignment(),
       label.getHorizontalAlignment(),
       label.getVerticalTextPosition(),
       label.getHorizontalTextPosition(),
       viewR,
       iconR,
       textR,
       label.getIconTextGap());
 }
 /**
  * Forwards the call to SwingUtilities.layoutCompoundLabel(). This method is here so that a
  * subclass could do Label specific layout and to shorten the method name a little.
  *
  * @see SwingUtilities#layoutCompoundLabel
  */
 protected String layoutCL(
     AbstractButton c,
     FontMetrics fontMetrics,
     String text,
     Icon icon,
     Rectangle viewR,
     Rectangle iconR,
     Rectangle textR) {
   return SwingUtilities.layoutCompoundLabel(
       c,
       fontMetrics,
       text,
       icon,
       c.getVerticalAlignment(),
       c.getHorizontalAlignment(),
       c.getVerticalTextPosition(),
       c.getHorizontalTextPosition(),
       viewR,
       iconR,
       textR,
       Methods.invokeGetter(c, "getIconTextGap", 4));
 }
Esempio n. 15
0
  private static int getLabelBaseline(JLabel label, int height) {
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
    FontMetrics fm = label.getFontMetrics(label.getFont());

    resetRects(label, height);

    SwingUtilities.layoutCompoundLabel(
        label,
        fm,
        "a",
        icon,
        label.getVerticalAlignment(),
        label.getHorizontalAlignment(),
        label.getVerticalTextPosition(),
        label.getHorizontalTextPosition(),
        viewRect,
        iconRect,
        textRect,
        label.getIconTextGap());

    return textRect.y + fm.getAscent();
  }
  public Dimension getPreferredSize() {
    Dimension basicSize = super.getPreferredSize();

    Icon icon = getIcon();
    FontMetrics fm = getFontMetrics(getFont());
    Rectangle viewRect = new Rectangle(0, 0, Short.MAX_VALUE, Short.MAX_VALUE);
    Insets insets = getInsets();
    int dx = insets.left + insets.right;
    int dy = insets.top + insets.bottom;

    Rectangle iconR = new Rectangle();
    Rectangle textR = new Rectangle();
    SwingUtilities.layoutCompoundLabel(
        this,
        fm,
        getText(),
        icon,
        SwingConstants.CENTER,
        horizontalTextAlignment(),
        SwingConstants.CENTER,
        horizontalTextPosition(),
        viewRect,
        iconR,
        textR,
        iconTextSpace());
    int x1 = Math.min(iconR.x, textR.x);
    int x2 = Math.max(iconR.x + iconR.width, textR.x + textR.width);
    int y1 = Math.min(iconR.y, textR.y);
    int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height);
    Dimension rv = new Dimension(x2 - x1 + dx, y2 - y1 + dy);

    rv.width += Math.max(basicSize.height - rv.height, 0);

    rv.width = Math.max(rv.width, basicSize.width);
    rv.height = Math.max(rv.height, basicSize.height);
    return rv;
  }
  protected void layoutLabel(
      int tabPlacement,
      FontMetrics metrics,
      int tabIndex,
      String title,
      Icon icon,
      Rectangle tabRect,
      Rectangle iconRect,
      Rectangle textRect,
      boolean isSelected) {
    textRect.x = textRect.y = iconRect.x = iconRect.y = 0;

    View v = getTextViewForTab(tabIndex);
    if (v != null) {
      tabPane.putClientProperty("html", v);
    }

    SwingUtilities.layoutCompoundLabel(
        (JComponent) tabPane,
        metrics,
        title,
        icon,
        SwingUtilities.CENTER,
        SwingUtilities.LEFT,
        SwingUtilities.CENTER,
        SwingUtilities.CENTER,
        tabRect,
        iconRect,
        textRect,
        textIconGap);

    tabPane.putClientProperty("html", null);

    iconRect.x = tabRect.x + 8;
    textRect.x = iconRect.x + iconRect.width + textIconGap;
  }
Esempio n. 18
0
  /** Calculates the preferred size of this button and UI. */
  @Override
  public Dimension getPreferredSize(JComponent c) {
    AbstractButton button = (AbstractButton) c;
    ButtonCluster cluster = ButtonCluster.getCluster(button);

    Rectangle scratchIconRect = new Rectangle();
    Rectangle scratchTextRect = new Rectangle();
    Rectangle scratchViewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);

    FontMetrics fm = button.getFontMetrics(button.getFont());
    SwingUtilities.layoutCompoundLabel(
        fm,
        button.getText(),
        button.getIcon(),
        button.getVerticalAlignment(),
        button.getHorizontalAlignment(),
        button.getVerticalTextPosition(),
        button.getHorizontalTextPosition(),
        scratchViewRect,
        scratchIconRect,
        scratchTextRect,
        button.getIconTextGap());

    Insets textInsets = getTextPadding();
    scratchTextRect.y -= textInsets.top;
    scratchTextRect.x -= textInsets.left;
    scratchTextRect.width += textInsets.left + textInsets.right;
    scratchTextRect.height += textInsets.top + textInsets.bottom;

    Insets iconInsets = getIconPadding();
    scratchIconRect.y -= iconInsets.top;
    scratchIconRect.x -= iconInsets.left;
    scratchIconRect.width += iconInsets.left + iconInsets.right;
    scratchIconRect.height += iconInsets.top + iconInsets.bottom;

    Rectangle sum = getSum(new Rectangle[] {scratchIconRect, scratchTextRect});

    if (cluster != null && cluster.isStandardized()) {
      /**
       * If standardize: the dimensions of this button need to make room for all other buttons in
       * the cluster.
       */
      AbstractButton[] buttons = cluster.getButtons();
      for (int a = 0; a < buttons.length; a++) {
        ButtonUI ui = buttons[a].getUI();
        if (ui instanceof FilledButtonUI) {
          FilledButtonUI fui = (FilledButtonUI) ui;
          Dimension contentSize = fui.getContentSize(buttons[a]);
          sum.width = Math.max(sum.width, contentSize.width);
          sum.height = Math.max(sum.height, contentSize.height);
        }
      }
    }

    Insets padding = getContentInsets(button);

    Shape customShape = (Shape) button.getClientProperty(SHAPE);

    if (customShape == null) {
      int minHeight = getPreferredHeight();
      if (sum.height < minHeight) sum.height = minHeight;
    }

    int horizontalPosition = getHorizontalPosition(button);
    int verticalPosition = getVerticalPosition(button);
    Dimension size = shape.getPreferredSize(null, sum.width, sum.height, padding, customShape);

    if (customShape == null) {
      PaintFocus focus = getFocusPainting(button);
      if (focus == PaintFocus.OUTSIDE || focus == PaintFocus.BOTH) {
        if (horizontalPosition == POS_ONLY) {
          size.width += 2 * focusSize;
        } else if (horizontalPosition != POS_MIDDLE) {
          size.width += focusSize;
        }
        if (verticalPosition == POS_ONLY) {
          size.height += 2 * focusSize;
        } else if (horizontalPosition != POS_MIDDLE) {
          size.height += focusSize;
        }
      }
    }

    return size;
  }
  public static Dimension getPreferredSize(AbstractButton b) {
    String style = (String) b.getClientProperty("Quaqua.Button.style");
    if (style == null) {
      style = "push";
    }
    if (style.equals("help")) {
      Icon helpIcon = UIManager.getIcon("Button.helpIcon");
      Insets insets = b.getInsets();

      return new Dimension(
          helpIcon.getIconWidth() + insets.left + insets.right,
          helpIcon.getIconHeight() + insets.top + insets.bottom);
    }
    if (b.getComponentCount() > 0) {
      return null;
    }

    int textIconGap = Methods.invokeGetter(b, "getIconTextGap", 4);
    Icon icon = (Icon) b.getIcon();
    String text = b.getText();

    Font font = b.getFont();
    FontMetrics fm = b.getFontMetrics(font);

    viewR.x = viewR.y = 0;
    viewR.width = Short.MAX_VALUE;
    viewR.height = Short.MAX_VALUE;
    iconR.x = iconR.y = iconR.width = iconR.height = 0;
    textR.x = textR.y = textR.width = textR.height = 0;

    SwingUtilities.layoutCompoundLabel(
        (JComponent) b,
        fm,
        text,
        icon,
        b.getVerticalAlignment(),
        b.getHorizontalAlignment(),
        b.getVerticalTextPosition(),
        b.getHorizontalTextPosition(),
        viewR,
        iconR,
        textR,
        (text == null ? 0 : textIconGap));

    /* The preferred size of the button is the size of
     * the text and icon rectangles plus the buttons insets.
     */

    Rectangle r = iconR.union(textR);

    // if (b.isBorderPainted()) {
    Insets insets = b.getInsets();
    r.width += insets.left + insets.right;
    r.height += insets.top + insets.bottom;
    // }
    if (!QuaquaUtilities.isSmallSizeVariant(b)
        && style.equals("push")
        && b.getIcon() == null
        && b.getText() != null) {
      r.width = Math.max(r.width, UIManager.getInt("Button.minimumWidth"));
    }
    return r.getSize();
  }
  /**
   * Compute and return the location of the icons origin, the location of origin of the text
   * baseline, and a possibly clipped version of the compound labels string. Locations are computed
   * relative to the viewR rectangle.
   */
  private static String layoutMenuItem(
      JComponent c,
      FontMetrics fm,
      String text,
      FontMetrics fmAccel,
      String acceleratorText,
      Icon icon,
      Icon checkIcon,
      Icon arrowIcon,
      int verticalAlignment,
      int horizontalAlignment,
      int verticalTextPosition,
      int horizontalTextPosition,
      Rectangle viewR,
      Rectangle iconR,
      Rectangle textR,
      Rectangle acceleratorR,
      Rectangle checkIconR,
      Rectangle arrowIconR,
      int textIconGap,
      int menuItemGap) {

    SwingUtilities.layoutCompoundLabel(
        c,
        fm,
        text,
        icon,
        verticalAlignment,
        horizontalAlignment,
        verticalTextPosition,
        horizontalTextPosition,
        viewR,
        iconR,
        textR,
        textIconGap);

    /* Initialize the acceelratorText bounds rectangle textR.  If a null
     * or and empty String was specified we substitute "" here
     * and use 0,0,0,0 for acceleratorTextR.
     */
    if ((acceleratorText == null) || acceleratorText.equals("")) {
      acceleratorR.width = acceleratorR.height = 0;
      acceleratorText = "";
    } else {
      acceleratorR.width = SwingUtilities2.stringWidth(c, fmAccel, acceleratorText);
      acceleratorR.height = fmAccel.getHeight();
    }

    /* Initialize the checkIcon bounds rectangle checkIconR.
     */

    if (checkIcon != null) {
      checkIconR.width = checkIcon.getIconWidth();
      checkIconR.height = checkIcon.getIconHeight();
    } else {
      checkIconR.width = checkIconR.height = 0;
    }

    /* Initialize the arrowIcon bounds rectangle arrowIconR.
     */

    if (arrowIcon != null) {
      arrowIconR.width = arrowIcon.getIconWidth();
      arrowIconR.height = arrowIcon.getIconHeight();
    } else {
      arrowIconR.width = arrowIconR.height = 0;
    }

    Rectangle labelR = iconR.union(textR);
    if (MotifGraphicsUtils.isLeftToRight(c)) {
      textR.x += checkIconR.width + menuItemGap;
      iconR.x += checkIconR.width + menuItemGap;

      // Position the Accelerator text rect
      acceleratorR.x = viewR.x + viewR.width - arrowIconR.width - menuItemGap - acceleratorR.width;

      // Position the Check and Arrow Icons
      checkIconR.x = viewR.x;
      arrowIconR.x = viewR.x + viewR.width - menuItemGap - arrowIconR.width;
    } else {
      textR.x -= (checkIconR.width + menuItemGap);
      iconR.x -= (checkIconR.width + menuItemGap);

      // Position the Accelerator text rect
      acceleratorR.x = viewR.x + arrowIconR.width + menuItemGap;

      // Position the Check and Arrow Icons
      checkIconR.x = viewR.x + viewR.width - checkIconR.width;
      arrowIconR.x = viewR.x + menuItemGap;
    }

    // Align the accelertor text and the check and arrow icons vertically
    // with the center of the label rect.
    acceleratorR.y = labelR.y + (labelR.height / 2) - (acceleratorR.height / 2);
    arrowIconR.y = labelR.y + (labelR.height / 2) - (arrowIconR.height / 2);
    checkIconR.y = labelR.y + (labelR.height / 2) - (checkIconR.height / 2);

    /*
    System.out.println("Layout: v=" +viewR+"  c="+checkIconR+" i="+
    iconR+" t="+textR+" acc="+acceleratorR+" a="+arrowIconR);
    */
    return text;
  }
  public void paint(final Graphics g, final JComponent c) {
    final AnchoredButton button = (AnchoredButton) c;

    final String text = button.getText();
    final Icon icon = (button.isEnabled()) ? button.getIcon() : button.getDisabledIcon();

    if ((icon == null) && (text == null)) {
      return;
    }

    final FontMetrics fm = button.getFontMetrics(button.getFont());
    ourViewInsets = c.getInsets(ourViewInsets);

    ourViewRect.x = ourViewInsets.left;
    ourViewRect.y = ourViewInsets.top;

    final ToolWindowAnchor anchor = button.getAnchor();

    // Use inverted height & width
    if (ToolWindowAnchor.RIGHT == anchor || ToolWindowAnchor.LEFT == anchor) {
      ourViewRect.height = c.getWidth() - (ourViewInsets.left + ourViewInsets.right);
      ourViewRect.width = c.getHeight() - (ourViewInsets.top + ourViewInsets.bottom);
    } else {
      ourViewRect.height = c.getHeight() - (ourViewInsets.left + ourViewInsets.right);
      ourViewRect.width = c.getWidth() - (ourViewInsets.top + ourViewInsets.bottom);
    }

    ourIconRect.x = ourIconRect.y = ourIconRect.width = ourIconRect.height = 0;
    ourTextRect.x = ourTextRect.y = ourTextRect.width = ourTextRect.height = 0;

    final String clippedText =
        SwingUtilities.layoutCompoundLabel(
            c,
            fm,
            text,
            icon,
            button.getVerticalAlignment(),
            button.getHorizontalAlignment(),
            button.getVerticalTextPosition(),
            button.getHorizontalTextPosition(),
            ourViewRect,
            ourIconRect,
            ourTextRect,
            button.getText() == null ? 0 : button.getIconTextGap());

    // Paint button's background

    final Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    final ButtonModel model = button.getModel();

    final Color background = button.getBackground();

    Color toBorder = model.isRollover() ? new Color(0, 0, 0, 50) : null;
    final boolean vertical = anchor == ToolWindowAnchor.LEFT || anchor == ToolWindowAnchor.RIGHT;

    if (model.isArmed() && model.isPressed() || model.isSelected()) {
      g2.setColor(new Color(0, 0, 0, 30));
      g2.fillRect(3, 3, button.getWidth() - (vertical ? 6 : 5), button.getHeight() - 6);

      g2.setColor(new Color(0, 0, 0, 120));
      g2.drawLine(2, 2, 3 + button.getWidth() - (vertical ? 7 : 6), 2);
      g2.drawLine(2, 3, 2, 3 + button.getHeight() - 7);

      g2.setColor(new Color(0, 0, 0, 40));
      g2.drawRect(3, 3, button.getWidth() - (vertical ? 7 : 6), button.getHeight() - 7);

      g2.setColor(new Color(255, 255, 255, 110));
      g2.drawLine(
          3,
          button.getHeight() - 3,
          3 + button.getWidth() - (vertical ? 6 : 5),
          button.getHeight() - 3);
      g2.drawLine(
          3 + button.getWidth() - (vertical ? 6 : 5),
          2,
          3 + button.getWidth() - (vertical ? 6 : 5),
          3 + button.getHeight() - 7);

      toBorder = null;
    }

    if (toBorder != null) {
      g.setColor(toBorder);
      g.drawRect(2, 2, button.getWidth() - (vertical ? 6 : 5), button.getHeight() - 6);
    }

    AffineTransform tr = null;
    if (ToolWindowAnchor.RIGHT == anchor || ToolWindowAnchor.LEFT == anchor) {
      tr = g2.getTransform();
      if (ToolWindowAnchor.RIGHT == anchor) {
        if (icon != null) { // do not rotate icon
          icon.paintIcon(c, g2, ourIconRect.y, ourIconRect.x);
        }
        g2.rotate(Math.PI / 2);
        g2.translate(0, -c.getWidth());
      } else {
        if (icon != null) { // do not rotate icon
          icon.paintIcon(
              c, g2, ourIconRect.y, c.getHeight() - ourIconRect.x - icon.getIconHeight());
        }
        g2.rotate(-Math.PI / 2);
        g2.translate(-c.getHeight(), 0);
      }
    } else {
      if (icon != null) {
        icon.paintIcon(c, g2, ourIconRect.x, ourIconRect.y);
      }
    }

    // paint text

    if (text != null) {
      if (model.isEnabled()) {
        if (model.isArmed() && model.isPressed() || model.isSelected()) {
          g.setColor(background);
        } else {
          g.setColor(button.getForeground());
        }
      } else {
        g.setColor(background.darker());
      }
      /* Draw the Text */
      if (model.isEnabled()) {
        /** * paint the text normally */
        g.setColor(button.getForeground());
        BasicGraphicsUtils.drawString(
            g, clippedText, button.getMnemonic2(), ourTextRect.x, ourTextRect.y + fm.getAscent());
      } else {
        /** * paint the text disabled ** */
        if (model.isSelected()) {
          g.setColor(c.getBackground());
        } else {
          g.setColor(getDisabledTextColor());
        }
        BasicGraphicsUtils.drawString(
            g, clippedText, button.getMnemonic2(), ourTextRect.x, ourTextRect.y + fm.getAscent());
      }
    }
    if (ToolWindowAnchor.RIGHT == anchor || ToolWindowAnchor.LEFT == anchor) {
      g2.setTransform(tr);
    }
  }
Esempio n. 22
0
  protected void updateLayout(AbstractButton button, ButtonInfo buttonInfo) {
    Shape customShape = (Shape) button.getClientProperty(SHAPE);
    int width = button.getWidth();
    int height = button.getHeight();
    int horizontalPosition = getHorizontalPosition(button);
    int verticalPosition = getVerticalPosition(button);

    String key = width + " " + height + " " + horizontalPosition + " " + verticalPosition;
    button.putClientProperty("FilledButtonUI.validationKey", key);

    int dx = 0;
    int dy = 0;

    if (getFocusPainting(button) == PaintFocus.OUTSIDE
        || getFocusPainting(button) == PaintFocus.BOTH) {
      if (horizontalPosition == POS_LEFT || horizontalPosition == POS_ONLY) {
        dx += focusSize;
        width -= focusSize;
      }
      if (horizontalPosition == POS_RIGHT || horizontalPosition == POS_ONLY) {
        width -= focusSize;
      }
      if (verticalPosition == POS_TOP || verticalPosition == POS_ONLY) {
        dy += focusSize;
        height -= focusSize;
      }
      if (verticalPosition == POS_BOTTOM || verticalPosition == POS_ONLY) {
        height -= focusSize;
      }
    } else {
      if ((verticalPosition == POS_BOTTOM || verticalPosition == POS_ONLY)
          && fill.getShadowHighlight(button) != null) {
        height--;
      }
    }

    ButtonInfo info = getButtonInfo(button);
    boolean showSeparators = isShowingSeparators(button);

    shape.getShape(
        info.fill,
        info.stroke,
        width,
        height,
        horizontalPosition,
        verticalPosition,
        showSeparators,
        customShape);

    AffineTransform translation = AffineTransform.getTranslateInstance(dx, dy);
    info.fill.transform(translation);
    info.stroke.transform(translation);

    Font font = button.getFont();
    if (font == null) font = new Font("Default", 0, 12);
    FontMetrics fm = button.getFontMetrics(font);

    info.viewRect.x =
        info.viewRect.y =
            info.textRect.x = info.textRect.y = info.textRect.width = info.textRect.height = 0;
    info.iconRect.x = info.iconRect.y = info.iconRect.width = info.iconRect.height = 0;
    info.viewRect.width = Short.MAX_VALUE;
    info.viewRect.height = Short.MAX_VALUE;

    SwingUtilities.layoutCompoundLabel(
        fm,
        button.getText(),
        button.getIcon(),
        button.getVerticalAlignment(),
        button.getHorizontalAlignment(),
        button.getVerticalTextPosition(),
        button.getHorizontalTextPosition(),
        info.viewRect,
        info.iconRect,
        info.textRect,
        button.getIconTextGap());

    Insets textInsets = getTextPadding();
    Insets iconInsets = getIconPadding();

    Rectangle tempTextRect = new Rectangle(info.textRect);
    Rectangle tempIconRect = new Rectangle(info.iconRect);
    if (info.textRect.width > 0) {
      tempTextRect.y -= textInsets.top;
      tempTextRect.x -= textInsets.left;
      tempTextRect.width += textInsets.left + textInsets.right;
      tempTextRect.height += textInsets.top + textInsets.bottom;
    }
    if (info.iconRect.width > 0) {
      tempIconRect.y -= iconInsets.top;
      tempIconRect.x -= iconInsets.left;
      tempIconRect.width += iconInsets.left + iconInsets.right;
      tempIconRect.height += iconInsets.top + iconInsets.bottom;
    }

    Rectangle sum = getSum(new Rectangle[] {tempIconRect, tempTextRect});

    Insets padding = getContentInsets(button);

    float centerX, centerY;
    if (button.getHorizontalAlignment() == SwingConstants.LEFT
        || button.getHorizontalAlignment() == SwingConstants.LEADING) {
      centerX = padding.left + sum.width / 2;
    } else if (button.getHorizontalAlignment() == SwingConstants.RIGHT
        || button.getHorizontalAlignment() == SwingConstants.TRAILING) {
      centerX = button.getWidth() - padding.right - sum.width / 2;
    } else {
      centerX = ((button.getWidth() - padding.left - padding.right)) / 2f;
    }
    // TODO: also take into account vertical alignment:
    centerY = ((button.getHeight() - padding.top - padding.bottom)) / 2f;

    float shiftX = centerX - (sum.width) / 2f - sum.x + padding.left;
    float shiftY = centerY - (sum.height) / 2f - sum.y + padding.top;

    if (customShape == null) {
      if (button.getVerticalAlignment() == SwingConstants.CENTER
          && button.getVerticalTextPosition() == SwingConstants.CENTER
          && info.textRect.width > 0) {
        int unusedAscent = getUnusedAscent(fm, font);
        int ascent = fm.getAscent() - unusedAscent;

        shiftY =
            (int) (-sum.y + centerY - ascent / 2 - unusedAscent + padding.top - textInsets.top);
      }
    }

    info.iconRect.setFrame(
        info.iconRect.x + shiftX,
        info.iconRect.y + shiftY,
        info.iconRect.width,
        info.iconRect.height);
    info.textRect.setRect(
        (int) (info.textRect.x + shiftX + .5f),
        (int) (info.textRect.y + shiftY + .5f),
        info.textRect.width,
        info.textRect.height);

    info.updateFillBounds();
  }
Esempio n. 23
0
 private static int getTabbedPaneBaseline(JTabbedPane tp, int height) {
   if (tp.getTabCount() > 0) {
     if (isAqua()) {
       return getAquaTabbedPaneBaseline(tp, height);
     }
     Insets insets = tp.getInsets();
     Insets contentBorderInsets = UIManager.getInsets("TabbedPane.contentBorderInsets");
     Insets tabAreaInsets =
         rotateInsets(UIManager.getInsets("TabbedPane.tabAreaInsets"), tp.getTabPlacement());
     FontMetrics metrics = tp.getFontMetrics(tp.getFont());
     int maxHeight = getMaxTabHeight(tp);
     iconRect.setBounds(0, 0, 0, 0);
     textRect.setBounds(0, 0, 0, 0);
     viewRect.setBounds(0, 0, Short.MAX_VALUE, maxHeight);
     SwingUtilities.layoutCompoundLabel(
         tp,
         metrics,
         "A",
         null,
         SwingUtilities.CENTER,
         SwingUtilities.CENTER,
         SwingUtilities.CENTER,
         SwingUtilities.TRAILING,
         viewRect,
         iconRect,
         textRect,
         0);
     int baseline = textRect.y + metrics.getAscent();
     switch (tp.getTabPlacement()) {
       case JTabbedPane.TOP:
         baseline += insets.top + tabAreaInsets.top;
         if (isWindows()) {
           if (tp.getTabCount() > 1) {
             baseline += 1;
           } else {
             baseline -= 1;
           }
         }
         return baseline;
       case JTabbedPane.BOTTOM:
         baseline = tp.getHeight() - insets.bottom - tabAreaInsets.bottom - maxHeight + baseline;
         if (isWindows()) {
           if (tp.getTabCount() > 1) {
             baseline += -1;
           } else {
             baseline += 1;
           }
         }
         return baseline;
       case JTabbedPane.LEFT:
       case JTabbedPane.RIGHT:
         if (isAqua()) {
           // Aqua rotates left/right text, so that there isn't a good
           // baseline.
           return -1;
         }
         baseline += insets.top + tabAreaInsets.top;
         if (isWindows()) {
           baseline += (maxHeight % 2);
         }
         return baseline;
     }
   }
   return -1;
 }
  private String layoutMenuItem(
      FontMetrics fontmetrics,
      String text,
      Icon icon,
      Icon checkIcon,
      Icon arrowIcon,
      int verticalAlignment,
      int horizontalAlignment,
      int verticalTextPosition,
      int horizontalTextPosition,
      Rectangle viewRect,
      Rectangle iconRect,
      Rectangle textRect,
      Rectangle acceleratorRect,
      Rectangle checkIconRect,
      Rectangle arrowIconRect,
      int textIconGap,
      int menuItemGap) {
    SwingUtilities.layoutCompoundLabel(
        menuItem,
        fontmetrics,
        text,
        icon,
        verticalAlignment,
        horizontalAlignment,
        verticalTextPosition,
        horizontalTextPosition,
        viewRect,
        iconRect,
        textRect,
        textIconGap);
    acceleratorRect.width = acceleratorRect.height = 0;

    /* Initialize the checkIcon bounds rectangle's width & height.
     */
    if (useCheckAndArrow()) {
      if (checkIcon != null) {
        checkIconRect.width = checkIcon.getIconWidth();
        checkIconRect.height = checkIcon.getIconHeight();
      } else {
        checkIconRect.width = checkIconRect.height = 0;
      }

      /* Initialize the arrowIcon bounds rectangle width & height.
       */
      if (arrowIcon != null) {
        arrowIconRect.width = arrowIcon.getIconWidth();
        arrowIconRect.height = arrowIcon.getIconHeight();
      } else {
        arrowIconRect.width = arrowIconRect.height = 0;
      }
      textRect.x += myMaxGutterIconWidth;
      iconRect.x += myMaxGutterIconWidth;
    }
    textRect.x += menuItemGap;
    iconRect.x += menuItemGap;
    Rectangle labelRect = iconRect.union(textRect);

    // Position the Accelerator text rect

    acceleratorRect.x += viewRect.width - arrowIconRect.width - menuItemGap - acceleratorRect.width;
    acceleratorRect.y = (viewRect.y + viewRect.height / 2) - acceleratorRect.height / 2;

    // Position the Check and Arrow Icons

    if (useCheckAndArrow()) {
      arrowIconRect.x += viewRect.width - arrowIconRect.width;
      arrowIconRect.y = (viewRect.y + labelRect.height / 2) - arrowIconRect.height / 2;
      if (checkIcon != null) {
        checkIconRect.y = (viewRect.y + labelRect.height / 2) - checkIconRect.height / 2;
        checkIconRect.x += (viewRect.x + myMaxGutterIconWidth / 2) - checkIcon.getIconWidth() / 2;
        a = viewRect.x;
        e = (viewRect.y + labelRect.height / 2) - myMaxGutterIconWidth / 2;
        k = viewRect.x + myMaxGutterIconWidth + 2;
      } else {
        checkIconRect.x = checkIconRect.y = 0;
      }
    }
    return text;
  }