Theme Editor: auto-reload .properties files on window activation, if modified outside

This commit is contained in:
Karl Tauber
2020-12-29 18:30:52 +01:00
parent 678b879a01
commit cda146366c
3 changed files with 55 additions and 11 deletions

View File

@@ -19,13 +19,17 @@ package com.formdev.flatlaf.themeeditor;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Window;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLayer;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.fife.ui.autocomplete.AutoCompletion;
import org.fife.ui.autocomplete.CompletionProvider;
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
@@ -124,8 +128,40 @@ class FlatThemeEditorPane
textArea.load( loc, StandardCharsets.ISO_8859_1 );
}
void save() throws IOException {
void reloadIfNecessary() {
if( textArea.isModifiedOutsideEditor() ) {
if( textArea.isDirty() ) {
if( JOptionPane.showConfirmDialog( this,
"The file '" + textArea.getFileName()
+ "' has been changed. Replace the editor contents with these changes?",
getWindowTitle(), JOptionPane.YES_NO_OPTION ) != JOptionPane.YES_OPTION )
{
textArea.syncLastSaveOrLoadTimeToActualFile();
return;
}
}
try {
textArea.reload();
} catch( IOException ex ) {
JOptionPane.showMessageDialog( this,
"Failed to reload '" + textArea.getFileName() + "'\n\nReason: " + ex.getMessage(),
getWindowTitle(), JOptionPane.WARNING_MESSAGE );
}
}
}
boolean saveIfDirty() {
try {
if( textArea.isDirty() )
textArea.save();
return true;
} catch( IOException ex ) {
JOptionPane.showMessageDialog( this,
"Failed to save '" + textArea.getFileName() + "'\n\nReason: " + ex.getMessage(),
getWindowTitle(), JOptionPane.WARNING_MESSAGE );
return false;
}
}
String getFileName() {
@@ -135,4 +171,9 @@ class FlatThemeEditorPane
boolean isDirty() {
return textArea.isDirty();
}
private String getWindowTitle() {
Window window = SwingUtilities.windowForComponent( this );
return (window instanceof JFrame) ? ((JFrame)window).getTitle() : null;
}
}

View File

@@ -110,16 +110,9 @@ public class FlatThemeFileEditor
private boolean saveAll() {
for( FlatThemeEditorPane themeEditorPane : getThemeEditorPanes() ) {
try {
if( themeEditorPane.isDirty() )
themeEditorPane.save();
} catch( IOException ex ) {
JOptionPane.showMessageDialog( this,
"Failed to save '" + themeEditorPane.getFileName() + "'\n\nReason: " + ex.getMessage(),
getTitle(), JOptionPane.WARNING_MESSAGE );
if( !themeEditorPane.saveIfDirty() )
return false;
}
}
return true;
}
@@ -132,6 +125,11 @@ public class FlatThemeFileEditor
exit();
}
private void windowActivated() {
for( FlatThemeEditorPane themeEditorPane : getThemeEditorPanes() )
themeEditorPane.reloadIfNecessary();
}
private void windowDeactivated() {
saveAll();
}
@@ -158,6 +156,10 @@ public class FlatThemeFileEditor
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("FlatLaf Theme Editor");
addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
FlatThemeFileEditor.this.windowActivated();
}
@Override
public void windowClosing(WindowEvent e) {
FlatThemeFileEditor.this.windowClosing();

View File

@@ -11,6 +11,7 @@ new FormModel {
"title": "FlatLaf Theme Editor"
addEvent( new FormEvent( "java.awt.event.WindowListener", "windowClosing", "windowClosing", false ) )
addEvent( new FormEvent( "java.awt.event.WindowListener", "windowDeactivated", "windowDeactivated", false ) )
addEvent( new FormEvent( "java.awt.event.WindowListener", "windowActivated", "windowActivated", false ) )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "hidemode 3"
"$columnConstraints": "[fill][grow,fill]"