From 8b84ae74c0264ef2f14629ea8bdd1421fa48218f Mon Sep 17 00:00:00 2001 From: Konloch Date: Sun, 29 Sep 2024 21:16:11 -0600 Subject: [PATCH] Better Synchronization Between On-Disk Editing and In-Memory Editing Probably introduced a bug or two. Compare issues with the previous implementation as that was tested slightly more. --- .../bytecodeviewer/plugin/PluginWriter.java | 114 ++++++++++++------ 1 file changed, 78 insertions(+), 36 deletions(-) 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 def80bcd2..82d4db3b8 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java @@ -19,6 +19,8 @@ package the.bytecode.club.bytecodeviewer.plugin; import com.google.common.io.Files; +import com.konloch.taskmanager.Task; +import com.konloch.taskmanager.TaskRunnable; import me.konloch.kontainer.io.DiskReader; import me.konloch.kontainer.io.DiskWriter; import org.apache.commons.compress.utils.FileNameUtils; @@ -36,9 +38,12 @@ import the.bytecode.club.bytecodeviewer.util.SyntaxLanguage; import javax.swing.*; +import javax.swing.text.DefaultCaret; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -66,6 +71,7 @@ public PluginWriter(PluginTemplate template) throws IOException { this.content = template.getContents(); this.pluginName = "Template." + template.getExtension(); + buildGUI(); } @@ -73,6 +79,7 @@ public PluginWriter(String content, String pluginName) { this.content = content; this.pluginName = pluginName; + buildGUI(); } @@ -86,9 +93,13 @@ public void buildGUI() area.setOnCtrlS(this::save); area.setText(content); area.setCaretPosition(0); + DefaultCaret caret = (DefaultCaret)area.getCaret(); + caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE); SyntaxLanguage.setLanguage(area, pluginName); content = null; + lastModifiedPluginWriterPane = System.currentTimeMillis(); + area.addKeyListener(new KeyAdapter() { @Override @@ -98,6 +109,19 @@ public void keyTyped(KeyEvent e) } }); + //TODO this could be replaced with a file watch service + // I'll probably come back and fix this in the future, but if anyone needs to replace it: + // - https://github.com/Konloch/GitWatch4J/ has a base you can use + + //every 1 second, read the file timestamps and if the file has changed throw trigger an update + BytecodeViewer.getTaskManager().delayLoop(1_000, task -> + { + if(!area.isValid()) + task.stop(); + else + updateUIFromDiskChanges(null); + }); + JButton run = new JButton("Run"); JMenuBar menuBar = new JMenuBar(); @@ -182,42 +206,8 @@ public void runPlugin() try { - 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 - } + //update the UI from disk changes / write to disk if plugin writer input has been modified + updateUIFromDiskChanges(tempFile); //run plugin from that location PluginManager.runPlugin(tempFile); @@ -286,6 +276,58 @@ public void setSourceFile(File file) menuSaveAs.updateUI(); menuSave.updateUI(); savePath = file; + setPluginName(file.getName()); } + + public synchronized void updateUIFromDiskChanges(File tempFile) + { + try + { + //opened a plugin from (Plugins>Open Plugin or Plugins>Recent Plugins) + if (savePath != null) + { + if(savePath.lastModified() <= lastModifiedPluginWriterPane) + { + if(tempFile != null) //when user clicks 'Run' instead of running every second + { + //original save path should be overwritten + 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 + { + //update content from latest disk data + content = DiskReader.loadAsString(savePath.getAbsolutePath()); + + //update plugin writer UI on disk update + SwingUtilities.invokeLater(() -> + { + try + { + area.setText(content); + } + catch (Exception e) + { + e.printStackTrace(); + } + }); + + lastModifiedPluginWriterPane = System.currentTimeMillis(); + + if(tempFile != null) + Files.write(content.getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location + } + } + else if(tempFile != null)//temp plugin editing (Plugins>New Java Plugin>Run) + { + Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } }