/** * Returns the baseline. * * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @see javax.swing.JComponent#getBaseline(int, int) * @since 1.6 */ public int getBaseline(JComponent c, int width, int height) { super.getBaseline(c, width, height); AbstractButton b = (AbstractButton) c; String text = b.getText(); if (text == null || "".equals(text)) { return -1; } FontMetrics fm = b.getFontMetrics(b.getFont()); layout(b, fm, width, height); return BasicHTML.getBaseline(b, textRect.y, fm.getAscent(), textRect.width, textRect.height); }
/** 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(); }
/** 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); }
protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) { ButtonModel model = b.getModel(); FontMetrics fm = JTattooUtilities.getFontMetrics(b, g, b.getFont()); int mnemIndex; if (JTattooUtilities.getJavaVersion() >= 1.4) { mnemIndex = b.getDisplayedMnemonicIndex(); } else { mnemIndex = JTattooUtilities.findDisplayedMnemonicIndex(b.getText(), model.getMnemonic()); } int offs = 0; if (model.isArmed() && model.isPressed()) { offs = 1; } Graphics2D g2D = (Graphics2D) g; Composite composite = g2D.getComposite(); AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f); g2D.setComposite(alpha); Color foreground = b.getForeground(); Color background = b.getBackground(); if (background instanceof ColorUIResource) { if (model.isPressed() && model.isArmed()) { foreground = AbstractLookAndFeel.getTheme().getSelectionForegroundColor(); } else if (model.isRollover()) { foreground = AbstractLookAndFeel.getTheme().getRolloverForegroundColor(); } } if (!model.isEnabled()) { foreground = AbstractLookAndFeel.getTheme().getDisabledForegroundColor(); } if (ColorHelper.getGrayValue(foreground) > 64) { g2D.setColor(Color.black); } else { g2D.setColor(Color.white); } JTattooUtilities.drawStringUnderlineCharAt( b, g, text, mnemIndex, textRect.x + offs + 1, textRect.y + offs + fm.getAscent() + 1); g2D.setComposite(composite); g2D.setColor(foreground); JTattooUtilities.drawStringUnderlineCharAt( b, g, text, mnemIndex, textRect.x + offs, textRect.y + offs + fm.getAscent()); }
@Override public void installUI(JComponent c) { AbstractButton button = (AbstractButton) c; ButtonInfo info = new ButtonInfo(button, this); button.putClientProperty(BUTTON_INFO_KEY, info); button.addMouseListener(info.basicListener); button.addMouseMotionListener(info.basicListener); button.addFocusListener(info.basicListener); button.addPropertyChangeListener(info.basicListener); button.addChangeListener(info.basicListener); button.addKeyListener(focusArrowListener); button.addComponentListener(componentListener); button.addKeyListener(keyArmingListener); button.setRequestFocusEnabled(false); button.setFocusable(true); button.addPropertyChangeListener(positionAndShapeListener); button.setOpaque(false); button.setRolloverEnabled(true); if (button.getIcon() != null) { Font font = UIManager.getFont("IconButton.font"); if (font != null) button.setFont(font); // miniature-ish } super.installUI(c); updateLayout(button, info); if (button.getFont() == null) { Font font = UIManager.getFont("Button.font"); if (font == null) { font = new Font("Default", 0, 13); } button.setFont(font); } }
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(); }
/** 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; }
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(); }