Skip to content

Commit

Permalink
Demo: auto-reload current theme in development environment if .proper…
Browse files Browse the repository at this point in the history
…ties files have changed
  • Loading branch information
DevCharly committed Aug 9, 2023
1 parent 9309166 commit 810146b
Showing 1 changed file with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
Expand Down Expand Up @@ -82,6 +83,7 @@ public void windowActivated( WindowEvent e ) {

private File lastDirectory;
private boolean isAdjustingThemesList;
private long lastLafChangeTime = System.currentTimeMillis();

public IJThemesPanel() {
initComponents();
Expand Down Expand Up @@ -410,14 +412,59 @@ public void removeNotify() {
}

private void lafChanged( PropertyChangeEvent e ) {
if( "lookAndFeel".equals( e.getPropertyName() ) )
if( "lookAndFeel".equals( e.getPropertyName() ) ) {
selectedCurrentLookAndFeel();
lastLafChangeTime = System.currentTimeMillis();
}
}

private void windowActivated() {
// refresh themes list on window activation
if( themesManager.hasThemesFromDirectoryChanged() )
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() {
Expand Down

0 comments on commit 810146b

Please sign in to comment.