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()); }
/** 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(); }
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(); }
/** * 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)); }
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; }
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(); }