Styling: support Spinner

This commit is contained in:
Karl Tauber
2021-06-21 19:08:41 +02:00
parent b46233087b
commit 14b06507cb
3 changed files with 86 additions and 20 deletions

View File

@@ -351,14 +351,15 @@ class UIDefaultsLoader
valueType = ValueType.STRING;
else if( key.endsWith( "Color" ) ||
(key.endsWith( "ground" ) &&
(key.endsWith( ".background" ) || key.endsWith( "Background" ) ||
key.endsWith( ".foreground" ) || key.endsWith( "Foreground" ))) )
(key.endsWith( ".background" ) || key.endsWith( "Background" ) || key.equals( "background" ) ||
key.endsWith( ".foreground" ) || key.endsWith( "Foreground" ) || key.equals( "foreground" ))) )
valueType = ValueType.COLOR;
else if( key.endsWith( ".border" ) || key.endsWith( "Border" ) )
valueType = ValueType.BORDER;
else if( key.endsWith( ".icon" ) || key.endsWith( "Icon" ) )
valueType = ValueType.ICON;
else if( key.endsWith( ".margin" ) || key.endsWith( ".padding" ) ||
else if( key.endsWith( ".margin" ) || key.equals( "margin" ) ||
key.endsWith( ".padding" ) || key.equals( "padding" ) ||
key.endsWith( "Margins" ) || key.endsWith( "Insets" ) )
valueType = ValueType.INSETS;
else if( key.endsWith( "Size" ) )

View File

@@ -32,6 +32,7 @@ import java.awt.event.FocusListener;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.JSpinner;
import javax.swing.JTextField;
@@ -42,6 +43,7 @@ import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicSpinnerUI;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JSpinner}.
@@ -81,26 +83,35 @@ public class FlatSpinnerUI
{
private Handler handler;
protected int minimumWidth;
protected String buttonStyle;
protected String arrowType;
@Styleable protected int minimumWidth;
@Styleable protected String buttonStyle;
@Styleable protected String arrowType;
protected boolean isIntelliJTheme;
protected Color borderColor;
protected Color disabledBorderColor;
protected Color disabledBackground;
protected Color disabledForeground;
protected Color focusedBackground;
protected Color buttonBackground;
protected Color buttonArrowColor;
protected Color buttonDisabledArrowColor;
protected Color buttonHoverArrowColor;
protected Color buttonPressedArrowColor;
protected Insets padding;
@Styleable protected Color borderColor;
@Styleable protected Color disabledBorderColor;
@Styleable protected Color disabledBackground;
@Styleable protected Color disabledForeground;
@Styleable protected Color focusedBackground;
@Styleable protected Color buttonBackground;
@Styleable protected Color buttonArrowColor;
@Styleable protected Color buttonDisabledArrowColor;
@Styleable protected Color buttonHoverArrowColor;
@Styleable protected Color buttonPressedArrowColor;
@Styleable protected Insets padding;
private Map<String, Object> oldStyleValues;
public static ComponentUI createUI( JComponent c ) {
return new FlatSpinnerUI();
}
@Override
public void installUI( JComponent c ) {
super.installUI( c );
applyStyle( FlatStyleSupport.getStyle( spinner ) );
}
@Override
protected void installDefaults() {
super.installDefaults();
@@ -123,9 +134,6 @@ public class FlatSpinnerUI
buttonPressedArrowColor = UIManager.getColor( "Spinner.buttonPressedArrowColor" );
padding = UIManager.getInsets( "Spinner.padding" );
// scale
padding = scale( padding );
MigLayoutVisualPadding.install( spinner );
}
@@ -145,6 +153,8 @@ public class FlatSpinnerUI
buttonPressedArrowColor = null;
padding = null;
oldStyleValues = null;
MigLayoutVisualPadding.uninstall( spinner );
}
@@ -174,6 +184,21 @@ public class FlatSpinnerUI
return handler;
}
/**
* @since TODO
*/
protected void applyStyle( Object style ) {
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
updateArrowButtons();
}
/**
* @since TODO
*/
protected Object applyStyleProperty( String key, Object value ) {
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
}
@Override
protected JComponent createEditor() {
JComponent editor = super.createEditor();
@@ -284,6 +309,15 @@ public class FlatSpinnerUI
return button;
}
private void updateArrowButtons() {
for( Component c : spinner.getComponents() ) {
if( c instanceof FlatArrowButton ) {
((FlatArrowButton)c).update( arrowType, buttonArrowColor,
buttonDisabledArrowColor, buttonHoverArrowColor, null, buttonPressedArrowColor, null );
}
}
}
@Override
public void update( Graphics g, JComponent c ) {
float focusWidth = FlatUIUtils.getBorderFocusWidth( c );
@@ -370,6 +404,7 @@ public class FlatSpinnerUI
@Override
public Dimension preferredLayoutSize( Container parent ) {
Insets insets = parent.getInsets();
Insets padding = scale( FlatSpinnerUI.this.padding );
Dimension editorSize = (editor != null) ? editor.getPreferredSize() : new Dimension( 0, 0 );
// the arrows width is the same as the inner height so that the arrows area is square
@@ -390,6 +425,7 @@ public class FlatSpinnerUI
public void layoutContainer( Container parent ) {
Dimension size = parent.getSize();
Insets insets = parent.getInsets();
Insets padding = scale( FlatSpinnerUI.this.padding );
Rectangle r = FlatUIUtils.subtractInsets( new Rectangle( size ), insets );
if( nextButton == null && previousButton == null ) {
@@ -465,6 +501,12 @@ public class FlatSpinnerUI
case FlatClientProperties.MINIMUM_WIDTH:
spinner.revalidate();
break;
case FlatClientProperties.STYLE:
applyStyle( e.getNewValue() );
spinner.revalidate();
spinner.repaint();
break;
}
}
}

View File

@@ -389,6 +389,29 @@ public class FlatStylingTests
ui.applyStyle( "tickColor: #fff" );
}
@Test
void spinner() {
FlatSpinnerUI ui = new FlatSpinnerUI();
// create arrow buttons
ui.installUI( new JSpinner() );
ui.applyStyle( "minimumWidth: 100" );
ui.applyStyle( "buttonStyle: button" );
ui.applyStyle( "arrowType: chevron" );
ui.applyStyle( "borderColor: #fff" );
ui.applyStyle( "disabledBorderColor: #fff" );
ui.applyStyle( "disabledBackground: #fff" );
ui.applyStyle( "disabledForeground: #fff" );
ui.applyStyle( "focusedBackground: #fff" );
ui.applyStyle( "buttonBackground: #fff" );
ui.applyStyle( "buttonArrowColor: #fff" );
ui.applyStyle( "buttonDisabledArrowColor: #fff" );
ui.applyStyle( "buttonHoverArrowColor: #fff" );
ui.applyStyle( "buttonPressedArrowColor: #fff" );
ui.applyStyle( "padding: 1,2,3,4" );
}
@Test
void splitPane() {
FlatSplitPaneUI ui = new FlatSplitPaneUI();