diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLineBorder.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLineBorder.java index a529f096..d8e3ea69 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLineBorder.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatLineBorder.java @@ -61,8 +61,8 @@ public class FlatLineBorder Graphics2D g2 = (Graphics2D) g.create(); try { FlatUIUtils.setRenderingHints( g2 ); - g2.setColor( lineColor ); - FlatUIUtils.paintComponentBorder( g2, x, y, width, height, 0f, scale( lineThickness ), 0f ); + g2.setColor( getLineColor() ); + FlatUIUtils.paintComponentBorder( g2, x, y, width, height, 0f, scale( getLineThickness() ), 0f ); } finally { g2.dispose(); } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListCellBorder.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListCellBorder.java index 16d3a23d..f70c071a 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListCellBorder.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListCellBorder.java @@ -16,11 +16,16 @@ package com.formdev.flatlaf.ui; +import static com.formdev.flatlaf.util.UIScale.scale; +import java.awt.Color; import java.awt.Component; import java.awt.Graphics; +import java.awt.Insets; +import java.util.function.Function; import javax.swing.JList; import javax.swing.SwingUtilities; import javax.swing.UIManager; +import javax.swing.plaf.ListUI; /** * Cell border for {@link javax.swing.DefaultListCellRenderer} @@ -33,12 +38,59 @@ import javax.swing.UIManager; public class FlatListCellBorder extends FlatLineBorder { - final boolean showCellFocusIndicator = UIManager.getBoolean( "List.showCellFocusIndicator" ); + protected boolean showCellFocusIndicator = UIManager.getBoolean( "List.showCellFocusIndicator" ); + + private Component c; protected FlatListCellBorder() { super( UIManager.getInsets( "List.cellMargins" ), UIManager.getColor( "List.cellFocusColor" ) ); } + @Override + public Insets getBorderInsets( Component c, Insets insets ) { + Insets margins = getStyleFromListUI( c, ui -> ui.cellMargins ); + if( margins != null ) { + boolean leftToRight = margins.left == margins.right || c.getComponentOrientation().isLeftToRight(); + insets.left = scale( leftToRight ? margins.left : margins.right ); + insets.top = scale( margins.top ); + insets.right = scale( leftToRight ? margins.right : margins.left ); + insets.bottom = scale( margins.bottom ); + return insets; + } + return super.getBorderInsets( c, insets ); + } + + @Override + public Color getLineColor() { + if( c != null ) { + Color color = getStyleFromListUI( c, ui -> ui.cellFocusColor ); + if( color != null ) + return color; + } + return super.getLineColor(); + } + + @Override + public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) { + this.c = c; + super.paintBorder( c, g, x, y, width, height ); + this.c = null; + } + + /** + * Because this borders are always shared for all lists, + * get border specific style from FlatListUI. + */ + static T getStyleFromListUI( Component c, Function f ) { + JList list = (JList) SwingUtilities.getAncestorOfClass( JList.class, c ); + if( list != null ) { + ListUI ui = list.getUI(); + if( ui instanceof FlatListUI ) + return f.apply( (FlatListUI) ui ); + } + return null; + } + //---- class Default ------------------------------------------------------ /** @@ -74,6 +126,8 @@ public class FlatListCellBorder { @Override public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) { + Boolean b = getStyleFromListUI( c, ui -> ui.showCellFocusIndicator ); + boolean showCellFocusIndicator = (b != null) ? b : this.showCellFocusIndicator; if( !showCellFocusIndicator ) return; 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 1d4f13db..6bebe094 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 @@ -18,6 +18,7 @@ package com.formdev.flatlaf.ui; import java.awt.Color; import java.awt.EventQueue; +import java.awt.Insets; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.beans.PropertyChangeListener; @@ -72,6 +73,11 @@ public class FlatListUI @Styleable protected Color selectionInactiveBackground; @Styleable protected Color selectionInactiveForeground; + // for FlatListCellBorder + @Styleable protected Insets cellMargins; + @Styleable protected Color cellFocusColor; + @Styleable protected boolean showCellFocusIndicator; + private Map oldStyleValues; public static ComponentUI createUI( JComponent c ) { diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableCellBorder.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableCellBorder.java index cd25abae..71427496 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableCellBorder.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableCellBorder.java @@ -16,11 +16,16 @@ package com.formdev.flatlaf.ui; +import static com.formdev.flatlaf.util.UIScale.scale; +import java.awt.Color; import java.awt.Component; import java.awt.Graphics; +import java.awt.Insets; +import java.util.function.Function; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.UIManager; +import javax.swing.plaf.TableUI; /** * Cell border for {@link javax.swing.table.DefaultTableCellRenderer} @@ -33,12 +38,59 @@ import javax.swing.UIManager; public class FlatTableCellBorder extends FlatLineBorder { - final boolean showCellFocusIndicator = UIManager.getBoolean( "Table.showCellFocusIndicator" ); + protected boolean showCellFocusIndicator = UIManager.getBoolean( "Table.showCellFocusIndicator" ); + + private Component c; protected FlatTableCellBorder() { super( UIManager.getInsets( "Table.cellMargins" ), UIManager.getColor( "Table.cellFocusColor" ) ); } + @Override + public Insets getBorderInsets( Component c, Insets insets ) { + Insets margins = getStyleFromTableUI( c, ui -> ui.cellMargins ); + if( margins != null ) { + boolean leftToRight = margins.left == margins.right || c.getComponentOrientation().isLeftToRight(); + insets.left = scale( leftToRight ? margins.left : margins.right ); + insets.top = scale( margins.top ); + insets.right = scale( leftToRight ? margins.right : margins.left ); + insets.bottom = scale( margins.bottom ); + return insets; + } + return super.getBorderInsets( c, insets ); + } + + @Override + public Color getLineColor() { + if( c != null ) { + Color color = getStyleFromTableUI( c, ui -> ui.cellFocusColor ); + if( color != null ) + return color; + } + return super.getLineColor(); + } + + @Override + public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) { + this.c = c; + super.paintBorder( c, g, x, y, width, height ); + this.c = null; + } + + /** + * Because this borders are always shared for all tables, + * get border specific style from FlatTableUI. + */ + static T getStyleFromTableUI( Component c, Function f ) { + JTable table = (JTable) SwingUtilities.getAncestorOfClass( JTable.class, c ); + if( table != null ) { + TableUI ui = table.getUI(); + if( ui instanceof FlatTableUI ) + return f.apply( (FlatTableUI) ui ); + } + return null; + } + //---- class Default ------------------------------------------------------ /** @@ -74,6 +126,9 @@ public class FlatTableCellBorder { @Override public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) { + Boolean b = getStyleFromTableUI( c, ui -> ui.showCellFocusIndicator ); + boolean showCellFocusIndicator = (b != null) ? b : this.showCellFocusIndicator; + if( !showCellFocusIndicator ) { JTable table = (JTable) SwingUtilities.getAncestorOfClass( JTable.class, c ); if( table != null && !isSelectionEditable( table ) ) 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 a422986d..7711b6f2 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 @@ -22,6 +22,7 @@ import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Insets; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.geom.Rectangle2D; @@ -101,6 +102,11 @@ public class FlatTableUI @Styleable protected Color selectionInactiveBackground; @Styleable protected Color selectionInactiveForeground; + // for FlatTableCellBorder + @Styleable protected Insets cellMargins; + @Styleable protected Color cellFocusColor; + @Styleable protected boolean showCellFocusIndicator; + private boolean oldShowHorizontalLines; private boolean oldShowVerticalLines; private Dimension oldIntercellSpacing; diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/FlatStylingTests.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/FlatStylingTests.java index 74ad4946..2d7c3d27 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/FlatStylingTests.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/FlatStylingTests.java @@ -182,6 +182,11 @@ public class FlatStylingTests ui.applyStyle( "selectionForeground: #fff" ); ui.applyStyle( "selectionInactiveBackground: #fff" ); ui.applyStyle( "selectionInactiveForeground: #fff" ); + + // FlatListCellBorder + ui.applyStyle( "cellMargins: 1,2,3,4" ); + ui.applyStyle( "cellFocusColor: #fff" ); + ui.applyStyle( "showCellFocusIndicator: true" ); } @Test @@ -481,6 +486,11 @@ public class FlatStylingTests ui.applyStyle( "selectionForeground: #fff" ); ui.applyStyle( "selectionInactiveBackground: #fff" ); ui.applyStyle( "selectionInactiveForeground: #fff" ); + + // FlatTableCellBorder + ui.applyStyle( "cellMargins: 1,2,3,4" ); + ui.applyStyle( "cellFocusColor: #fff" ); + ui.applyStyle( "showCellFocusIndicator: true" ); } @Test