Skip to content

Commit

Permalink
Better Plugin Writing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Konloch committed Sep 30, 2024
1 parent c29f0fb commit 805dac8
Showing 1 changed file with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -232,6 +275,7 @@ public void save()
DiskWriter.replaceFile(savePath.getAbsolutePath(), area.getText(), false);
addRecentPlugin(savePath);
}, "Plugin Editor Save");

exportThread.start();
}

Expand Down

0 comments on commit 805dac8

Please sign in to comment.