mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-13 23:37:13 -06:00
Styling: support ToolBar and ToolBar.Separator
This commit is contained in:
@@ -29,7 +29,7 @@ import javax.swing.plaf.basic.BasicBorders;
|
|||||||
public class FlatMarginBorder
|
public class FlatMarginBorder
|
||||||
extends BasicBorders.MarginBorder
|
extends BasicBorders.MarginBorder
|
||||||
{
|
{
|
||||||
private final int left, right, top, bottom;
|
protected int left, right, top, bottom;
|
||||||
|
|
||||||
public FlatMarginBorder() {
|
public FlatMarginBorder() {
|
||||||
left = right = top = bottom = 0;
|
left = right = top = bottom = 0;
|
||||||
|
|||||||
@@ -22,9 +22,11 @@ import java.awt.Graphics;
|
|||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
|
import java.util.function.Function;
|
||||||
import javax.swing.JToolBar;
|
import javax.swing.JToolBar;
|
||||||
import javax.swing.SwingConstants;
|
import javax.swing.SwingConstants;
|
||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
|
import javax.swing.plaf.ToolBarUI;
|
||||||
import com.formdev.flatlaf.util.UIScale;
|
import com.formdev.flatlaf.util.UIScale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,7 +44,7 @@ public class FlatToolBarBorder
|
|||||||
private static final int DOT_SIZE = 2;
|
private static final int DOT_SIZE = 2;
|
||||||
private static final int GRIP_SIZE = DOT_SIZE * 3;
|
private static final int GRIP_SIZE = DOT_SIZE * 3;
|
||||||
|
|
||||||
protected final Color gripColor = UIManager.getColor( "ToolBar.gripColor" );
|
protected Color gripColor = UIManager.getColor( "ToolBar.gripColor" );
|
||||||
|
|
||||||
public FlatToolBarBorder() {
|
public FlatToolBarBorder() {
|
||||||
super( UIManager.getInsets( "ToolBar.borderMargins" ) );
|
super( UIManager.getInsets( "ToolBar.borderMargins" ) );
|
||||||
@@ -56,7 +58,8 @@ public class FlatToolBarBorder
|
|||||||
try {
|
try {
|
||||||
FlatUIUtils.setRenderingHints( g2 );
|
FlatUIUtils.setRenderingHints( g2 );
|
||||||
|
|
||||||
g2.setColor( gripColor );
|
Color color = getStyleFromToolBarUI( c, ui -> ui.gripColor );
|
||||||
|
g2.setColor( (color != null) ? color : gripColor );
|
||||||
paintGrip( c, g2, x, y, width, height );
|
paintGrip( c, g2, x, y, width, height );
|
||||||
} finally {
|
} finally {
|
||||||
g2.dispose();
|
g2.dispose();
|
||||||
@@ -90,7 +93,14 @@ public class FlatToolBarBorder
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Insets getBorderInsets( Component c, Insets insets ) {
|
public Insets getBorderInsets( Component c, Insets insets ) {
|
||||||
insets = super.getBorderInsets( c, insets );
|
Insets m = getStyleFromToolBarUI( c, ui -> ui.borderMargins );
|
||||||
|
if( m != null ) {
|
||||||
|
int t = top, l = left, b = bottom, r = right;
|
||||||
|
top = m.top; left = m.left; bottom = m.bottom; right = m.right;
|
||||||
|
insets = super.getBorderInsets( c, insets );
|
||||||
|
top = t; left = l; bottom = b; right = r;
|
||||||
|
} else
|
||||||
|
insets = super.getBorderInsets( c, insets );
|
||||||
|
|
||||||
// add grip inset if floatable
|
// add grip inset if floatable
|
||||||
if( c instanceof JToolBar && ((JToolBar)c).isFloatable() ) {
|
if( c instanceof JToolBar && ((JToolBar)c).isFloatable() ) {
|
||||||
@@ -106,4 +116,17 @@ public class FlatToolBarBorder
|
|||||||
|
|
||||||
return insets;
|
return insets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Because this border is shared for all toolbars,
|
||||||
|
* get border specific style from FlatToolBarUI.
|
||||||
|
*/
|
||||||
|
static <T> T getStyleFromToolBarUI( Component c, Function<FlatToolBarUI, T> f ) {
|
||||||
|
if( c instanceof JToolBar ) {
|
||||||
|
ToolBarUI ui = ((JToolBar)c).getUI();
|
||||||
|
if( ui instanceof FlatToolBarUI )
|
||||||
|
return f.apply( (FlatToolBarUI) ui );
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ import java.awt.Dimension;
|
|||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.util.Map;
|
||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
import javax.swing.JSeparator;
|
import javax.swing.JSeparator;
|
||||||
import javax.swing.JToolBar;
|
import javax.swing.JToolBar;
|
||||||
@@ -29,6 +31,8 @@ import javax.swing.SwingConstants;
|
|||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
import javax.swing.plaf.ComponentUI;
|
import javax.swing.plaf.ComponentUI;
|
||||||
import javax.swing.plaf.basic.BasicToolBarSeparatorUI;
|
import javax.swing.plaf.basic.BasicToolBarSeparatorUI;
|
||||||
|
import com.formdev.flatlaf.FlatClientProperties;
|
||||||
|
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides the Flat LaF UI delegate for {@link javax.swing.JToolBar.Separator}.
|
* Provides the Flat LaF UI delegate for {@link javax.swing.JToolBar.Separator}.
|
||||||
@@ -45,13 +49,32 @@ public class FlatToolBarSeparatorUI
|
|||||||
{
|
{
|
||||||
private static final int LINE_WIDTH = 1;
|
private static final int LINE_WIDTH = 1;
|
||||||
|
|
||||||
protected int separatorWidth;
|
@Styleable protected int separatorWidth;
|
||||||
protected Color separatorColor;
|
@Styleable protected Color separatorColor;
|
||||||
|
|
||||||
|
private final boolean shared;
|
||||||
private boolean defaults_initialized = false;
|
private boolean defaults_initialized = false;
|
||||||
|
private PropertyChangeListener propertyChangeListener;
|
||||||
|
private Map<String, Object> oldStyleValues;
|
||||||
|
|
||||||
public static ComponentUI createUI( JComponent c ) {
|
public static ComponentUI createUI( JComponent c ) {
|
||||||
return FlatUIUtils.createSharedUI( FlatToolBarSeparatorUI.class, FlatToolBarSeparatorUI::new );
|
return FlatUIUtils.canUseSharedUI( c )
|
||||||
|
? FlatUIUtils.createSharedUI( FlatToolBarSeparatorUI.class, () -> new FlatToolBarSeparatorUI( true ) )
|
||||||
|
: new FlatToolBarSeparatorUI( false );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since TODO
|
||||||
|
*/
|
||||||
|
protected FlatToolBarSeparatorUI( boolean shared ) {
|
||||||
|
this.shared = shared;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void installUI( JComponent c ) {
|
||||||
|
super.installUI( c );
|
||||||
|
|
||||||
|
applyStyle( FlatStyleSupport.getStyle( c ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -73,7 +96,52 @@ public class FlatToolBarSeparatorUI
|
|||||||
@Override
|
@Override
|
||||||
protected void uninstallDefaults( JSeparator s ) {
|
protected void uninstallDefaults( JSeparator s ) {
|
||||||
super.uninstallDefaults( s );
|
super.uninstallDefaults( s );
|
||||||
|
|
||||||
defaults_initialized = false;
|
defaults_initialized = false;
|
||||||
|
oldStyleValues = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void installListeners( JSeparator s ) {
|
||||||
|
super.installListeners( s );
|
||||||
|
|
||||||
|
propertyChangeListener = FlatStyleSupport.createPropertyChangeListener(
|
||||||
|
s, style -> applyStyle( s, this, style ), null );
|
||||||
|
s.addPropertyChangeListener( FlatClientProperties.STYLE, propertyChangeListener );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void uninstallListeners( JSeparator s ) {
|
||||||
|
super.uninstallListeners( s );
|
||||||
|
|
||||||
|
s.removePropertyChangeListener( FlatClientProperties.STYLE, propertyChangeListener );
|
||||||
|
propertyChangeListener = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void applyStyle( JSeparator s, FlatToolBarSeparatorUI ui, Object style ) {
|
||||||
|
// unshare component UI if necessary
|
||||||
|
if( style != null && ui.shared ) {
|
||||||
|
s.updateUI();
|
||||||
|
ui = (FlatToolBarSeparatorUI) s.getUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.applyStyle( style );
|
||||||
|
s.revalidate();
|
||||||
|
s.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since TODO
|
||||||
|
*/
|
||||||
|
protected void applyStyle( Object style ) {
|
||||||
|
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since TODO
|
||||||
|
*/
|
||||||
|
protected Object applyStyleProperty( String key, Object value ) {
|
||||||
|
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -16,15 +16,19 @@
|
|||||||
|
|
||||||
package com.formdev.flatlaf.ui;
|
package com.formdev.flatlaf.ui;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.event.ContainerEvent;
|
import java.awt.event.ContainerEvent;
|
||||||
import java.awt.event.ContainerListener;
|
import java.awt.event.ContainerListener;
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.util.Map;
|
||||||
import javax.swing.AbstractButton;
|
import javax.swing.AbstractButton;
|
||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
import javax.swing.border.Border;
|
import javax.swing.border.Border;
|
||||||
import javax.swing.plaf.ComponentUI;
|
import javax.swing.plaf.ComponentUI;
|
||||||
import javax.swing.plaf.basic.BasicToolBarUI;
|
import javax.swing.plaf.basic.BasicToolBarUI;
|
||||||
|
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides the Flat LaF UI delegate for {@link javax.swing.JToolBar}.
|
* Provides the Flat LaF UI delegate for {@link javax.swing.JToolBar}.
|
||||||
@@ -41,15 +45,40 @@ import javax.swing.plaf.basic.BasicToolBarUI;
|
|||||||
* @uiDefault ToolBar.floatingForeground Color
|
* @uiDefault ToolBar.floatingForeground Color
|
||||||
* @uiDefault ToolBar.isRollover boolean
|
* @uiDefault ToolBar.isRollover boolean
|
||||||
*
|
*
|
||||||
|
* <!-- FlatToolBarBorder -->
|
||||||
|
*
|
||||||
|
* @uiDefault ToolBar.borderMargins Insets
|
||||||
|
* @uiDefault ToolBar.gripColor Color
|
||||||
|
*
|
||||||
* @author Karl Tauber
|
* @author Karl Tauber
|
||||||
*/
|
*/
|
||||||
public class FlatToolBarUI
|
public class FlatToolBarUI
|
||||||
extends BasicToolBarUI
|
extends BasicToolBarUI
|
||||||
{
|
{
|
||||||
|
// for FlatToolBarBorder
|
||||||
|
@Styleable protected Insets borderMargins;
|
||||||
|
@Styleable protected Color gripColor;
|
||||||
|
|
||||||
|
private Map<String, Object> oldStyleValues;
|
||||||
|
|
||||||
public static ComponentUI createUI( JComponent c ) {
|
public static ComponentUI createUI( JComponent c ) {
|
||||||
return new FlatToolBarUI();
|
return new FlatToolBarUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void installUI( JComponent c ) {
|
||||||
|
super.installUI( c );
|
||||||
|
|
||||||
|
applyStyle( FlatStyleSupport.getStyle( c ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void uninstallUI( JComponent c ) {
|
||||||
|
super.uninstallUI( c );
|
||||||
|
|
||||||
|
oldStyleValues = null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ContainerListener createToolBarContListener() {
|
protected ContainerListener createToolBarContListener() {
|
||||||
return new ToolBarContListener() {
|
return new ToolBarContListener() {
|
||||||
@@ -73,6 +102,25 @@ public class FlatToolBarUI
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected PropertyChangeListener createPropertyListener() {
|
||||||
|
return FlatStyleSupport.createPropertyChangeListener( toolBar, this::applyStyle, super.createPropertyListener() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since TODO
|
||||||
|
*/
|
||||||
|
protected void applyStyle( Object style ) {
|
||||||
|
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since TODO
|
||||||
|
*/
|
||||||
|
protected Object applyStyleProperty( String key, Object value ) {
|
||||||
|
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
|
||||||
|
}
|
||||||
|
|
||||||
// disable rollover border
|
// disable rollover border
|
||||||
@Override protected void setBorderToRollover( Component c ) {}
|
@Override protected void setBorderToRollover( Component c ) {}
|
||||||
@Override protected void setBorderToNonRollover( Component c ) {}
|
@Override protected void setBorderToNonRollover( Component c ) {}
|
||||||
|
|||||||
@@ -585,6 +585,22 @@ public class FlatStylingTests
|
|||||||
ui.applyStyle( b, "tab.focusBackground: #fff" );
|
ui.applyStyle( b, "tab.focusBackground: #fff" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toolBar() {
|
||||||
|
FlatToolBarUI ui = new FlatToolBarUI();
|
||||||
|
|
||||||
|
ui.applyStyle( "borderMargins: 1,2,3,4" );
|
||||||
|
ui.applyStyle( "gripColor: #fff" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toolBarSeparator() {
|
||||||
|
FlatToolBarSeparatorUI ui = new FlatToolBarSeparatorUI( false );
|
||||||
|
|
||||||
|
ui.applyStyle( "separatorWidth: 6" );
|
||||||
|
ui.applyStyle( "separatorColor: #fff" );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void tree() {
|
void tree() {
|
||||||
FlatTreeUI ui = new FlatTreeUI();
|
FlatTreeUI ui = new FlatTreeUI();
|
||||||
|
|||||||
Reference in New Issue
Block a user