Demo: restore last used theme on startup

This commit is contained in:
Karl Tauber
2019-12-01 13:10:38 +01:00
parent d76f0e2241
commit 3fbc21347a
6 changed files with 126 additions and 60 deletions

View File

@@ -164,8 +164,6 @@ class ControlBar
if( lafClassName.equals( UIManager.getLookAndFeel().getClass().getName() ) ) if( lafClassName.equals( UIManager.getLookAndFeel().getClass().getName() ) )
return; return;
FlatLafDemo.prefs.put( FlatLafDemo.KEY_LAF, lafClassName );
EventQueue.invokeLater( () -> { EventQueue.invokeLater( () -> {
try { try {
// change look and feel // change look and feel

View File

@@ -30,7 +30,7 @@ class DemoFrame
extends JFrame extends JFrame
{ {
DemoFrame() { DemoFrame() {
int tabIndex = FlatLafDemo.prefs.getInt( FlatLafDemo.KEY_TAB, 0 ); int tabIndex = DemoPrefs.getState().getInt( FlatLafDemo.KEY_TAB, 0 );
initComponents(); initComponents();
controlBar.initialize( this, tabbedPane ); controlBar.initialize( this, tabbedPane );
@@ -48,7 +48,7 @@ class DemoFrame
} }
private void selectedTabChanged() { private void selectedTabChanged() {
FlatLafDemo.prefs.putInt( FlatLafDemo.KEY_TAB, tabbedPane.getSelectedIndex() ); DemoPrefs.getState().putInt( FlatLafDemo.KEY_TAB, tabbedPane.getSelectedIndex() );
} }
private void initComponents() { private void initComponents() {

View File

@@ -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() );
} );
}
}

View File

@@ -16,10 +16,7 @@
package com.formdev.flatlaf.demo; package com.formdev.flatlaf.demo;
import java.util.prefs.Preferences;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import com.formdev.flatlaf.FlatLightLaf;
/** /**
* @author Karl Tauber * @author Karl Tauber
@@ -27,29 +24,14 @@ import com.formdev.flatlaf.FlatLightLaf;
public class FlatLafDemo public class FlatLafDemo
{ {
static final String PREFS_ROOT_PATH = "/flatlaf-demo"; static final String PREFS_ROOT_PATH = "/flatlaf-demo";
static final String KEY_LAF = "laf";
static final String KEY_TAB = "tab"; static final String KEY_TAB = "tab";
static Preferences prefs;
public static void main( String[] args ) { public static void main( String[] args ) {
SwingUtilities.invokeLater( () -> { SwingUtilities.invokeLater( () -> {
prefs = Preferences.userRoot().node( PREFS_ROOT_PATH ); DemoPrefs.init( PREFS_ROOT_PATH );
// set look and feel // set look and feel
try { DemoPrefs.initLaf( args );
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();
}
// create frame // create frame
DemoFrame frame = new DemoFrame(); DemoFrame frame = new DemoFrame();

View File

@@ -37,6 +37,7 @@ import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.function.Predicate;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.CompoundBorder; import javax.swing.border.CompoundBorder;
import javax.swing.event.*; import javax.swing.event.*;
@@ -46,6 +47,7 @@ import com.formdev.flatlaf.FlatIntelliJLaf;
import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf; import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.IntelliJTheme; import com.formdev.flatlaf.IntelliJTheme;
import com.formdev.flatlaf.demo.DemoPrefs;
import com.formdev.flatlaf.extras.FlatSVGIcon; import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.formdev.flatlaf.util.StringUtils; import com.formdev.flatlaf.util.StringUtils;
import net.miginfocom.swing.*; import net.miginfocom.swing.*;
@@ -222,12 +224,15 @@ public class IJThemesPanel
} else if( themeInfo.themeFile != null ) { } else if( themeInfo.themeFile != null ) {
try { try {
FlatLaf.install( IntelliJTheme.createLaf( new FileInputStream( themeInfo.themeFile ) ) ); FlatLaf.install( IntelliJTheme.createLaf( new FileInputStream( themeInfo.themeFile ) ) );
DemoPrefs.getState().put( DemoPrefs.KEY_LAF_INTELLIJ_THEME, DemoPrefs.FILE_PREFIX + themeInfo.themeFile );
} catch( Exception ex ) { } catch( Exception ex ) {
ex.printStackTrace(); ex.printStackTrace();
showInformationDialog( "Failed to load '" + themeInfo.themeFile + "'.", ex ); showInformationDialog( "Failed to load '" + themeInfo.themeFile + "'.", ex );
} }
} else } else {
IntelliJTheme.install( getClass().getResourceAsStream( themeInfo.resourceName ) ); IntelliJTheme.install( getClass().getResourceAsStream( themeInfo.resourceName ) );
DemoPrefs.getState().put( DemoPrefs.KEY_LAF_INTELLIJ_THEME, DemoPrefs.RESOURCE_PREFIX + themeInfo.resourceName );
}
// update all components // update all components
FlatLaf.updateUI(); FlatLaf.updateUI();
@@ -326,22 +331,36 @@ public class IJThemesPanel
private void selectedCurrentLookAndFeel() { private void selectedCurrentLookAndFeel() {
LookAndFeel lookAndFeel = UIManager.getLookAndFeel(); LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
String intelliJTheme = UIManager.getLookAndFeelDefaults().getString( DemoPrefs.INTELLIJ_THEME_UI_KEY );
if( intelliJTheme == null && lookAndFeel instanceof IntelliJTheme.ThemeLaf )
return;
Predicate<IJThemeInfo> 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; int newSel = -1;
if( !(lookAndFeel instanceof IntelliJTheme.ThemeLaf) ) { for( int i = 0; i < themes.size(); i++ ) {
String lafClassName = lookAndFeel.getClass().getName(); if( test.test( themes.get( i ) ) ) {
for( int i = 0; i < themes.size(); i++ ) { newSel = i;
if( lafClassName.equals( themes.get( i ).lafClassName ) ) { break;
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() { private void initComponents() {

View File

@@ -24,7 +24,6 @@ import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.prefs.Preferences;
import javax.swing.*; import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo; import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.plaf.ColorUIResource; import javax.swing.plaf.ColorUIResource;
@@ -37,6 +36,7 @@ import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf; import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.IntelliJTheme; import com.formdev.flatlaf.IntelliJTheme;
import com.formdev.flatlaf.demo.LookAndFeelsComboBox; import com.formdev.flatlaf.demo.LookAndFeelsComboBox;
import com.formdev.flatlaf.demo.DemoPrefs;
import com.formdev.flatlaf.demo.intellijthemes.*; import com.formdev.flatlaf.demo.intellijthemes.*;
import com.formdev.flatlaf.extras.*; import com.formdev.flatlaf.extras.*;
import com.formdev.flatlaf.extras.TriStateCheckBox.State; import com.formdev.flatlaf.extras.TriStateCheckBox.State;
@@ -52,7 +52,6 @@ public class FlatTestFrame
extends JFrame extends JFrame
{ {
private static final String PREFS_ROOT_PATH = "/flatlaf-test"; 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 static final String KEY_SCALE_FACTOR = "scaleFactor";
private final String title; private final String title;
@@ -63,29 +62,17 @@ public class FlatTestFrame
public boolean useApplyComponentOrientation; public boolean useApplyComponentOrientation;
public static FlatTestFrame create( String[] args, String title ) { public static FlatTestFrame create( String[] args, String title ) {
Preferences prefs = Preferences.userRoot().node( PREFS_ROOT_PATH ); DemoPrefs.init( PREFS_ROOT_PATH );
// set scale factor // set scale factor
if( System.getProperty( "flatlaf.uiScale", System.getProperty( "sun.java2d.uiScale" ) ) == null ) { 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 ) if( scaleFactor != null )
System.setProperty( "flatlaf.uiScale", scaleFactor ); System.setProperty( "flatlaf.uiScale", scaleFactor );
} }
// set look and feel // set look and feel
try { DemoPrefs.initLaf( args );
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();
}
// create frame // create frame
return new FlatTestFrame( title ); return new FlatTestFrame( title );
@@ -239,8 +226,6 @@ public class FlatTestFrame
// hide popup to avoid occasional StackOverflowError when updating UI // hide popup to avoid occasional StackOverflowError when updating UI
lookAndFeelComboBox.setPopupVisible( false ); lookAndFeelComboBox.setPopupVisible( false );
Preferences.userRoot().node( PREFS_ROOT_PATH ).put( KEY_LAF, lafClassName );
applyLookAndFeel( lafClassName, null, false ); applyLookAndFeel( lafClassName, null, false );
} }
@@ -358,14 +343,12 @@ public class FlatTestFrame
// hide popup to avoid occasional StackOverflowError when updating UI // hide popup to avoid occasional StackOverflowError when updating UI
scaleFactorComboBox.setPopupVisible( false ); scaleFactorComboBox.setPopupVisible( false );
Preferences prefs = Preferences.userRoot().node( PREFS_ROOT_PATH );
if( scaleFactor != null ) { if( scaleFactor != null ) {
System.setProperty( "flatlaf.uiScale", scaleFactor ); System.setProperty( "flatlaf.uiScale", scaleFactor );
prefs.put( KEY_SCALE_FACTOR, scaleFactor ); DemoPrefs.getState().put( KEY_SCALE_FACTOR, scaleFactor );
} else { } else {
System.clearProperty( "flatlaf.uiScale" ); System.clearProperty( "flatlaf.uiScale" );
prefs.remove( KEY_SCALE_FACTOR ); DemoPrefs.getState().remove( KEY_SCALE_FACTOR );
} }
LookAndFeel lookAndFeel = UIManager.getLookAndFeel(); LookAndFeel lookAndFeel = UIManager.getLookAndFeel();