3737
3838import javax .swing .*;
3939import java .awt .*;
40+ import java .awt .event .KeyAdapter ;
41+ import java .awt .event .KeyEvent ;
4042import java .io .File ;
4143import java .io .IOException ;
4244import 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