Styling: reduce duplicate code in list and table cell borders

This commit is contained in:
Karl Tauber
2021-06-24 17:53:19 +02:00
parent 5e5aa17e14
commit 8ba7f7f961
3 changed files with 14 additions and 20 deletions

View File

@@ -50,6 +50,12 @@ public class FlatEmptyBorder
@Override @Override
public Insets getBorderInsets( Component c, Insets insets ) { public Insets getBorderInsets( Component c, Insets insets ) {
return scaleInsets( c, insets, top, left, bottom, right );
}
protected static Insets scaleInsets( Component c, Insets insets,
int top, int left, int bottom, int right )
{
boolean leftToRight = left == right || c.getComponentOrientation().isLeftToRight(); boolean leftToRight = left == right || c.getComponentOrientation().isLeftToRight();
insets.left = scale( leftToRight ? left : right ); insets.left = scale( leftToRight ? left : right );
insets.top = scale( top ); insets.top = scale( top );

View File

@@ -16,7 +16,6 @@
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.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Graphics; import java.awt.Graphics;
@@ -48,15 +47,10 @@ public class FlatListCellBorder
@Override @Override
public Insets getBorderInsets( Component c, Insets insets ) { public Insets getBorderInsets( Component c, Insets insets ) {
Insets margins = getStyleFromListUI( c, ui -> ui.cellMargins ); Insets m = getStyleFromListUI( c, ui -> ui.cellMargins );
if( margins != null ) { if( m != null )
boolean leftToRight = margins.left == margins.right || c.getComponentOrientation().isLeftToRight(); return scaleInsets( c, insets, m.top, m.left, m.bottom, m.right );
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 ); return super.getBorderInsets( c, insets );
} }

View File

@@ -16,7 +16,6 @@
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.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Graphics; import java.awt.Graphics;
@@ -48,15 +47,10 @@ public class FlatTableCellBorder
@Override @Override
public Insets getBorderInsets( Component c, Insets insets ) { public Insets getBorderInsets( Component c, Insets insets ) {
Insets margins = getStyleFromTableUI( c, ui -> ui.cellMargins ); Insets m = getStyleFromTableUI( c, ui -> ui.cellMargins );
if( margins != null ) { if( m != null )
boolean leftToRight = margins.left == margins.right || c.getComponentOrientation().isLeftToRight(); return scaleInsets( c, insets, m.top, m.left, m.bottom, m.right );
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 ); return super.getBorderInsets( c, insets );
} }