Skip to content

Commit 805dac8

Browse files
committed
Better Plugin Writing
Resolves #394 You can now write plugins from the Plugin Writer in BCV, or from disk. BCV will read the last edit dates on both and use the latest version. As a bonus BCV will self-update the pane to keep the BCV plugin writer synced with your local file. Local files are also overwritten when the plugin writer is used to edit the plugin, then ran.
1 parent c29f0fb commit 805dac8

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

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

+49-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
import javax.swing.*;
3939
import java.awt.*;
40+
import java.awt.event.KeyAdapter;
41+
import java.awt.event.KeyEvent;
4042
import java.io.File;
4143
import java.io.IOException;
4244
import java.nio.charset.StandardCharsets;
@@ -58,6 +60,7 @@ public class PluginWriter extends JFrame
5860
private String content;
5961
private String pluginName;
6062
private File savePath;
63+
private long lastModifiedPluginWriterPane = 0;
6164

6265
public PluginWriter(PluginTemplate template) throws IOException
6366
{
@@ -86,6 +89,15 @@ public void buildGUI()
8689
SyntaxLanguage.setLanguage(area, pluginName);
8790
content = null;
8891

92+
area.addKeyListener(new KeyAdapter()
93+
{
94+
@Override
95+
public void keyTyped(KeyEvent e)
96+
{
97+
lastModifiedPluginWriterPane = System.currentTimeMillis();
98+
}
99+
});
100+
89101
JButton run = new JButton("Run");
90102

91103
JMenuBar menuBar = new JMenuBar();
@@ -170,11 +182,42 @@ public void runPlugin()
170182

171183
try
172184
{
173-
//write to temporary file location
174-
if(savePath != null)
175-
Files.copy(savePath, tempFile);
176-
else
177-
Files.write(area.getText().getBytes(StandardCharsets.UTF_8), tempFile);
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+
}
178221

179222
//run plugin from that location
180223
PluginManager.runPlugin(tempFile);
@@ -232,6 +275,7 @@ public void save()
232275
DiskWriter.replaceFile(savePath.getAbsolutePath(), area.getText(), false);
233276
addRecentPlugin(savePath);
234277
}, "Plugin Editor Save");
278+
235279
exportThread.start();
236280
}
237281

0 commit comments

Comments
 (0)