Extras: added extension classes for JButton and JToggleButton (issue #117)

This commit is contained in:
Karl Tauber
2020-12-11 17:18:35 +01:00
parent 821efaff40
commit 511a4044d7
6 changed files with 466 additions and 111 deletions

View File

@@ -0,0 +1,94 @@
/*
* Copyright 2020 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
*
* https://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.extras.components;
import static com.formdev.flatlaf.FlatClientProperties.*;
import javax.swing.JButton;
/**
* Subclass of {@link JButton} that provides easy access to FlatLaf specific client properties.
*
* @author Karl Tauber
*/
public class FlatButton
extends JButton
implements FlatComponentExtension
{
// NOTE: enum names must be equal to allowed strings
public enum ButtonType { none, square, roundRect, tab, help, toolBarButton };
/**
* Returns type of a button.
*/
public ButtonType getButtonType() {
return getClientPropertyEnumString( BUTTON_TYPE, ButtonType.class, null, ButtonType.none );
}
/**
* Specifies type of a button.
*/
public void setButtonType( ButtonType buttonType ) {
if( buttonType == ButtonType.none )
buttonType = null;
putClientPropertyEnumString( BUTTON_TYPE, buttonType );
}
/**
* Returns whether the button preferred size will be made square (quadratically).
*/
public boolean isSquareSize() {
return getClientPropertyBoolean( SQUARE_SIZE, false );
}
/**
* Specifies whether the button preferred size will be made square (quadratically).
*/
public void setSquareSize( boolean squareSize ) {
putClientPropertyBoolean( SQUARE_SIZE, squareSize, false );
}
/**
* Returns minimum width of a component.
*/
public int getMinimumWidth() {
return getClientPropertyInt( MINIMUM_WIDTH, "Button.minimumWidth" );
}
/**
* Specifies minimum width of a component.
*/
public void setMinimumWidth( int minimumWidth ) {
putClientProperty( MINIMUM_WIDTH, (minimumWidth >= 0) ? minimumWidth : null );
}
/**
* Returns minimum height of a component.
*/
public int getMinimumHeight() {
return getClientPropertyInt( MINIMUM_HEIGHT, 0 );
}
/**
* Specifies minimum height of a component.
*/
public void setMinimumHeight( int minimumHeight ) {
putClientProperty( MINIMUM_HEIGHT, (minimumHeight >= 0) ? minimumHeight : null );
}
}

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.extras.components; package com.formdev.flatlaf.extras.components;
import java.awt.Color;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.UIManager; import javax.swing.UIManager;
@@ -53,6 +54,23 @@ public interface FlatComponentExtension
} }
default int getClientPropertyInt( Object key, String defaultValueKey ) {
Object value = getClientProperty( key );
return (value instanceof Integer) ? (int) value : UIManager.getInt( defaultValueKey );
}
default int getClientPropertyInt( Object key, int defaultValue ) {
Object value = getClientProperty( key );
return (value instanceof Integer) ? (int) value : defaultValue;
}
default Color getClientPropertyColor( Object key, String defaultValueKey ) {
Object value = getClientProperty( key );
return (value instanceof Color) ? (Color) value : UIManager.getColor( defaultValueKey );
}
default <T extends Enum<T>> T getClientPropertyEnumString( Object key, Class<T> enumType, default <T extends Enum<T>> T getClientPropertyEnumString( Object key, Class<T> enumType,
String defaultValueKey, T defaultValue ) String defaultValueKey, T defaultValue )
{ {

View File

@@ -0,0 +1,138 @@
/*
* Copyright 2020 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
*
* https://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.extras.components;
import static com.formdev.flatlaf.FlatClientProperties.*;
import java.awt.Color;
import javax.swing.JToggleButton;
import com.formdev.flatlaf.extras.components.FlatButton.ButtonType;
/**
* Subclass of {@link JToggleButton} that provides easy access to FlatLaf specific client properties.
*
* @author Karl Tauber
*/
public class FlatToggleButton
extends JToggleButton
implements FlatComponentExtension
{
/**
* Returns type of a button.
*/
public ButtonType getButtonType() {
return getClientPropertyEnumString( BUTTON_TYPE, ButtonType.class, null, ButtonType.none );
}
/**
* Specifies type of a button.
*/
public void setButtonType( ButtonType buttonType ) {
if( buttonType == ButtonType.none )
buttonType = null;
putClientPropertyEnumString( BUTTON_TYPE, buttonType );
}
/**
* Returns whether the button preferred size will be made square (quadratically).
*/
public boolean isSquareSize() {
return getClientPropertyBoolean( SQUARE_SIZE, false );
}
/**
* Specifies whether the button preferred size will be made square (quadratically).
*/
public void setSquareSize( boolean squareSize ) {
putClientPropertyBoolean( SQUARE_SIZE, squareSize, false );
}
/**
* Returns minimum width of a component.
*/
public int getMinimumWidth() {
return getClientPropertyInt( MINIMUM_WIDTH, "ToggleButton.minimumWidth" );
}
/**
* Specifies minimum width of a component.
*/
public void setMinimumWidth( int minimumWidth ) {
putClientProperty( MINIMUM_WIDTH, (minimumWidth >= 0) ? minimumWidth : null );
}
/**
* Returns minimum height of a component.
*/
public int getMinimumHeight() {
return getClientPropertyInt( MINIMUM_HEIGHT, 0 );
}
/**
* Specifies minimum height of a component.
*/
public void setMinimumHeight( int minimumHeight ) {
putClientProperty( MINIMUM_HEIGHT, (minimumHeight >= 0) ? minimumHeight : null );
}
/**
* Returns height of underline if toggle button type is {@link ButtonType#tab}.
*/
public int getTabUnderlineHeight() {
return getClientPropertyInt( TAB_BUTTON_UNDERLINE_HEIGHT, "ToggleButton.tab.underlineHeight" );
}
/**
* Specifies height of underline if toggle button type is {@link ButtonType#tab}.
*/
public void setTabUnderlineHeight( int tabUnderlineHeight ) {
putClientProperty( TAB_BUTTON_UNDERLINE_HEIGHT, (tabUnderlineHeight >= 0) ? tabUnderlineHeight : null );
}
/**
* Returns color of underline if toggle button type is {@link ButtonType#tab}.
*/
public Color getTabUnderlineColor() {
return getClientPropertyColor( TAB_BUTTON_UNDERLINE_COLOR, "ToggleButton.tab.underlineColor" );
}
/**
* Specifies color of underline if toggle button type is {@link ButtonType#tab}.
*/
public void setTabUnderlineColor( Color tabUnderlineColor ) {
putClientProperty( TAB_BUTTON_UNDERLINE_COLOR, tabUnderlineColor );
}
/**
* Returns background color if selected and toggle button type is {@link ButtonType#tab}.
*/
public Color getTabSelectedBackground() {
return getClientPropertyColor( TAB_BUTTON_SELECTED_BACKGROUND, "ToggleButton.tab.selectedBackground" );
}
/**
* Specifies background color if selected and toggle button type is {@link ButtonType#tab}.
*/
public void setTabSelectedBackground( Color tabSelectedBackground ) {
putClientProperty( TAB_BUTTON_SELECTED_BACKGROUND, tabSelectedBackground );
}
}

View File

@@ -21,6 +21,7 @@ import javax.swing.*;
import javax.swing.border.*; import javax.swing.border.*;
import com.formdev.flatlaf.FlatClientProperties; import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.extras.components.*; import com.formdev.flatlaf.extras.components.*;
import com.formdev.flatlaf.extras.components.FlatButton.ButtonType;
import net.miginfocom.swing.*; import net.miginfocom.swing.*;
/** /**
@@ -38,6 +39,8 @@ public class FlatComponentsTest
FlatComponentsTest() { FlatComponentsTest() {
initComponents(); initComponents();
buttonTypeComboBox.init( ButtonType.class, true );
} }
private void changeProgress() { private void changeProgress() {
@@ -116,13 +119,16 @@ public class FlatComponentsTest
} }
private void buttonTypeChanged() { private void buttonTypeChanged() {
String buttonType = (String) buttonTypeComboBox.getSelectedItem(); ButtonType buttonType = buttonTypeComboBox.getSelectedValue();
if( "-".equals( buttonType ) ) String buttonTypeStr = (buttonType != null && buttonType != ButtonType.none) ? buttonType.toString() : null;
buttonType = null;
for( Component c : getComponents() ) { for( Component c : getComponents() ) {
if( c instanceof AbstractButton ) if( c instanceof FlatButton )
((AbstractButton)c).putClientProperty( FlatClientProperties.BUTTON_TYPE, buttonType ); ((FlatButton)c).setButtonType( buttonType );
else if( c instanceof FlatToggleButton )
((FlatToggleButton)c).setButtonType( buttonType );
else if( c instanceof AbstractButton )
((AbstractButton)c).putClientProperty( FlatClientProperties.BUTTON_TYPE, buttonTypeStr );
} }
} }
@@ -154,14 +160,14 @@ public class FlatComponentsTest
FlatComponentsTest.TestMultiLineLabel testMultiLineLabel1 = new FlatComponentsTest.TestMultiLineLabel(); FlatComponentsTest.TestMultiLineLabel testMultiLineLabel1 = new FlatComponentsTest.TestMultiLineLabel();
JLabel buttonLabel = new JLabel(); JLabel buttonLabel = new JLabel();
JButton button1 = new JButton(); JButton button1 = new JButton();
JButton button17 = new JButton(); FlatButton button17 = new FlatButton();
JButton button22 = new JButton(); FlatButton button22 = new FlatButton();
JButton button2 = new JButton(); JButton button2 = new JButton();
JButton button18 = new JButton(); FlatButton button18 = new FlatButton();
JButton button23 = new JButton(); FlatButton button23 = new FlatButton();
FlatComponentsTest.TestDefaultButton button5 = new FlatComponentsTest.TestDefaultButton(); FlatComponentsTest.TestDefaultButton button5 = new FlatComponentsTest.TestDefaultButton();
JButton button3 = new JButton(); FlatButton button3 = new FlatButton();
JButton button12 = new JButton(); FlatButton button12 = new FlatButton();
JButton button13 = new JButton(); JButton button13 = new JButton();
JButton button14 = new JButton(); JButton button14 = new JButton();
JButton button15 = new JButton(); JButton button15 = new JButton();
@@ -169,11 +175,11 @@ public class FlatComponentsTest
JButton button20 = new JButton(); JButton button20 = new JButton();
JLabel toggleButtonLabel = new JLabel(); JLabel toggleButtonLabel = new JLabel();
JToggleButton toggleButton1 = new JToggleButton(); JToggleButton toggleButton1 = new JToggleButton();
JToggleButton toggleButton9 = new JToggleButton(); FlatToggleButton toggleButton9 = new FlatToggleButton();
JToggleButton toggleButton19 = new JToggleButton(); FlatToggleButton toggleButton19 = new FlatToggleButton();
JToggleButton toggleButton2 = new JToggleButton(); JToggleButton toggleButton2 = new JToggleButton();
JToggleButton toggleButton10 = new JToggleButton(); FlatToggleButton toggleButton10 = new FlatToggleButton();
JToggleButton toggleButton20 = new JToggleButton(); FlatToggleButton toggleButton20 = new FlatToggleButton();
JToggleButton toggleButton3 = new JToggleButton(); JToggleButton toggleButton3 = new JToggleButton();
JToggleButton toggleButton4 = new JToggleButton(); JToggleButton toggleButton4 = new JToggleButton();
JToggleButton toggleButton11 = new JToggleButton(); JToggleButton toggleButton11 = new JToggleButton();
@@ -186,8 +192,8 @@ public class FlatComponentsTest
JCheckBox checkBox2 = new JCheckBox(); JCheckBox checkBox2 = new JCheckBox();
JCheckBox checkBox3 = new JCheckBox(); JCheckBox checkBox3 = new JCheckBox();
JCheckBox checkBox4 = new JCheckBox(); JCheckBox checkBox4 = new JCheckBox();
JToggleButton toggleButton5 = new JToggleButton(); FlatToggleButton toggleButton5 = new FlatToggleButton();
JToggleButton toggleButton8 = new JToggleButton(); FlatToggleButton toggleButton8 = new FlatToggleButton();
JLabel radioButtonLabel = new JLabel(); JLabel radioButtonLabel = new JLabel();
JRadioButton radioButton1 = new JRadioButton(); JRadioButton radioButton1 = new JRadioButton();
JRadioButton radioButton2 = new JRadioButton(); JRadioButton radioButton2 = new JRadioButton();
@@ -281,7 +287,7 @@ public class FlatComponentsTest
JPanel panel3 = new JPanel(); JPanel panel3 = new JPanel();
JButton button21 = new JButton(); JButton button21 = new JButton();
JPanel panel5 = new JPanel(); JPanel panel5 = new JPanel();
buttonTypeComboBox = new JComboBox<>(); buttonTypeComboBox = new FlatTestEnumComboBox<>();
borderPaintedCheckBox = new JCheckBox(); borderPaintedCheckBox = new JCheckBox();
roundRectCheckBox = new JCheckBox(); roundRectCheckBox = new JCheckBox();
contentAreaFilledCheckBox = new JCheckBox(); contentAreaFilledCheckBox = new JCheckBox();
@@ -327,16 +333,16 @@ public class FlatComponentsTest
JToggleButton toggleButton17 = new JToggleButton(); JToggleButton toggleButton17 = new JToggleButton();
JLabel label3 = new JLabel(); JLabel label3 = new JLabel();
JToolBar toolBar3 = new JToolBar(); JToolBar toolBar3 = new JToolBar();
JButton button26 = new JButton(); FlatButton button26 = new FlatButton();
JButton button27 = new JButton(); FlatButton button27 = new FlatButton();
JToggleButton toggleButton23 = new JToggleButton(); FlatToggleButton toggleButton23 = new FlatToggleButton();
JToggleButton toggleButton24 = new JToggleButton(); FlatToggleButton toggleButton24 = new FlatToggleButton();
JLabel label4 = new JLabel(); JLabel label4 = new JLabel();
JToolBar toolBar4 = new JToolBar(); JToolBar toolBar4 = new JToolBar();
JButton button28 = new JButton(); FlatButton button28 = new FlatButton();
JButton button29 = new JButton(); FlatButton button29 = new FlatButton();
JToggleButton toggleButton25 = new JToggleButton(); FlatToggleButton toggleButton25 = new FlatToggleButton();
JToggleButton toggleButton26 = new JToggleButton(); FlatToggleButton toggleButton26 = new FlatToggleButton();
//======== this ======== //======== this ========
setLayout(new MigLayout( setLayout(new MigLayout(
@@ -406,14 +412,14 @@ public class FlatComponentsTest
//---- button17 ---- //---- button17 ----
button17.setText("Sq"); button17.setText("Sq");
button17.putClientProperty("JButton.buttonType", "square"); button17.setButtonType(FlatButton.ButtonType.square);
button17.putClientProperty("JComponent.minimumWidth", 0); button17.setMinimumWidth(0);
add(button17, "cell 1 1"); add(button17, "cell 1 1");
//---- button22 ---- //---- button22 ----
button22.setText("Rd"); button22.setText("Rd");
button22.putClientProperty("JButton.buttonType", "roundRect"); button22.setButtonType(FlatButton.ButtonType.roundRect);
button22.putClientProperty("JComponent.minimumWidth", 0); button22.setMinimumWidth(0);
add(button22, "cell 1 1"); add(button22, "cell 1 1");
//---- button2 ---- //---- button2 ----
@@ -425,16 +431,16 @@ public class FlatComponentsTest
//---- button18 ---- //---- button18 ----
button18.setText("Sq"); button18.setText("Sq");
button18.putClientProperty("JButton.buttonType", "square"); button18.setButtonType(FlatButton.ButtonType.square);
button18.setEnabled(false); button18.setEnabled(false);
button18.putClientProperty("JComponent.minimumWidth", 0); button18.setMinimumWidth(0);
add(button18, "cell 2 1"); add(button18, "cell 2 1");
//---- button23 ---- //---- button23 ----
button23.setText("Rd"); button23.setText("Rd");
button23.putClientProperty("JButton.buttonType", "roundRect"); button23.setButtonType(FlatButton.ButtonType.roundRect);
button23.setEnabled(false); button23.setEnabled(false);
button23.putClientProperty("JComponent.minimumWidth", 0); button23.setMinimumWidth(0);
add(button23, "cell 2 1"); add(button23, "cell 2 1");
//---- button5 ---- //---- button5 ----
@@ -445,12 +451,12 @@ public class FlatComponentsTest
//---- button3 ---- //---- button3 ----
button3.setText("Help"); button3.setText("Help");
button3.putClientProperty("JButton.buttonType", "help"); button3.setButtonType(FlatButton.ButtonType.help);
add(button3, "cell 4 1"); add(button3, "cell 4 1");
//---- button12 ---- //---- button12 ----
button12.setText("Help"); button12.setText("Help");
button12.putClientProperty("JButton.buttonType", "help"); button12.setButtonType(FlatButton.ButtonType.help);
button12.setEnabled(false); button12.setEnabled(false);
add(button12, "cell 4 1"); add(button12, "cell 4 1");
@@ -485,12 +491,12 @@ public class FlatComponentsTest
//---- toggleButton9 ---- //---- toggleButton9 ----
toggleButton9.setText("Sq"); toggleButton9.setText("Sq");
toggleButton9.putClientProperty("JButton.buttonType", "square"); toggleButton9.setButtonType(FlatButton.ButtonType.square);
add(toggleButton9, "cell 1 2"); add(toggleButton9, "cell 1 2");
//---- toggleButton19 ---- //---- toggleButton19 ----
toggleButton19.setText("Rd"); toggleButton19.setText("Rd");
toggleButton19.putClientProperty("JButton.buttonType", "roundRect"); toggleButton19.setButtonType(FlatButton.ButtonType.roundRect);
add(toggleButton19, "cell 1 2"); add(toggleButton19, "cell 1 2");
//---- toggleButton2 ---- //---- toggleButton2 ----
@@ -500,13 +506,13 @@ public class FlatComponentsTest
//---- toggleButton10 ---- //---- toggleButton10 ----
toggleButton10.setText("Sq"); toggleButton10.setText("Sq");
toggleButton10.putClientProperty("JButton.buttonType", "square"); toggleButton10.setButtonType(FlatButton.ButtonType.square);
toggleButton10.setEnabled(false); toggleButton10.setEnabled(false);
add(toggleButton10, "cell 2 2"); add(toggleButton10, "cell 2 2");
//---- toggleButton20 ---- //---- toggleButton20 ----
toggleButton20.setText("Rd"); toggleButton20.setText("Rd");
toggleButton20.putClientProperty("JButton.buttonType", "roundRect"); toggleButton20.setButtonType(FlatButton.ButtonType.roundRect);
toggleButton20.setEnabled(false); toggleButton20.setEnabled(false);
add(toggleButton20, "cell 2 2"); add(toggleButton20, "cell 2 2");
@@ -574,13 +580,13 @@ public class FlatComponentsTest
//---- toggleButton5 ---- //---- toggleButton5 ----
toggleButton5.setText("Tab"); toggleButton5.setText("Tab");
toggleButton5.putClientProperty("JButton.buttonType", "tab"); toggleButton5.setButtonType(FlatButton.ButtonType.tab);
toggleButton5.setSelected(true); toggleButton5.setSelected(true);
add(toggleButton5, "cell 5 3"); add(toggleButton5, "cell 5 3");
//---- toggleButton8 ---- //---- toggleButton8 ----
toggleButton8.setText("Tab"); toggleButton8.setText("Tab");
toggleButton8.putClientProperty("JButton.buttonType", "tab"); toggleButton8.setButtonType(FlatButton.ButtonType.tab);
toggleButton8.setEnabled(false); toggleButton8.setEnabled(false);
toggleButton8.setSelected(true); toggleButton8.setSelected(true);
add(toggleButton8, "cell 5 3"); add(toggleButton8, "cell 5 3");
@@ -1110,13 +1116,6 @@ public class FlatComponentsTest
"[]")); "[]"));
//---- buttonTypeComboBox ---- //---- buttonTypeComboBox ----
buttonTypeComboBox.setModel(new DefaultComboBoxModel<>(new String[] {
"-",
"square",
"roundRect",
"tab",
"help"
}));
buttonTypeComboBox.addActionListener(e -> buttonTypeChanged()); buttonTypeComboBox.addActionListener(e -> buttonTypeChanged());
panel5.add(buttonTypeComboBox, "cell 0 0"); panel5.add(buttonTypeComboBox, "cell 0 0");
@@ -1347,24 +1346,24 @@ public class FlatComponentsTest
//---- button26 ---- //---- button26 ----
button26.setIcon(UIManager.getIcon("Tree.closedIcon")); button26.setIcon(UIManager.getIcon("Tree.closedIcon"));
button26.putClientProperty("JButton.buttonType", "square"); button26.setButtonType(FlatButton.ButtonType.square);
toolBar3.add(button26); toolBar3.add(button26);
//---- button27 ---- //---- button27 ----
button27.setIcon(UIManager.getIcon("Tree.openIcon")); button27.setIcon(UIManager.getIcon("Tree.openIcon"));
button27.putClientProperty("JButton.buttonType", "square"); button27.setButtonType(FlatButton.ButtonType.square);
toolBar3.add(button27); toolBar3.add(button27);
//---- toggleButton23 ---- //---- toggleButton23 ----
toggleButton23.setIcon(UIManager.getIcon("FileView.computerIcon")); toggleButton23.setIcon(UIManager.getIcon("FileView.computerIcon"));
toggleButton23.setSelected(true); toggleButton23.setSelected(true);
toggleButton23.putClientProperty("JButton.buttonType", "square"); toggleButton23.setButtonType(FlatButton.ButtonType.square);
toolBar3.add(toggleButton23); toolBar3.add(toggleButton23);
//---- toggleButton24 ---- //---- toggleButton24 ----
toggleButton24.setIcon(UIManager.getIcon("FileView.floppyDriveIcon")); toggleButton24.setIcon(UIManager.getIcon("FileView.floppyDriveIcon"));
toggleButton24.setSelected(true); toggleButton24.setSelected(true);
toggleButton24.putClientProperty("JButton.buttonType", "square"); toggleButton24.setButtonType(FlatButton.ButtonType.square);
toolBar3.add(toggleButton24); toolBar3.add(toggleButton24);
} }
add(toolBar3, "cell 1 23 5 1"); add(toolBar3, "cell 1 23 5 1");
@@ -1378,24 +1377,24 @@ public class FlatComponentsTest
//---- button28 ---- //---- button28 ----
button28.setIcon(UIManager.getIcon("Tree.closedIcon")); button28.setIcon(UIManager.getIcon("Tree.closedIcon"));
button28.putClientProperty("JButton.buttonType", "roundRect"); button28.setButtonType(FlatButton.ButtonType.roundRect);
toolBar4.add(button28); toolBar4.add(button28);
//---- button29 ---- //---- button29 ----
button29.setIcon(UIManager.getIcon("Tree.openIcon")); button29.setIcon(UIManager.getIcon("Tree.openIcon"));
button29.putClientProperty("JButton.buttonType", "roundRect"); button29.setButtonType(FlatButton.ButtonType.roundRect);
toolBar4.add(button29); toolBar4.add(button29);
//---- toggleButton25 ---- //---- toggleButton25 ----
toggleButton25.setIcon(UIManager.getIcon("FileView.computerIcon")); toggleButton25.setIcon(UIManager.getIcon("FileView.computerIcon"));
toggleButton25.setSelected(true); toggleButton25.setSelected(true);
toggleButton25.putClientProperty("JButton.buttonType", "roundRect"); toggleButton25.setButtonType(FlatButton.ButtonType.roundRect);
toolBar4.add(toggleButton25); toolBar4.add(toggleButton25);
//---- toggleButton26 ---- //---- toggleButton26 ----
toggleButton26.setIcon(UIManager.getIcon("FileView.floppyDriveIcon")); toggleButton26.setIcon(UIManager.getIcon("FileView.floppyDriveIcon"));
toggleButton26.setSelected(true); toggleButton26.setSelected(true);
toggleButton26.putClientProperty("JButton.buttonType", "roundRect"); toggleButton26.setButtonType(FlatButton.ButtonType.roundRect);
toolBar4.add(toggleButton26); toolBar4.add(toggleButton26);
} }
add(toolBar4, "cell 1 23 5 1"); add(toolBar4, "cell 1 23 5 1");
@@ -1419,7 +1418,7 @@ public class FlatComponentsTest
private JTextField textField1; private JTextField textField1;
private FlatProgressBar progressBar3; private FlatProgressBar progressBar3;
private FlatProgressBar progressBar4; private FlatProgressBar progressBar4;
private JComboBox<String> buttonTypeComboBox; private FlatTestEnumComboBox<ButtonType> buttonTypeComboBox;
private JCheckBox borderPaintedCheckBox; private JCheckBox borderPaintedCheckBox;
private JCheckBox roundRectCheckBox; private JCheckBox roundRectCheckBox;
private JCheckBox contentAreaFilledCheckBox; private JCheckBox contentAreaFilledCheckBox;

View File

@@ -53,19 +53,19 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 1" "value": "cell 1 1"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button17" name: "button17"
"text": "Sq" "text": "Sq"
"$client.JButton.buttonType": "square" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square
"$client.JComponent.minimumWidth": 0 "minimumWidth": 0
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 1" "value": "cell 1 1"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button22" name: "button22"
"text": "Rd" "text": "Rd"
"$client.JButton.buttonType": "roundRect" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect
"$client.JComponent.minimumWidth": 0 "minimumWidth": 0
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 1" "value": "cell 1 1"
} ) } )
@@ -78,21 +78,21 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 1" "value": "cell 2 1"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button18" name: "button18"
"text": "Sq" "text": "Sq"
"$client.JButton.buttonType": "square" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square
"enabled": false "enabled": false
"$client.JComponent.minimumWidth": 0 "minimumWidth": 0
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 1" "value": "cell 2 1"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button23" name: "button23"
"text": "Rd" "text": "Rd"
"$client.JButton.buttonType": "roundRect" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect
"enabled": false "enabled": false
"$client.JComponent.minimumWidth": 0 "minimumWidth": 0
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 1" "value": "cell 2 1"
} ) } )
@@ -104,17 +104,17 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 1" "value": "cell 3 1"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button3" name: "button3"
"text": "Help" "text": "Help"
"$client.JButton.buttonType": "help" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType help
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 1" "value": "cell 4 1"
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button12" name: "button12"
"text": "Help" "text": "Help"
"$client.JButton.buttonType": "help" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType help
"enabled": false "enabled": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 1" "value": "cell 4 1"
@@ -162,17 +162,17 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2" "value": "cell 1 2"
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) {
name: "toggleButton9" name: "toggleButton9"
"text": "Sq" "text": "Sq"
"$client.JButton.buttonType": "square" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2" "value": "cell 1 2"
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) {
name: "toggleButton19" name: "toggleButton19"
"text": "Rd" "text": "Rd"
"$client.JButton.buttonType": "roundRect" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2" "value": "cell 1 2"
} ) } )
@@ -183,18 +183,18 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 2" "value": "cell 2 2"
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) {
name: "toggleButton10" name: "toggleButton10"
"text": "Sq" "text": "Sq"
"$client.JButton.buttonType": "square" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square
"enabled": false "enabled": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 2" "value": "cell 2 2"
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) {
name: "toggleButton20" name: "toggleButton20"
"text": "Rd" "text": "Rd"
"$client.JButton.buttonType": "roundRect" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect
"enabled": false "enabled": false
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 2" "value": "cell 2 2"
@@ -285,18 +285,18 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 3" "value": "cell 4 3"
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) {
name: "toggleButton5" name: "toggleButton5"
"text": "Tab" "text": "Tab"
"$client.JButton.buttonType": "tab" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType tab
"selected": true "selected": true
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 3" "value": "cell 5 3"
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) {
name: "toggleButton8" name: "toggleButton8"
"text": "Tab" "text": "Tab"
"$client.JButton.buttonType": "tab" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType tab
"enabled": false "enabled": false
"selected": true "selected": true
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
@@ -963,18 +963,11 @@ new FormModel {
} ) { } ) {
name: "panel5" name: "panel5"
"border": new javax.swing.border.TitledBorder( "Control" ) "border": new javax.swing.border.TitledBorder( "Control" )
add( new FormComponent( "javax.swing.JComboBox" ) { add( new FormComponent( "com.formdev.flatlaf.testing.FlatTestEnumComboBox" ) {
name: "buttonTypeComboBox" name: "buttonTypeComboBox"
"model": new javax.swing.DefaultComboBoxModel {
selectedItem: "-"
addElement( "-" )
addElement( "square" )
addElement( "roundRect" )
addElement( "tab" )
addElement( "help" )
}
auxiliary() { auxiliary() {
"JavaCodeGenerator.variableLocal": false "JavaCodeGenerator.variableLocal": false
"JavaCodeGenerator.typeParameters": "ButtonType"
} }
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "buttonTypeChanged", false ) ) addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "buttonTypeChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
@@ -1317,27 +1310,27 @@ new FormModel {
} ) } )
add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) { add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
name: "toolBar3" name: "toolBar3"
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button26" name: "button26"
"icon": #SwingIcon1 "icon": #SwingIcon1
"$client.JButton.buttonType": "square" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button27" name: "button27"
"icon": #SwingIcon2 "icon": #SwingIcon2
"$client.JButton.buttonType": "square" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) {
name: "toggleButton23" name: "toggleButton23"
"icon": #SwingIcon4 "icon": #SwingIcon4
"selected": true "selected": true
"$client.JButton.buttonType": "square" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) {
name: "toggleButton24" name: "toggleButton24"
"icon": #SwingIcon5 "icon": #SwingIcon5
"selected": true "selected": true
"$client.JButton.buttonType": "square" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square
} ) } )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 23 5 1" "value": "cell 1 23 5 1"
@@ -1350,27 +1343,27 @@ new FormModel {
} ) } )
add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) { add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
name: "toolBar4" name: "toolBar4"
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button28" name: "button28"
"icon": #SwingIcon1 "icon": #SwingIcon1
"$client.JButton.buttonType": "roundRect" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect
} ) } )
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) {
name: "button29" name: "button29"
"icon": #SwingIcon2 "icon": #SwingIcon2
"$client.JButton.buttonType": "roundRect" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) {
name: "toggleButton25" name: "toggleButton25"
"icon": #SwingIcon4 "icon": #SwingIcon4
"selected": true "selected": true
"$client.JButton.buttonType": "roundRect" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect
} ) } )
add( new FormComponent( "javax.swing.JToggleButton" ) { add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) {
name: "toggleButton26" name: "toggleButton26"
"icon": #SwingIcon5 "icon": #SwingIcon5
"selected": true "selected": true
"$client.JButton.buttonType": "roundRect" "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect
} ) } )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 23 5 1" "value": "cell 1 23 5 1"

View File

@@ -0,0 +1,113 @@
/*
* Copyright 2020 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
*
* https://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.testing;
import java.awt.Component;
import java.util.Objects;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
/**
* @author Karl Tauber
*/
public class FlatTestEnumComboBox<E>
extends JComboBox<E>
{
@SuppressWarnings( "unchecked" )
public void init( Class<E> enumType, boolean supportDefault ) {
setRenderer( new EnumComboBoxRenderer() );
setModel( new EnumComboBoxModel<>( enumType, supportDefault ) );
}
@SuppressWarnings( "unchecked" )
public E getSelectedValue() {
return (E) getSelectedItem();
}
@Override
public int getSelectedIndex() {
if( getSelectedItem() == null && getItemCount() > 0 && getItemAt( 0 ) == null )
return 0;
return super.getSelectedIndex();
}
//---- class EnumComboBoxRenderer -----------------------------------------
public static class EnumComboBoxRenderer
extends BasicComboBoxRenderer
{
@SuppressWarnings( "rawtypes" )
@Override
public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) {
setEnabled( value != null || isSelected );
if( value == null )
value = "(default)";
return super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
}
}
//---- class EnumComboBoxModel --------------------------------------------
public static class EnumComboBoxModel<E>
extends AbstractListModel<E>
implements ComboBoxModel<E>
{
private final boolean supportDefault;
private final E[] values;
private Object selectedItem;
public EnumComboBoxModel( Class<E> enumType, boolean supportDefault ) {
this.supportDefault = supportDefault;
values = enumType.getEnumConstants();
if( !supportDefault )
selectedItem = values[0];
}
@Override
public int getSize() {
return values.length + (supportDefault ? 1 : 0);
}
@Override
public E getElementAt( int index ) {
if( supportDefault ) {
if( index == 0 )
return null;
index--;
}
return values[index];
}
@Override
public Object getSelectedItem() {
return selectedItem;
}
@Override
public void setSelectedItem( Object anItem ) {
if( !Objects.equals( selectedItem, anItem ) ) {
selectedItem = anItem;
fireContentsChanged( this, -1, -1 );
}
}
}
}