From 4515c6cae9813406d1678b95cca778e299496673 Mon Sep 17 00:00:00 2001 From: Emmanuel Bourg Date: Wed, 3 Nov 2021 10:21:59 +0100 Subject: [PATCH 1/2] Table: change the foreground color when the table is disabled (issue #387) --- flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java | 1 + .../src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java index 883382938..55a7ac6e6 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -402,6 +402,7 @@ public UIDefaults getDefaults() { "RadioButton.disabledText", "RadioButtonMenuItem.disabledForeground", "Spinner.disabledForeground", + "Table.disabledForeground", "ToggleButton.disabledText" ); putDefaults( defaults, defaults.getColor( "textText" ), "DesktopIcon.foreground" ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java index 930b04b87..3189142f1 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java @@ -55,6 +55,7 @@ * @uiDefault Table.foreground Color * @uiDefault Table.selectionBackground Color * @uiDefault Table.selectionForeground Color + * @uiDefault Table.disabledForeground Color * @uiDefault Table.gridColor Color * @uiDefault Table.scrollPaneBorder Border * @uiDefault Table.dropLineColor Color @@ -203,6 +204,11 @@ protected void installListeners() { table.revalidate(); table.repaint(); break; + + case "enabled": + boolean enabled = Boolean.TRUE.equals( e.getNewValue() ); + table.setForeground( UIManager.getColor( enabled ? "Table.foreground" : "Table.disabledForeground" ) ); + break; } }; table.addPropertyChangeListener( propertyChangeListener ); From 2b38cb7dc8ce5e65d70b90b5512249c28c6e3cbf Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 18 Nov 2021 23:14:30 +0100 Subject: [PATCH 2/2] Table: completed implementation for disabled foreground (PR #411) - change table foreground only if not modified from outside - keep foreground disabled when switching Laf - support styling --- .../com/formdev/flatlaf/ui/FlatTableUI.java | 37 +++++++++++++++++-- .../flatlaf/ui/TestFlatStyleableInfo.java | 1 + .../formdev/flatlaf/ui/TestFlatStyling.java | 1 + .../dumps/uidefaults/FlatDarkLaf_1.8.0.txt | 1 + .../dumps/uidefaults/FlatLightLaf_1.8.0.txt | 1 + .../dumps/uidefaults/FlatTestLaf_1.8.0.txt | 1 + .../flatlaf/themeeditor/FlatLafUIKeys.txt | 1 + 7 files changed, 40 insertions(+), 3 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java index 3189142f1..52635c361 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java @@ -35,6 +35,7 @@ import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; +import javax.swing.plaf.UIResource; import javax.swing.plaf.basic.BasicTableUI; import javax.swing.table.JTableHeader; import com.formdev.flatlaf.FlatClientProperties; @@ -55,7 +56,6 @@ * @uiDefault Table.foreground Color * @uiDefault Table.selectionBackground Color * @uiDefault Table.selectionForeground Color - * @uiDefault Table.disabledForeground Color * @uiDefault Table.gridColor Color * @uiDefault Table.scrollPaneBorder Border * @uiDefault Table.dropLineColor Color @@ -79,6 +79,7 @@ * @uiDefault Table.showVerticalLines boolean * @uiDefault Table.showTrailingVerticalLine boolean * @uiDefault Table.intercellSpacing Dimension + * @uiDefault Table.disabledForeground Color * @uiDefault Table.selectionInactiveBackground Color * @uiDefault Table.selectionInactiveForeground Color * @@ -103,6 +104,8 @@ public class FlatTableUI /** @since 1.6 */ @Styleable protected boolean showTrailingVerticalLine; protected Dimension intercellSpacing; + private Color foreground; + /** @since 2 */ @Styleable protected Color disabledForeground; @Styleable protected Color selectionBackground; @Styleable protected Color selectionForeground; @Styleable protected Color selectionInactiveBackground; @@ -113,6 +116,7 @@ public class FlatTableUI @Styleable protected Color cellFocusColor; @Styleable protected boolean showCellFocusIndicator; + private Color oldDisabledForeground; private boolean oldShowHorizontalLines; private boolean oldShowVerticalLines; private Dimension oldIntercellSpacing; @@ -128,6 +132,7 @@ public static ComponentUI createUI( JComponent c ) { public void installUI( JComponent c ) { super.installUI( c ); + updateForeground(); installStyle(); } @@ -140,6 +145,8 @@ protected void installDefaults() { showTrailingVerticalLine = UIManager.getBoolean( "Table.showTrailingVerticalLine" ); intercellSpacing = UIManager.getDimension( "Table.intercellSpacing" ); + foreground = UIManager.getColor( "Table.foreground" ); + disabledForeground = UIManager.getColor( "Table.disabledForeground" ); selectionBackground = UIManager.getColor( "Table.selectionBackground" ); selectionForeground = UIManager.getColor( "Table.selectionForeground" ); selectionInactiveBackground = UIManager.getColor( "Table.selectionInactiveBackground" ); @@ -170,11 +177,15 @@ protected void installDefaults() { protected void uninstallDefaults() { super.uninstallDefaults(); + foreground = null; + disabledForeground = null; selectionBackground = null; selectionForeground = null; selectionInactiveBackground = null; selectionInactiveForeground = null; + oldDisabledForeground = null; + oldStyleValues = null; // restore old show horizontal/vertical lines (if not modified) @@ -206,8 +217,7 @@ protected void installListeners() { break; case "enabled": - boolean enabled = Boolean.TRUE.equals( e.getNewValue() ); - table.setForeground( UIManager.getColor( enabled ? "Table.foreground" : "Table.disabledForeground" ) ); + updateForeground(); break; } }; @@ -254,6 +264,8 @@ protected void installStyle() { /** @since 2 */ protected void applyStyle( Object style ) { + oldDisabledForeground = disabledForeground; + Color oldSelectionBackground = selectionBackground; Color oldSelectionForeground = selectionForeground; Color oldSelectionInactiveBackground = selectionInactiveBackground; @@ -261,6 +273,8 @@ protected void applyStyle( Object style ) { oldStyleValues = FlatStylingSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty ); + updateForeground(); + // update selection background if( selectionBackground != oldSelectionBackground ) { Color selBg = table.getSelectionBackground(); @@ -291,6 +305,23 @@ public Map> getStyleableInfos( JComponent c ) { return FlatStylingSupport.getAnnotatedStyleableInfos( this ); } + // similar to FlatTextFieldUI.updateBackground() + private void updateForeground() { + Color oldForeground = table.getForeground(); + if( !(oldForeground instanceof UIResource) ) + return; + + // do not update foreground if it currently has a unknown color (assigned from outside) + if( oldForeground != foreground && + oldForeground != disabledForeground && + oldForeground != oldDisabledForeground ) + return; + + Color newForeground = table.isEnabled() ? foreground : disabledForeground; + if( newForeground != oldForeground ) + table.setForeground( newForeground ); + } + /** * Toggle selection colors from focused to inactive and vice versa. * diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java index 868d7ddb1..3a4499307 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java @@ -728,6 +728,7 @@ void table() { Map> expected = expectedMap( "showTrailingVerticalLine", boolean.class, + "disabledForeground", Color.class, "selectionBackground", Color.class, "selectionForeground", Color.class, "selectionInactiveBackground", Color.class, diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java index f8b585bf0..fb2b4b754 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java @@ -894,6 +894,7 @@ void table() { FlatTableUI ui = (FlatTableUI) c.getUI(); ui.applyStyle( "showTrailingVerticalLine: true" ); + ui.applyStyle( "disabledForeground: #fff" ); ui.applyStyle( "selectionBackground: #fff" ); ui.applyStyle( "selectionForeground: #fff" ); ui.applyStyle( "selectionInactiveBackground: #fff" ); diff --git a/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0.txt b/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0.txt index f1845746a..5f911770d 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0.txt @@ -1059,6 +1059,7 @@ Table.cellMargins 2,3,2,3 javax.swing.plaf.InsetsUIResource [UI] Table.cellNoFocusBorder [lazy] 2,3,2,3 false com.formdev.flatlaf.ui.FlatTableCellBorder$Default [UI] lineColor=#000000 HSL 0 0 0 javax.swing.plaf.ColorUIResource [UI] lineThickness=1.000000 Table.consistentHomeEndKeyBehavior true Table.descendingSortIcon [lazy] 10,5 com.formdev.flatlaf.icons.FlatDescendingSortIcon [UI] +Table.disabledForeground #8c8c8c HSL 0 0 55 javax.swing.plaf.ColorUIResource [UI] Table.dropCellBackground [lazy] #3c588b HSL 219 40 39 javax.swing.plaf.ColorUIResource [UI] Table.dropCellForeground [lazy] #bbbbbb HSL 0 0 73 javax.swing.plaf.ColorUIResource [UI] Table.dropLineColor [lazy] #6d8ac0 HSL 219 40 59 javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0.txt b/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0.txt index cd9780c89..4b3bd3005 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0.txt @@ -1064,6 +1064,7 @@ Table.cellMargins 2,3,2,3 javax.swing.plaf.InsetsUIResource [UI] Table.cellNoFocusBorder [lazy] 2,3,2,3 false com.formdev.flatlaf.ui.FlatTableCellBorder$Default [UI] lineColor=#000000 HSL 0 0 0 javax.swing.plaf.ColorUIResource [UI] lineThickness=1.000000 Table.consistentHomeEndKeyBehavior true Table.descendingSortIcon [lazy] 10,5 com.formdev.flatlaf.icons.FlatDescendingSortIcon [UI] +Table.disabledForeground #8c8c8c HSL 0 0 55 javax.swing.plaf.ColorUIResource [UI] Table.dropCellBackground [lazy] #3f8fd9 HSL 209 67 55 javax.swing.plaf.ColorUIResource [UI] Table.dropCellForeground [lazy] #ffffff HSL 0 0 100 javax.swing.plaf.ColorUIResource [UI] Table.dropLineColor [lazy] #6aa7e1 HSL 209 66 65 javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt b/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt index 7a4650925..b2b5d12e0 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt @@ -1070,6 +1070,7 @@ Table.cellMargins 2,3,2,3 javax.swing.plaf.InsetsUIResource [UI] Table.cellNoFocusBorder [lazy] 2,3,2,3 false com.formdev.flatlaf.ui.FlatTableCellBorder$Default [UI] lineColor=#ff0000 HSL 0 100 50 javax.swing.plaf.ColorUIResource [UI] lineThickness=1.000000 Table.consistentHomeEndKeyBehavior true Table.descendingSortIcon [lazy] 10,5 com.formdev.flatlaf.icons.FlatDescendingSortIcon [UI] +Table.disabledForeground #000088 HSL 240 100 27 javax.swing.plaf.ColorUIResource [UI] Table.dropCellBackground #ff0000 HSL 0 100 50 javax.swing.plaf.ColorUIResource [UI] Table.dropCellForeground #00ff00 HSL 120 100 50 javax.swing.plaf.ColorUIResource [UI] Table.dropLineColor #0000ff HSL 240 100 50 javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt b/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt index 5370d6c7e..aec611bb3 100644 --- a/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt +++ b/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt @@ -824,6 +824,7 @@ Table.cellMargins Table.cellNoFocusBorder Table.consistentHomeEndKeyBehavior Table.descendingSortIcon +Table.disabledForeground Table.dropCellBackground Table.dropCellForeground Table.dropLineColor