diff --git a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/components/FlatButton.java b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/components/FlatButton.java new file mode 100644 index 00000000..d76eba16 --- /dev/null +++ b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/components/FlatButton.java @@ -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 ); + } +} diff --git a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/components/FlatComponentExtension.java b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/components/FlatComponentExtension.java index e488137b..139ea768 100644 --- a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/components/FlatComponentExtension.java +++ b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/components/FlatComponentExtension.java @@ -16,6 +16,7 @@ package com.formdev.flatlaf.extras.components; +import java.awt.Color; import javax.swing.JComponent; 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 getClientPropertyEnumString( Object key, Class enumType, String defaultValueKey, T defaultValue ) { diff --git a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/components/FlatToggleButton.java b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/components/FlatToggleButton.java new file mode 100644 index 00000000..3bccdd6e --- /dev/null +++ b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/components/FlatToggleButton.java @@ -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 ); + } +} diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java index 41198240..e696fa17 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.java @@ -21,6 +21,7 @@ import javax.swing.*; import javax.swing.border.*; import com.formdev.flatlaf.FlatClientProperties; import com.formdev.flatlaf.extras.components.*; +import com.formdev.flatlaf.extras.components.FlatButton.ButtonType; import net.miginfocom.swing.*; /** @@ -38,6 +39,8 @@ public class FlatComponentsTest FlatComponentsTest() { initComponents(); + + buttonTypeComboBox.init( ButtonType.class, true ); } private void changeProgress() { @@ -116,13 +119,16 @@ public class FlatComponentsTest } private void buttonTypeChanged() { - String buttonType = (String) buttonTypeComboBox.getSelectedItem(); - if( "-".equals( buttonType ) ) - buttonType = null; + ButtonType buttonType = buttonTypeComboBox.getSelectedValue(); + String buttonTypeStr = (buttonType != null && buttonType != ButtonType.none) ? buttonType.toString() : null; for( Component c : getComponents() ) { - if( c instanceof AbstractButton ) - ((AbstractButton)c).putClientProperty( FlatClientProperties.BUTTON_TYPE, buttonType ); + if( c instanceof FlatButton ) + ((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(); JLabel buttonLabel = new JLabel(); JButton button1 = new JButton(); - JButton button17 = new JButton(); - JButton button22 = new JButton(); + FlatButton button17 = new FlatButton(); + FlatButton button22 = new FlatButton(); JButton button2 = new JButton(); - JButton button18 = new JButton(); - JButton button23 = new JButton(); + FlatButton button18 = new FlatButton(); + FlatButton button23 = new FlatButton(); FlatComponentsTest.TestDefaultButton button5 = new FlatComponentsTest.TestDefaultButton(); - JButton button3 = new JButton(); - JButton button12 = new JButton(); + FlatButton button3 = new FlatButton(); + FlatButton button12 = new FlatButton(); JButton button13 = new JButton(); JButton button14 = new JButton(); JButton button15 = new JButton(); @@ -169,11 +175,11 @@ public class FlatComponentsTest JButton button20 = new JButton(); JLabel toggleButtonLabel = new JLabel(); JToggleButton toggleButton1 = new JToggleButton(); - JToggleButton toggleButton9 = new JToggleButton(); - JToggleButton toggleButton19 = new JToggleButton(); + FlatToggleButton toggleButton9 = new FlatToggleButton(); + FlatToggleButton toggleButton19 = new FlatToggleButton(); JToggleButton toggleButton2 = new JToggleButton(); - JToggleButton toggleButton10 = new JToggleButton(); - JToggleButton toggleButton20 = new JToggleButton(); + FlatToggleButton toggleButton10 = new FlatToggleButton(); + FlatToggleButton toggleButton20 = new FlatToggleButton(); JToggleButton toggleButton3 = new JToggleButton(); JToggleButton toggleButton4 = new JToggleButton(); JToggleButton toggleButton11 = new JToggleButton(); @@ -186,8 +192,8 @@ public class FlatComponentsTest JCheckBox checkBox2 = new JCheckBox(); JCheckBox checkBox3 = new JCheckBox(); JCheckBox checkBox4 = new JCheckBox(); - JToggleButton toggleButton5 = new JToggleButton(); - JToggleButton toggleButton8 = new JToggleButton(); + FlatToggleButton toggleButton5 = new FlatToggleButton(); + FlatToggleButton toggleButton8 = new FlatToggleButton(); JLabel radioButtonLabel = new JLabel(); JRadioButton radioButton1 = new JRadioButton(); JRadioButton radioButton2 = new JRadioButton(); @@ -281,7 +287,7 @@ public class FlatComponentsTest JPanel panel3 = new JPanel(); JButton button21 = new JButton(); JPanel panel5 = new JPanel(); - buttonTypeComboBox = new JComboBox<>(); + buttonTypeComboBox = new FlatTestEnumComboBox<>(); borderPaintedCheckBox = new JCheckBox(); roundRectCheckBox = new JCheckBox(); contentAreaFilledCheckBox = new JCheckBox(); @@ -327,16 +333,16 @@ public class FlatComponentsTest JToggleButton toggleButton17 = new JToggleButton(); JLabel label3 = new JLabel(); JToolBar toolBar3 = new JToolBar(); - JButton button26 = new JButton(); - JButton button27 = new JButton(); - JToggleButton toggleButton23 = new JToggleButton(); - JToggleButton toggleButton24 = new JToggleButton(); + FlatButton button26 = new FlatButton(); + FlatButton button27 = new FlatButton(); + FlatToggleButton toggleButton23 = new FlatToggleButton(); + FlatToggleButton toggleButton24 = new FlatToggleButton(); JLabel label4 = new JLabel(); JToolBar toolBar4 = new JToolBar(); - JButton button28 = new JButton(); - JButton button29 = new JButton(); - JToggleButton toggleButton25 = new JToggleButton(); - JToggleButton toggleButton26 = new JToggleButton(); + FlatButton button28 = new FlatButton(); + FlatButton button29 = new FlatButton(); + FlatToggleButton toggleButton25 = new FlatToggleButton(); + FlatToggleButton toggleButton26 = new FlatToggleButton(); //======== this ======== setLayout(new MigLayout( @@ -406,14 +412,14 @@ public class FlatComponentsTest //---- button17 ---- button17.setText("Sq"); - button17.putClientProperty("JButton.buttonType", "square"); - button17.putClientProperty("JComponent.minimumWidth", 0); + button17.setButtonType(FlatButton.ButtonType.square); + button17.setMinimumWidth(0); add(button17, "cell 1 1"); //---- button22 ---- button22.setText("Rd"); - button22.putClientProperty("JButton.buttonType", "roundRect"); - button22.putClientProperty("JComponent.minimumWidth", 0); + button22.setButtonType(FlatButton.ButtonType.roundRect); + button22.setMinimumWidth(0); add(button22, "cell 1 1"); //---- button2 ---- @@ -425,16 +431,16 @@ public class FlatComponentsTest //---- button18 ---- button18.setText("Sq"); - button18.putClientProperty("JButton.buttonType", "square"); + button18.setButtonType(FlatButton.ButtonType.square); button18.setEnabled(false); - button18.putClientProperty("JComponent.minimumWidth", 0); + button18.setMinimumWidth(0); add(button18, "cell 2 1"); //---- button23 ---- button23.setText("Rd"); - button23.putClientProperty("JButton.buttonType", "roundRect"); + button23.setButtonType(FlatButton.ButtonType.roundRect); button23.setEnabled(false); - button23.putClientProperty("JComponent.minimumWidth", 0); + button23.setMinimumWidth(0); add(button23, "cell 2 1"); //---- button5 ---- @@ -445,12 +451,12 @@ public class FlatComponentsTest //---- button3 ---- button3.setText("Help"); - button3.putClientProperty("JButton.buttonType", "help"); + button3.setButtonType(FlatButton.ButtonType.help); add(button3, "cell 4 1"); //---- button12 ---- button12.setText("Help"); - button12.putClientProperty("JButton.buttonType", "help"); + button12.setButtonType(FlatButton.ButtonType.help); button12.setEnabled(false); add(button12, "cell 4 1"); @@ -485,12 +491,12 @@ public class FlatComponentsTest //---- toggleButton9 ---- toggleButton9.setText("Sq"); - toggleButton9.putClientProperty("JButton.buttonType", "square"); + toggleButton9.setButtonType(FlatButton.ButtonType.square); add(toggleButton9, "cell 1 2"); //---- toggleButton19 ---- toggleButton19.setText("Rd"); - toggleButton19.putClientProperty("JButton.buttonType", "roundRect"); + toggleButton19.setButtonType(FlatButton.ButtonType.roundRect); add(toggleButton19, "cell 1 2"); //---- toggleButton2 ---- @@ -500,13 +506,13 @@ public class FlatComponentsTest //---- toggleButton10 ---- toggleButton10.setText("Sq"); - toggleButton10.putClientProperty("JButton.buttonType", "square"); + toggleButton10.setButtonType(FlatButton.ButtonType.square); toggleButton10.setEnabled(false); add(toggleButton10, "cell 2 2"); //---- toggleButton20 ---- toggleButton20.setText("Rd"); - toggleButton20.putClientProperty("JButton.buttonType", "roundRect"); + toggleButton20.setButtonType(FlatButton.ButtonType.roundRect); toggleButton20.setEnabled(false); add(toggleButton20, "cell 2 2"); @@ -574,13 +580,13 @@ public class FlatComponentsTest //---- toggleButton5 ---- toggleButton5.setText("Tab"); - toggleButton5.putClientProperty("JButton.buttonType", "tab"); + toggleButton5.setButtonType(FlatButton.ButtonType.tab); toggleButton5.setSelected(true); add(toggleButton5, "cell 5 3"); //---- toggleButton8 ---- toggleButton8.setText("Tab"); - toggleButton8.putClientProperty("JButton.buttonType", "tab"); + toggleButton8.setButtonType(FlatButton.ButtonType.tab); toggleButton8.setEnabled(false); toggleButton8.setSelected(true); add(toggleButton8, "cell 5 3"); @@ -1110,13 +1116,6 @@ public class FlatComponentsTest "[]")); //---- buttonTypeComboBox ---- - buttonTypeComboBox.setModel(new DefaultComboBoxModel<>(new String[] { - "-", - "square", - "roundRect", - "tab", - "help" - })); buttonTypeComboBox.addActionListener(e -> buttonTypeChanged()); panel5.add(buttonTypeComboBox, "cell 0 0"); @@ -1347,24 +1346,24 @@ public class FlatComponentsTest //---- button26 ---- button26.setIcon(UIManager.getIcon("Tree.closedIcon")); - button26.putClientProperty("JButton.buttonType", "square"); + button26.setButtonType(FlatButton.ButtonType.square); toolBar3.add(button26); //---- button27 ---- button27.setIcon(UIManager.getIcon("Tree.openIcon")); - button27.putClientProperty("JButton.buttonType", "square"); + button27.setButtonType(FlatButton.ButtonType.square); toolBar3.add(button27); //---- toggleButton23 ---- toggleButton23.setIcon(UIManager.getIcon("FileView.computerIcon")); toggleButton23.setSelected(true); - toggleButton23.putClientProperty("JButton.buttonType", "square"); + toggleButton23.setButtonType(FlatButton.ButtonType.square); toolBar3.add(toggleButton23); //---- toggleButton24 ---- toggleButton24.setIcon(UIManager.getIcon("FileView.floppyDriveIcon")); toggleButton24.setSelected(true); - toggleButton24.putClientProperty("JButton.buttonType", "square"); + toggleButton24.setButtonType(FlatButton.ButtonType.square); toolBar3.add(toggleButton24); } add(toolBar3, "cell 1 23 5 1"); @@ -1378,24 +1377,24 @@ public class FlatComponentsTest //---- button28 ---- button28.setIcon(UIManager.getIcon("Tree.closedIcon")); - button28.putClientProperty("JButton.buttonType", "roundRect"); + button28.setButtonType(FlatButton.ButtonType.roundRect); toolBar4.add(button28); //---- button29 ---- button29.setIcon(UIManager.getIcon("Tree.openIcon")); - button29.putClientProperty("JButton.buttonType", "roundRect"); + button29.setButtonType(FlatButton.ButtonType.roundRect); toolBar4.add(button29); //---- toggleButton25 ---- toggleButton25.setIcon(UIManager.getIcon("FileView.computerIcon")); toggleButton25.setSelected(true); - toggleButton25.putClientProperty("JButton.buttonType", "roundRect"); + toggleButton25.setButtonType(FlatButton.ButtonType.roundRect); toolBar4.add(toggleButton25); //---- toggleButton26 ---- toggleButton26.setIcon(UIManager.getIcon("FileView.floppyDriveIcon")); toggleButton26.setSelected(true); - toggleButton26.putClientProperty("JButton.buttonType", "roundRect"); + toggleButton26.setButtonType(FlatButton.ButtonType.roundRect); toolBar4.add(toggleButton26); } add(toolBar4, "cell 1 23 5 1"); @@ -1419,7 +1418,7 @@ public class FlatComponentsTest private JTextField textField1; private FlatProgressBar progressBar3; private FlatProgressBar progressBar4; - private JComboBox buttonTypeComboBox; + private FlatTestEnumComboBox buttonTypeComboBox; private JCheckBox borderPaintedCheckBox; private JCheckBox roundRectCheckBox; private JCheckBox contentAreaFilledCheckBox; diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd index da17c68a..1e87e0b5 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponentsTest.jfd @@ -53,19 +53,19 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 1" } ) - add( new FormComponent( "javax.swing.JButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) { name: "button17" "text": "Sq" - "$client.JButton.buttonType": "square" - "$client.JComponent.minimumWidth": 0 + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square + "minimumWidth": 0 }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 1" } ) - add( new FormComponent( "javax.swing.JButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) { name: "button22" "text": "Rd" - "$client.JButton.buttonType": "roundRect" - "$client.JComponent.minimumWidth": 0 + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect + "minimumWidth": 0 }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 1" } ) @@ -78,21 +78,21 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 2 1" } ) - add( new FormComponent( "javax.swing.JButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) { name: "button18" "text": "Sq" - "$client.JButton.buttonType": "square" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square "enabled": false - "$client.JComponent.minimumWidth": 0 + "minimumWidth": 0 }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 2 1" } ) - add( new FormComponent( "javax.swing.JButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) { name: "button23" "text": "Rd" - "$client.JButton.buttonType": "roundRect" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect "enabled": false - "$client.JComponent.minimumWidth": 0 + "minimumWidth": 0 }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 2 1" } ) @@ -104,17 +104,17 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 3 1" } ) - add( new FormComponent( "javax.swing.JButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) { name: "button3" "text": "Help" - "$client.JButton.buttonType": "help" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType help }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 4 1" } ) - add( new FormComponent( "javax.swing.JButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) { name: "button12" "text": "Help" - "$client.JButton.buttonType": "help" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType help "enabled": false }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 4 1" @@ -162,17 +162,17 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 2" } ) - add( new FormComponent( "javax.swing.JToggleButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) { name: "toggleButton9" "text": "Sq" - "$client.JButton.buttonType": "square" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 2" } ) - add( new FormComponent( "javax.swing.JToggleButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) { name: "toggleButton19" "text": "Rd" - "$client.JButton.buttonType": "roundRect" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 2" } ) @@ -183,18 +183,18 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 2 2" } ) - add( new FormComponent( "javax.swing.JToggleButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) { name: "toggleButton10" "text": "Sq" - "$client.JButton.buttonType": "square" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square "enabled": false }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 2 2" } ) - add( new FormComponent( "javax.swing.JToggleButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) { name: "toggleButton20" "text": "Rd" - "$client.JButton.buttonType": "roundRect" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect "enabled": false }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 2 2" @@ -285,18 +285,18 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 4 3" } ) - add( new FormComponent( "javax.swing.JToggleButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) { name: "toggleButton5" "text": "Tab" - "$client.JButton.buttonType": "tab" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType tab "selected": true }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 5 3" } ) - add( new FormComponent( "javax.swing.JToggleButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) { name: "toggleButton8" "text": "Tab" - "$client.JButton.buttonType": "tab" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType tab "enabled": false "selected": true }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { @@ -963,18 +963,11 @@ new FormModel { } ) { name: "panel5" "border": new javax.swing.border.TitledBorder( "Control" ) - add( new FormComponent( "javax.swing.JComboBox" ) { + add( new FormComponent( "com.formdev.flatlaf.testing.FlatTestEnumComboBox" ) { name: "buttonTypeComboBox" - "model": new javax.swing.DefaultComboBoxModel { - selectedItem: "-" - addElement( "-" ) - addElement( "square" ) - addElement( "roundRect" ) - addElement( "tab" ) - addElement( "help" ) - } auxiliary() { "JavaCodeGenerator.variableLocal": false + "JavaCodeGenerator.typeParameters": "ButtonType" } addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "buttonTypeChanged", false ) ) }, 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 ) ) { name: "toolBar3" - add( new FormComponent( "javax.swing.JButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) { name: "button26" "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" "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" "icon": #SwingIcon4 "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" "icon": #SwingIcon5 "selected": true - "$client.JButton.buttonType": "square" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType square } ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 23 5 1" @@ -1350,27 +1343,27 @@ new FormModel { } ) add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) { name: "toolBar4" - add( new FormComponent( "javax.swing.JButton" ) { + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatButton" ) { name: "button28" "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" "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" "icon": #SwingIcon4 "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" "icon": #SwingIcon5 "selected": true - "$client.JButton.buttonType": "roundRect" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType roundRect } ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 23 5 1" diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestEnumComboBox.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestEnumComboBox.java new file mode 100644 index 00000000..b92355b6 --- /dev/null +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestEnumComboBox.java @@ -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 + extends JComboBox +{ + @SuppressWarnings( "unchecked" ) + public void init( Class 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 + extends AbstractListModel + implements ComboBoxModel + { + private final boolean supportDefault; + private final E[] values; + private Object selectedItem; + + public EnumComboBoxModel( Class 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 ); + } + } + } +}