package com.jedics.swing.plaf;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JComponent;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.metal.MetalButtonUI;
import javax.swing.text.View;
import sun.swing.SwingUtilities2;
/**
* MetalButtonUI implementation
* <p>
* <strong>Warning:</strong> Serialized objects of this class will not be
* compatible with future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running the
* same version of Swing. As of 1.4, support for long term storage of all
* JavaBeans<sup><font size="-2">TM</font></sup> has been added to the
* <code>java.beans</code> package. Please see {@link java.beans.XMLEncoder}.
*
* @version 1.39 11/30/05
* @author Tom Santos
*/
public class JediButtonUI extends MetalButtonUI {
public static final int BUTTON_X_PADDING = 12;
public static final int BUTTON_Y_PADDING = 2;
private final static JediButtonUI solareButtonUI = new JediButtonUI();
public JediButtonUI() {
}
public static ComponentUI createUI(JComponent c) {
return solareButtonUI;
}
public void installDefaults(AbstractButton b) {
super.installDefaults(b);
b.setBorderPainted(false);
b.setMinimumSize(b.getPreferredSize());
b.setPreferredSize(new Dimension(b.getPreferredSize().width
+ (2 * BUTTON_X_PADDING), b.getPreferredSize().height
+ (2 * BUTTON_Y_PADDING)));
LookAndFeel.installProperty(b, "opaque", Boolean.FALSE);
}
public void uninstallDefaults(AbstractButton b) {
super.uninstallDefaults(b);
}
public void update(Graphics g, JComponent c) {
paint(g, c);
}
private static Rectangle viewRect = new Rectangle();
private static Rectangle textRect = new Rectangle();
private static Rectangle iconRect = new Rectangle();
public void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
String text = layout(b, SwingUtilities2.getFontMetrics(b, g), b
.getWidth(), b.getHeight());
clearTextShiftOffset();
if (!model.isEnabled()) {
g.setColor(JLAFConstants.LIGHT_GRAY);
g.fillRoundRect(0, 0, c.getWidth()-1, c.getHeight()-1, 10, 7);
g.setColor(JLAFConstants.DARK_GRAY);
g.drawRoundRect(0, 0, c.getWidth()-1, c.getHeight()-1, 9, 5);
} else if (model.isArmed() && model.isPressed()) {
// g.setColor(JLAFConstants.INVISIBLE);
// g.fillRoundRect(0, 0, c.getWidth(), c.getHeight(), 10, 7);
g.setColor(JLAFConstants.DARK_GRAY);
g.drawRoundRect(0, 0, c.getWidth()-1, c.getHeight()-1, 9, 5);
} else if (model.isRollover() || model.isArmed()) {
// g.setColor(JLAFConstants.INVISIBLE);
// g.fillRoundRect(0, 0, c.getWidth(), c.getHeight(), 10, 7);
g.setColor(JLAFConstants.LIGHT_GRAY);
g.drawRoundRect(0, 0, c.getWidth()-1, c.getHeight()-1, 9, 5);
} else {
// g.setColor(JLAFConstants.INVISIBLE);
// g.fillRoundRect(0, 0, c.getWidth(), c.getHeight(), 10, 7);
g.setColor(JLAFConstants.BLACK);
g.drawRoundRect(0, 0, c.getWidth()-1, c.getHeight()-1, 9, 5);
}
// Paint the Icon
if (b.getIcon() != null) {
super.paintIcon(g, c, iconRect);
}
if (text != null && !text.equals("")) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, textRect);
} else {
paintText(g, b, textRect, text);
}
}
}
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
int mnemIndex = b.getDisplayedMnemonicIndex();
/* Draw the Text */
if (model.isEnabled()) {
if (model.isArmed() && model.isPressed()) {
g.setColor(JLAFConstants.BLACK);
} else if (model.isRollover()) {
g.setColor(JLAFConstants.DARK_GRAY);
} else {
g.setColor(JLAFConstants.DARK_GRAY);
}
} else {
g.setColor(JLAFConstants.DARK_GRAY);
}
SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemIndex,
textRect.x, textRect.y + fm.getAscent());
}
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());
}
}