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 6abb3a5d..222c4c41 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 @@ -164,8 +164,6 @@ class ControlBar if( lafClassName.equals( UIManager.getLookAndFeel().getClass().getName() ) ) return; - FlatLafDemo.prefs.put( FlatLafDemo.KEY_LAF, lafClassName ); - EventQueue.invokeLater( () -> { try { // change look and feel diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoFrame.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoFrame.java index 673dcb64..11fcd6af 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoFrame.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoFrame.java @@ -30,7 +30,7 @@ class DemoFrame extends JFrame { DemoFrame() { - int tabIndex = FlatLafDemo.prefs.getInt( FlatLafDemo.KEY_TAB, 0 ); + int tabIndex = DemoPrefs.getState().getInt( FlatLafDemo.KEY_TAB, 0 ); initComponents(); controlBar.initialize( this, tabbedPane ); @@ -48,7 +48,7 @@ class DemoFrame } private void selectedTabChanged() { - FlatLafDemo.prefs.putInt( FlatLafDemo.KEY_TAB, tabbedPane.getSelectedIndex() ); + DemoPrefs.getState().putInt( FlatLafDemo.KEY_TAB, tabbedPane.getSelectedIndex() ); } private void initComponents() { diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoPrefs.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoPrefs.java new file mode 100644 index 00000000..5a0317fb --- /dev/null +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/DemoPrefs.java @@ -0,0 +1,84 @@ +/* + * 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.io.FileInputStream; +import java.util.prefs.Preferences; +import javax.swing.UIManager; +import com.formdev.flatlaf.FlatLaf; +import com.formdev.flatlaf.FlatLightLaf; +import com.formdev.flatlaf.IntelliJTheme; +import com.formdev.flatlaf.demo.intellijthemes.IJThemesPanel; + +/** + * @author Karl Tauber + */ +public class DemoPrefs +{ + public static final String KEY_LAF = "laf"; + public static final String KEY_LAF_INTELLIJ_THEME = "lafIntelliJTheme"; + + public static final String RESOURCE_PREFIX = "res:"; + public static final String FILE_PREFIX = "file:"; + + public static final String INTELLIJ_THEME_UI_KEY = "__FlatLaf.demo.intelliJTheme"; + + private static Preferences state; + + public static Preferences getState() { + return state; + } + + public static void init( String rootPath ) { + state = Preferences.userRoot().node( rootPath ); + } + + public static void initLaf( String[] args ) { + // set look and feel + try { + if( args.length > 0 ) + UIManager.setLookAndFeel( args[0] ); + else { + String lafClassName = state.get( KEY_LAF, FlatLightLaf.class.getName() ); + if( IntelliJTheme.ThemeLaf.class.getName().equals( lafClassName ) ) { + String intelliJTheme = state.get( KEY_LAF_INTELLIJ_THEME, "" ); + if( intelliJTheme.startsWith( RESOURCE_PREFIX ) ) + IntelliJTheme.install( IJThemesPanel.class.getResourceAsStream( intelliJTheme.substring( RESOURCE_PREFIX.length() ) ) ); + else if( intelliJTheme.startsWith( FILE_PREFIX ) ) + FlatLaf.install( IntelliJTheme.createLaf( new FileInputStream( intelliJTheme.substring( FILE_PREFIX.length() ) ) ) ); + else + FlatLightLaf.install(); + + if( !intelliJTheme.isEmpty() ) + UIManager.getLookAndFeelDefaults().put( INTELLIJ_THEME_UI_KEY, intelliJTheme ); + } else + UIManager.setLookAndFeel( lafClassName ); + } + } catch( Exception ex ) { + ex.printStackTrace(); + + // fallback + FlatLightLaf.install(); + } + + // remember active look and feel + UIManager.addPropertyChangeListener( e -> { + if( "lookAndFeel".equals( e.getPropertyName() ) ) + state.put( KEY_LAF, UIManager.getLookAndFeel().getClass().getName() ); + } ); + } +} diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/FlatLafDemo.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/FlatLafDemo.java index 344c7fbe..7042335d 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/FlatLafDemo.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/FlatLafDemo.java @@ -16,10 +16,7 @@ package com.formdev.flatlaf.demo; -import java.util.prefs.Preferences; import javax.swing.SwingUtilities; -import javax.swing.UIManager; -import com.formdev.flatlaf.FlatLightLaf; /** * @author Karl Tauber @@ -27,29 +24,14 @@ import com.formdev.flatlaf.FlatLightLaf; public class FlatLafDemo { static final String PREFS_ROOT_PATH = "/flatlaf-demo"; - static final String KEY_LAF = "laf"; static final String KEY_TAB = "tab"; - static Preferences prefs; - public static void main( String[] args ) { SwingUtilities.invokeLater( () -> { - prefs = Preferences.userRoot().node( PREFS_ROOT_PATH ); + DemoPrefs.init( PREFS_ROOT_PATH ); // set look and feel - try { - if( args.length > 0 ) - UIManager.setLookAndFeel( args[0] ); - else { - String lafClassName = prefs.get( KEY_LAF, FlatLightLaf.class.getName() ); - UIManager.setLookAndFeel( lafClassName ); - } - } catch( Exception ex ) { - ex.printStackTrace(); - - // fallback - FlatLightLaf.install(); - } + DemoPrefs.initLaf( args ); // create frame DemoFrame frame = new DemoFrame(); diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/intellijthemes/IJThemesPanel.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/intellijthemes/IJThemesPanel.java index 076bf504..d5b5269d 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/intellijthemes/IJThemesPanel.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/intellijthemes/IJThemesPanel.java @@ -37,6 +37,7 @@ import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Objects; +import java.util.function.Predicate; import javax.swing.*; import javax.swing.border.CompoundBorder; import javax.swing.event.*; @@ -46,6 +47,7 @@ import com.formdev.flatlaf.FlatIntelliJLaf; import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLightLaf; import com.formdev.flatlaf.IntelliJTheme; +import com.formdev.flatlaf.demo.DemoPrefs; import com.formdev.flatlaf.extras.FlatSVGIcon; import com.formdev.flatlaf.util.StringUtils; import net.miginfocom.swing.*; @@ -222,12 +224,15 @@ public class IJThemesPanel } else if( themeInfo.themeFile != null ) { try { FlatLaf.install( IntelliJTheme.createLaf( new FileInputStream( themeInfo.themeFile ) ) ); + DemoPrefs.getState().put( DemoPrefs.KEY_LAF_INTELLIJ_THEME, DemoPrefs.FILE_PREFIX + themeInfo.themeFile ); } catch( Exception ex ) { ex.printStackTrace(); showInformationDialog( "Failed to load '" + themeInfo.themeFile + "'.", ex ); } - } else + } else { IntelliJTheme.install( getClass().getResourceAsStream( themeInfo.resourceName ) ); + DemoPrefs.getState().put( DemoPrefs.KEY_LAF_INTELLIJ_THEME, DemoPrefs.RESOURCE_PREFIX + themeInfo.resourceName ); + } // update all components FlatLaf.updateUI(); @@ -326,22 +331,36 @@ public class IJThemesPanel private void selectedCurrentLookAndFeel() { LookAndFeel lookAndFeel = UIManager.getLookAndFeel(); + String intelliJTheme = UIManager.getLookAndFeelDefaults().getString( DemoPrefs.INTELLIJ_THEME_UI_KEY ); + + if( intelliJTheme == null && lookAndFeel instanceof IntelliJTheme.ThemeLaf ) + return; + + Predicate test; + if( intelliJTheme != null && intelliJTheme.startsWith( DemoPrefs.RESOURCE_PREFIX ) ) { + String resourceName = intelliJTheme.substring( DemoPrefs.RESOURCE_PREFIX.length() ); + test = ti -> Objects.equals( ti.resourceName, resourceName ); + } else if( intelliJTheme != null && intelliJTheme.startsWith( DemoPrefs.FILE_PREFIX ) ) { + File themeFile = new File( intelliJTheme.substring( DemoPrefs.FILE_PREFIX.length() ) ); + test = ti -> Objects.equals( ti.themeFile, themeFile ); + } else { + String lafClassName = lookAndFeel.getClass().getName(); + test = ti -> Objects.equals( ti.lafClassName, lafClassName ); + } int newSel = -1; - if( !(lookAndFeel instanceof IntelliJTheme.ThemeLaf) ) { - String lafClassName = lookAndFeel.getClass().getName(); - for( int i = 0; i < themes.size(); i++ ) { - if( lafClassName.equals( themes.get( i ).lafClassName ) ) { - newSel = i; - break; - } + for( int i = 0; i < themes.size(); i++ ) { + if( test.test( themes.get( i ) ) ) { + newSel = i; + break; } - - if( newSel >= 0 ) - themesList.setSelectedIndex( newSel ); - else - themesList.clearSelection(); } + + if( newSel >= 0 ) { + if( newSel != themesList.getSelectedIndex() ) + themesList.setSelectedIndex( newSel ); + } else + themesList.clearSelection(); } private void initComponents() { 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 196c224d..62a47a54 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 @@ -24,7 +24,6 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; 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; @@ -37,6 +36,7 @@ import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLightLaf; import com.formdev.flatlaf.IntelliJTheme; import com.formdev.flatlaf.demo.LookAndFeelsComboBox; +import com.formdev.flatlaf.demo.DemoPrefs; import com.formdev.flatlaf.demo.intellijthemes.*; import com.formdev.flatlaf.extras.*; import com.formdev.flatlaf.extras.TriStateCheckBox.State; @@ -52,7 +52,6 @@ public class FlatTestFrame extends JFrame { private static final String PREFS_ROOT_PATH = "/flatlaf-test"; - private static final String KEY_LAF = "laf"; private static final String KEY_SCALE_FACTOR = "scaleFactor"; private final String title; @@ -63,29 +62,17 @@ public class FlatTestFrame public boolean useApplyComponentOrientation; public static FlatTestFrame create( String[] args, String title ) { - Preferences prefs = Preferences.userRoot().node( PREFS_ROOT_PATH ); + DemoPrefs.init( PREFS_ROOT_PATH ); // set scale factor if( System.getProperty( "flatlaf.uiScale", System.getProperty( "sun.java2d.uiScale" ) ) == null ) { - String scaleFactor = prefs.get( KEY_SCALE_FACTOR, null ); + String scaleFactor = DemoPrefs.getState().get( KEY_SCALE_FACTOR, null ); if( scaleFactor != null ) System.setProperty( "flatlaf.uiScale", scaleFactor ); } // set look and feel - try { - if( args.length > 0 ) - UIManager.setLookAndFeel( args[0] ); - else { - String lafClassName = prefs.get( KEY_LAF, FlatLightLaf.class.getName() ); - UIManager.setLookAndFeel( lafClassName ); - } - } catch( Exception ex ) { - ex.printStackTrace(); - - // fallback - FlatLightLaf.install(); - } + DemoPrefs.initLaf( args ); // create frame return new FlatTestFrame( title ); @@ -239,8 +226,6 @@ public class FlatTestFrame // hide popup to avoid occasional StackOverflowError when updating UI lookAndFeelComboBox.setPopupVisible( false ); - Preferences.userRoot().node( PREFS_ROOT_PATH ).put( KEY_LAF, lafClassName ); - applyLookAndFeel( lafClassName, null, false ); } @@ -358,14 +343,12 @@ public class FlatTestFrame // hide popup to avoid occasional StackOverflowError when updating UI scaleFactorComboBox.setPopupVisible( false ); - Preferences prefs = Preferences.userRoot().node( PREFS_ROOT_PATH ); - if( scaleFactor != null ) { System.setProperty( "flatlaf.uiScale", scaleFactor ); - prefs.put( KEY_SCALE_FACTOR, scaleFactor ); + DemoPrefs.getState().put( KEY_SCALE_FACTOR, scaleFactor ); } else { System.clearProperty( "flatlaf.uiScale" ); - prefs.remove( KEY_SCALE_FACTOR ); + DemoPrefs.getState().remove( KEY_SCALE_FACTOR ); } LookAndFeel lookAndFeel = UIManager.getLookAndFeel();