From c12adf12e7eebd57976702178e449041341fdb9c Mon Sep 17 00:00:00 2001 From: Dar Date: Thu, 28 Nov 2024 10:18:22 +0100 Subject: [PATCH 1/4] new: support for alternate row color in JList --- .../java/com/formdev/flatlaf/ui/FlatListUI.java | 13 +++++++++++++ .../formdev/flatlaf/ui/TestFlatStyleableInfo.java | 1 + .../formdev/flatlaf/ui/TestFlatStyleableValue.java | 1 + .../com/formdev/flatlaf/ui/TestFlatStyling.java | 1 + .../formdev/flatlaf/themeeditor/FlatLafUIKeys.txt | 1 + 5 files changed, 17 insertions(+) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java index b6d0b712..c1fa40bc 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java @@ -57,6 +57,7 @@ import com.formdev.flatlaf.util.UIScale; * @uiDefault List.foreground Color * @uiDefault List.selectionBackground Color * @uiDefault List.selectionForeground Color + * @uiDefault List.alternateRowColor Color * @uiDefault List.dropLineColor Color * @uiDefault List.border Border * @uiDefault List.cellRenderer ListCellRenderer @@ -93,6 +94,7 @@ public class FlatListUI @Styleable protected Color selectionForeground; @Styleable protected Color selectionInactiveBackground; @Styleable protected Color selectionInactiveForeground; + /** @since 3.6 */ @Styleable protected Color alternateRowColor; /** @since 3 */ @Styleable protected Insets selectionInsets; /** @since 3 */ @Styleable protected int selectionArc; @@ -129,6 +131,7 @@ public class FlatListUI selectionForeground = UIManager.getColor( "List.selectionForeground" ); selectionInactiveBackground = UIManager.getColor( "List.selectionInactiveBackground" ); selectionInactiveForeground = UIManager.getColor( "List.selectionInactiveForeground" ); + alternateRowColor = UIManager.getColor( "List.alternateRowColor" ); selectionInsets = UIManager.getInsets( "List.selectionInsets" ); selectionArc = UIManager.getInt( "List.selectionArc" ); @@ -143,6 +146,7 @@ public class FlatListUI selectionForeground = null; selectionInactiveBackground = null; selectionInactiveForeground = null; + alternateRowColor = null; oldStyleValues = null; } @@ -304,6 +308,15 @@ public class FlatListUI Component rendererComponent = cellRenderer.getListCellRendererComponent( list, dataModel.getElementAt( row ), row, isSelected, FlatUIUtils.isPermanentFocusOwner( list ) && (row == leadIndex) ); + + if( alternateRowColor != null && row % 2 != 0 ) { + g.setColor( alternateRowColor ); + + // paint respecting selection arc + final float arc = UIScale.scale( selectionArc / 2f ); + FlatUIUtils.paintSelection( (Graphics2D) g, 0, rowBounds.y, list.getWidth(), rowBounds.height, + UIScale.scale( selectionInsets ), arc, arc, arc, arc, 0 ); + } // boolean isFileList = Boolean.TRUE.equals( list.getClientProperty( "List.isFileList" ) ); 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 596ac6c8..be7949a0 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 @@ -270,6 +270,7 @@ public class TestFlatStyleableInfo "selectionForeground", Color.class, "selectionInactiveBackground", Color.class, "selectionInactiveForeground", Color.class, + "alternateRowColor", Color.class, "selectionInsets", Insets.class, "selectionArc", int.class, diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableValue.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableValue.java index f0ae4484..4ff65259 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableValue.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableValue.java @@ -370,6 +370,7 @@ public class TestFlatStyleableValue testColor( c, ui, "selectionForeground", 0x123456 ); testColor( c, ui, "selectionInactiveBackground", 0x123456 ); testColor( c, ui, "selectionInactiveForeground", 0x123456 ); + testColor( c, ui, "alternateRowColor", 0x123456 ); testInsets( c, ui, "selectionInsets", 1,2,3,4 ); testInteger( c, ui, "selectionArc", 123 ); 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 d20e49fb..d8271441 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 @@ -433,6 +433,7 @@ public class TestFlatStyling ui.applyStyle( "selectionForeground: #fff" ); ui.applyStyle( "selectionInactiveBackground: #fff" ); ui.applyStyle( "selectionInactiveForeground: #fff" ); + ui.applyStyle( "alternateRowColor: #fff" ); ui.applyStyle( "selectionInsets: 1,2,3,4" ); ui.applyStyle( "selectionArc: 8" ); 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 7f3ec73a..3d7bd7d1 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 @@ -443,6 +443,7 @@ List.cellNoFocusBorder List.cellRenderer List.dropCellBackground List.dropCellForeground +List.alternateRowColor List.dropLineColor List.focusCellHighlightBorder List.focusInputMap From 19055d5a187313a4d96ab3bb572900e3dbb1f991 Mon Sep 17 00:00:00 2001 From: Dar Date: Tue, 3 Dec 2024 17:08:04 +0100 Subject: [PATCH 2/4] mod: combobox does not support alternate row color --- .../com/formdev/flatlaf/ui/FlatListUI.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java index c1fa40bc..01a9896f 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java @@ -28,6 +28,7 @@ import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.beans.PropertyChangeListener; import java.util.Map; +import javax.swing.DefaultComboBoxModel; import javax.swing.DefaultListCellRenderer; import javax.swing.JComponent; import javax.swing.JList; @@ -308,16 +309,7 @@ public class FlatListUI Component rendererComponent = cellRenderer.getListCellRendererComponent( list, dataModel.getElementAt( row ), row, isSelected, FlatUIUtils.isPermanentFocusOwner( list ) && (row == leadIndex) ); - - if( alternateRowColor != null && row % 2 != 0 ) { - g.setColor( alternateRowColor ); - - // paint respecting selection arc - final float arc = UIScale.scale( selectionArc / 2f ); - FlatUIUtils.paintSelection( (Graphics2D) g, 0, rowBounds.y, list.getWidth(), rowBounds.height, - UIScale.scale( selectionInsets ), arc, arc, arc, arc, 0 ); - } - + // boolean isFileList = Boolean.TRUE.equals( list.getClientProperty( "List.isFileList" ) ); int cx, cw; @@ -331,6 +323,18 @@ public class FlatListUI cx = rowBounds.x; cw = rowBounds.width; } + + // filelist/combobox does not support alternate row color + if( !isFileList && !(dataModel instanceof DefaultComboBoxModel) ) { + if( alternateRowColor != null && row % 2 != 0 ) { + g.setColor( alternateRowColor ); + + // paint respecting selection arc + final float arc = UIScale.scale( selectionArc / 2f ); + FlatUIUtils.paintSelection( (Graphics2D) g, 0, rowBounds.y, list.getWidth(), rowBounds.height, + UIScale.scale( selectionInsets ), arc, arc, arc, arc, 0 ); + } + } // rounded selection or selection insets if( isSelected && From 6404b8de2ac8f4d60a71c085cb8c8be8a16c6bb3 Mon Sep 17 00:00:00 2001 From: Dar Date: Mon, 16 Dec 2024 15:48:54 +0100 Subject: [PATCH 3/4] fix: filter combobox by name --- .../src/main/java/com/formdev/flatlaf/ui/FlatListUI.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java index 01a9896f..2fe2bab4 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java @@ -324,8 +324,8 @@ public class FlatListUI cw = rowBounds.width; } - // filelist/combobox does not support alternate row color - if( !isFileList && !(dataModel instanceof DefaultComboBoxModel) ) { + // combobox does not support alternate row color + if ( !"ComboBox.list".equals( list.getName() ) ) { if( alternateRowColor != null && row % 2 != 0 ) { g.setColor( alternateRowColor ); From abf77d5399012c6dd4bdbf979e6e211522d4c92e Mon Sep 17 00:00:00 2001 From: Dar Date: Mon, 16 Dec 2024 15:55:15 +0100 Subject: [PATCH 4/4] mod: code format --- .../src/main/java/com/formdev/flatlaf/ui/FlatListUI.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java index 2fe2bab4..1514a37a 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java @@ -325,7 +325,7 @@ public class FlatListUI } // combobox does not support alternate row color - if ( !"ComboBox.list".equals( list.getName() ) ) { + if( !"ComboBox.list".equals( list.getName() ) ) { if( alternateRowColor != null && row % 2 != 0 ) { g.setColor( alternateRowColor );