mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-06 22:10:54 +03:00
Styling: support MenuBar
This commit is contained in:
@@ -32,6 +32,7 @@ import javax.swing.UIManager;
|
||||
import javax.swing.plaf.basic.BasicBorders;
|
||||
import com.formdev.flatlaf.FlatClientProperties;
|
||||
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
|
||||
import com.formdev.flatlaf.ui.FlatStyleSupport.StyleableBorder;
|
||||
import com.formdev.flatlaf.util.DerivedColor;
|
||||
|
||||
/**
|
||||
@@ -61,6 +62,7 @@ import com.formdev.flatlaf.util.DerivedColor;
|
||||
*/
|
||||
public class FlatBorder
|
||||
extends BasicBorders.MarginBorder
|
||||
implements StyleableBorder
|
||||
{
|
||||
@Styleable protected int focusWidth = UIManager.getInt( "Component.focusWidth" );
|
||||
@Styleable protected float innerFocusWidth = FlatUIUtils.getUIFloat( "Component.innerFocusWidth", 0 );
|
||||
@@ -79,6 +81,7 @@ public class FlatBorder
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
@Override
|
||||
public Object applyStyleProperty( String key, Object value ) {
|
||||
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ import java.awt.Graphics;
|
||||
import java.awt.Insets;
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.UIManager;
|
||||
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
|
||||
import com.formdev.flatlaf.ui.FlatStyleSupport.StyleableBorder;
|
||||
|
||||
/**
|
||||
* Border for {@link javax.swing.JMenuBar}.
|
||||
@@ -33,8 +35,17 @@ import javax.swing.UIManager;
|
||||
*/
|
||||
public class FlatMenuBarBorder
|
||||
extends FlatMarginBorder
|
||||
implements StyleableBorder
|
||||
{
|
||||
private final Color borderColor = UIManager.getColor( "MenuBar.borderColor" );
|
||||
@Styleable protected Color borderColor = UIManager.getColor( "MenuBar.borderColor" );
|
||||
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
@Override
|
||||
public Object applyStyleProperty( String key, Object value ) {
|
||||
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
|
||||
|
||||
@@ -20,6 +20,9 @@ import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Window;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import javax.swing.AbstractAction;
|
||||
import javax.swing.ActionMap;
|
||||
import javax.swing.JComponent;
|
||||
@@ -35,6 +38,7 @@ import javax.swing.plaf.ActionMapUIResource;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.UIResource;
|
||||
import javax.swing.plaf.basic.BasicMenuBarUI;
|
||||
import com.formdev.flatlaf.FlatClientProperties;
|
||||
import com.formdev.flatlaf.FlatLaf;
|
||||
import com.formdev.flatlaf.util.SystemInfo;
|
||||
|
||||
@@ -47,6 +51,9 @@ import com.formdev.flatlaf.util.SystemInfo;
|
||||
* @uiDefault MenuBar.background Color
|
||||
* @uiDefault MenuBar.foreground Color
|
||||
* @uiDefault MenuBar.border Border
|
||||
*
|
||||
* <!-- FlatMenuBarUI -->
|
||||
*
|
||||
* @uiDefault TitlePane.unifiedBackground boolean
|
||||
*
|
||||
* @author Karl Tauber
|
||||
@@ -54,6 +61,10 @@ import com.formdev.flatlaf.util.SystemInfo;
|
||||
public class FlatMenuBarUI
|
||||
extends BasicMenuBarUI
|
||||
{
|
||||
private PropertyChangeListener propertyChangeListener;
|
||||
private Map<String, Object> oldStyleValues;
|
||||
private AtomicBoolean borderShared;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatMenuBarUI();
|
||||
}
|
||||
@@ -63,6 +74,13 @@ public class FlatMenuBarUI
|
||||
* Do not add any functionality here.
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void installUI( JComponent c ) {
|
||||
super.installUI( c );
|
||||
|
||||
applyStyle( FlatStyleSupport.getStyle( c ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installDefaults() {
|
||||
super.installDefaults();
|
||||
@@ -70,6 +88,31 @@ public class FlatMenuBarUI
|
||||
LookAndFeel.installProperty( menuBar, "opaque", false );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void uninstallDefaults() {
|
||||
super.uninstallDefaults();
|
||||
|
||||
oldStyleValues = null;
|
||||
borderShared = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installListeners() {
|
||||
super.installListeners();
|
||||
|
||||
propertyChangeListener = FlatStyleSupport.createPropertyChangeListener(
|
||||
menuBar, this::applyStyle, null );
|
||||
menuBar.addPropertyChangeListener( FlatClientProperties.STYLE, propertyChangeListener );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void uninstallListeners() {
|
||||
super.uninstallListeners();
|
||||
|
||||
menuBar.removePropertyChangeListener( FlatClientProperties.STYLE, propertyChangeListener );
|
||||
propertyChangeListener = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void installKeyboardActions() {
|
||||
super.installKeyboardActions();
|
||||
@@ -82,6 +125,22 @@ public class FlatMenuBarUI
|
||||
map.put( "takeFocus", new TakeFocus() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
protected void applyStyle( Object style ) {
|
||||
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
|
||||
}
|
||||
|
||||
/**
|
||||
* @since TODO
|
||||
*/
|
||||
protected Object applyStyleProperty( String key, Object value ) {
|
||||
if( borderShared == null )
|
||||
borderShared = new AtomicBoolean( true );
|
||||
return FlatStyleSupport.applyToAnnotatedObjectOrBorder( this, key, value, menuBar, borderShared );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update( Graphics g, JComponent c ) {
|
||||
// paint background
|
||||
|
||||
@@ -58,6 +58,10 @@ public class FlatStyleSupport
|
||||
boolean dot() default false;
|
||||
}
|
||||
|
||||
public interface StyleableBorder {
|
||||
Object applyStyleProperty( String key, Object value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses styles in CSS syntax ("key1: value1; key2: value2; ..."),
|
||||
* converts the value strings into binary and invokes the given function
|
||||
@@ -284,7 +288,7 @@ public class FlatStyleSupport
|
||||
return applyToAnnotatedObject( obj, key, value );
|
||||
} catch( UnknownStyleException ex ) {
|
||||
Border border = c.getBorder();
|
||||
if( border instanceof FlatBorder ) {
|
||||
if( border instanceof StyleableBorder ) {
|
||||
if( borderShared.get() ) {
|
||||
border = cloneBorder( border );
|
||||
c.setBorder( border );
|
||||
@@ -292,7 +296,7 @@ public class FlatStyleSupport
|
||||
}
|
||||
|
||||
try {
|
||||
return ((FlatBorder)border).applyStyleProperty( key, value );
|
||||
return ((StyleableBorder)border).applyStyleProperty( key, value );
|
||||
} catch( UnknownStyleException ex2 ) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@@ -221,6 +221,15 @@ public class TestFlatStyling
|
||||
ui.applyStyle( "showCellFocusIndicator: true" );
|
||||
}
|
||||
|
||||
@Test
|
||||
void menuBar() {
|
||||
JMenuBar c = new JMenuBar();
|
||||
FlatMenuBarUI ui = (FlatMenuBarUI) c.getUI();
|
||||
|
||||
// FlatMenuBarBorder
|
||||
ui.applyStyle( "borderColor: #fff" );
|
||||
}
|
||||
|
||||
@Test
|
||||
void menu() {
|
||||
JMenu c = new JMenu();
|
||||
|
||||
Reference in New Issue
Block a user