mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27: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
|
||||
extends BasicBorders.MarginBorder
|
||||
{
|
||||
private final int left, right, top, bottom;
|
||||
protected int left, right, top, bottom;
|
||||
|
||||
public FlatMarginBorder() {
|
||||
left = right = top = bottom = 0;
|
||||
|
||||
@@ -22,9 +22,11 @@ import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Insets;
|
||||
import java.awt.Rectangle;
|
||||
import java.util.function.Function;
|
||||
import javax.swing.JToolBar;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ToolBarUI;
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
|
||||
/**
|
||||
@@ -42,7 +44,7 @@ public class FlatToolBarBorder
|
||||
private static final int DOT_SIZE = 2;
|
||||
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() {
|
||||
super( UIManager.getInsets( "ToolBar.borderMargins" ) );
|
||||
@@ -56,7 +58,8 @@ public class FlatToolBarBorder
|
||||
try {
|
||||
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 );
|
||||
} finally {
|
||||
g2.dispose();
|
||||
@@ -90,7 +93,14 @@ public class FlatToolBarBorder
|
||||
|
||||
@Override
|
||||
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
|
||||
if( c instanceof JToolBar && ((JToolBar)c).isFloatable() ) {
|
||||
@@ -106,4 +116,17 @@ public class FlatToolBarBorder
|
||||
|
||||
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.Graphics2D;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.util.Map;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JSeparator;
|
||||
import javax.swing.JToolBar;
|
||||
@@ -29,6 +31,8 @@ import javax.swing.SwingConstants;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
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}.
|
||||
@@ -45,13 +49,32 @@ public class FlatToolBarSeparatorUI
|
||||
{
|
||||
private static final int LINE_WIDTH = 1;
|
||||
|
||||
protected int separatorWidth;
|
||||
protected Color separatorColor;
|
||||
@Styleable protected int separatorWidth;
|
||||
@Styleable protected Color separatorColor;
|
||||
|
||||
private final boolean shared;
|
||||
private boolean defaults_initialized = false;
|
||||
private PropertyChangeListener propertyChangeListener;
|
||||
private Map<String, Object> oldStyleValues;
|
||||
|
||||
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
|
||||
@@ -73,7 +96,52 @@ public class FlatToolBarSeparatorUI
|
||||
@Override
|
||||
protected void uninstallDefaults( JSeparator s ) {
|
||||
super.uninstallDefaults( s );
|
||||
|
||||
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
|
||||
|
||||
@@ -16,15 +16,19 @@
|
||||
|
||||
package com.formdev.flatlaf.ui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.ContainerEvent;
|
||||
import java.awt.event.ContainerListener;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.util.Map;
|
||||
import javax.swing.AbstractButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.border.Border;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicToolBarUI;
|
||||
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
|
||||
|
||||
/**
|
||||
* 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.isRollover boolean
|
||||
*
|
||||
* <!-- FlatToolBarBorder -->
|
||||
*
|
||||
* @uiDefault ToolBar.borderMargins Insets
|
||||
* @uiDefault ToolBar.gripColor Color
|
||||
*
|
||||
* @author Karl Tauber
|
||||
*/
|
||||
public class FlatToolBarUI
|
||||
extends BasicToolBarUI
|
||||
{
|
||||
// for FlatToolBarBorder
|
||||
@Styleable protected Insets borderMargins;
|
||||
@Styleable protected Color gripColor;
|
||||
|
||||
private Map<String, Object> oldStyleValues;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
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
|
||||
protected ContainerListener createToolBarContListener() {
|
||||
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
|
||||
@Override protected void setBorderToRollover( Component c ) {}
|
||||
@Override protected void setBorderToNonRollover( Component c ) {}
|
||||
|
||||
@@ -585,6 +585,22 @@ public class FlatStylingTests
|
||||
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
|
||||
void tree() {
|
||||
FlatTreeUI ui = new FlatTreeUI();
|
||||
|
||||
Reference in New Issue
Block a user