mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-06 14:00:55 +03:00
Theme Editor: support loading/resolving base properties
This commit is contained in:
@@ -19,8 +19,10 @@ package com.formdev.flatlaf.themeeditor;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import javax.swing.JLayer;
|
||||
import javax.swing.JPanel;
|
||||
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
|
||||
@@ -94,6 +96,10 @@ class FlatThemeEditorPane
|
||||
return font.deriveFont( (float) newFontSize );
|
||||
}
|
||||
|
||||
void setBaseFiles( List<File> baseFiles ) {
|
||||
textArea.propertiesSupport.setBaseFiles( baseFiles );
|
||||
}
|
||||
|
||||
void load( FileLocation loc ) throws IOException {
|
||||
textArea.load( loc, StandardCharsets.ISO_8859_1 );
|
||||
}
|
||||
|
||||
@@ -16,10 +16,15 @@
|
||||
|
||||
package com.formdev.flatlaf.themeeditor;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.swing.*;
|
||||
import org.fife.ui.rsyntaxtextarea.FileLocation;
|
||||
import com.formdev.flatlaf.FlatLightLaf;
|
||||
@@ -39,12 +44,17 @@ public class FlatThemeFileEditor
|
||||
? args[0]
|
||||
: "theme-editor-test.properties" ); // TODO
|
||||
|
||||
List<File> baseFiles = new ArrayList<>();
|
||||
for( int i = 1; i < args.length; i++ )
|
||||
baseFiles.add( new File( args[i] ) );
|
||||
|
||||
SwingUtilities.invokeLater( () -> {
|
||||
FlatLightLaf.install();
|
||||
FlatInspector.install( "ctrl alt shift X" );
|
||||
|
||||
FlatThemeFileEditor frame = new FlatThemeFileEditor();
|
||||
|
||||
frame.themeEditorArea.setBaseFiles( baseFiles );
|
||||
try {
|
||||
frame.themeEditorArea.load( FileLocation.create( file ) );
|
||||
} catch( IOException ex ) {
|
||||
|
||||
@@ -17,9 +17,12 @@
|
||||
package com.formdev.flatlaf.themeeditor;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.function.Function;
|
||||
@@ -42,11 +45,15 @@ class FlatThemePropertiesSupport
|
||||
private Properties propertiesCache;
|
||||
private final Map<Integer, Object> parsedValueCache = new HashMap<>();
|
||||
|
||||
private File[] baseFiles;
|
||||
private long[] baseFilesLastModified;
|
||||
private Properties[] basePropertiesCache;
|
||||
|
||||
FlatThemePropertiesSupport( FlatSyntaxTextArea textArea ) {
|
||||
this.textArea = textArea;
|
||||
|
||||
propertiesGetter = key -> {
|
||||
return getProperties().getProperty( key );
|
||||
return getProperty( key );
|
||||
};
|
||||
resolver = v -> {
|
||||
return resolveValue( v );
|
||||
@@ -55,6 +62,14 @@ class FlatThemePropertiesSupport
|
||||
textArea.getDocument().addDocumentListener( this );
|
||||
}
|
||||
|
||||
void setBaseFiles( List<File> baseFiles ) {
|
||||
int size = baseFiles.size();
|
||||
this.baseFiles = baseFiles.toArray( new File[size] );
|
||||
|
||||
baseFilesLastModified = new long[size];
|
||||
basePropertiesCache = new Properties[size];
|
||||
}
|
||||
|
||||
private String resolveValue( String value ) {
|
||||
return UIDefaultsLoaderAccessor.resolveValue( value, propertiesGetter );
|
||||
}
|
||||
@@ -102,6 +117,37 @@ class FlatThemePropertiesSupport
|
||||
}
|
||||
}
|
||||
|
||||
private String getProperty( String key ) {
|
||||
// look in current text area
|
||||
String value = getProperties().getProperty( key );
|
||||
if( value != null )
|
||||
return value;
|
||||
|
||||
if( baseFiles == null )
|
||||
return null;
|
||||
|
||||
// look in base properties files
|
||||
for( int i = 0; i < baseFiles.length; i++ ) {
|
||||
long lastModified = baseFiles[i].lastModified();
|
||||
if( baseFilesLastModified[i] != lastModified ) {
|
||||
// (re)load base properties file
|
||||
baseFilesLastModified[i] = lastModified;
|
||||
basePropertiesCache[i] = new Properties();
|
||||
try {
|
||||
basePropertiesCache[i].load( new FileInputStream( baseFiles[i] ) );
|
||||
} catch( IOException ex ) {
|
||||
ex.printStackTrace(); //TODO
|
||||
}
|
||||
}
|
||||
|
||||
value = basePropertiesCache[i].getProperty( key );
|
||||
if( value != null )
|
||||
return value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Properties getProperties() {
|
||||
if( propertiesCache != null )
|
||||
return propertiesCache;
|
||||
|
||||
Reference in New Issue
Block a user