access UIManager only from installDefaults() (for performance and to be GUI builder friendly)

This commit is contained in:
Karl Tauber
2019-08-27 12:32:54 +02:00
parent e1eb51e04d
commit 0d382a0d25
7 changed files with 151 additions and 63 deletions

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.FontMetrics; import java.awt.FontMetrics;
@@ -33,11 +34,26 @@ import sun.swing.SwingUtilities2;
/** /**
* Provides the Flat LaF UI delegate for {@link javax.swing.JButton}. * Provides the Flat LaF UI delegate for {@link javax.swing.JButton}.
* *
* TODO document used UI defaults of superclass
*
* @uiDefault Component.focusWidth int
* @uiDefault Button.arc int
* @uiDefault Button.disabledText Color
* @uiDefault Button.default.background Color
* @uiDefault Button.default.foreground Color
*
* @author Karl Tauber * @author Karl Tauber
*/ */
public class FlatButtonUI public class FlatButtonUI
extends BasicButtonUI extends BasicButtonUI
{ {
protected int focusWidth;
protected int arc;
protected Color disabledText;
protected Color defaultBackground;
protected Color defaultForeground;
private static ComponentUI instance; private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
@@ -46,6 +62,18 @@ public class FlatButtonUI
return instance; return instance;
} }
@Override
protected void installDefaults( AbstractButton b ) {
super.installDefaults( b );
focusWidth = UIManager.getInt( "Component.focusWidth" );
arc = UIManager.getInt( "Button.arc" );
disabledText = UIManager.getColor( "Button.disabledText" );
defaultBackground = UIManager.getColor( "Button.default.background" );
defaultForeground = UIManager.getColor( "Button.default.foreground" );
}
static boolean isContentAreaFilled( Component c ) { static boolean isContentAreaFilled( Component c ) {
return !(c instanceof AbstractButton) || ((AbstractButton)c).isContentAreaFilled(); return !(c instanceof AbstractButton) || ((AbstractButton)c).isContentAreaFilled();
} }
@@ -64,8 +92,8 @@ public class FlatButtonUI
try { try {
FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.setRenderingHints( g2 );
float focusWidth = FlatUIUtils.getFocusWidth( c ); float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
float arc = FlatUIUtils.getButtonArc( c ); float arc = (c.getBorder() instanceof FlatButtonBorder) ? scale( (float) this.arc ) : 0;
g2.setColor( getBackground( c ) ); g2.setColor( getBackground( c ) );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc );
@@ -84,7 +112,7 @@ public class FlatButtonUI
FontMetrics fm = SwingUtilities2.getFontMetrics( c, g ); FontMetrics fm = SwingUtilities2.getFontMetrics( c, g );
int mnemonicIndex = b.getDisplayedMnemonicIndex(); int mnemonicIndex = b.getDisplayedMnemonicIndex();
g.setColor( b.getModel().isEnabled() ? getForeground( c ) : UIManager.getColor( "Button.disabledText" ) ); g.setColor( b.getModel().isEnabled() ? getForeground( c ) : disabledText );
SwingUtilities2.drawStringUnderlineCharAt( c, g, text, mnemonicIndex, SwingUtilities2.drawStringUnderlineCharAt( c, g, text, mnemonicIndex,
textRect.x + getTextShiftOffset(), textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset() ); textRect.y + fm.getAscent() + getTextShiftOffset() );
@@ -92,11 +120,11 @@ public class FlatButtonUI
private Color getBackground( Component c ) { private Color getBackground( Component c ) {
boolean def = FlatButtonUI.isDefaultButton( c ); boolean def = FlatButtonUI.isDefaultButton( c );
return def ? UIManager.getColor( "Button.default.background" ) : c.getBackground(); return def ? defaultBackground : c.getBackground();
} }
private Color getForeground( Component c ) { private Color getForeground( Component c ) {
boolean def = FlatButtonUI.isDefaultButton( c ); boolean def = FlatButtonUI.isDefaultButton( c );
return def ? UIManager.getColor( "Button.default.foreground" ) : c.getForeground(); return def ? defaultForeground : c.getForeground();
} }
} }

View File

@@ -43,11 +43,37 @@ import com.formdev.flatlaf.util.UIScale;
/** /**
* Provides the Flat LaF UI delegate for {@link javax.swing.JComboBox}. * Provides the Flat LaF UI delegate for {@link javax.swing.JComboBox}.
* *
* TODO document used UI defaults of superclass
*
* @uiDefault Component.focusWidth int
* @uiDefault Component.arc int
* @uiDefault Component.borderColor Color
* @uiDefault Component.disabledBorderColor Color
* @uiDefault ComboBox.disabledBackground Color
* @uiDefault ComboBox.disabledForeground Color
* @uiDefault ComboBox.buttonBackground Color
* @uiDefault ComboBox.buttonEditableBackground Color
* @uiDefault ComboBox.buttonArrowColor Color
* @uiDefault ComboBox.buttonDisabledArrowColor Color
*
* @author Karl Tauber * @author Karl Tauber
*/ */
public class FlatComboBoxUI public class FlatComboBoxUI
extends BasicComboBoxUI extends BasicComboBoxUI
{ {
protected int focusWidth;
protected int arc;
protected Color borderColor;
protected Color disabledBorderColor;
protected Color disabledBackground;
protected Color disabledForeground;
protected Color buttonBackground;
protected Color buttonEditableBackground;
protected Color buttonArrowColor;
protected Color buttonDisabledArrowColor;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatComboBoxUI(); return new FlatComboBoxUI();
} }
@@ -57,6 +83,19 @@ public class FlatComboBoxUI
super.installDefaults(); super.installDefaults();
padding = UIScale.scale( padding ); padding = UIScale.scale( padding );
focusWidth = UIManager.getInt( "Component.focusWidth" );
arc = UIManager.getInt( "Component.arc" );
borderColor = UIManager.getColor( "Component.borderColor" );
disabledBorderColor = UIManager.getColor( "Component.disabledBorderColor" );
disabledBackground = UIManager.getColor( "ComboBox.disabledBackground" );
disabledForeground = UIManager.getColor( "ComboBox.disabledForeground" );
buttonBackground = UIManager.getColor( "ComboBox.buttonBackground" );
buttonEditableBackground = UIManager.getColor( "ComboBox.buttonEditableBackground" );
buttonArrowColor = UIManager.getColor( "ComboBox.buttonArrowColor" );
buttonDisabledArrowColor = UIManager.getColor( "ComboBox.buttonDisabledArrowColor" );
} }
@Override @Override
@@ -132,14 +171,12 @@ public class FlatComboBoxUI
private void updateEditorColors() { private void updateEditorColors() {
boolean enabled = editor.isEnabled(); boolean enabled = editor.isEnabled();
editor.setBackground( enabled editor.setBackground( enabled ? comboBox.getBackground() : disabledBackground );
? comboBox.getBackground()
: UIManager.getColor( "ComboBox.disabledBackground" ) );
editor.setForeground( (enabled || editor instanceof JTextComponent) editor.setForeground( (enabled || editor instanceof JTextComponent)
? comboBox.getForeground() ? comboBox.getForeground()
: UIManager.getColor( "ComboBox.disabledForeground" ) ); : disabledForeground );
if( editor instanceof JTextComponent ) if( editor instanceof JTextComponent )
((JTextComponent)editor).setDisabledTextColor( UIManager.getColor( "ComboBox.disabledForeground" ) ); ((JTextComponent)editor).setDisabledTextColor( disabledForeground );
} }
@Override @Override
@@ -157,24 +194,20 @@ public class FlatComboBoxUI
int width = c.getWidth(); int width = c.getWidth();
int height = c.getHeight(); int height = c.getHeight();
float focusWidth = FlatUIUtils.getFocusWidth( c ); float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
float arc = FlatUIUtils.getComponentArc( c ); float arc = (c.getBorder() instanceof FlatRoundBorder) ? scale( (float) this.arc ) : 0;
int arrowX = arrowButton.getX(); int arrowX = arrowButton.getX();
int arrowWidth = arrowButton.getWidth(); int arrowWidth = arrowButton.getWidth();
boolean isLeftToRight = comboBox.getComponentOrientation().isLeftToRight(); boolean isLeftToRight = comboBox.getComponentOrientation().isLeftToRight();
// paint background // paint background
g2.setColor( comboBox.isEnabled() g2.setColor( comboBox.isEnabled() ? c.getBackground() : disabledBackground );
? c.getBackground()
: UIManager.getColor( "ComboBox.disabledBackground" ) );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc );
// paint arrow button background // paint arrow button background
g2.setColor( UIManager.getColor( comboBox.isEnabled() g2.setColor( comboBox.isEnabled()
? (comboBox.isEditable() ? (comboBox.isEditable() ? buttonEditableBackground : buttonBackground)
? "ComboBox.buttonEditableBackground" : disabledBackground );
: "ComboBox.buttonBackground" )
: "ComboBox.disabledBackground" ) );
Shape oldClip = g2.getClip(); Shape oldClip = g2.getClip();
if( isLeftToRight ) if( isLeftToRight )
g2.clipRect( arrowX, 0, width - arrowX, height ); g2.clipRect( arrowX, 0, width - arrowX, height );
@@ -185,7 +218,7 @@ public class FlatComboBoxUI
if( comboBox.isEditable() ) { if( comboBox.isEditable() ) {
// paint vertical line between value and arrow button // paint vertical line between value and arrow button
g2.setColor( FlatUIUtils.getBorderColor( comboBox.isEnabled(), false ) ); g2.setColor( comboBox.isEnabled() ? borderColor : disabledBorderColor );
float lw = scale( 1f ); float lw = scale( 1f );
float lx = isLeftToRight ? arrowX : arrowX + arrowWidth - lw; float lx = isLeftToRight ? arrowX : arrowX + arrowWidth - lw;
g2.fill( new Rectangle2D.Float( lx, focusWidth, lw, height - (focusWidth * 2) ) ); g2.fill( new Rectangle2D.Float( lx, focusWidth, lw, height - (focusWidth * 2) ) );
@@ -203,7 +236,7 @@ public class FlatComboBoxUI
//---- class FlatArrowButton ---------------------------------------------- //---- class FlatArrowButton ----------------------------------------------
private static class FlatArrowButton private class FlatArrowButton
extends BasicArrowButton extends BasicArrowButton
{ {
FlatArrowButton() { FlatArrowButton() {
@@ -228,9 +261,7 @@ public class FlatComboBoxUI
arrow.lineTo( x + (w / 2f), y + h ); arrow.lineTo( x + (w / 2f), y + h );
arrow.closePath(); arrow.closePath();
g.setColor( UIManager.getColor( isEnabled() g.setColor( isEnabled() ? buttonArrowColor : buttonDisabledArrowColor );
? "ComboBox.buttonArrowColor"
: "ComboBox.buttonDisabledArrowColor" ) );
((Graphics2D)g).fill( arrow ); ((Graphics2D)g).fill( arrow );
} }
} }

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JLabel; import javax.swing.JLabel;
@@ -27,11 +28,18 @@ import sun.swing.SwingUtilities2;
/** /**
* Provides the Flat LaF UI delegate for {@link javax.swing.JLabel}. * Provides the Flat LaF UI delegate for {@link javax.swing.JLabel}.
* *
* @uiDefault Label.background Color only used if opaque
* @uiDefault Label.foreground Color
* @uiDefault Label.disabledForeground Color
* @uiDefault Label.font Font
*
* @author Karl Tauber * @author Karl Tauber
*/ */
public class FlatLabelUI public class FlatLabelUI
extends BasicLabelUI extends BasicLabelUI
{ {
private Color disabledForeground;
private static ComponentUI instance; private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
@@ -40,10 +48,17 @@ public class FlatLabelUI
return instance; return instance;
} }
@Override
protected void installDefaults( JLabel c ) {
super.installDefaults( c );
disabledForeground = UIManager.getColor( "Label.disabledForeground" );
}
@Override @Override
protected void paintDisabledText( JLabel l, Graphics g, String s, int textX, int textY ) { protected void paintDisabledText( JLabel l, Graphics g, String s, int textX, int textY ) {
int mnemIndex = l.getDisplayedMnemonicIndex(); int mnemIndex = l.getDisplayedMnemonicIndex();
g.setColor( UIManager.getColor( "Label.disabledForeground" ) ); g.setColor( disabledForeground );
SwingUtilities2.drawStringUnderlineCharAt( l, g, s, mnemIndex, textX, textY ); SwingUtilities2.drawStringUnderlineCharAt( l, g, s, mnemIndex, textX, textY );
} }
} }

View File

@@ -16,11 +16,13 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.event.FocusEvent; import java.awt.event.FocusEvent;
import java.awt.event.FocusListener; import java.awt.event.FocusListener;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicPasswordFieldUI; import javax.swing.plaf.basic.BasicPasswordFieldUI;
import javax.swing.text.JTextComponent; import javax.swing.text.JTextComponent;
@@ -28,17 +30,30 @@ import javax.swing.text.JTextComponent;
/** /**
* Provides the Flat LaF UI delegate for {@link javax.swing.JPasswordField}. * Provides the Flat LaF UI delegate for {@link javax.swing.JPasswordField}.
* *
* TODO document used UI defaults of superclass
*
* @uiDefault Component.focusWidth int
*
* @author Karl Tauber * @author Karl Tauber
*/ */
public class FlatPasswordFieldUI public class FlatPasswordFieldUI
extends BasicPasswordFieldUI extends BasicPasswordFieldUI
{ {
protected int focusWidth;
private final Handler handler = new Handler(); private final Handler handler = new Handler();
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatPasswordFieldUI(); return new FlatPasswordFieldUI();
} }
@Override
protected void installDefaults() {
super.installDefaults();
focusWidth = UIManager.getInt( "Component.focusWidth" );
}
@Override @Override
protected void installListeners() { protected void installListeners() {
super.installListeners(); super.installListeners();
@@ -63,7 +78,7 @@ public class FlatPasswordFieldUI
try { try {
FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.setRenderingHints( g2 );
float focusWidth = FlatUIUtils.getFocusWidth( c ); float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
g2.setColor( c.getBackground() ); g2.setColor( c.getBackground() );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 );

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.UIManager; import javax.swing.UIManager;
@@ -26,24 +27,38 @@ import javax.swing.text.JTextComponent;
/** /**
* Provides the Flat LaF UI delegate for {@link javax.swing.JTextArea}. * Provides the Flat LaF UI delegate for {@link javax.swing.JTextArea}.
* *
* TODO document used UI defaults of superclass
*
* @uiDefault ComboBox.disabledBackground Color
* @uiDefault ComboBox.inactiveBackground Color
*
* @author Karl Tauber * @author Karl Tauber
*/ */
public class FlatTextAreaUI public class FlatTextAreaUI
extends BasicTextAreaUI extends BasicTextAreaUI
{ {
protected Color disabledBackground;
protected Color inactiveBackground;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatTextAreaUI(); return new FlatTextAreaUI();
} }
@Override
protected void installDefaults() {
super.installDefaults();
disabledBackground = UIManager.getColor( "TextArea.disabledBackground" );
inactiveBackground = UIManager.getColor( "TextArea.inactiveBackground" );
}
@Override @Override
protected void paintBackground( Graphics g ) { protected void paintBackground( Graphics g ) {
JTextComponent c = getComponent(); JTextComponent c = getComponent();
g.setColor( !c.isEnabled() g.setColor( !c.isEnabled()
? UIManager.getColor( "TextArea.disabledBackground" ) ? disabledBackground
: (!c.isEditable() : (!c.isEditable() ? inactiveBackground : c.getBackground()) );
? UIManager.getColor( "TextArea.inactiveBackground" )
: c.getBackground() ) );
g.fillRect( 0, 0, c.getWidth(), c.getHeight() ); g.fillRect( 0, 0, c.getWidth(), c.getHeight() );
} }
} }

View File

@@ -16,11 +16,13 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.event.FocusEvent; import java.awt.event.FocusEvent;
import java.awt.event.FocusListener; import java.awt.event.FocusListener;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTextFieldUI; import javax.swing.plaf.basic.BasicTextFieldUI;
import javax.swing.text.JTextComponent; import javax.swing.text.JTextComponent;
@@ -28,17 +30,30 @@ import javax.swing.text.JTextComponent;
/** /**
* Provides the Flat LaF UI delegate for {@link javax.swing.JTextField}. * Provides the Flat LaF UI delegate for {@link javax.swing.JTextField}.
* *
* TODO document used UI defaults of superclass
*
* @uiDefault Component.focusWidth int
*
* @author Karl Tauber * @author Karl Tauber
*/ */
public class FlatTextFieldUI public class FlatTextFieldUI
extends BasicTextFieldUI extends BasicTextFieldUI
{ {
protected int focusWidth;
private final Handler handler = new Handler(); private final Handler handler = new Handler();
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatTextFieldUI(); return new FlatTextFieldUI();
} }
@Override
protected void installDefaults() {
super.installDefaults();
focusWidth = UIManager.getInt( "Component.focusWidth" );
}
@Override @Override
protected void installListeners() { protected void installListeners() {
super.installListeners(); super.installListeners();
@@ -63,7 +78,7 @@ public class FlatTextFieldUI
try { try {
FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.setRenderingHints( g2 );
float focusWidth = FlatUIUtils.getFocusWidth( c ); float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
g2.setColor( c.getBackground() ); g2.setColor( c.getBackground() );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 );

View File

@@ -16,7 +16,6 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color; import java.awt.Color;
import java.awt.Container; import java.awt.Container;
import java.awt.Graphics; import java.awt.Graphics;
@@ -57,36 +56,6 @@ public class FlatUIUtils
return (value instanceof Integer) ? (Integer) value : defaultValue; return (value instanceof Integer) ? (Integer) value : defaultValue;
} }
public static float getFocusWidth() {
return scale( (float) getUIInt( "Component.focusWidth", 2 ) );
}
public static float getComponentArc() {
return scale( (float) getUIInt( "Component.arc", 5 ) );
}
public static float getButtonArc() {
return scale( (float) getUIInt( "Button.arc", 6 ) );
}
public static float getFocusWidth( JComponent c ) {
return (c.getBorder() instanceof FlatBorder) ? getFocusWidth() : 0;
}
public static float getComponentArc( JComponent c ) {
return (c.getBorder() instanceof FlatBorder) ? getComponentArc() : 0;
}
public static float getButtonArc( JComponent c ) {
return (c.getBorder() instanceof FlatBorder) ? getButtonArc() : 0;
}
public static Color getBorderColor( boolean enabled, boolean focused ) {
return UIManager.getColor( enabled
? (focused ? "Component.focusedBorderColor" : "Component.borderColor")
: "Component.disabledBorderColor" );
}
/** /**
* Sets rendering hints used for painting. * Sets rendering hints used for painting.
*/ */