FlatBorder: access UIManager only on construction (for performance and to be GUI builder friendly)

This commit is contained in:
Karl Tauber
2019-08-27 11:11:03 +02:00
parent 34bb502880
commit e1eb51e04d
4 changed files with 60 additions and 17 deletions

View File

@@ -33,11 +33,30 @@ import javax.swing.text.JTextComponent;
/**
* Border for various components (e.g. {@link javax.swing.JTextField}).
*
* There is empty space around the component border, if Component.focusWidth > 0,
* which is used to paint focus border.
*
* Because there is empty space (if focus border is not painted),
* UI delegates that use this border (or subclasses) must invoke
* {@link FlatUIUtils#paintParentBackground} to paint the empty space correctly.
*
* @uiDefault Component.focusWidth int
* @uiDefault Component.focusColor Color
* @uiDefault Component.borderColor Color
* @uiDefault Component.disabledBorderColor Color
* @uiDefault Component.focusedBorderColor Color
*
* @author Karl Tauber
*/
public class FlatBorder
extends BasicBorders.MarginBorder
{
protected final int focusWidth = UIManager.getInt( "Component.focusWidth" );
protected final Color focusColor = UIManager.getColor( "Component.focusColor" );
protected final Color borderColor = UIManager.getColor( "Component.borderColor" );
protected final Color disabledBorderColor = UIManager.getColor( "Component.disabledBorderColor" );
protected final Color focusedBorderColor = UIManager.getColor( "Component.focusedBorderColor" );
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
Graphics2D g2 = (Graphics2D) g.create();
@@ -61,12 +80,14 @@ public class FlatBorder
}
protected Color getFocusColor( Component c ) {
return UIManager.getColor( "Component.focusColor" );
return focusColor;
}
protected Paint getBorderColor( Component c ) {
boolean enabled = c.isEnabled() && (!(c instanceof JTextComponent) || ((JTextComponent)c).isEditable());
return FlatUIUtils.getBorderColor( enabled, isFocused( c ) );
return enabled
? (isFocused( c ) ? focusedBorderColor : borderColor)
: disabledBorderColor;
}
protected boolean isFocused( Component c ) {
@@ -94,11 +115,11 @@ public class FlatBorder
}
protected float getFocusWidth() {
return FlatUIUtils.getFocusWidth();
return scale( (float) focusWidth );
}
protected float getLineWidth() {
return FlatUIUtils.getLineWidth();
return scale( 1f );
}
protected float getArc() {

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
@@ -26,11 +27,31 @@ import javax.swing.UIManager;
/**
* Border for {@link javax.swing.JButton}.
*
* @uiDefault Button.startBorderColor Color
* @uiDefault Button.endBorderColor Color
* @uiDefault Button.disabledBorderColor Color
* @uiDefault Button.focusedBorderColor Color
* @uiDefault Button.default.startBorderColor Color
* @uiDefault Button.default.endBorderColor Color
* @uiDefault Button.default.focusedBorderColor Color
* @uiDefault Button.default.focusColor Color
* @uiDefault Button.arc int
*
* @author Karl Tauber
*/
public class FlatButtonBorder
extends FlatBorder
{
protected final Color startBorderColor = UIManager.getColor( "Button.startBorderColor" );
protected final Color endBorderColor = UIManager.getColor( "Button.endBorderColor" );
protected final Color disabledBorderColor = UIManager.getColor( "Button.disabledBorderColor" );
protected final Color focusedBorderColor = UIManager.getColor( "Button.focusedBorderColor" );
protected final Color defaultStartBorderColor = UIManager.getColor( "Button.default.startBorderColor" );
protected final Color defaultEndBorderColor = UIManager.getColor( "Button.default.endBorderColor" );
protected final Color defaultFocusedBorderColor = UIManager.getColor( "Button.default.focusedBorderColor" );
protected final Color defaultFocusColor = UIManager.getColor( "Button.default.focusColor" );
protected final int arc = UIManager.getInt( "Button.arc" );
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
if( FlatButtonUI.isContentAreaFilled( c ) )
@@ -39,9 +60,7 @@ public class FlatButtonBorder
@Override
protected Color getFocusColor( Component c ) {
return UIManager.getColor( FlatButtonUI.isDefaultButton( c )
? "Button.default.focusColor"
: "Component.focusColor" );
return FlatButtonUI.isDefaultButton( c ) ? defaultFocusColor : super.getFocusColor( c );
}
@Override
@@ -49,20 +68,20 @@ public class FlatButtonBorder
if( c.isEnabled() ) {
boolean def = FlatButtonUI.isDefaultButton( c );
if( c.hasFocus() )
return UIManager.getColor( def ? "Button.default.focusedBorderColor" : "Button.focusedBorderColor" );
return def ? defaultFocusedBorderColor : focusedBorderColor;
Color startColor = UIManager.getColor( def ? "Button.default.startBorderColor" : "Button.startBorderColor" );
Color endColor = UIManager.getColor( def ? "Button.default.endBorderColor" : "Button.endBorderColor" );
Color startColor = def ? defaultStartBorderColor : startBorderColor;
Color endColor = def ? defaultEndBorderColor : endBorderColor;
return (startColor.equals( endColor ) )
? startColor
: new GradientPaint( 0, getFocusWidth(), startColor,
0, c.getHeight() - getFocusWidth() - 1f, endColor );
} else
return UIManager.getColor( "Button.disabledBorderColor" );
return disabledBorderColor;
}
@Override
protected float getArc() {
return FlatUIUtils.getButtonArc();
return scale( (float) arc );
}
}

View File

@@ -16,16 +16,23 @@
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import javax.swing.UIManager;
/**
* Border for various components (e.g. {@link javax.swing.JComboBox}).
*
* @uiDefault Component.arc int
*
* @author Karl Tauber
*/
public class FlatRoundBorder
extends FlatBorder
{
protected final int arc = UIManager.getInt( "Component.arc" );
@Override
protected float getArc() {
return FlatUIUtils.getComponentArc();
return scale( (float) arc );
}
}

View File

@@ -61,10 +61,6 @@ public class FlatUIUtils
return scale( (float) getUIInt( "Component.focusWidth", 2 ) );
}
public static float getLineWidth() {
return scale( 1f );
}
public static float getComponentArc() {
return scale( (float) getUIInt( "Component.arc", 5 ) );
}