From 4a852bc7c246917bb38a11900afda3cdfccf39ca Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Fri, 23 Aug 2019 10:29:57 +0200 Subject: [PATCH] ComboBox: fixed background colors and padding --- .../com/formdev/flatlaf/ui/FlatButtonUI.java | 4 +- .../formdev/flatlaf/ui/FlatComboBoxUI.java | 113 ++++++++++++++---- .../flatlaf/ui/FlatPasswordFieldUI.java | 2 +- .../formdev/flatlaf/ui/FlatTextFieldUI.java | 2 +- .../com/formdev/flatlaf/ui/FlatUIUtils.java | 27 +++++ .../com/formdev/flatlaf/util/UIScale.java | 10 ++ 6 files changed, 128 insertions(+), 30 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java index ba312895..0f29dfb9 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java @@ -64,8 +64,8 @@ public class FlatButtonUI try { FlatUIUtils.setRenderingHints( g2 ); - float focusWidth = FlatUIUtils.getFocusWidth(); - float arc = FlatUIUtils.getButtonArc(); + float focusWidth = FlatUIUtils.getFocusWidth( c ); + float arc = FlatUIUtils.getButtonArc( c ); g2.setColor( getBackground( c ) ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java index fc2d869f..30704fa0 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java @@ -18,17 +18,22 @@ 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; import java.awt.Graphics2D; +import java.awt.LayoutManager; import java.awt.Shape; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicArrowButton; import javax.swing.plaf.basic.BasicComboBoxUI; +import com.formdev.flatlaf.util.UIScale; /** * Provides the Flat LaF UI delegate for {@link javax.swing.JComboBox}. @@ -42,6 +47,62 @@ public class FlatComboBoxUI return new FlatComboBoxUI(); } + @Override + protected void installDefaults() { + super.installDefaults(); + + padding = UIScale.scale( padding ); + } + + @Override + protected LayoutManager createLayoutManager() { + return new BasicComboBoxUI.ComboBoxLayoutManager() { + @Override + public void layoutContainer( Container parent ) { + super.layoutContainer( parent ); + + if ( editor != null && padding != null ) { + // fix editor bounds by subtracting padding + editor.setBounds( FlatUIUtils.subtract( editor.getBounds(), padding ) ); + } + } + }; + } + + @Override + protected PropertyChangeListener createPropertyChangeListener() { + return new BasicComboBoxUI.PropertyChangeHandler() { + @Override + public void propertyChange( PropertyChangeEvent e ) { + super.propertyChange( e ); + + Object source = e.getSource(); + String propertyName = e.getPropertyName(); + + if( editor != null && + ((source == comboBox && propertyName == "background") || + (source == editor && propertyName == "enabled")) ) + { + // fix editor component background color + updateEditorBackground(); + } + } + }; + } + + @Override + protected void configureEditor() { + super.configureEditor(); + + updateEditorBackground(); + } + + private void updateEditorBackground() { + editor.setBackground( editor.isEnabled() + ? comboBox.getBackground() + : UIManager.getColor( "ComboBox.disabledBackground" ) ); + } + @Override protected JButton createArrowButton() { return new FlatArrowButton(); @@ -52,36 +113,36 @@ public class FlatComboBoxUI if( c.isOpaque() ) { FlatUIUtils.paintParentBackground( g, c ); - if( c.isEnabled() || comboBox.isEditable() ) { - Graphics2D g2 = (Graphics2D) g; - FlatUIUtils.setRenderingHints( g2 ); + Graphics2D g2 = (Graphics2D) g; + FlatUIUtils.setRenderingHints( g2 ); - int width = c.getWidth(); - int height = c.getHeight(); - float focusWidth = FlatUIUtils.getFocusWidth(); - float arc = FlatUIUtils.getComponentArc(); - int arrowX = arrowButton.getX(); + int width = c.getWidth(); + int height = c.getHeight(); + float focusWidth = FlatUIUtils.getFocusWidth( c ); + float arc = FlatUIUtils.getComponentArc( c ); + int arrowX = arrowButton.getX(); - if( c.isEnabled() ) { - // paint background - g2.setColor( c.getBackground() ); - FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); + // paint background + g2.setColor( comboBox.isEnabled() + ? c.getBackground() + : UIManager.getColor( "ComboBox.disabledBackground" ) ); + FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); - // paint arrow button background - g2.setColor( UIManager.getColor( comboBox.isEditable() - ? "ComboBox.buttonEditableBackground" - : "ComboBox.buttonBackground" ) ); - Shape oldClip = g2.getClip(); - g2.clipRect( arrowX, 0, width - arrowX, height ); - FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); - g2.setClip( oldClip ); - } + // paint arrow button background + g2.setColor( UIManager.getColor( comboBox.isEnabled() + ? (comboBox.isEditable() + ? "ComboBox.buttonEditableBackground" + : "ComboBox.buttonBackground" ) + : "ComboBox.disabledBackground" ) ); + Shape oldClip = g2.getClip(); + g2.clipRect( arrowX, 0, width - arrowX, height ); + FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc ); + g2.setClip( oldClip ); - if( comboBox.isEditable() ) { - // paint vertical line between value and arrow button - g2.setColor( FlatUIUtils.getBorderColor( comboBox.isEnabled(), false ) ); - g2.fill( new Rectangle2D.Float( arrowX, focusWidth, scale( 1f ), height - (focusWidth * 2) ) ); - } + if( comboBox.isEditable() ) { + // paint vertical line between value and arrow button + g2.setColor( FlatUIUtils.getBorderColor( comboBox.isEnabled(), false ) ); + g2.fill( new Rectangle2D.Float( arrowX, focusWidth, scale( 1f ), height - (focusWidth * 2) ) ); } } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java index 35c7d8b5..a25ab4c0 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPasswordFieldUI.java @@ -63,7 +63,7 @@ public class FlatPasswordFieldUI try { FlatUIUtils.setRenderingHints( g2 ); - float focusWidth = FlatUIUtils.getFocusWidth(); + float focusWidth = FlatUIUtils.getFocusWidth( c ); g2.setColor( c.getBackground() ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java index 3b336344..7a8e4167 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTextFieldUI.java @@ -63,7 +63,7 @@ public class FlatTextFieldUI try { FlatUIUtils.setRenderingHints( g2 ); - float focusWidth = FlatUIUtils.getFocusWidth(); + float focusWidth = FlatUIUtils.getFocusWidth( c ); g2.setColor( c.getBackground() ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, 0 ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java index 30a6fc62..5bfb994d 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java @@ -21,6 +21,8 @@ import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Insets; +import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.Path2D; import java.awt.geom.RoundRectangle2D; @@ -37,6 +39,19 @@ public class FlatUIUtils { public static final boolean MAC_USE_QUARTZ = Boolean.getBoolean( "apple.awt.graphics.UseQuartz" ); + public static Rectangle subtract( Rectangle r, Insets insets ) { + return new Rectangle( + r.x + insets.left, + r.y + insets.top, + r.width - insets.left - insets.right, + r.height - insets.top - insets.bottom ); + } + + public static Color getUIColor( String key, int defaultColorRGB ) { + Color color = UIManager.getColor( key ); + return (color != null) ? color : new Color( defaultColorRGB ); + } + public static int getUIInt( String key, int defaultValue ) { Object value = UIManager.get( key ); return (value instanceof Integer) ? (Integer) value : defaultValue; @@ -58,6 +73,18 @@ public class FlatUIUtils 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") diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/UIScale.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/UIScale.java index 0d56f429..1130b829 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/UIScale.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/UIScale.java @@ -20,12 +20,14 @@ import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics2D; import java.awt.GraphicsEnvironment; +import java.awt.Insets; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.lang.reflect.Method; import javax.swing.LookAndFeel; import javax.swing.UIManager; import javax.swing.plaf.DimensionUIResource; +import javax.swing.plaf.InsetsUIResource; import javax.swing.plaf.UIResource; /** @@ -195,4 +197,12 @@ public class UIScale ? new DimensionUIResource( scale( dimension.width ), scale( dimension.height ) ) : new Dimension ( scale( dimension.width ), scale( dimension.height ) )); } + + public static Insets scale( Insets insets ) { + return (insets == null || scaleFactor == 1f) + ? insets + : (insets instanceof UIResource + ? new InsetsUIResource( scale( insets.top ), scale( insets.left ), scale( insets.bottom ), scale( insets.right ) ) + : new Insets ( scale( insets.top ), scale( insets.left ), scale( insets.bottom ), scale( insets.right ) )); + } }