Exemplo n.º 1
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();
  }