FlatButtonBorder now extends FlatBorder

This commit is contained in:
Karl Tauber
2019-08-21 21:40:26 +02:00
parent 0b02236c6b
commit 7250cb5522
3 changed files with 30 additions and 51 deletions

View File

@@ -17,6 +17,7 @@
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
@@ -44,20 +45,25 @@ public class FlatBorder
float focusWidth = getFocusWidth();
float lineWidth = getLineWidth();
float arc = getArc();
if( isFocused( c ) ) {
g2.setColor( UIManager.getColor( "Component.focusColor" ) );
FlatUIUtils.paintOutlineBorder( g2, x, y, width, height, focusWidth, lineWidth, 0 );
g2.setColor( getFocusColor( c ) );
FlatUIUtils.paintOutlineBorder( g2, x, y, width, height, focusWidth, lineWidth, arc );
}
g2.setPaint( getBorderColor( c ) );
FlatUIUtils.drawRoundRectangle( g2, x, y, width, height, focusWidth, lineWidth, 0 );
FlatUIUtils.drawRoundRectangle( g2, x, y, width, height, focusWidth, lineWidth, arc );
} finally {
g2.dispose();
}
}
private Paint getBorderColor( Component c ) {
protected Color getFocusColor( Component c ) {
return UIManager.getColor( "Component.focusColor" );
}
protected Paint getBorderColor( Component c ) {
boolean editable = !(c instanceof JTextComponent) || ((JTextComponent)c).isEditable();
return UIManager.getColor( c.isEnabled() && editable
? (isFocused( c )
@@ -66,7 +72,7 @@ public class FlatBorder
: "Component.disabledBorderColor" );
}
private boolean isFocused( Component c ) {
protected boolean isFocused( Component c ) {
if( c instanceof JScrollPane ) {
JViewport viewport = ((JScrollPane)c).getViewport();
Component view = (viewport != null) ? viewport.getView() : null;
@@ -94,4 +100,8 @@ public class FlatBorder
protected float getLineWidth() {
return FlatUIUtils.getLineWidth();
}
protected float getArc() {
return 0;
}
}

View File

@@ -16,16 +16,12 @@
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicBorders;
/**
* Border for {@link javax.swing.JButton}.
@@ -33,35 +29,23 @@ import javax.swing.plaf.basic.BasicBorders;
* @author Karl Tauber
*/
public class FlatButtonBorder
extends BasicBorders.MarginBorder
extends FlatBorder
{
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
if( !FlatButtonUI.isContentAreaFilled( c ) )
return;
Graphics2D g2 = (Graphics2D) g.create();
try {
FlatUIUtils.setRenderingHints( g2 );
float focusWidth = getFocusWidth();
float lineWidth = getLineWidth();
float arc = FlatUIUtils.getButtonArc();
if( c.hasFocus() ) {
g2.setColor( UIManager.getColor( FlatButtonUI.isDefaultButton( c )
? "Button.default.focusColor" : "Component.focusColor" ) );
FlatUIUtils.paintOutlineBorder( g2, x, y, width, height, focusWidth, lineWidth, arc );
if( FlatButtonUI.isContentAreaFilled( c ) )
super.paintBorder( c, g, x, y, width, height );
}
g2.setPaint( getBorderColor( c ) );
FlatUIUtils.drawRoundRectangle( g2, x, y, width, height, focusWidth, lineWidth, arc );
} finally {
g2.dispose();
}
@Override
protected Color getFocusColor( Component c ) {
return UIManager.getColor( FlatButtonUI.isDefaultButton( c )
? "Button.default.focusColor"
: "Component.focusColor" );
}
private Paint getBorderColor( Component c ) {
@Override
protected Paint getBorderColor( Component c ) {
if( c.isEnabled() ) {
boolean def = FlatButtonUI.isDefaultButton( c );
if( c.hasFocus() )
@@ -78,22 +62,7 @@ public class FlatButtonBorder
}
@Override
public Insets getBorderInsets( Component c, Insets insets ) {
float ow = getFocusWidth() + getLineWidth();
insets = super.getBorderInsets( c, insets );
insets.top = Math.round( scale( (float) insets.top ) + ow );
insets.left = Math.round( scale( (float) insets.left ) + ow );
insets.bottom = Math.round( scale( (float) insets.bottom ) + ow );
insets.right = Math.round( scale( (float) insets.right ) + ow );
return insets;
}
protected float getFocusWidth() {
return FlatUIUtils.getFocusWidth();
}
protected float getLineWidth() {
return FlatUIUtils.getLineWidth();
protected float getArc() {
return FlatUIUtils.getButtonArc();
}
}

View File

@@ -47,7 +47,7 @@ public class FlatButtonUI
}
static boolean isContentAreaFilled( Component c ) {
return c instanceof AbstractButton && ((AbstractButton)c).isContentAreaFilled();
return !(c instanceof AbstractButton) || ((AbstractButton)c).isContentAreaFilled();
}
static boolean isDefaultButton( Component c ) {