mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-10 22:17:13 -06:00
Demo: auto-reload current theme in development environment if .properties files have changed
This commit is contained in:
@@ -32,6 +32,7 @@ import java.io.FileInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -82,6 +83,7 @@ public class IJThemesPanel
|
|||||||
|
|
||||||
private File lastDirectory;
|
private File lastDirectory;
|
||||||
private boolean isAdjustingThemesList;
|
private boolean isAdjustingThemesList;
|
||||||
|
private long lastLafChangeTime = System.currentTimeMillis();
|
||||||
|
|
||||||
public IJThemesPanel() {
|
public IJThemesPanel() {
|
||||||
initComponents();
|
initComponents();
|
||||||
@@ -410,14 +412,59 @@ public class IJThemesPanel
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void lafChanged( PropertyChangeEvent e ) {
|
private void lafChanged( PropertyChangeEvent e ) {
|
||||||
if( "lookAndFeel".equals( e.getPropertyName() ) )
|
if( "lookAndFeel".equals( e.getPropertyName() ) ) {
|
||||||
selectedCurrentLookAndFeel();
|
selectedCurrentLookAndFeel();
|
||||||
|
lastLafChangeTime = System.currentTimeMillis();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void windowActivated() {
|
private void windowActivated() {
|
||||||
// refresh themes list on window activation
|
// refresh themes list on window activation
|
||||||
if( themesManager.hasThemesFromDirectoryChanged() )
|
if( themesManager.hasThemesFromDirectoryChanged() )
|
||||||
updateThemesList();
|
updateThemesList();
|
||||||
|
else {
|
||||||
|
// check whether core .properties files of current Laf have changed
|
||||||
|
// in development environment since last Laf change and reload theme
|
||||||
|
LookAndFeel laf = UIManager.getLookAndFeel();
|
||||||
|
if( laf instanceof FlatLaf ) {
|
||||||
|
List<Class<?>> lafClasses = new ArrayList<>();
|
||||||
|
|
||||||
|
if( laf instanceof IntelliJTheme.ThemeLaf ) {
|
||||||
|
boolean dark = ((FlatLaf)laf).isDark();
|
||||||
|
lafClasses.add( FlatLaf.class );
|
||||||
|
lafClasses.add( dark ? FlatDarkLaf.class : FlatLightLaf.class );
|
||||||
|
lafClasses.add( dark ? FlatDarculaLaf.class : FlatIntelliJLaf.class );
|
||||||
|
lafClasses.add( IntelliJTheme.ThemeLaf.class );
|
||||||
|
} else {
|
||||||
|
for( Class<?> lafClass = laf.getClass();
|
||||||
|
FlatLaf.class.isAssignableFrom( lafClass );
|
||||||
|
lafClass = lafClass.getSuperclass() )
|
||||||
|
{
|
||||||
|
lafClasses.add( 0, lafClass );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean reload = false;
|
||||||
|
for( Class<?> lafClass : lafClasses ) {
|
||||||
|
String propertiesName = '/' + lafClass.getName().replace( '.', '/' ) + ".properties";
|
||||||
|
URL url = lafClass.getResource( propertiesName );
|
||||||
|
if( url != null && "file".equals( url.getProtocol() ) ) {
|
||||||
|
try {
|
||||||
|
File file = new File( url.toURI() );
|
||||||
|
if( file.lastModified() > lastLafChangeTime ) {
|
||||||
|
reload = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch( URISyntaxException ex ) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if( reload )
|
||||||
|
setTheme( themesList.getSelectedValue() );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void selectedCurrentLookAndFeel() {
|
private void selectedCurrentLookAndFeel() {
|
||||||
|
|||||||
Reference in New Issue
Block a user