diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java index 15486632..ede321fb 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java @@ -23,6 +23,7 @@ import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.*; +import javax.swing.UIManager.LookAndFeelInfo; import javax.swing.plaf.metal.MetalLookAndFeel; import javax.swing.plaf.nimbus.NimbusLookAndFeel; import com.formdev.flatlaf.*; @@ -43,11 +44,11 @@ class ControlBar initComponents(); // initialize look and feels combo box - DefaultComboBoxModel lafModel = new DefaultComboBoxModel<>(); - lafModel.addElement( new LafInfo( "Flat Light (F1)", FlatLightLaf.class.getName() ) ); - lafModel.addElement( new LafInfo( "Flat Dark (F2)", FlatDarkLaf.class.getName() ) ); - lafModel.addElement( new LafInfo( "Flat IntelliJ (F3)", FlatIntelliJLaf.class.getName() ) ); - lafModel.addElement( new LafInfo( "Flat Darcula (F4)", FlatDarculaLaf.class.getName() ) ); + DefaultComboBoxModel lafModel = new DefaultComboBoxModel<>(); + lafModel.addElement( new LookAndFeelInfo( "Flat Light (F1)", FlatLightLaf.class.getName() ) ); + lafModel.addElement( new LookAndFeelInfo( "Flat Dark (F2)", FlatDarkLaf.class.getName() ) ); + lafModel.addElement( new LookAndFeelInfo( "Flat IntelliJ (F3)", FlatIntelliJLaf.class.getName() ) ); + lafModel.addElement( new LookAndFeelInfo( "Flat Darcula (F4)", FlatDarculaLaf.class.getName() ) ); UIManager.LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels(); for( UIManager.LookAndFeelInfo lookAndFeel : lookAndFeels ) { @@ -66,19 +67,11 @@ class ControlBar else if( className.equals( NimbusLookAndFeel.class.getName() ) ) name += " (F11)"; - lafModel.addElement( new LafInfo( name, className ) ); + lafModel.addElement( new LookAndFeelInfo( name, className ) ); } - LookAndFeel activeLaf = UIManager.getLookAndFeel(); - String activeLafClassName = activeLaf.getClass().getName(); - int sel = lafModel.getIndexOf( new LafInfo( null, activeLafClassName ) ); - if( sel < 0 ) { - lafModel.addElement( new LafInfo( activeLaf.getName(), activeLafClassName ) ); - sel = lafModel.getSize() - 1; - } - lafModel.setSelectedItem( lafModel.getElementAt( sel ) ); - lookAndFeelComboBox.setModel( lafModel ); + lookAndFeelComboBox.selectedLookAndFeel( UIManager.getLookAndFeel() ); UIManager.addPropertyChangeListener( e -> { if( "lookAndFeel".equals( e.getPropertyName() ) ) { @@ -161,26 +154,23 @@ class ControlBar } private void selectLookAndFeel( String lafClassName ) { - DefaultComboBoxModel lafModel = (DefaultComboBoxModel) lookAndFeelComboBox.getModel(); - int sel = lafModel.getIndexOf( new LafInfo( null, lafClassName ) ); - if( sel >= 0 ) - lookAndFeelComboBox.setSelectedIndex( sel ); + lookAndFeelComboBox.setSelectedLookAndFeel( lafClassName ); } private void lookAndFeelChanged() { - LafInfo newLaf = (LafInfo) lookAndFeelComboBox.getSelectedItem(); - if( newLaf == null ) + String lafClassName = lookAndFeelComboBox.getSelectedLookAndFeel(); + if( lafClassName == null ) return; - if( newLaf.className.equals( UIManager.getLookAndFeel().getClass().getName() ) ) + if( lafClassName.equals( UIManager.getLookAndFeel().getClass().getName() ) ) return; - FlatLafDemo.prefs.put( FlatLafDemo.KEY_LAF, newLaf.className ); + FlatLafDemo.prefs.put( FlatLafDemo.KEY_LAF, lafClassName ); EventQueue.invokeLater( () -> { try { // change look and feel - UIManager.setLookAndFeel( newLaf.className ); + UIManager.setLookAndFeel( lafClassName ); // update all components FlatLaf.updateUI(); @@ -250,7 +240,7 @@ class ControlBar private void initComponents() { // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents separator1 = new JSeparator(); - lookAndFeelComboBox = new JComboBox<>(); + lookAndFeelComboBox = new LookAndFeelsComboBox(); rightToLeftCheckBox = new JCheckBox(); enabledCheckBox = new JCheckBox(); infoLabel = new JLabel(); @@ -300,33 +290,10 @@ class ControlBar // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables private JSeparator separator1; - private JComboBox lookAndFeelComboBox; + private LookAndFeelsComboBox lookAndFeelComboBox; private JCheckBox rightToLeftCheckBox; private JCheckBox enabledCheckBox; private JLabel infoLabel; private JButton closeButton; // JFormDesigner - End of variables declaration //GEN-END:variables - - //---- class LafInfo ------------------------------------------------------ - - static class LafInfo - { - final String name; - final String className; - - LafInfo( String name, String className ) { - this.name = name; - this.className = className; - } - - @Override - public boolean equals( Object obj ) { - return obj instanceof LafInfo && className.equals( ((LafInfo)obj).className ); - } - - @Override - public String toString() { - return name; - } - } } diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.jfd b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.jfd index a7b4a7c5..a7ea971e 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.jfd +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.jfd @@ -15,11 +15,8 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 0 0 5 1" } ) - add( new FormComponent( "javax.swing.JComboBox" ) { + add( new FormComponent( "com.formdev.flatlaf.demo.LookAndFeelsComboBox" ) { name: "lookAndFeelComboBox" - auxiliary() { - "JavaCodeGenerator.typeParameters": "LafInfo" - } addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "lookAndFeelChanged", false ) ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 0 1" diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/LookAndFeelsComboBox.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/LookAndFeelsComboBox.java new file mode 100644 index 00000000..7c5f1e81 --- /dev/null +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/LookAndFeelsComboBox.java @@ -0,0 +1,93 @@ +/* + * 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.demo; + +import java.awt.Component; +import javax.swing.ComboBoxModel; +import javax.swing.JComboBox; +import javax.swing.JList; +import javax.swing.LookAndFeel; +import javax.swing.MutableComboBoxModel; +import javax.swing.UIManager; +import javax.swing.UIManager.LookAndFeelInfo; +import javax.swing.plaf.basic.BasicComboBoxRenderer; + +/** + * @author Karl Tauber + */ +public class LookAndFeelsComboBox + extends JComboBox +{ + @SuppressWarnings( "unchecked" ) + public LookAndFeelsComboBox() { + setRenderer( new BasicComboBoxRenderer() { + @Override + @SuppressWarnings( "rawtypes" ) + public Component getListCellRendererComponent( JList list, Object value, + int index, boolean isSelected, boolean cellHasFocus ) + { + value = ((LookAndFeelInfo)value).getName(); + return super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus ); + } + } ); + } + + public void addLookAndFeel( String name, String className ) { + getMutableModel().addElement( new LookAndFeelInfo( name, className ) ); + } + + public String getSelectedLookAndFeel() { + Object sel = getSelectedItem(); + return (sel instanceof LookAndFeelInfo) ? ((LookAndFeelInfo)sel).getClassName() : null; + } + + public void setSelectedLookAndFeel( String className ) { + int index = getIndexOfLookAndFeel( className ); + if( index >= 0 ) + setSelectedIndex( index ); + } + + public void selectedLookAndFeel( LookAndFeel lookAndFeel ) { + String className = lookAndFeel.getClass().getName(); + int index = getIndexOfLookAndFeel( className ); + if( index < 0 ) { + addLookAndFeel( lookAndFeel.getName(), className ); + index = getItemCount() - 1; + } + setSelectedIndex( index ); + } + + public void removeLookAndFeel( String className ) { + int index = getIndexOfLookAndFeel( className ); + if( index >= 0 ) + getMutableModel().removeElementAt( index ); + } + + public int getIndexOfLookAndFeel( String className ) { + ComboBoxModel model = getModel(); + int size = model.getSize(); + for( int i = 0; i < size; i++ ) { + if( className.equals( model.getElementAt( i ).getClassName() ) ) + return i; + } + return -1; + } + + private MutableComboBoxModel getMutableModel() { + return (MutableComboBoxModel) getModel(); + } +} diff --git a/flatlaf-testing/build.gradle.kts b/flatlaf-testing/build.gradle.kts index 0db1f897..16e23182 100644 --- a/flatlaf-testing/build.gradle.kts +++ b/flatlaf-testing/build.gradle.kts @@ -25,6 +25,7 @@ dependencies { implementation( project( ":flatlaf-extras" ) ) implementation( project( ":flatlaf-swingx" ) ) implementation( project( ":flatlaf-jide-oss" ) ) + implementation( project( ":flatlaf-demo" ) ) implementation( "com.miglayout:miglayout-swing:5.2" ) implementation( "com.jgoodies:jgoodies-forms:1.9.0" ) diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java index 8910ebee..1a955ad3 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java @@ -26,6 +26,7 @@ import java.util.function.BiConsumer; import java.util.function.Supplier; import java.util.prefs.Preferences; import javax.swing.*; +import javax.swing.UIManager.LookAndFeelInfo; import javax.swing.plaf.ColorUIResource; import javax.swing.plaf.metal.MetalLookAndFeel; import javax.swing.plaf.nimbus.NimbusLookAndFeel; @@ -34,6 +35,7 @@ import com.formdev.flatlaf.FlatDarkLaf; import com.formdev.flatlaf.FlatIntelliJLaf; import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLightLaf; +import com.formdev.flatlaf.demo.LookAndFeelsComboBox; import com.formdev.flatlaf.extras.*; import com.formdev.flatlaf.extras.TriStateCheckBox.State; import com.formdev.flatlaf.ui.FlatUIUtils; @@ -93,12 +95,12 @@ public class FlatTestFrame initComponents(); // initialize look and feels combo box - DefaultComboBoxModel lafModel = new DefaultComboBoxModel<>(); - lafModel.addElement( new LafInfo( "Flat Light (F1)", FlatLightLaf.class.getName() ) ); - lafModel.addElement( new LafInfo( "Flat Dark (F2)", FlatDarkLaf.class.getName() ) ); - lafModel.addElement( new LafInfo( "Flat IntelliJ (F3)", FlatIntelliJLaf.class.getName() ) ); - lafModel.addElement( new LafInfo( "Flat Darcula (F4)", FlatDarculaLaf.class.getName() ) ); - lafModel.addElement( new LafInfo( "Flat Test (F8)", FlatTestLaf.class.getName() ) ); + DefaultComboBoxModel lafModel = new DefaultComboBoxModel<>(); + lafModel.addElement( new LookAndFeelInfo( "Flat Light (F1)", FlatLightLaf.class.getName() ) ); + lafModel.addElement( new LookAndFeelInfo( "Flat Dark (F2)", FlatDarkLaf.class.getName() ) ); + lafModel.addElement( new LookAndFeelInfo( "Flat IntelliJ (F3)", FlatIntelliJLaf.class.getName() ) ); + lafModel.addElement( new LookAndFeelInfo( "Flat Darcula (F4)", FlatDarculaLaf.class.getName() ) ); + lafModel.addElement( new LookAndFeelInfo( "Flat Test (F8)", FlatTestLaf.class.getName() ) ); UIManager.LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels(); for( UIManager.LookAndFeelInfo lookAndFeel : lookAndFeels ) { @@ -117,19 +119,11 @@ public class FlatTestFrame else if( className.equals( NimbusLookAndFeel.class.getName() ) ) name += " (F11)"; - lafModel.addElement( new LafInfo( name, className ) ); + lafModel.addElement( new LookAndFeelInfo( name, className ) ); } - LookAndFeel activeLaf = UIManager.getLookAndFeel(); - String activeLafClassName = activeLaf.getClass().getName(); - int sel = lafModel.getIndexOf( new LafInfo( null, activeLafClassName ) ); - if( sel < 0 ) { - lafModel.addElement( new LafInfo( activeLaf.getName(), activeLafClassName ) ); - sel = lafModel.getSize() - 1; - } - lafModel.setSelectedItem( lafModel.getElementAt( sel ) ); - lookAndFeelComboBox.setModel( lafModel ); + lookAndFeelComboBox.selectedLookAndFeel( UIManager.getLookAndFeel() ); updateScaleFactorComboBox(); String scaleFactor = System.getProperty( "flatlaf.uiScale", System.getProperty( "sun.java2d.uiScale" ) ); @@ -230,26 +224,23 @@ public class FlatTestFrame } private void selectLookAndFeel( String lafClassName ) { - DefaultComboBoxModel lafModel = (DefaultComboBoxModel) lookAndFeelComboBox.getModel(); - int sel = lafModel.getIndexOf( new LafInfo( null, lafClassName ) ); - if( sel >= 0 ) - lookAndFeelComboBox.setSelectedIndex( sel ); + lookAndFeelComboBox.setSelectedLookAndFeel( lafClassName ); } private void lookAndFeelChanged() { - LafInfo newLaf = (LafInfo) lookAndFeelComboBox.getSelectedItem(); - if( newLaf == null ) + String lafClassName = lookAndFeelComboBox.getSelectedLookAndFeel(); + if( lafClassName == null ) return; - if( newLaf.className.equals( UIManager.getLookAndFeel().getClass().getName() ) ) + if( lafClassName.equals( UIManager.getLookAndFeel().getClass().getName() ) ) return; // hide popup to avoid occasional StackOverflowError when updating UI lookAndFeelComboBox.setPopupVisible( false ); - Preferences.userRoot().node( PREFS_ROOT_PATH ).put( KEY_LAF, newLaf.className ); + Preferences.userRoot().node( PREFS_ROOT_PATH ).put( KEY_LAF, lafClassName ); - applyLookAndFeel( newLaf.className, false ); + applyLookAndFeel( lafClassName, false ); } private void applyLookAndFeel( String lafClassName, boolean pack ) { @@ -442,7 +433,7 @@ public class FlatTestFrame dialogPane = new JPanel(); contentPanel = new JRootPane(); buttonBar = new JPanel(); - lookAndFeelComboBox = new JComboBox<>(); + lookAndFeelComboBox = new LookAndFeelsComboBox(); scaleFactorComboBox = new JComboBox<>(); rightToLeftCheckBox = new JCheckBox(); enabledCheckBox = new JCheckBox(); @@ -564,7 +555,7 @@ public class FlatTestFrame private JPanel dialogPane; private JRootPane contentPanel; private JPanel buttonBar; - private JComboBox lookAndFeelComboBox; + private LookAndFeelsComboBox lookAndFeelComboBox; private JComboBox scaleFactorComboBox; private JCheckBox rightToLeftCheckBox; private JCheckBox enabledCheckBox; @@ -574,27 +565,4 @@ public class FlatTestFrame private TriStateCheckBox opaqueTriStateCheckBox; private JButton closeButton; // JFormDesigner - End of variables declaration //GEN-END:variables - - //---- class LafInfo ------------------------------------------------------ - - static class LafInfo - { - final String name; - final String className; - - LafInfo( String name, String className ) { - this.name = name; - this.className = className; - } - - @Override - public boolean equals( Object obj ) { - return obj instanceof LafInfo && className.equals( ((LafInfo)obj).className ); - } - - @Override - public String toString() { - return name; - } - } } diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.jfd index 74298f06..d2ce88e7 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.jfd @@ -25,11 +25,8 @@ new FormModel { "$rowSpecs": "[fill]" } ) { name: "buttonBar" - add( new FormComponent( "javax.swing.JComboBox" ) { + add( new FormComponent( "com.formdev.flatlaf.demo.LookAndFeelsComboBox" ) { name: "lookAndFeelComboBox" - auxiliary() { - "JavaCodeGenerator.typeParameters": "LafInfo" - } addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "lookAndFeelChanged", false ) ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 0 0"