Example #1
0
  /**
   * Returns one of the PAINT_FOCUS constants declared in this object. If PAINT_NO_FOCUS is used,
   * then this class automatically paints no focus. In that case a subclass would be responsible for
   * any rendering of focus.
   *
   * <p>If PAINT_FOCUS_OUTSIDE is used, then a few rings of focus are painted outside this component
   * under this component. The order of painting is: <br>
   * 1. Shadow Highlight <br>
   * 2. Focus <br>
   * 3. Background Fill <br>
   * 4. Stroke
   *
   * <p>If PAINT_FOCUS_INSIDE is used, then a few rings of focus are painted inside this component.
   * The order of painting is: <br>
   * 1. Shadow Highlight <br>
   * 2. Background Fill <br>
   * 3. Focus <br>
   * 4. Stroke
   *
   * <p>If PAINT_FOCUS_BOTH is used, then the focus appears both inside and outside the filled area.
   * The order of painting is: <br>
   * 1. Shadow Highlight <br>
   * 2. Background Fill <br>
   * 3. Stroke <br>
   * 4. Focus
   *
   * <p>By default this returns PAINT_FOCUS_OUTSIDE if a shape is defined, or if this button is in a
   * single row or column of buttons. (That is, if the horizontal or vertical position is "only").
   * Otherwise this returns PAINT_FOCUS_INSIDE, because once you get to inner buttons the focus
   * <i>has</i> to be painted on the inside to remain visible.
   */
  public PaintFocus getFocusPainting(AbstractButton button) {
    PaintFocus f = (PaintFocus) button.getClientProperty(PAINT_FOCUS_BEHAVIOR_KEY);
    if (f != null) return f;

    Shape shape = (Shape) button.getClientProperty(SHAPE);
    if (shape != null) return PaintFocus.OUTSIDE;

    int horizontalPosition = getHorizontalPosition(button);
    int verticalPosition = getVerticalPosition(button);

    if (!(horizontalPosition == POS_ONLY || verticalPosition == POS_ONLY)) {
      return PaintFocus.INSIDE;
    }
    return PaintFocus.OUTSIDE;
  }
Example #2
0
 public void keyPressed(KeyEvent e) {
   int code = e.getKeyCode();
   if (code == KeyEvent.VK_SPACE) {
     AbstractButton button = (AbstractButton) e.getSource();
     Boolean wasPressed = (Boolean) button.getClientProperty(SPACEBAR_PRESSED);
     if (wasPressed == null || wasPressed.booleanValue() == false) {
       button.putClientProperty(SPACEBAR_PRESSED, Boolean.TRUE);
       button.doClick();
     }
   }
 }
Example #3
0
  /** {@inheritDoc} */
  @Override
  public int getBaseline(JComponent c, int width, int height) {
    if (c == null) {
      throw new NullPointerException("Component must be non-null");
    }
    if (width < 0 || height < 0) {
      throw new IllegalArgumentException("Width and height must be >= 0");
    }
    AbstractButton b = (AbstractButton) c;
    String text = b.getText();
    if (text == null || "".equals(text)) {
      return -1;
    }
    Insets i = b.getInsets();
    Rectangle viewRect = new Rectangle();
    Rectangle textRect = new Rectangle();
    Rectangle iconRect = new Rectangle();
    viewRect.x = i.left;
    viewRect.y = i.top;
    viewRect.width = width - (i.right + viewRect.x);
    viewRect.height = height - (i.bottom + viewRect.y);

    // layout the text and icon
    SynthContext context = getContext(b);
    FontMetrics fm = context.getComponent().getFontMetrics(context.getStyle().getFont(context));
    context
        .getStyle()
        .getGraphicsUtils(context)
        .layoutText(
            context,
            fm,
            b.getText(),
            b.getIcon(),
            b.getHorizontalAlignment(),
            b.getVerticalAlignment(),
            b.getHorizontalTextPosition(),
            b.getVerticalTextPosition(),
            viewRect,
            iconRect,
            textRect,
            b.getIconTextGap());
    View view = (View) b.getClientProperty(BasicHTML.propertyKey);
    int baseline;
    if (view != null) {
      baseline = BasicHTML.getHTMLBaseline(view, textRect.width, textRect.height);
      if (baseline >= 0) {
        baseline += textRect.y;
      }
    } else {
      baseline = textRect.y + fm.getAscent();
    }
    return baseline;
  }
Example #4
0
 private static int getButtonBaselineResizeBehavior(AbstractButton button) {
   if (button.getClientProperty("html") != null) {
     return BRB_OTHER;
   }
   switch (button.getVerticalAlignment()) {
     case AbstractButton.TOP:
       return BRB_CONSTANT_ASCENT;
     case AbstractButton.BOTTOM:
       return BRB_CONSTANT_DESCENT;
     case AbstractButton.CENTER:
       return BRB_CENTER_OFFSET;
   }
   return BRB_OTHER;
 }
Example #5
0
  /**
   * This checks to see if updateLayout() needs to be called. Generally this returns true if the
   * width, height, or segment position has changed.
   */
  protected boolean isLayoutValid(AbstractButton button) {
    int horizontalPosition = getHorizontalPosition(button);
    int verticalPosition = getVerticalPosition(button);
    int width = button.getWidth();
    int height = button.getHeight();

    String key = width + " " + height + " " + horizontalPosition + " " + verticalPosition;

    String oldKey = (String) button.getClientProperty("FilledButtonUI.validationKey");
    if (oldKey == null) return false;

    if (oldKey.equals(key)) return true;

    return false;
  }
Example #6
0
    private void paintOceanBorder(Component c, Graphics g, int x, int y, int w, int h) {
      AbstractButton button = (AbstractButton) c;
      ButtonModel model = ((AbstractButton) c).getModel();

      g.translate(x, y);
      if (MetalUtils.isToolBarButton(button)) {
        if (model.isEnabled()) {
          if (model.isPressed()) {
            g.setColor(MetalLookAndFeel.getWhite());
            g.fillRect(1, h - 1, w - 1, 1);
            g.fillRect(w - 1, 1, 1, h - 1);
            g.setColor(MetalLookAndFeel.getControlDarkShadow());
            g.drawRect(0, 0, w - 2, h - 2);
            g.fillRect(1, 1, w - 3, 1);
          } else if (model.isSelected() || model.isRollover()) {
            g.setColor(MetalLookAndFeel.getWhite());
            g.fillRect(1, h - 1, w - 1, 1);
            g.fillRect(w - 1, 1, 1, h - 1);
            g.setColor(MetalLookAndFeel.getControlDarkShadow());
            g.drawRect(0, 0, w - 2, h - 2);
          } else {
            g.setColor(MetalLookAndFeel.getWhite());
            g.drawRect(1, 1, w - 2, h - 2);
            g.setColor(UIManager.getColor("Button.toolBarBorderBackground"));
            g.drawRect(0, 0, w - 2, h - 2);
          }
        } else {
          g.setColor(UIManager.getColor("Button.disabledToolBarBorderBackground"));
          g.drawRect(0, 0, w - 2, h - 2);
        }
      } else if (model.isEnabled()) {
        boolean pressed = model.isPressed();
        boolean armed = model.isArmed();

        if ((c instanceof JButton) && ((JButton) c).isDefaultButton()) {
          g.setColor(MetalLookAndFeel.getControlDarkShadow());
          g.drawRect(0, 0, w - 1, h - 1);
          g.drawRect(1, 1, w - 3, h - 3);
        } else if (pressed) {
          g.setColor(MetalLookAndFeel.getControlDarkShadow());
          g.fillRect(0, 0, w, 2);
          g.fillRect(0, 2, 2, h - 2);
          g.fillRect(w - 1, 1, 1, h - 1);
          g.fillRect(1, h - 1, w - 2, 1);
        } else if (model.isRollover() && button.getClientProperty(NO_BUTTON_ROLLOVER) == null) {
          g.setColor(MetalLookAndFeel.getPrimaryControl());
          g.drawRect(0, 0, w - 1, h - 1);
          g.drawRect(2, 2, w - 5, h - 5);
          g.setColor(MetalLookAndFeel.getControlDarkShadow());
          g.drawRect(1, 1, w - 3, h - 3);
        } else {
          g.setColor(MetalLookAndFeel.getControlDarkShadow());
          g.drawRect(0, 0, w - 1, h - 1);
        }
      } else {
        g.setColor(MetalLookAndFeel.getInactiveControlTextColor());
        g.drawRect(0, 0, w - 1, h - 1);
        if ((c instanceof JButton) && ((JButton) c).isDefaultButton()) {
          g.drawRect(1, 1, w - 3, h - 3);
        }
      }
    }
  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();
  }
Example #8
0
 protected boolean isEnabled(AbstractButton button) {
   Boolean b = (Boolean) button.getClientProperty(ENABLED_STATE);
   if (b != null) return b;
   return button.isEnabled();
 }
Example #9
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;
  }
Example #10
0
 /**
  * Return true if this button should render as if the mouse is over it. This first checks the
  * ROLLOVER client property to determine if an artificial state is being induced; if that is
  * undefined then the ButtonModel's isRollover() method is used.
  */
 protected static boolean isRollover(AbstractButton button) {
   Boolean b = (Boolean) button.getClientProperty(ROLLOVER);
   if (b == null) return button.getModel().isRollover();
   return b;
 }
Example #11
0
 /**
  * Return true if the spacebar is pressed. This consults the SPACEBAR_PRESSED property, which is
  * correctly controlled if a FilledButtonUI is installed in a given button.
  *
  * <p>This is visually the same as setting the ButtonModel's pressed state to true. However if
  * that property were used and the mouse was interacting with this button: the two agents (the
  * KeyListener and the MouseListener) would constantly be overriding the other's work.
  */
 protected static boolean isSpacebarPressed(AbstractButton button) {
   Boolean b = (Boolean) button.getClientProperty(SPACEBAR_PRESSED);
   if (b == null) return false;
   return b.booleanValue();
 }
Example #12
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();
  }
Example #13
0
 protected boolean isShowingSeparators(AbstractButton button) {
   Boolean showSeparators = (Boolean) button.getClientProperty(SHOW_SEPARATORS);
   if (showSeparators == null) return true;
   return showSeparators;
 }
Example #14
0
 protected static ButtonInfo getButtonInfo(AbstractButton button) {
   ButtonInfo i = (ButtonInfo) button.getClientProperty(BUTTON_INFO_KEY);
   if (i == null) throw new NullPointerException("installUI was not called");
   return i;
 }
Example #15
0
 protected static boolean isStrokePainted(AbstractButton b) {
   Boolean t = (Boolean) b.getClientProperty(STROKE_PAINTED);
   if (t != null) return t;
   return b.isBorderPainted();
 }
Example #16
0
 /**
  * Return true if this button should render as if it has the keyboard focus. This first checks the
  * HAS_FOCUS client property to determine if an artificial state is being induced; if that is
  * undefined then the button's hasFocus() method is consulted.
  */
 protected static boolean hasFocus(AbstractButton button) {
   Boolean hasFocus = (Boolean) button.getClientProperty(HAS_FOCUS);
   if (hasFocus == null) hasFocus = button.hasFocus();
   return hasFocus;
 }