Button and ToggleButton: support square button style

This commit is contained in:
Karl Tauber
2020-01-08 13:57:42 +01:00
parent 8e84112837
commit af7c181596
10 changed files with 116 additions and 13 deletions

View File

@@ -27,14 +27,25 @@ public interface FlatClientProperties
/**
* Specifies type of a button.
* <p>
* <strong>Component</strong> {@link javax.swing.JButton}<br>
* <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_HELP}
* <strong>Allowed Values</strong> {@link #BUTTON_TYPE_SQUARE} and {@link #BUTTON_TYPE_HELP}
*/
String BUTTON_TYPE = "JButton.buttonType";
/**
* Paint the button with square edges.
* <p>
* <strong>Components</strong> {@link javax.swing.JButton} and {@link javax.swing.JToggleButton}
*
* @see #BUTTON_TYPE
*/
String BUTTON_TYPE_SQUARE = "square";
/**
* Paint a help button (circle with question mark).
* <p>
* <strong>Components</strong> {@link javax.swing.JButton}
*
* @see #BUTTON_TYPE
*/
@@ -45,7 +56,7 @@ public interface FlatClientProperties
* <p>
* <strong>Component</strong> {@link javax.swing.JCheckBox}<br>
* <strong>Value type</strong> {@link java.lang.String}<br>
* <strong>Allowed Values</strong> {@link SELECTED_STATE_INDETERMINATE}
* <strong>Allowed Values</strong> {@link #SELECTED_STATE_INDETERMINATE}
*/
String SELECTED_STATE = "JButton.selectedState";

View File

@@ -75,7 +75,7 @@ public class FlatBorder
boolean isCellEditor = isTableCellEditor( c );
float focusWidth = isCellEditor ? 0 : getFocusWidth();
float borderWidth = getBorderWidth( c );
float arc = isCellEditor ? 0 : getArc();
float arc = isCellEditor ? 0 : getArc( c );
if( isFocused( c ) ) {
g2.setColor( getFocusColor( c ) );
@@ -173,7 +173,7 @@ public class FlatBorder
return getLineWidth();
}
protected float getArc() {
protected float getArc( Component c ) {
return 0;
}
}

View File

@@ -110,7 +110,7 @@ public class FlatButtonBorder
}
@Override
protected float getArc() {
return scale( (float) arc );
protected float getArc( Component c ) {
return FlatButtonUI.isSquareButton( c ) ? 0 : scale( (float) arc );
}
}

View File

@@ -209,6 +209,10 @@ 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 );
}
static boolean isHelpButton( Component c ) {
return c instanceof JButton && clientPropertyEquals( (JButton) c, BUTTON_TYPE, BUTTON_TYPE_HELP );
}
@@ -237,7 +241,8 @@ public class FlatButtonUI
Border border = c.getBorder();
float focusWidth = (border instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
float arc = (border instanceof FlatButtonBorder || isToolBarButton( c )) ? scale( (float) this.arc ) : 0;
float arc = ((border instanceof FlatButtonBorder && !isSquareButton( c )) || isToolBarButton( c ))
? scale( (float) this.arc ) : 0;
boolean def = isDefaultButton( c );
// paint shadow

View File

@@ -17,6 +17,7 @@
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Component;
import javax.swing.UIManager;
/**
@@ -32,7 +33,7 @@ public class FlatRoundBorder
protected final int arc = UIManager.getInt( "Component.arc" );
@Override
protected float getArc() {
protected float getArc( Component c ) {
return scale( (float) arc );
}
}