Theme Editor: update open tabs when .properties files were added or removed to directory (on window activation)

This commit is contained in:
Karl Tauber
2020-12-29 23:12:23 +01:00
parent cda146366c
commit d2109cef86
2 changed files with 73 additions and 27 deletions

View File

@@ -56,6 +56,8 @@ class FlatThemeEditorPane
private final RTextScrollPane scrollPane; private final RTextScrollPane scrollPane;
private final FlatSyntaxTextArea textArea; private final FlatSyntaxTextArea textArea;
private File file;
FlatThemeEditorPane() { FlatThemeEditorPane() {
super( new BorderLayout() ); super( new BorderLayout() );
@@ -124,11 +126,33 @@ class FlatThemeEditorPane
textArea.propertiesSupport.setBaseFiles( baseFiles ); textArea.propertiesSupport.setBaseFiles( baseFiles );
} }
void load( FileLocation loc ) throws IOException { File getFile() {
textArea.load( loc, StandardCharsets.ISO_8859_1 ); return file;
}
void load( File file ) throws IOException {
this.file = file;
textArea.load( FileLocation.create( file ), StandardCharsets.ISO_8859_1 );
}
boolean reloadIfNecessary() {
if( !file.isFile() ) {
if( textArea.isDirty() ) {
if( JOptionPane.showOptionDialog( this,
"The file '" + textArea.getFileName()
+ "' has been deleted. Replace the editor contents with these changes?",
getWindowTitle(), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, new Object[] { "Save", "Close" }, "Save" ) == JOptionPane.YES_OPTION )
{
saveIfDirty();
return true;
}
}
return false;
} }
void reloadIfNecessary() {
if( textArea.isModifiedOutsideEditor() ) { if( textArea.isModifiedOutsideEditor() ) {
if( textArea.isDirty() ) { if( textArea.isDirty() ) {
if( JOptionPane.showConfirmDialog( this, if( JOptionPane.showConfirmDialog( this,
@@ -137,7 +161,7 @@ class FlatThemeEditorPane
getWindowTitle(), JOptionPane.YES_NO_OPTION ) != JOptionPane.YES_OPTION ) getWindowTitle(), JOptionPane.YES_NO_OPTION ) != JOptionPane.YES_OPTION )
{ {
textArea.syncLastSaveOrLoadTimeToActualFile(); textArea.syncLastSaveOrLoadTimeToActualFile();
return; return true;
} }
} }
@@ -149,6 +173,8 @@ class FlatThemeEditorPane
getWindowTitle(), JOptionPane.WARNING_MESSAGE ); getWindowTitle(), JOptionPane.WARNING_MESSAGE );
} }
} }
return true;
} }
boolean saveIfDirty() { boolean saveIfDirty() {
@@ -164,10 +190,6 @@ class FlatThemeEditorPane
} }
} }
String getFileName() {
return textArea.getFileName();
}
boolean isDirty() { boolean isDirty() {
return textArea.isDirty(); return textArea.isDirty();
} }

View File

@@ -24,11 +24,11 @@ import java.awt.event.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.function.Supplier; import java.util.function.Supplier;
import javax.swing.*; import javax.swing.*;
import com.formdev.flatlaf.extras.components.*; import com.formdev.flatlaf.extras.components.*;
import net.miginfocom.swing.*; import net.miginfocom.swing.*;
import org.fife.ui.rsyntaxtextarea.FileLocation;
import com.formdev.flatlaf.FlatLightLaf; import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.extras.FlatInspector; import com.formdev.flatlaf.extras.FlatInspector;
import com.formdev.flatlaf.extras.FlatUIDefaultsInspector; import com.formdev.flatlaf.extras.FlatUIDefaultsInspector;
@@ -43,6 +43,8 @@ import com.formdev.flatlaf.util.UIScale;
public class FlatThemeFileEditor public class FlatThemeFileEditor
extends JFrame extends JFrame
{ {
private File dir;
public static void main( String[] args ) { public static void main( String[] args ) {
File dir = new File( args.length > 0 File dir = new File( args.length > 0
? args[0] ? args[0]
@@ -69,43 +71,66 @@ public class FlatThemeFileEditor
initComponents(); initComponents();
} }
public void loadDirectory( File dir ) { private void loadDirectory( File dir ) {
this.dir = dir;
try { try {
directoryField.setText( dir.getCanonicalPath() ); directoryField.setText( dir.getCanonicalPath() );
} catch( IOException ex ) { } catch( IOException ex ) {
directoryField.setText( dir.getAbsolutePath() ); directoryField.setText( dir.getAbsolutePath() );
} }
File[] propertiesFiles = dir.listFiles( (d, name) -> { for( File file : getPropertiesFiles( dir ) )
return name.endsWith( ".properties" );
} );
Arrays.sort( propertiesFiles );
for( File file : propertiesFiles )
openFile( file ); openFile( file );
} }
public void openFile( File file ) { private void updateDirectory() {
FlatThemeEditorPane themeEditorArea = new FlatThemeEditorPane(); // update open tabs and remove tabs if file was removed
HashSet<File> openFiles = new HashSet<>();
FlatThemeEditorPane[] themeEditorPanes = getThemeEditorPanes();
for( int i = themeEditorPanes.length - 1; i >= 0; i-- ) {
FlatThemeEditorPane themeEditorPane = themeEditorPanes[i];
if( themeEditorPane.reloadIfNecessary() )
openFiles.add( themeEditorPane.getFile() );
else
tabbedPane.removeTabAt( i );
}
// open newly created files
for( File file : getPropertiesFiles( dir ) ) {
if( !openFiles.contains( file ) )
openFile( file );
}
}
private File[] getPropertiesFiles( File dir ) {
File[] propertiesFiles = dir.listFiles( (d, name) -> {
return name.endsWith( ".properties" );
} );
Arrays.sort( propertiesFiles );
return propertiesFiles;
}
private void openFile( File file ) {
FlatThemeEditorPane themeEditorPane = new FlatThemeEditorPane();
try { try {
themeEditorArea.load( FileLocation.create( file ) ); themeEditorPane.load( file );
} catch( IOException ex ) { } catch( IOException ex ) {
ex.printStackTrace(); // TODO ex.printStackTrace(); // TODO
} }
Supplier<String> titleFun = () -> { Supplier<String> titleFun = () -> {
return (themeEditorArea.isDirty() ? "* " : "") return (themeEditorPane.isDirty() ? "* " : "")
+ StringUtils.removeTrailing( themeEditorArea.getFileName(), ".properties" ); + StringUtils.removeTrailing( themeEditorPane.getFile().getName(), ".properties" );
}; };
themeEditorArea.addPropertyChangeListener( FlatThemeEditorPane.DIRTY_PROPERTY, e -> { themeEditorPane.addPropertyChangeListener( FlatThemeEditorPane.DIRTY_PROPERTY, e -> {
int index = tabbedPane.indexOfComponent( themeEditorArea ); int index = tabbedPane.indexOfComponent( themeEditorPane );
if( index >= 0 ) if( index >= 0 )
tabbedPane.setTitleAt( index, titleFun.get() ); tabbedPane.setTitleAt( index, titleFun.get() );
} ); } );
tabbedPane.addTab( titleFun.get(), null, themeEditorArea, file.getAbsolutePath() ); tabbedPane.addTab( titleFun.get(), null, themeEditorPane, file.getAbsolutePath() );
} }
private boolean saveAll() { private boolean saveAll() {
@@ -126,8 +151,7 @@ public class FlatThemeFileEditor
} }
private void windowActivated() { private void windowActivated() {
for( FlatThemeEditorPane themeEditorPane : getThemeEditorPanes() ) updateDirectory();
themeEditorPane.reloadIfNecessary();
} }
private void windowDeactivated() { private void windowDeactivated() {