diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java index 63e6ee82d..def80bcd2 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java @@ -37,6 +37,8 @@ import javax.swing.*; import java.awt.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -58,6 +60,7 @@ public class PluginWriter extends JFrame private String content; private String pluginName; private File savePath; + private long lastModifiedPluginWriterPane = 0; public PluginWriter(PluginTemplate template) throws IOException { @@ -86,6 +89,15 @@ public void buildGUI() SyntaxLanguage.setLanguage(area, pluginName); content = null; + area.addKeyListener(new KeyAdapter() + { + @Override + public void keyTyped(KeyEvent e) + { + lastModifiedPluginWriterPane = System.currentTimeMillis(); + } + }); + JButton run = new JButton("Run"); JMenuBar menuBar = new JMenuBar(); @@ -170,11 +182,42 @@ public void runPlugin() try { - //write to temporary file location - if(savePath != null) - Files.copy(savePath, tempFile); - else - Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); + if(savePath != null) //opened a plugin from (Plugins>Open Plugin or Plugins>Recent Plugins) + { + //original save path should be overwritten + if(savePath.lastModified() <= lastModifiedPluginWriterPane) + { + Files.write(area.getText().getBytes(StandardCharsets.UTF_8), savePath); //overwrite original plugin location with new data + Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location + } + else + { + Files.copy(savePath, tempFile); //write to temporary file location + + //update content from latest disk data + content = DiskReader.loadAsString(savePath.getAbsolutePath()); + + //update plugin writer UI on disk update + SwingUtilities.invokeLater(()-> + { + try + { + int caretPosition = area.getCaretPosition(); + + area.setText(content); + area.setCaretPosition(caretPosition); + } + catch (Exception e) + { + e.printStackTrace(); + } + }); + } + } + else //temp plugin editing (Plugins>New Java Plugin>Run) + { + Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location + } //run plugin from that location PluginManager.runPlugin(tempFile); @@ -232,6 +275,7 @@ public void save() DiskWriter.replaceFile(savePath.getAbsolutePath(), area.getText(), false); addRecentPlugin(savePath); }, "Plugin Editor Save"); + exportThread.start(); }