mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-10 22:17:13 -06:00
access UIManager only from installDefaults() (for performance and to be GUI builder friendly)
This commit is contained in:
@@ -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.FontMetrics;
|
||||
@@ -33,11 +34,26 @@ import sun.swing.SwingUtilities2;
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public class FlatButtonUI
|
||||
extends BasicButtonUI
|
||||
{
|
||||
protected int focusWidth;
|
||||
protected int arc;
|
||||
|
||||
protected Color disabledText;
|
||||
protected Color defaultBackground;
|
||||
protected Color defaultForeground;
|
||||
|
||||
private static ComponentUI instance;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
@@ -46,6 +62,18 @@ public class FlatButtonUI
|
||||
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 ) {
|
||||
return !(c instanceof AbstractButton) || ((AbstractButton)c).isContentAreaFilled();
|
||||
}
|
||||
@@ -64,8 +92,8 @@ public class FlatButtonUI
|
||||
try {
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
float focusWidth = FlatUIUtils.getFocusWidth( c );
|
||||
float arc = FlatUIUtils.getButtonArc( c );
|
||||
float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
|
||||
float arc = (c.getBorder() instanceof FlatButtonBorder) ? scale( (float) this.arc ) : 0;
|
||||
|
||||
g2.setColor( getBackground( c ) );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc );
|
||||
@@ -84,7 +112,7 @@ public class FlatButtonUI
|
||||
FontMetrics fm = SwingUtilities2.getFontMetrics( c, g );
|
||||
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,
|
||||
textRect.x + getTextShiftOffset(),
|
||||
textRect.y + fm.getAscent() + getTextShiftOffset() );
|
||||
@@ -92,11 +120,11 @@ public class FlatButtonUI
|
||||
|
||||
private Color getBackground( Component 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 ) {
|
||||
boolean def = FlatButtonUI.isDefaultButton( c );
|
||||
return def ? UIManager.getColor( "Button.default.foreground" ) : c.getForeground();
|
||||
return def ? defaultForeground : c.getForeground();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,11 +43,37 @@ import com.formdev.flatlaf.util.UIScale;
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public class FlatComboBoxUI
|
||||
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 ) {
|
||||
return new FlatComboBoxUI();
|
||||
}
|
||||
@@ -57,6 +83,19 @@ public class FlatComboBoxUI
|
||||
super.installDefaults();
|
||||
|
||||
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
|
||||
@@ -132,14 +171,12 @@ public class FlatComboBoxUI
|
||||
|
||||
private void updateEditorColors() {
|
||||
boolean enabled = editor.isEnabled();
|
||||
editor.setBackground( enabled
|
||||
? comboBox.getBackground()
|
||||
: UIManager.getColor( "ComboBox.disabledBackground" ) );
|
||||
editor.setBackground( enabled ? comboBox.getBackground() : disabledBackground );
|
||||
editor.setForeground( (enabled || editor instanceof JTextComponent)
|
||||
? comboBox.getForeground()
|
||||
: UIManager.getColor( "ComboBox.disabledForeground" ) );
|
||||
: disabledForeground );
|
||||
if( editor instanceof JTextComponent )
|
||||
((JTextComponent)editor).setDisabledTextColor( UIManager.getColor( "ComboBox.disabledForeground" ) );
|
||||
((JTextComponent)editor).setDisabledTextColor( disabledForeground );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -157,24 +194,20 @@ public class FlatComboBoxUI
|
||||
|
||||
int width = c.getWidth();
|
||||
int height = c.getHeight();
|
||||
float focusWidth = FlatUIUtils.getFocusWidth( c );
|
||||
float arc = FlatUIUtils.getComponentArc( c );
|
||||
float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
|
||||
float arc = (c.getBorder() instanceof FlatRoundBorder) ? scale( (float) this.arc ) : 0;
|
||||
int arrowX = arrowButton.getX();
|
||||
int arrowWidth = arrowButton.getWidth();
|
||||
boolean isLeftToRight = comboBox.getComponentOrientation().isLeftToRight();
|
||||
|
||||
// paint background
|
||||
g2.setColor( comboBox.isEnabled()
|
||||
? c.getBackground()
|
||||
: UIManager.getColor( "ComboBox.disabledBackground" ) );
|
||||
g2.setColor( comboBox.isEnabled() ? c.getBackground() : disabledBackground );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc );
|
||||
|
||||
// paint arrow button background
|
||||
g2.setColor( UIManager.getColor( comboBox.isEnabled()
|
||||
? (comboBox.isEditable()
|
||||
? "ComboBox.buttonEditableBackground"
|
||||
: "ComboBox.buttonBackground" )
|
||||
: "ComboBox.disabledBackground" ) );
|
||||
g2.setColor( comboBox.isEnabled()
|
||||
? (comboBox.isEditable() ? buttonEditableBackground : buttonBackground)
|
||||
: disabledBackground );
|
||||
Shape oldClip = g2.getClip();
|
||||
if( isLeftToRight )
|
||||
g2.clipRect( arrowX, 0, width - arrowX, height );
|
||||
@@ -185,7 +218,7 @@ public class FlatComboBoxUI
|
||||
|
||||
if( comboBox.isEditable() ) {
|
||||
// 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 lx = isLeftToRight ? arrowX : arrowX + arrowWidth - lw;
|
||||
g2.fill( new Rectangle2D.Float( lx, focusWidth, lw, height - (focusWidth * 2) ) );
|
||||
@@ -203,7 +236,7 @@ public class FlatComboBoxUI
|
||||
|
||||
//---- class FlatArrowButton ----------------------------------------------
|
||||
|
||||
private static class FlatArrowButton
|
||||
private class FlatArrowButton
|
||||
extends BasicArrowButton
|
||||
{
|
||||
FlatArrowButton() {
|
||||
@@ -228,9 +261,7 @@ public class FlatComboBoxUI
|
||||
arrow.lineTo( x + (w / 2f), y + h );
|
||||
arrow.closePath();
|
||||
|
||||
g.setColor( UIManager.getColor( isEnabled()
|
||||
? "ComboBox.buttonArrowColor"
|
||||
: "ComboBox.buttonDisabledArrowColor" ) );
|
||||
g.setColor( isEnabled() ? buttonArrowColor : buttonDisabledArrowColor );
|
||||
((Graphics2D)g).fill( arrow );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.formdev.flatlaf.ui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JLabel;
|
||||
@@ -27,11 +28,18 @@ import sun.swing.SwingUtilities2;
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public class FlatLabelUI
|
||||
extends BasicLabelUI
|
||||
{
|
||||
private Color disabledForeground;
|
||||
|
||||
private static ComponentUI instance;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
@@ -40,10 +48,17 @@ public class FlatLabelUI
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installDefaults( JLabel c ) {
|
||||
super.installDefaults( c );
|
||||
|
||||
disabledForeground = UIManager.getColor( "Label.disabledForeground" );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintDisabledText( JLabel l, Graphics g, String s, int textX, int textY ) {
|
||||
int mnemIndex = l.getDisplayedMnemonicIndex();
|
||||
g.setColor( UIManager.getColor( "Label.disabledForeground" ) );
|
||||
g.setColor( disabledForeground );
|
||||
SwingUtilities2.drawStringUnderlineCharAt( l, g, s, mnemIndex, textX, textY );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,13 @@
|
||||
|
||||
package com.formdev.flatlaf.ui;
|
||||
|
||||
import static com.formdev.flatlaf.util.UIScale.scale;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicPasswordFieldUI;
|
||||
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}.
|
||||
*
|
||||
* TODO document used UI defaults of superclass
|
||||
*
|
||||
* @uiDefault Component.focusWidth int
|
||||
*
|
||||
* @author Karl Tauber
|
||||
*/
|
||||
public class FlatPasswordFieldUI
|
||||
extends BasicPasswordFieldUI
|
||||
{
|
||||
protected int focusWidth;
|
||||
|
||||
private final Handler handler = new Handler();
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatPasswordFieldUI();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installDefaults() {
|
||||
super.installDefaults();
|
||||
|
||||
focusWidth = UIManager.getInt( "Component.focusWidth" );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installListeners() {
|
||||
super.installListeners();
|
||||
@@ -63,7 +78,7 @@ public class FlatPasswordFieldUI
|
||||
try {
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
float focusWidth = FlatUIUtils.getFocusWidth( c );
|
||||
float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
|
||||
|
||||
g2.setColor( c.getBackground() );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 );
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.formdev.flatlaf.ui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.UIManager;
|
||||
@@ -26,24 +27,38 @@ import javax.swing.text.JTextComponent;
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public class FlatTextAreaUI
|
||||
extends BasicTextAreaUI
|
||||
{
|
||||
protected Color disabledBackground;
|
||||
protected Color inactiveBackground;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatTextAreaUI();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installDefaults() {
|
||||
super.installDefaults();
|
||||
|
||||
disabledBackground = UIManager.getColor( "TextArea.disabledBackground" );
|
||||
inactiveBackground = UIManager.getColor( "TextArea.inactiveBackground" );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintBackground( Graphics g ) {
|
||||
JTextComponent c = getComponent();
|
||||
|
||||
g.setColor( !c.isEnabled()
|
||||
? UIManager.getColor( "TextArea.disabledBackground" )
|
||||
: (!c.isEditable()
|
||||
? UIManager.getColor( "TextArea.inactiveBackground" )
|
||||
: c.getBackground() ) );
|
||||
? disabledBackground
|
||||
: (!c.isEditable() ? inactiveBackground : c.getBackground()) );
|
||||
g.fillRect( 0, 0, c.getWidth(), c.getHeight() );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,13 @@
|
||||
|
||||
package com.formdev.flatlaf.ui;
|
||||
|
||||
import static com.formdev.flatlaf.util.UIScale.scale;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicTextFieldUI;
|
||||
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}.
|
||||
*
|
||||
* TODO document used UI defaults of superclass
|
||||
*
|
||||
* @uiDefault Component.focusWidth int
|
||||
*
|
||||
* @author Karl Tauber
|
||||
*/
|
||||
public class FlatTextFieldUI
|
||||
extends BasicTextFieldUI
|
||||
{
|
||||
protected int focusWidth;
|
||||
|
||||
private final Handler handler = new Handler();
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatTextFieldUI();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installDefaults() {
|
||||
super.installDefaults();
|
||||
|
||||
focusWidth = UIManager.getInt( "Component.focusWidth" );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installListeners() {
|
||||
super.installListeners();
|
||||
@@ -63,7 +78,7 @@ public class FlatTextFieldUI
|
||||
try {
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
float focusWidth = FlatUIUtils.getFocusWidth( c );
|
||||
float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
|
||||
|
||||
g2.setColor( c.getBackground() );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 );
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package com.formdev.flatlaf.ui;
|
||||
|
||||
import static com.formdev.flatlaf.util.UIScale.scale;
|
||||
import java.awt.Color;
|
||||
import java.awt.Container;
|
||||
import java.awt.Graphics;
|
||||
@@ -57,36 +56,6 @@ public class FlatUIUtils
|
||||
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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user