BasicButtonUI.java [plain text]
package javax.swing.plaf.basic;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.ComponentUI;
public class BasicButtonUI extends ButtonUI
{
int defaultTextIconGap = 3;
int defaultTextShiftOffset = 0;
public static ComponentUI createUI(final JComponent c)
{
return new BasicButtonUI();
}
public int getDefaultTextIconGap(AbstractButton b)
{
return defaultTextIconGap;
}
protected void installDefaults(AbstractButton b)
{
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
b.setForeground(defaults.getColor("Button.foreground"));
b.setBackground(defaults.getColor("Button.background"));
b.setMargin(defaults.getInsets("Button.margin"));
b.setBorder(defaults.getBorder("Button.border"));
}
protected void uninstallDefaults(AbstractButton b)
{
b.setForeground(null);
b.setBackground(null);
b.setBorder(null);
b.setMargin(null);
}
protected BasicButtonListener listener;
protected BasicButtonListener createButtonListener(AbstractButton b)
{
return new BasicButtonListener();
}
public void installListeners(AbstractButton b)
{
listener = createButtonListener(b);
b.addChangeListener(listener);
b.addPropertyChangeListener(listener);
b.addFocusListener(listener);
b.addMouseListener(listener);
b.addMouseMotionListener(listener);
}
public void uninstallListeners(AbstractButton b)
{
b.removeChangeListener(listener);
b.removePropertyChangeListener(listener);
b.removeFocusListener(listener);
b.removeMouseListener(listener);
b.removeMouseMotionListener(listener);
}
protected void installKeyboardActions(AbstractButton b)
{
}
protected void uninstallKeyboardActions(AbstractButton b)
{
}
public void installUI(final JComponent c)
{
super.installUI(c);
if (c instanceof AbstractButton)
{
AbstractButton b = (AbstractButton) c;
installDefaults(b);
installListeners(b);
installKeyboardActions(b);
}
}
public Dimension getPreferredSize(JComponent c)
{
AbstractButton b = (AbstractButton)c;
Dimension d =
BasicGraphicsUtils.getPreferredButtonSize
(b, defaultTextIconGap + defaultTextShiftOffset);
return d;
}
static private Icon currentIcon(AbstractButton b)
{
Icon i = b.getIcon();
ButtonModel model = b.getModel();
if (model.isPressed() && b.getPressedIcon() != null)
i = b.getPressedIcon();
else if (model.isRollover())
{
if (b.isSelected() && b.getRolloverSelectedIcon() != null)
i = b.getRolloverSelectedIcon();
else if (b.getRolloverIcon() != null)
i = b.getRolloverIcon();
}
else if (b.isSelected())
{
if (b.isEnabled() && b.getSelectedIcon() != null)
i = b.getSelectedIcon();
else if (b.getDisabledSelectedIcon() != null)
i = b.getDisabledSelectedIcon();
}
else if (! b.isEnabled() && b.getDisabledIcon() != null)
i = b.getDisabledIcon();
return i;
}
public void paint(Graphics g, JComponent c)
{
AbstractButton b = (AbstractButton) c;
Rectangle tr = new Rectangle();
Rectangle ir = new Rectangle();
Rectangle vr = new Rectangle();
Rectangle br = new Rectangle();
Font f = c.getFont();
g.setFont(f);
SwingUtilities.calculateInnerArea(b, br);
SwingUtilities.calculateInsetArea(br, b.getMargin(), vr);
String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f),
b.getText(),
currentIcon(b),
b.getVerticalAlignment(),
b.getHorizontalAlignment(),
b.getVerticalTextPosition(),
b.getHorizontalTextPosition(),
vr, ir, tr,
defaultTextIconGap
+ defaultTextShiftOffset);
if ((b.getModel().isArmed() && b.getModel().isPressed())
|| b.isSelected())
paintButtonPressed(g, br, c);
else
paintButtonNormal(g, br, c);
paintIcon(g, c, ir);
if (text != null)
paintText(g, c, tr, b.getText());
paintFocus(g, c, vr, tr, ir);
}
protected void paintFocus(Graphics g, JComponent c, Rectangle vr,
Rectangle tr, Rectangle ir)
{
AbstractButton b = (AbstractButton) c;
if (b.hasFocus() && b.isFocusPainted())
{
Graphics2D g2 = (Graphics2D) g;
Stroke saved_stroke = g2.getStroke();
Color saved_color = g2.getColor();
float dashes[] = new float[] {1.0f, 1.0f};
BasicStroke s = new BasicStroke(1.0f,
BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_MITER,
10, dashes, 0.0f);
g2.setStroke(s);
g2.setColor(Color.BLACK);
g2.drawRect(vr.x + 2,
vr.y + 2,
vr.width - 4,
vr.height - 4);
g2.setStroke(saved_stroke);
g2.setColor(saved_color);
}
}
protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect)
{
AbstractButton b = (AbstractButton) c;
Icon i = currentIcon(b);
if (i != null)
{
int x = iconRect.x;
int y = iconRect.y;
i.paintIcon(c, g, x, y);
}
}
protected void paintButtonPressed(Graphics g, Rectangle area, JComponent b)
{
if (((AbstractButton)b).isContentAreaFilled())
{
g.setColor(b.getBackground().darker());
g.fillRect(area.x, area.y, area.width, area.height);
}
}
protected void paintButtonNormal(Graphics g, Rectangle area, JComponent b)
{
if (((AbstractButton)b).isContentAreaFilled())
{
g.setColor(b.getBackground());
g.fillRect(area.x, area.y, area.width, area.height);
}
}
protected void paintText(Graphics g, JComponent c, Rectangle textRect,
String text)
{
Font f = c.getFont();
g.setFont(f);
FontMetrics fm = g.getFontMetrics(f);
g.setColor(c.getForeground());
BasicGraphicsUtils.drawString(g, text, 0,
textRect.x,
textRect.y + fm.getAscent());
}
}