Skip to content

Commit 8b84ae7

Browse files
committed
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.
1 parent d343bce commit 8b84ae7

File tree

1 file changed

+78
-36
lines changed

1 file changed

+78
-36
lines changed

src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginWriter.java

+78-36
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package the.bytecode.club.bytecodeviewer.plugin;
2020

2121
import com.google.common.io.Files;
22+
import com.konloch.taskmanager.Task;
23+
import com.konloch.taskmanager.TaskRunnable;
2224
import me.konloch.kontainer.io.DiskReader;
2325
import me.konloch.kontainer.io.DiskWriter;
2426
import org.apache.commons.compress.utils.FileNameUtils;
@@ -36,9 +38,12 @@
3638
import the.bytecode.club.bytecodeviewer.util.SyntaxLanguage;
3739

3840
import javax.swing.*;
41+
import javax.swing.text.DefaultCaret;
3942
import java.awt.*;
4043
import java.awt.event.KeyAdapter;
4144
import java.awt.event.KeyEvent;
45+
import java.awt.event.MouseAdapter;
46+
import java.awt.event.MouseEvent;
4247
import java.io.File;
4348
import java.io.IOException;
4449
import java.nio.charset.StandardCharsets;
@@ -66,13 +71,15 @@ public PluginWriter(PluginTemplate template) throws IOException
6671
{
6772
this.content = template.getContents();
6873
this.pluginName = "Template." + template.getExtension();
74+
6975
buildGUI();
7076
}
7177

7278
public PluginWriter(String content, String pluginName)
7379
{
7480
this.content = content;
7581
this.pluginName = pluginName;
82+
7683
buildGUI();
7784
}
7885

@@ -86,9 +93,13 @@ public void buildGUI()
8693
area.setOnCtrlS(this::save);
8794
area.setText(content);
8895
area.setCaretPosition(0);
96+
DefaultCaret caret = (DefaultCaret)area.getCaret();
97+
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
8998
SyntaxLanguage.setLanguage(area, pluginName);
9099
content = null;
91100

101+
lastModifiedPluginWriterPane = System.currentTimeMillis();
102+
92103
area.addKeyListener(new KeyAdapter()
93104
{
94105
@Override
@@ -98,6 +109,19 @@ public void keyTyped(KeyEvent e)
98109
}
99110
});
100111

112+
//TODO this could be replaced with a file watch service
113+
// I'll probably come back and fix this in the future, but if anyone needs to replace it:
114+
// - https://github.com/Konloch/GitWatch4J/ has a base you can use
115+
116+
//every 1 second, read the file timestamps and if the file has changed throw trigger an update
117+
BytecodeViewer.getTaskManager().delayLoop(1_000, task ->
118+
{
119+
if(!area.isValid())
120+
task.stop();
121+
else
122+
updateUIFromDiskChanges(null);
123+
});
124+
101125
JButton run = new JButton("Run");
102126

103127
JMenuBar menuBar = new JMenuBar();
@@ -182,42 +206,8 @@ public void runPlugin()
182206

183207
try
184208
{
185-
if(savePath != null) //opened a plugin from (Plugins>Open Plugin or Plugins>Recent Plugins)
186-
{
187-
//original save path should be overwritten
188-
if(savePath.lastModified() <= lastModifiedPluginWriterPane)
189-
{
190-
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), savePath); //overwrite original plugin location with new data
191-
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location
192-
}
193-
else
194-
{
195-
Files.copy(savePath, tempFile); //write to temporary file location
196-
197-
//update content from latest disk data
198-
content = DiskReader.loadAsString(savePath.getAbsolutePath());
199-
200-
//update plugin writer UI on disk update
201-
SwingUtilities.invokeLater(()->
202-
{
203-
try
204-
{
205-
int caretPosition = area.getCaretPosition();
206-
207-
area.setText(content);
208-
area.setCaretPosition(caretPosition);
209-
}
210-
catch (Exception e)
211-
{
212-
e.printStackTrace();
213-
}
214-
});
215-
}
216-
}
217-
else //temp plugin editing (Plugins>New Java Plugin>Run)
218-
{
219-
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location
220-
}
209+
//update the UI from disk changes / write to disk if plugin writer input has been modified
210+
updateUIFromDiskChanges(tempFile);
221211

222212
//run plugin from that location
223213
PluginManager.runPlugin(tempFile);
@@ -286,6 +276,58 @@ public void setSourceFile(File file)
286276
menuSaveAs.updateUI();
287277
menuSave.updateUI();
288278
savePath = file;
279+
289280
setPluginName(file.getName());
290281
}
282+
283+
public synchronized void updateUIFromDiskChanges(File tempFile)
284+
{
285+
try
286+
{
287+
//opened a plugin from (Plugins>Open Plugin or Plugins>Recent Plugins)
288+
if (savePath != null)
289+
{
290+
if(savePath.lastModified() <= lastModifiedPluginWriterPane)
291+
{
292+
if(tempFile != null) //when user clicks 'Run' instead of running every second
293+
{
294+
//original save path should be overwritten
295+
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), savePath); //overwrite original plugin location with new data
296+
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location
297+
}
298+
}
299+
else
300+
{
301+
//update content from latest disk data
302+
content = DiskReader.loadAsString(savePath.getAbsolutePath());
303+
304+
//update plugin writer UI on disk update
305+
SwingUtilities.invokeLater(() ->
306+
{
307+
try
308+
{
309+
area.setText(content);
310+
}
311+
catch (Exception e)
312+
{
313+
e.printStackTrace();
314+
}
315+
});
316+
317+
lastModifiedPluginWriterPane = System.currentTimeMillis();
318+
319+
if(tempFile != null)
320+
Files.write(content.getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location
321+
}
322+
}
323+
else if(tempFile != null)//temp plugin editing (Plugins>New Java Plugin>Run)
324+
{
325+
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile); //write to temporary file location
326+
}
327+
}
328+
catch (Exception e)
329+
{
330+
e.printStackTrace();
331+
}
332+
}
291333
}

0 commit comments

Comments
 (0)