diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java index f8384290..214df023 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java @@ -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 { - textArea.save(); + 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; + } } diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.java index aa585931..0dedf981 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.java @@ -95,7 +95,7 @@ public class FlatThemeFileEditor } Supplier titleFun = () -> { - return (themeEditorArea.isDirty() ? "*" : "") + return (themeEditorArea.isDirty() ? "* " : "") + StringUtils.removeTrailing( themeEditorArea.getFileName(), ".properties" ); }; @@ -110,15 +110,8 @@ 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(); diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.jfd b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.jfd index fe8bd768..98c68d40 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.jfd +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.jfd @@ -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]"