IntelliJ Themes Demo: search for .theme.json files in current working directory and show them in themes list

This commit is contained in:
Karl Tauber
2019-11-13 12:59:03 +01:00
parent da0c562ac2
commit 537f6703c0
3 changed files with 73 additions and 24 deletions

View File

@@ -16,6 +16,8 @@
package com.formdev.flatlaf.demo.intellijthemes;
import java.io.File;
/**
* @author Karl Tauber
*/
@@ -24,12 +26,14 @@ class IJThemeInfo
final String name;
final String resourceName;
final String sourceCodeUrl;
final File themeFile;
final String lafClassName;
IJThemeInfo( String name, String resourceName, String sourceCodeUrl, String lafClassName ) {
IJThemeInfo( String name, String resourceName, String sourceCodeUrl, File themeFile, String lafClassName ) {
this.name = name;
this.resourceName = resourceName;
this.sourceCodeUrl = sourceCodeUrl;
this.themeFile = themeFile;
this.lafClassName = lafClassName;
}

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.demo.intellijthemes;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
@@ -30,6 +31,7 @@ import com.formdev.flatlaf.util.StringUtils;
class IJThemesManager
{
final List<IJThemeInfo> bundledThemes = new ArrayList<>();
final List<IJThemeInfo> moreThemes = new ArrayList<>();
void loadBundledThemes() {
// load themes.properties
@@ -46,7 +48,22 @@ class IJThemesManager
String resourceName = (String) e.getKey();
List<String> strs = StringUtils.split( (String) e.getValue(), ',' );
bundledThemes.add( new IJThemeInfo( strs.get( 0 ), resourceName, strs.get( 1 ), null ) );
bundledThemes.add( new IJThemeInfo( strs.get( 0 ), resourceName, strs.get( 1 ), null, null ) );
}
}
void loadThemesFromDirectory() {
// get current working directory
File directory = new File( "" ).getAbsoluteFile();
File[] themeFiles = directory.listFiles( (dir, name) -> name.endsWith( ".theme.json" ) );
if( themeFiles == null )
return;
moreThemes.clear();
for( File f : themeFiles ) {
String name = StringUtils.removeTrailing( f.getName(), ".theme.json" );
moreThemes.add( new IJThemeInfo( name, null, null, f, null ) );
}
}
}

View File

@@ -20,7 +20,10 @@ import java.awt.Component;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import javax.swing.*;
import javax.swing.border.CompoundBorder;
@@ -48,18 +51,43 @@ public class IJThemesPanel
// load theme infos
themesManager.loadBundledThemes();
themesManager.loadThemesFromDirectory();
// sort themes by name
themes.addAll( themesManager.bundledThemes );
themes.sort( (t1, t2) -> t1.name.compareToIgnoreCase( t2.name ) );
int intellijThemesCount = themes.size();
Comparator<? super IJThemeInfo> comparator = (t1, t2) -> t1.name.compareToIgnoreCase( t2.name );
themesManager.bundledThemes.sort( comparator );
themesManager.moreThemes.sort( comparator );
// insert core themes at beginning
themes.add( 0, new IJThemeInfo( "Flat Light", null, null, FlatLightLaf.class.getName() ) );
themes.add( 1, new IJThemeInfo( "Flat Dark", null, null, FlatDarkLaf.class.getName() ) );
themes.add( 2, new IJThemeInfo( "Flat IntelliJ", null, null, FlatIntelliJLaf.class.getName() ) );
themes.add( 3, new IJThemeInfo( "Flat Darcula", null, null, FlatDarculaLaf.class.getName() ) );
int coreThemesCount = themes.size() - intellijThemesCount;
HashMap<Integer, String> categories = new HashMap<>();
// add core themes at beginning
categories.put( themes.size(), "Core Themes" );
themes.add( new IJThemeInfo( "Flat Light", null, null, null, FlatLightLaf.class.getName() ) );
themes.add( new IJThemeInfo( "Flat Dark", null, null, null, FlatDarkLaf.class.getName() ) );
themes.add( new IJThemeInfo( "Flat IntelliJ", null, null, null, FlatIntelliJLaf.class.getName() ) );
themes.add( new IJThemeInfo( "Flat Darcula", null, null, null, FlatDarculaLaf.class.getName() ) );
// add bundles themes
categories.put( themes.size(), "IntelliJ Themes" );
themes.addAll( themesManager.bundledThemes );
// add themes from directory
categories.put( themes.size(), "Current Directory" );
themes.addAll( themesManager.moreThemes );
// create renderer
themesList.setCellRenderer( new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent( JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus )
{
String title = categories.get( index );
JComponent c = (JComponent) super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
if( title != null )
c.setBorder( new CompoundBorder( new ListCellTitledBorder( themesList, title ), c.getBorder() ) );
return c;
}
} );
// fill themes list
themesList.setModel( new AbstractListModel<IJThemeInfo>() {
@@ -72,19 +100,6 @@ public class IJThemesPanel
return themes.get( index );
}
} );
themesList.setCellRenderer( new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent( JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus )
{
String title = (index == 0) ? "Core Themes" : (index == coreThemesCount ? "IntelliJ Themes" : null);
JComponent c = (JComponent) super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
if( title != null )
c.setBorder( new CompoundBorder( new ListCellTitledBorder( themesList, title ), c.getBorder() ) );
return c;
}
} );
}
private void themesListValueChanged( ListSelectionEvent e ) {
@@ -106,6 +121,14 @@ public class IJThemesPanel
UIManager.setLookAndFeel( themeInfo.lafClassName );
} catch( Exception ex ) {
ex.printStackTrace();
showInformationDialog( "Failed to create '" + themeInfo.lafClassName + "'.", ex );
}
} else if( themeInfo.themeFile != null ) {
try {
FlatLaf.install( IntelliJTheme.createLaf( new FileInputStream( themeInfo.themeFile ) ) );
} catch( Exception ex ) {
ex.printStackTrace();
showInformationDialog( "Failed to load '" + themeInfo.themeFile + "'.", ex );
}
} else
IntelliJTheme.install( getClass().getResourceAsStream( themeInfo.resourceName ) );
@@ -114,6 +137,11 @@ public class IJThemesPanel
FlatLaf.updateUI();
}
private void showInformationDialog( String message, Exception ex ) {
JOptionPane.showMessageDialog( null, message + "\n\n" + ex.getMessage(),
"FlatLaf", JOptionPane.INFORMATION_MESSAGE );
}
@Override
public void addNotify() {
super.addNotify();