Demo: moved Laf combobox related code to new class LookAndFeelsComboBox

This commit is contained in:
Karl Tauber
2019-11-12 16:30:41 +01:00
parent a02784fcba
commit 3092fced3c
6 changed files with 130 additions and 107 deletions

View File

@@ -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<LafInfo> 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<LookAndFeelInfo> 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<LafInfo> lafModel = (DefaultComboBoxModel<LafInfo>) 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<LafInfo> 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;
}
}
}

View File

@@ -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"

View File

@@ -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<UIManager.LookAndFeelInfo>
{
@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<LookAndFeelInfo> 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<LookAndFeelInfo> getMutableModel() {
return (MutableComboBoxModel<LookAndFeelInfo>) getModel();
}
}

View File

@@ -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" )

View File

@@ -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<LafInfo> 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<LookAndFeelInfo> 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<LafInfo> lafModel = (DefaultComboBoxModel<LafInfo>) 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<LafInfo> lookAndFeelComboBox;
private LookAndFeelsComboBox lookAndFeelComboBox;
private JComboBox<String> 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;
}
}
}

View File

@@ -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"