ToggleButton implementation

This commit is contained in:
Karl Tauber
2019-09-05 11:06:32 +02:00
parent cd8f969291
commit 4aa3a52c80
8 changed files with 375 additions and 172 deletions

View File

@@ -71,14 +71,16 @@ public class FlatButtonUI
protected void installDefaults( AbstractButton b ) {
super.installDefaults( b );
focusWidth = UIManager.getInt( "Component.focusWidth" );
arc = UIManager.getInt( "Button.arc" );
String prefix = getPropertyPrefix();
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" );
focusWidth = UIManager.getInt( "Component.focusWidth" );
arc = UIManager.getInt( prefix + "arc" );
disabledText = UIManager.getColor( prefix + "disabledText" );
defaultBackground = UIManager.getColor( prefix + "default.background" );
defaultForeground = UIManager.getColor( prefix + "default.foreground" );
toolbarHoverBackground = UIManager.getColor( prefix + "toolbar.hoverBackground" );
toolbarPressedBackground = UIManager.getColor( prefix + "toolbar.pressedBackground" );
}
static boolean isContentAreaFilled( Component c ) {
@@ -94,7 +96,8 @@ public class FlatButtonUI
if( c.isOpaque() && isContentAreaFilled( c ) ) {
FlatUIUtils.paintParentBackground( g, c );
if( c.isEnabled() ) {
Color background = getBackground( c );
if( background != null ) {
Graphics2D g2 = (Graphics2D) g.create();
try {
FlatUIUtils.setRenderingHints( g2 );
@@ -103,7 +106,7 @@ public class FlatButtonUI
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 ) );
g2.setColor( background );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc );
} finally {
g2.dispose();
@@ -126,7 +129,10 @@ public class FlatButtonUI
textRect.y + fm.getAscent() + getTextShiftOffset() );
}
private Color getBackground( JComponent c ) {
protected Color getBackground( JComponent c ) {
if( !c.isEnabled() )
return null;
ButtonModel model = ((AbstractButton)c).getModel();
// toolbar button
@@ -144,7 +150,7 @@ public class FlatButtonUI
return def ? defaultBackground : c.getBackground();
}
private Color getForeground( Component c ) {
protected Color getForeground( JComponent c ) {
boolean def = isDefaultButton( c );
return def ? defaultForeground : c.getForeground();
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.ui;
import java.awt.Color;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JToggleButton}.
*
* TODO document used UI defaults of superclass
*
* @uiDefault Component.focusWidth int
* @uiDefault ToggleButton.arc int
* @uiDefault ToggleButton.disabledText Color
* @uiDefault ToggleButton.toolbar.hoverBackground Color
* @uiDefault ToggleButton.toolbar.pressedBackground Color
* @uiDefault ToggleButton.selectedBackground Color
* @uiDefault ToggleButton.selectedForeground Color
*
* @author Karl Tauber
*/
public class FlatToggleButtonUI
extends FlatButtonUI
{
protected Color selectedBackground;
protected Color selectedForeground;
protected Color disabledSelectedBackground;
private static ComponentUI instance;
public static ComponentUI createUI( JComponent c ) {
if( instance == null )
instance = new FlatToggleButtonUI();
return instance;
}
@Override
protected String getPropertyPrefix() {
return "ToggleButton.";
}
@Override
protected void installDefaults( AbstractButton b ) {
super.installDefaults( b );
selectedBackground = UIManager.getColor( "ToggleButton.selectedBackground" );
selectedForeground = UIManager.getColor( "ToggleButton.selectedForeground" );
disabledSelectedBackground = UIManager.getColor( "ToggleButton.disabledSelectedBackground" );
}
@Override
protected Color getBackground( JComponent c ) {
ButtonModel model = ((AbstractButton)c).getModel();
if( model.isSelected() ) {
return FlatUIUtils.isToolBarButton( c )
? toolbarPressedBackground
: (c.isEnabled() ? selectedBackground : disabledSelectedBackground);
}
return super.getBackground( c );
}
@Override
protected Color getForeground( JComponent c ) {
ButtonModel model = ((AbstractButton)c).getModel();
if( model.isSelected() && !FlatUIUtils.isToolBarButton( c ) )
return selectedForeground;
return super.getForeground( c );
}
}

View File

@@ -191,6 +191,13 @@ TableHeader.separatorColor=585858
TableHeader.bottomSeparatorColor=585858
#---- ToggleButton ----
ToggleButton.selectedBackground=5c6164
ToggleButton.selectedForeground=@foreground
ToggleButton.disabledSelectedBackground=525658
#---- ToolTip ----
ToolTip.background=4b4d4d

View File

@@ -45,6 +45,7 @@ TableHeaderUI=com.formdev.flatlaf.ui.FlatTableHeaderUI
TextAreaUI=com.formdev.flatlaf.ui.FlatTextAreaUI
TextFieldUI=com.formdev.flatlaf.ui.FlatTextFieldUI
TextPaneUI=com.formdev.flatlaf.ui.FlatTextPaneUI
ToggleButtonUI=com.formdev.flatlaf.ui.FlatToggleButtonUI
ToolBarUI=com.formdev.flatlaf.ui.FlatToolBarUI
ToolBarSeparatorUI=com.formdev.flatlaf.ui.FlatToolBarSeparatorUI
ToolTipUI=javax.swing.plaf.basic.BasicToolTipUI
@@ -261,6 +262,22 @@ TextPane.background=@textComponentBackground
TextPane.margin=@textComponentMargin
#---- ToggleButton ----
ToggleButton.border=com.formdev.flatlaf.ui.FlatButtonBorder
ToggleButton.arc=6
ToggleButton.background=@@Button.background
ToggleButton.startBorderColor=@@Button.startBorderColor
ToggleButton.endBorderColor=@@Button.endBorderColor
ToggleButton.disabledBorderColor=@@Button.disabledBorderColor
ToggleButton.focusedBorderColor=@@Button.focusedBorderColor
ToggleButton.toolbar.hoverBackground=@@Button.toolbar.hoverBackground
ToggleButton.toolbar.pressedBackground=@@Button.toolbar.pressedBackground
#---- ToolBar ----
ToolBar.border=com.formdev.flatlaf.ui.FlatToolBarBorder

View File

@@ -191,6 +191,13 @@ TableHeader.separatorColor=e5e5e5
TableHeader.bottomSeparatorColor=e5e5e5
#---- ToggleButton ----
ToggleButton.selectedBackground=cfcfcf
ToggleButton.selectedForeground=@foreground
ToggleButton.disabledSelectedBackground=dfdfdf
#---- ToolTip ----
ToolTip.background=f7f7f7