Styling: support cell borders of List and Table

This commit is contained in:
Karl Tauber
2021-06-24 10:45:48 +02:00
parent 007ee38cb4
commit 06bc53692a
6 changed files with 135 additions and 4 deletions

View File

@@ -61,8 +61,8 @@ public class FlatLineBorder
Graphics2D g2 = (Graphics2D) g.create(); Graphics2D g2 = (Graphics2D) g.create();
try { try {
FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.setRenderingHints( g2 );
g2.setColor( lineColor ); g2.setColor( getLineColor() );
FlatUIUtils.paintComponentBorder( g2, x, y, width, height, 0f, scale( lineThickness ), 0f ); FlatUIUtils.paintComponentBorder( g2, x, y, width, height, 0f, scale( getLineThickness() ), 0f );
} finally { } finally {
g2.dispose(); g2.dispose();
} }

View File

@@ -16,11 +16,16 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Insets;
import java.util.function.Function;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.plaf.ListUI;
/** /**
* Cell border for {@link javax.swing.DefaultListCellRenderer} * Cell border for {@link javax.swing.DefaultListCellRenderer}
@@ -33,12 +38,59 @@ import javax.swing.UIManager;
public class FlatListCellBorder public class FlatListCellBorder
extends FlatLineBorder extends FlatLineBorder
{ {
final boolean showCellFocusIndicator = UIManager.getBoolean( "List.showCellFocusIndicator" ); protected boolean showCellFocusIndicator = UIManager.getBoolean( "List.showCellFocusIndicator" );
private Component c;
protected FlatListCellBorder() { protected FlatListCellBorder() {
super( UIManager.getInsets( "List.cellMargins" ), UIManager.getColor( "List.cellFocusColor" ) ); 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> T getStyleFromListUI( Component c, Function<FlatListUI, T> 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 ------------------------------------------------------ //---- class Default ------------------------------------------------------
/** /**
@@ -74,6 +126,8 @@ public class FlatListCellBorder
{ {
@Override @Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) { 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 ) if( !showCellFocusIndicator )
return; return;

View File

@@ -18,6 +18,7 @@ package com.formdev.flatlaf.ui;
import java.awt.Color; import java.awt.Color;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.awt.Insets;
import java.awt.event.FocusEvent; import java.awt.event.FocusEvent;
import java.awt.event.FocusListener; import java.awt.event.FocusListener;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
@@ -72,6 +73,11 @@ public class FlatListUI
@Styleable protected Color selectionInactiveBackground; @Styleable protected Color selectionInactiveBackground;
@Styleable protected Color selectionInactiveForeground; @Styleable protected Color selectionInactiveForeground;
// for FlatListCellBorder
@Styleable protected Insets cellMargins;
@Styleable protected Color cellFocusColor;
@Styleable protected boolean showCellFocusIndicator;
private Map<String, Object> oldStyleValues; private Map<String, Object> oldStyleValues;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {

View File

@@ -16,11 +16,16 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Insets;
import java.util.function.Function;
import javax.swing.JTable; import javax.swing.JTable;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.plaf.TableUI;
/** /**
* Cell border for {@link javax.swing.table.DefaultTableCellRenderer} * Cell border for {@link javax.swing.table.DefaultTableCellRenderer}
@@ -33,12 +38,59 @@ import javax.swing.UIManager;
public class FlatTableCellBorder public class FlatTableCellBorder
extends FlatLineBorder extends FlatLineBorder
{ {
final boolean showCellFocusIndicator = UIManager.getBoolean( "Table.showCellFocusIndicator" ); protected boolean showCellFocusIndicator = UIManager.getBoolean( "Table.showCellFocusIndicator" );
private Component c;
protected FlatTableCellBorder() { protected FlatTableCellBorder() {
super( UIManager.getInsets( "Table.cellMargins" ), UIManager.getColor( "Table.cellFocusColor" ) ); 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> T getStyleFromTableUI( Component c, Function<FlatTableUI, T> 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 ------------------------------------------------------ //---- class Default ------------------------------------------------------
/** /**
@@ -74,6 +126,9 @@ public class FlatTableCellBorder
{ {
@Override @Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) { 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 ) { if( !showCellFocusIndicator ) {
JTable table = (JTable) SwingUtilities.getAncestorOfClass( JTable.class, c ); JTable table = (JTable) SwingUtilities.getAncestorOfClass( JTable.class, c );
if( table != null && !isSelectionEditable( table ) ) if( table != null && !isSelectionEditable( table ) )

View File

@@ -22,6 +22,7 @@ import java.awt.Dimension;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.FocusEvent; import java.awt.event.FocusEvent;
import java.awt.event.FocusListener; import java.awt.event.FocusListener;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
@@ -101,6 +102,11 @@ public class FlatTableUI
@Styleable protected Color selectionInactiveBackground; @Styleable protected Color selectionInactiveBackground;
@Styleable protected Color selectionInactiveForeground; @Styleable protected Color selectionInactiveForeground;
// for FlatTableCellBorder
@Styleable protected Insets cellMargins;
@Styleable protected Color cellFocusColor;
@Styleable protected boolean showCellFocusIndicator;
private boolean oldShowHorizontalLines; private boolean oldShowHorizontalLines;
private boolean oldShowVerticalLines; private boolean oldShowVerticalLines;
private Dimension oldIntercellSpacing; private Dimension oldIntercellSpacing;

View File

@@ -182,6 +182,11 @@ public class FlatStylingTests
ui.applyStyle( "selectionForeground: #fff" ); ui.applyStyle( "selectionForeground: #fff" );
ui.applyStyle( "selectionInactiveBackground: #fff" ); ui.applyStyle( "selectionInactiveBackground: #fff" );
ui.applyStyle( "selectionInactiveForeground: #fff" ); ui.applyStyle( "selectionInactiveForeground: #fff" );
// FlatListCellBorder
ui.applyStyle( "cellMargins: 1,2,3,4" );
ui.applyStyle( "cellFocusColor: #fff" );
ui.applyStyle( "showCellFocusIndicator: true" );
} }
@Test @Test
@@ -481,6 +486,11 @@ public class FlatStylingTests
ui.applyStyle( "selectionForeground: #fff" ); ui.applyStyle( "selectionForeground: #fff" );
ui.applyStyle( "selectionInactiveBackground: #fff" ); ui.applyStyle( "selectionInactiveBackground: #fff" );
ui.applyStyle( "selectionInactiveForeground: #fff" ); ui.applyStyle( "selectionInactiveForeground: #fff" );
// FlatTableCellBorder
ui.applyStyle( "cellMargins: 1,2,3,4" );
ui.applyStyle( "cellFocusColor: #fff" );
ui.applyStyle( "showCellFocusIndicator: true" );
} }
@Test @Test