示例#1
0
 public void propertyChange(PropertyChangeEvent evt) {
   String name = evt.getPropertyName();
   if (name.equals(VERTICAL_POSITION)
       || name.equals(HORIZONTAL_POSITION)
       || name.equals(SHAPE)
       || name.equals("JButton.segmentPosition")) { // see Apple Tech Note 2196
     AbstractButton button = (AbstractButton) evt.getSource();
     ButtonUI ui = button.getUI();
     if (ui instanceof FilledButtonUI) {
       FilledButtonUI s = (FilledButtonUI) ui;
       s.updateLayout(button, getButtonInfo(button));
       button.invalidate();
       button.repaint();
     }
   }
 }
示例#2
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;
  }