mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27:13 -06:00
Button: support for buttons in toolbars
This commit is contained in:
@@ -24,9 +24,11 @@ import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import javax.swing.AbstractButton;
|
||||
import javax.swing.ButtonModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.border.Border;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
import javax.swing.plaf.basic.BasicButtonUI;
|
||||
|
||||
@@ -40,6 +42,8 @@ import javax.swing.plaf.basic.BasicButtonUI;
|
||||
* @uiDefault Button.disabledText Color
|
||||
* @uiDefault Button.default.background Color
|
||||
* @uiDefault Button.default.foreground Color
|
||||
* @uiDefault Button.toolbar.hoverBackground Color
|
||||
* @uiDefault Button.toolbar.pressedBackground Color
|
||||
*
|
||||
* @author Karl Tauber
|
||||
*/
|
||||
@@ -52,6 +56,8 @@ public class FlatButtonUI
|
||||
protected Color disabledText;
|
||||
protected Color defaultBackground;
|
||||
protected Color defaultForeground;
|
||||
protected Color toolbarHoverBackground;
|
||||
protected Color toolbarPressedBackground;
|
||||
|
||||
private static ComponentUI instance;
|
||||
|
||||
@@ -71,6 +77,8 @@ public class FlatButtonUI
|
||||
disabledText = UIManager.getColor( "Button.disabledText" );
|
||||
defaultBackground = UIManager.getColor( "Button.default.background" );
|
||||
defaultForeground = UIManager.getColor( "Button.default.foreground" );
|
||||
toolbarHoverBackground = UIManager.getColor( "Button.toolbar.hoverBackground" );
|
||||
toolbarPressedBackground = UIManager.getColor( "Button.toolbar.pressedBackground" );
|
||||
}
|
||||
|
||||
static boolean isContentAreaFilled( Component c ) {
|
||||
@@ -91,8 +99,9 @@ public class FlatButtonUI
|
||||
try {
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
|
||||
float arc = (c.getBorder() instanceof FlatButtonBorder) ? scale( (float) this.arc ) : 0;
|
||||
Border border = c.getBorder();
|
||||
float focusWidth = (border instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
|
||||
float arc = (border instanceof FlatButtonBorder || FlatUIUtils.isToolBarButton( c )) ? scale( (float) this.arc ) : 0;
|
||||
|
||||
g2.setColor( getBackground( c ) );
|
||||
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc );
|
||||
@@ -117,7 +126,20 @@ public class FlatButtonUI
|
||||
textRect.y + fm.getAscent() + getTextShiftOffset() );
|
||||
}
|
||||
|
||||
private Color getBackground( Component c ) {
|
||||
private Color getBackground( JComponent c ) {
|
||||
ButtonModel model = ((AbstractButton)c).getModel();
|
||||
|
||||
// toolbar button
|
||||
if( FlatUIUtils.isToolBarButton( c ) ) {
|
||||
if( model.isPressed() )
|
||||
return toolbarPressedBackground;
|
||||
if( model.isRollover() )
|
||||
return toolbarHoverBackground;
|
||||
|
||||
// use background of toolbar
|
||||
return c.getParent().getBackground();
|
||||
}
|
||||
|
||||
boolean def = isDefaultButton( c );
|
||||
return def ? defaultBackground : c.getBackground();
|
||||
}
|
||||
|
||||
@@ -17,11 +17,8 @@
|
||||
package com.formdev.flatlaf.ui;
|
||||
|
||||
import static com.formdev.flatlaf.util.UIScale.scale;
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.Insets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
import javax.swing.AbstractButton;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.UIManager;
|
||||
@@ -44,10 +41,6 @@ public class FlatToolBarUI
|
||||
extends BasicToolBarUI
|
||||
{
|
||||
private Border rolloverBorder;
|
||||
private final HashMap<AbstractButton, Color> backgroundTable = new HashMap<>();
|
||||
|
||||
/** Cache non-UIResource button color. */
|
||||
private Color buttonBackground;
|
||||
|
||||
public static ComponentUI createUI( JComponent c ) {
|
||||
return new FlatToolBarUI();
|
||||
@@ -58,7 +51,6 @@ public class FlatToolBarUI
|
||||
super.uninstallUI( c );
|
||||
|
||||
rolloverBorder = null;
|
||||
buttonBackground = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -82,55 +74,6 @@ public class FlatToolBarUI
|
||||
return rolloverBorder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setBorderToRollover( Component c ) {
|
||||
super.setBorderToRollover( c );
|
||||
setToRollover( c );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setBorderToNonRollover( Component c ) {
|
||||
super.setBorderToNonRollover( c );
|
||||
setToRollover( c );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setBorderToNormal( Component c ) {
|
||||
super.setBorderToNormal( c );
|
||||
setToNormal( c );
|
||||
}
|
||||
|
||||
private void setToRollover( Component c ) {
|
||||
if( c instanceof AbstractButton ) {
|
||||
AbstractButton b = (AbstractButton) c;
|
||||
|
||||
Color background = backgroundTable.get( b );
|
||||
if( background == null || background instanceof UIResource )
|
||||
backgroundTable.put( b, b.getBackground() );
|
||||
|
||||
if( b.getBackground() instanceof UIResource )
|
||||
b.setBackground( getButtonBackground() );
|
||||
}
|
||||
}
|
||||
|
||||
private void setToNormal( Component c ) {
|
||||
if( c instanceof AbstractButton ) {
|
||||
AbstractButton b = (AbstractButton) c;
|
||||
|
||||
Color background = backgroundTable.remove( b );
|
||||
b.setBackground( background );
|
||||
}
|
||||
}
|
||||
|
||||
private Color getButtonBackground() {
|
||||
// use toolbar background as button background
|
||||
// must be not an instance of UIResource
|
||||
Color toolBarBackground = toolBar.getBackground();
|
||||
if( !Objects.equals( buttonBackground, toolBarBackground ) )
|
||||
buttonBackground = FlatUIUtils.nonUIResource( toolBarBackground );
|
||||
return buttonBackground;
|
||||
}
|
||||
|
||||
//---- class FlatRolloverMarginBorder -------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.awt.RenderingHints;
|
||||
import java.awt.geom.Path2D;
|
||||
import java.awt.geom.RoundRectangle2D;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JToolBar;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.ColorUIResource;
|
||||
import com.formdev.flatlaf.util.JavaCompatibility;
|
||||
@@ -62,6 +63,10 @@ public class FlatUIUtils
|
||||
return (c instanceof ColorUIResource) ? new Color( c.getRGB(), true ) : c;
|
||||
}
|
||||
|
||||
public static boolean isToolBarButton( JComponent c ) {
|
||||
return c.getParent() instanceof JToolBar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets rendering hints used for painting.
|
||||
*/
|
||||
|
||||
@@ -74,6 +74,9 @@ Button.default.endBorderColor=4c708c
|
||||
Button.default.focusedBorderColor=537699
|
||||
Button.default.focusColor=43688c
|
||||
|
||||
Button.toolbar.hoverBackground=4c5052
|
||||
Button.toolbar.pressedBackground=5c6164
|
||||
|
||||
|
||||
#---- CheckBox ----
|
||||
|
||||
|
||||
@@ -74,6 +74,9 @@ Button.default.endBorderColor=3167ad
|
||||
Button.default.focusedBorderColor=a8cef6
|
||||
Button.default.focusColor=97c3f3
|
||||
|
||||
Button.toolbar.hoverBackground=dfdfdf
|
||||
Button.toolbar.pressedBackground=cfcfcf
|
||||
|
||||
|
||||
#---- CheckBox ----
|
||||
|
||||
|
||||
@@ -58,6 +58,9 @@ Button.default.endBorderColor=ff0000
|
||||
Button.default.focusedBorderColor=537699
|
||||
Button.default.focusColor=ff0000
|
||||
|
||||
Button.toolbar.hoverBackground=ffffff
|
||||
Button.toolbar.pressedBackground=eeeeee
|
||||
|
||||
|
||||
#---- CheckBox ----
|
||||
|
||||
|
||||
Reference in New Issue
Block a user