下面列出了javax.swing.AbstractButton#isEnabled ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void paintText(Graphics g, AbstractButton b, Rectangle textRect,
String text) {
// this hack helps us render a button in a different enabled state
// than what it really is:
ButtonState.Boolean state = (ButtonState.Boolean) b
.getClientProperty(PROPERTY_BOOLEAN_BUTTON_STATE);
if (state != null && state.isEnabled() != b.isEnabled()) {
scratchButton.setText(b.getText());
scratchButton.setDisplayedMnemonicIndex(b
.getDisplayedMnemonicIndex());
scratchButton.setForeground(b.getForeground());
scratchButton.setFont(b.getFont());
scratchButton.setBackground(b.getBackground());
scratchButton.setEnabled(state.isEnabled());
super.paintText(g, scratchButton, textRect, text);
return;
}
super.paintText(g, b, textRect, text);
}
/**
* Drasw a button in a toolbar.
*
* @param b
* the button
* @param g
* the graphics instance
*/
public static void drawToolbarButton(Graphics g, AbstractButton b) {
if (!b.isEnabled()) {
return;
}
if (b.getModel().isSelected() && b.isRolloverEnabled() || b.getModel().isPressed() && b.getModel().isArmed()
&& b.isRolloverEnabled()) {
if (b.isContentAreaFilled()) {
drawButton(b, g, createShapeForButton(b));
}
if (b.isBorderPainted()) {
drawButtonBorder(b, g, createBorderShapeForButton(b));
}
} else if (b.getModel().isRollover() && b.isRolloverEnabled()) {
if (b.isBorderPainted()) {
drawButtonBorder(b, g, createBorderShapeForButton(b));
}
}
}
private void refresh(final AbstractButton b) {
b.setBackground(UIUtils.getProfilerResultsBackground());
boolean hovered = Boolean.TRUE.equals(b.getClientProperty(PROP_HOVERED));
boolean filled = b.isEnabled() && (hovered || b.isSelected() || b.isFocusOwner());
b.setOpaque(filled);
b.setContentAreaFilled(filled);
b.repaint();
}
@Override
public void mouseEntered(MouseEvent evt) {
Object src = evt.getSource();
if (src instanceof AbstractButton) {
AbstractButton button = (AbstractButton) evt.getSource();
if (button.isEnabled()) {
button.setContentAreaFilled(true);
button.setBorderPainted(true);
}
}
}
public @Override void mouseEntered(MouseEvent evt) {
Object src = evt.getSource();
if (src instanceof AbstractButton) {
AbstractButton button = (AbstractButton)evt.getSource();
if (button.isEnabled()) {
button.setContentAreaFilled(true);
button.setBorderPainted(true);
}
}
}
@Override
public void mouseEntered(MouseEvent evt) {
Object src = evt.getSource();
if (src instanceof AbstractButton) {
AbstractButton button = (AbstractButton) evt.getSource();
if (button.isEnabled()) {
button.setContentAreaFilled(true);
button.setBorderPainted(true);
}
}
}
/**
* Triggers the drawing of the border when the mouse entered the button area.
*
* @param e the mouse event.
*/
public void mouseEntered(final MouseEvent e) {
if (e.getSource() instanceof AbstractButton) {
final AbstractButton button = (AbstractButton) e.getSource();
if (button.isEnabled()) {
button.setBorderPainted(true);
}
}
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
if (c instanceof AbstractButton) {
AbstractButton b = (AbstractButton)c;
ButtonModel model = b.getModel();
boolean isPressed;
boolean isRollover;
boolean isEnabled;
isPressed = model.isPressed() && model.isArmed();
isRollover = b.isRolloverEnabled() && model.isRollover();
isEnabled = b.isEnabled();
if (!isEnabled) {
paintDisabled(b, g, x, y, width, height);
} else {
if (isPressed) {
paintPressed(b, g, x, y, width, height);
} else if (isRollover) {
paintRollover(b, g, x, y, width, height);
} else {
paintNormal(b, g, x, y, width, height);
}
}
}
}
private void refresh(final AbstractButton b) {
b.setBackground(UIUtils.getProfilerResultsBackground());
boolean hovered = Boolean.TRUE.equals(b.getClientProperty(PROP_HOVERED));
boolean filled = b.isEnabled() && (hovered || b.isSelected() || b.isFocusOwner());
b.setOpaque(filled);
b.setContentAreaFilled(filled);
b.repaint();
}
private void refresh(final AbstractButton b) {
b.setBackground(UISupport.getDefaultBackground());
boolean hovered = Boolean.TRUE.equals(b.getClientProperty(PROP_HOVERED));
boolean filled = b.isEnabled() && (hovered || b.isSelected() || b.isFocusOwner());
b.setOpaque(filled);
b.setContentAreaFilled(filled);
b.repaint();
}
/**
* When a double-click is observed a button is clicked.
*
* @param button
* when a double-click is observed this button's
* <code>doClick()</code> method is invoked if it is enabled.
*/
public DoubleClickListener(final AbstractButton button) {
this(new Runnable() {
public void run() {
if (button.isEnabled())
button.doClick();
}
});
if (button == null)
throw new NullPointerException();
}
/**
* Draws the given button border with the specified shape.
*
* @param b
* @param g
* @param shape
*/
public static void drawButtonBorder(AbstractButton b, Graphics g, Shape shape) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
boolean darkBorder = Boolean.parseBoolean(String.valueOf(b.getClientProperty(RapidLookTools.PROPERTY_BUTTON_DARK_BORDER)));
if (darkBorder) {
if (b.isEnabled()) {
if (b.hasFocus()) {
g2.setColor(Colors.BUTTON_BORDER_DARK_FOCUS);
} else {
g2.setColor(Colors.BUTTON_BORDER_DARK);
}
} else {
g2.setColor(Colors.BUTTON_BORDER_DARK_DISABLED);
}
} else {
if (b.isEnabled()) {
if (b.hasFocus()) {
g2.setColor(Colors.BUTTON_BORDER_FOCUS);
} else {
g2.setColor(Colors.BUTTON_BORDER);
}
} else {
g2.setColor(Colors.BUTTON_BORDER_DISABLED);
}
}
g2.draw(shape);
}
@Override
protected void paintText(Graphics graphics, AbstractButton button,
Rectangle textRect, String text) {
if (button.isEnabled()) {
super.paintText(graphics, button, textRect, text);
} else {
int shift = graphics.getFontMetrics().getAscent();
StyleUtil.paintDisabledText(style, graphics, text, textRect.x, textRect.y + shift);
}
}
@Override
protected void paintText(Graphics graphics, AbstractButton button,
Rectangle textRect, String text) {
if (button.isEnabled()) {
super.paintText(graphics, button, textRect, text);
} else {
int shift = graphics.getFontMetrics().getAscent();
StyleUtil.paintDisabledText(style, graphics, text, textRect.x, textRect.y + shift);
}
}
/**
* Returns the Icon used in calculating the pref/min/max size.
*
* @param b DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
protected Icon getSizingIcon(AbstractButton b) {
// NOTE: this is slightly different than BasicButtonUI, where it
// would just use getIcon, but this should be ok.
Icon icon = (b.isEnabled() || b.getDisabledIcon() == null) ? b.getIcon() : b.getDisabledIcon();
if (icon == null) {
icon = getDefaultIcon(b);
}
return icon;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
if (c instanceof AbstractButton) {
AbstractButton b = (AbstractButton)c;
ButtonModel model = b.getModel();
boolean isPressed;
boolean isRollover;
boolean isEnabled;
isPressed = model.isPressed() && model.isArmed();
isRollover = b.isRolloverEnabled() && model.isRollover();
isEnabled = b.isEnabled();
if (!isEnabled) {
paintDisabled(b, g, x, y, width, height);
} else {
if (isPressed) {
paintPressed(b, g, x, y, width, height);
} else if (isRollover) {
paintRollover(b, g, x, y, width, height);
} else {
paintNormal(b, g, x, y, width, height);
}
}
}
}
private void setSelectedState(AbstractButton b) {
if (b.isEnabled()) {
b.setBorderPainted(true);
b.setBackground(SELECTED_BACKGROUND_COLOR);
} else {
b.setBorderPainted(false);
b.setBackground(getDefaultBackground().darker());
}
}
/**
* Draws the given button with the specified shape.
*
* @param b
* @param g
* @param shape
*/
public static void drawButton(AbstractButton b, Graphics g, Shape shape) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int h = (int) shape.getBounds().getHeight();
ColorUIResource colorGradientStart;
ColorUIResource colorGradientEnd;
boolean highlighted = Boolean.parseBoolean(String.valueOf(b.getClientProperty(PROPERTY_BUTTON_HIGHLIGHT)));
boolean highlightedDark = Boolean.parseBoolean(String.valueOf(b.getClientProperty(PROPERTY_BUTTON_HIGHLIGHT_DARK)));
boolean highlightedWhite = Boolean.parseBoolean(String.valueOf(b.getClientProperty(PROPERTY_BUTTON_HIGHLIGHT_WHITE)));
if (highlighted) {
if (b.isEnabled()) {
if (b.getModel().isPressed() || b.getModel().isSelected()) {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_PRESSED_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_PRESSED_GRADIENT_END;
} else if (b.getModel().isRollover()) {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_ROLLOVER_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_ROLLOVER_GRADIENT_END;
} else {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_GRADIENT_END;
}
} else {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_DISABLED_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_DISABLED_GRADIENT_END;
}
} else if (highlightedDark) {
if (b.isEnabled()) {
if (b.getModel().isPressed() || b.getModel().isSelected()) {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_DARK_PRESSED_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_DARK_PRESSED_GRADIENT_END;
} else if (b.getModel().isRollover()) {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_DARK_ROLLOVER_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_DARK_ROLLOVER_GRADIENT_END;
} else {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_DARK_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_DARK_GRADIENT_END;
}
} else {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_DARK_DISABLED_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_DARK_DISABLED_GRADIENT_END;
}
} else if (highlightedWhite) {
if (b.isEnabled()) {
if (b.getModel().isPressed() || b.getModel().isSelected()) {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_WHITE_PRESSED_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_WHITE_PRESSED_GRADIENT_END;
} else if (b.getModel().isRollover()) {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_WHITE_ROLLOVER_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_WHITE_ROLLOVER_GRADIENT_END;
} else {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_WHITE_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_WHITE_GRADIENT_END;
}
} else {
colorGradientStart = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_WHITE_DISABLED_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_HIGHLIGHTED_WHITE_DISABLED_GRADIENT_END;
}
} else {
if (b.isEnabled()) {
if (b.getModel().isPressed() || b.getModel().isSelected()) {
colorGradientStart = Colors.BUTTON_BACKGROUND_PRESSED_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_PRESSED_GRADIENT_END;
} else if (b.getModel().isRollover()) {
colorGradientStart = Colors.BUTTON_BACKGROUND_ROLLOVER_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_ROLLOVER_GRADIENT_END;
} else {
colorGradientStart = Colors.BUTTON_BACKGROUND_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_GRADIENT_END;
}
} else {
colorGradientStart = Colors.BUTTON_BACKGROUND_DISABLED_GRADIENT_START;
colorGradientEnd = Colors.BUTTON_BACKGROUND_DISABLED_GRADIENT_END;
}
}
Paint gp = new GradientPaint(0, 0, colorGradientStart, 0, h, colorGradientEnd);
g2.setPaint(gp);
g2.fill(shape);
}
private void setRolloverStateState(AbstractButton b) {
if (b.isEnabled()) {
b.setBorderPainted(true);
b.setBackground(ROLLOVER_BACKGROUND_COLOR);
}
}