mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-12 15:07:11 -06:00
Button and ToggleButton: support round button style (set client property JButton.buttonType to roundRect)
This commit is contained in:
@@ -30,7 +30,8 @@ public interface FlatClientProperties
|
||||
* <p>
|
||||
* <strong>Components</strong> {@link javax.swing.JButton} and {@link javax.swing.JToggleButton}<br>
|
||||
* <strong>Value type</strong> {@link java.lang.String}<br>
|
||||
* <strong>Allowed Values</strong> {@link #BUTTON_TYPE_SQUARE} and {@link #BUTTON_TYPE_HELP}
|
||||
* <strong>Allowed Values</strong> {@link #BUTTON_TYPE_SQUARE}, {@link #BUTTON_TYPE_ROUND_RECT},
|
||||
* {@link #BUTTON_TYPE_TAB} and {@link #BUTTON_TYPE_HELP}
|
||||
*/
|
||||
String BUTTON_TYPE = "JButton.buttonType";
|
||||
|
||||
@@ -43,6 +44,15 @@ public interface FlatClientProperties
|
||||
*/
|
||||
String BUTTON_TYPE_SQUARE = "square";
|
||||
|
||||
/**
|
||||
* Paint the button with round edges.
|
||||
* <p>
|
||||
* <strong>Components</strong> {@link javax.swing.JButton} and {@link javax.swing.JToggleButton}
|
||||
*
|
||||
* @see #BUTTON_TYPE
|
||||
*/
|
||||
String BUTTON_TYPE_ROUND_RECT = "roundRect";
|
||||
|
||||
/**
|
||||
* Paint the toggle button in tab style.
|
||||
* <p>
|
||||
@@ -240,4 +250,14 @@ public interface FlatClientProperties
|
||||
Object value = c.getClientProperty( key );
|
||||
return (value instanceof Color) ? (Color) value : defaultValue;
|
||||
}
|
||||
|
||||
static int clientPropertyChoice( JComponent c, String key, String... choices ) {
|
||||
Object value = c.getClientProperty( key );
|
||||
for( int i = 0; i < choices.length; i++ ) {
|
||||
if( choices[i].equals( value ) )
|
||||
return i;
|
||||
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ public class FlatButtonBorder
|
||||
|
||||
@Override
|
||||
protected float getFocusWidth( Component c ) {
|
||||
return FlatToggleButtonUI.isTabButton( c ) ? 0 : super.getFocusWidth(c );
|
||||
return FlatToggleButtonUI.isTabButton( c ) ? 0 : super.getFocusWidth( c );
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -135,6 +135,10 @@ public class FlatButtonBorder
|
||||
|
||||
@Override
|
||||
protected float getArc( Component c ) {
|
||||
return FlatButtonUI.isSquareButton( c ) ? 0 : scale( (float) arc );
|
||||
switch( FlatButtonUI.getButtonType( c ) ) {
|
||||
case FlatButtonUI.TYPE_SQUARE: return 0;
|
||||
case FlatButtonUI.TYPE_ROUND_RECT: return Float.MAX_VALUE;
|
||||
default: return scale( (float) arc );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,8 +236,15 @@ public class FlatButtonUI
|
||||
(icon == null && text != null && ("...".equals( text ) || text.length() == 1));
|
||||
}
|
||||
|
||||
static boolean isSquareButton( Component c ) {
|
||||
return c instanceof AbstractButton && clientPropertyEquals( (AbstractButton) c, BUTTON_TYPE, BUTTON_TYPE_SQUARE );
|
||||
// same indices as in parameters to clientPropertyChoice()
|
||||
static final int TYPE_OTHER = -1;
|
||||
static final int TYPE_SQUARE = 0;
|
||||
static final int TYPE_ROUND_RECT = 1;
|
||||
|
||||
static int getButtonType( Component c ) {
|
||||
return (c instanceof AbstractButton)
|
||||
? clientPropertyChoice( (AbstractButton) c, BUTTON_TYPE, BUTTON_TYPE_SQUARE, BUTTON_TYPE_ROUND_RECT )
|
||||
: TYPE_OTHER;
|
||||
}
|
||||
|
||||
static boolean isHelpButton( Component c ) {
|
||||
@@ -273,10 +280,20 @@ public class FlatButtonUI
|
||||
FlatUIUtils.setRenderingHints( g2 );
|
||||
|
||||
Border border = c.getBorder();
|
||||
int buttonType = FlatButtonUI.getButtonType( c );
|
||||
boolean isToolBarButton = isToolBarButton( c );
|
||||
float focusWidth = (border instanceof FlatBorder && !isToolBarButton) ? scale( (float) getFocusWidth( c ) ) : 0;
|
||||
float arc = ((border instanceof FlatButtonBorder && !isSquareButton( c )) || isToolBarButton)
|
||||
? scale( (float) this.arc ) : 0;
|
||||
float arc;
|
||||
|
||||
if( buttonType == TYPE_SQUARE )
|
||||
arc = 0;
|
||||
else if( buttonType == TYPE_ROUND_RECT )
|
||||
arc = Float.MAX_VALUE;
|
||||
else if( border instanceof FlatButtonBorder || isToolBarButton )
|
||||
arc = scale( (float) this.arc );
|
||||
else
|
||||
arc = 0;
|
||||
|
||||
boolean def = isDefaultButton( c );
|
||||
|
||||
int x = 0;
|
||||
@@ -401,7 +418,7 @@ public class FlatButtonUI
|
||||
return new Dimension( helpButtonIcon.getIconWidth(), helpButtonIcon.getIconHeight() );
|
||||
|
||||
Dimension prefSize = super.getPreferredSize( c );
|
||||
if ( prefSize == null )
|
||||
if( prefSize == null )
|
||||
return null;
|
||||
|
||||
// make button square if it is a icon-only button
|
||||
|
||||
Reference in New Issue
Block a user