mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-07 14:30:56 +03:00
IntelliJ Themes Demo: refresh themes list (from current working directory) on window activation
This commit is contained in:
@@ -20,6 +20,7 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
@@ -32,6 +33,7 @@ class IJThemesManager
|
|||||||
{
|
{
|
||||||
final List<IJThemeInfo> bundledThemes = new ArrayList<>();
|
final List<IJThemeInfo> bundledThemes = new ArrayList<>();
|
||||||
final List<IJThemeInfo> moreThemes = new ArrayList<>();
|
final List<IJThemeInfo> moreThemes = new ArrayList<>();
|
||||||
|
private final Map<File,Long> lastModifiedMap = new HashMap<>();
|
||||||
|
|
||||||
void loadBundledThemes() {
|
void loadBundledThemes() {
|
||||||
// load themes.properties
|
// load themes.properties
|
||||||
@@ -44,6 +46,7 @@ class IJThemesManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add info about bundled themes
|
// add info about bundled themes
|
||||||
|
bundledThemes.clear();
|
||||||
for( Map.Entry<Object, Object> e : properties.entrySet() ) {
|
for( Map.Entry<Object, Object> e : properties.entrySet() ) {
|
||||||
String resourceName = (String) e.getKey();
|
String resourceName = (String) e.getKey();
|
||||||
List<String> strs = StringUtils.split( (String) e.getValue(), ',' );
|
List<String> strs = StringUtils.split( (String) e.getValue(), ',' );
|
||||||
@@ -60,10 +63,22 @@ class IJThemesManager
|
|||||||
if( themeFiles == null )
|
if( themeFiles == null )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
lastModifiedMap.clear();
|
||||||
|
lastModifiedMap.put( directory, directory.lastModified() );
|
||||||
|
|
||||||
moreThemes.clear();
|
moreThemes.clear();
|
||||||
for( File f : themeFiles ) {
|
for( File f : themeFiles ) {
|
||||||
String name = StringUtils.removeTrailing( f.getName(), ".theme.json" );
|
String name = StringUtils.removeTrailing( f.getName(), ".theme.json" );
|
||||||
moreThemes.add( new IJThemeInfo( name, null, null, f, null ) );
|
moreThemes.add( new IJThemeInfo( name, null, null, f, null ) );
|
||||||
|
lastModifiedMap.put( f, f.lastModified() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean hasThemesFromDirectoryChanged() {
|
||||||
|
for( Map.Entry<File, Long> e : lastModifiedMap.entrySet() ) {
|
||||||
|
if( e.getKey().lastModified() != e.getValue().longValue() )
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,10 @@ package com.formdev.flatlaf.demo.intellijthemes;
|
|||||||
|
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.awt.EventQueue;
|
import java.awt.EventQueue;
|
||||||
|
import java.awt.Window;
|
||||||
|
import java.awt.event.WindowAdapter;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.awt.event.WindowListener;
|
||||||
import java.beans.PropertyChangeEvent;
|
import java.beans.PropertyChangeEvent;
|
||||||
import java.beans.PropertyChangeListener;
|
import java.beans.PropertyChangeListener;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@@ -25,6 +29,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Comparator;
|
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 javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.border.CompoundBorder;
|
import javax.swing.border.CompoundBorder;
|
||||||
import javax.swing.event.*;
|
import javax.swing.event.*;
|
||||||
@@ -44,11 +49,37 @@ public class IJThemesPanel
|
|||||||
{
|
{
|
||||||
private final IJThemesManager themesManager = new IJThemesManager();
|
private final IJThemesManager themesManager = new IJThemesManager();
|
||||||
private final List<IJThemeInfo> themes = new ArrayList<>();
|
private final List<IJThemeInfo> themes = new ArrayList<>();
|
||||||
|
private final HashMap<Integer, String> categories = new HashMap<>();
|
||||||
private final PropertyChangeListener lafListener = this::lafChanged;
|
private final PropertyChangeListener lafListener = this::lafChanged;
|
||||||
|
private final WindowListener windowListener = new WindowAdapter() {
|
||||||
|
@Override
|
||||||
|
public void windowActivated( WindowEvent e ) {
|
||||||
|
IJThemesPanel.this.windowActivated();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
private Window window;
|
||||||
|
|
||||||
public IJThemesPanel() {
|
public IJThemesPanel() {
|
||||||
initComponents();
|
initComponents();
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
|
||||||
|
updateThemesList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateThemesList() {
|
||||||
// load theme infos
|
// load theme infos
|
||||||
themesManager.loadBundledThemes();
|
themesManager.loadBundledThemes();
|
||||||
themesManager.loadThemesFromDirectory();
|
themesManager.loadThemesFromDirectory();
|
||||||
@@ -58,7 +89,8 @@ public class IJThemesPanel
|
|||||||
themesManager.bundledThemes.sort( comparator );
|
themesManager.bundledThemes.sort( comparator );
|
||||||
themesManager.moreThemes.sort( comparator );
|
themesManager.moreThemes.sort( comparator );
|
||||||
|
|
||||||
HashMap<Integer, String> categories = new HashMap<>();
|
themes.clear();
|
||||||
|
categories.clear();
|
||||||
|
|
||||||
// add core themes at beginning
|
// add core themes at beginning
|
||||||
categories.put( themes.size(), "Core Themes" );
|
categories.put( themes.size(), "Core Themes" );
|
||||||
@@ -75,19 +107,8 @@ public class IJThemesPanel
|
|||||||
categories.put( themes.size(), "Current Directory" );
|
categories.put( themes.size(), "Current Directory" );
|
||||||
themes.addAll( themesManager.moreThemes );
|
themes.addAll( themesManager.moreThemes );
|
||||||
|
|
||||||
// create renderer
|
// remember selection
|
||||||
themesList.setCellRenderer( new DefaultListCellRenderer() {
|
IJThemeInfo oldSel = themesList.getSelectedValue();
|
||||||
@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
|
// fill themes list
|
||||||
themesList.setModel( new AbstractListModel<IJThemeInfo>() {
|
themesList.setModel( new AbstractListModel<IJThemeInfo>() {
|
||||||
@@ -100,6 +121,21 @@ public class IJThemesPanel
|
|||||||
return themes.get( index );
|
return themes.get( index );
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
// restore selection
|
||||||
|
if( oldSel != null ) {
|
||||||
|
for( int i = 0; i < themes.size(); i++ ) {
|
||||||
|
IJThemeInfo theme = themes.get( i );
|
||||||
|
if( oldSel.name.equals( theme.name ) &&
|
||||||
|
Objects.equals( oldSel.resourceName, theme.resourceName ) &&
|
||||||
|
Objects.equals( oldSel.themeFile, theme.themeFile ) &&
|
||||||
|
Objects.equals( oldSel.lafClassName, theme.lafClassName ) )
|
||||||
|
{
|
||||||
|
themesList.setSelectedIndex( i );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void themesListValueChanged( ListSelectionEvent e ) {
|
private void themesListValueChanged( ListSelectionEvent e ) {
|
||||||
@@ -148,6 +184,10 @@ public class IJThemesPanel
|
|||||||
|
|
||||||
selectedCurrentLookAndFeel();
|
selectedCurrentLookAndFeel();
|
||||||
UIManager.addPropertyChangeListener( lafListener );
|
UIManager.addPropertyChangeListener( lafListener );
|
||||||
|
|
||||||
|
window = SwingUtilities.windowForComponent( this );
|
||||||
|
if( window != null )
|
||||||
|
window.addWindowListener( windowListener );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -155,13 +195,24 @@ public class IJThemesPanel
|
|||||||
super.removeNotify();
|
super.removeNotify();
|
||||||
|
|
||||||
UIManager.removePropertyChangeListener( lafListener );
|
UIManager.removePropertyChangeListener( lafListener );
|
||||||
|
|
||||||
|
if( window != null ) {
|
||||||
|
window.removeWindowListener( windowListener );
|
||||||
|
window = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void lafChanged( PropertyChangeEvent e ) {
|
private void lafChanged( PropertyChangeEvent e ) {
|
||||||
if( "lookAndFeel".equals( e.getPropertyName() ) )
|
if( "lookAndFeel".equals( e.getPropertyName() ) )
|
||||||
selectedCurrentLookAndFeel();
|
selectedCurrentLookAndFeel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void windowActivated() {
|
||||||
|
// refresh themes list on window activation
|
||||||
|
if( themesManager.hasThemesFromDirectoryChanged() )
|
||||||
|
updateThemesList();
|
||||||
|
}
|
||||||
|
|
||||||
private void selectedCurrentLookAndFeel() {
|
private void selectedCurrentLookAndFeel() {
|
||||||
LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
|
LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user