37
37
38
38
import javax .swing .*;
39
39
import java .awt .*;
40
+ import java .awt .event .KeyAdapter ;
41
+ import java .awt .event .KeyEvent ;
40
42
import java .io .File ;
41
43
import java .io .IOException ;
42
44
import java .nio .charset .StandardCharsets ;
@@ -58,6 +60,7 @@ public class PluginWriter extends JFrame
58
60
private String content ;
59
61
private String pluginName ;
60
62
private File savePath ;
63
+ private long lastModifiedPluginWriterPane = 0 ;
61
64
62
65
public PluginWriter (PluginTemplate template ) throws IOException
63
66
{
@@ -86,6 +89,15 @@ public void buildGUI()
86
89
SyntaxLanguage .setLanguage (area , pluginName );
87
90
content = null ;
88
91
92
+ area .addKeyListener (new KeyAdapter ()
93
+ {
94
+ @ Override
95
+ public void keyTyped (KeyEvent e )
96
+ {
97
+ lastModifiedPluginWriterPane = System .currentTimeMillis ();
98
+ }
99
+ });
100
+
89
101
JButton run = new JButton ("Run" );
90
102
91
103
JMenuBar menuBar = new JMenuBar ();
@@ -170,11 +182,42 @@ public void runPlugin()
170
182
171
183
try
172
184
{
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
+ }
178
221
179
222
//run plugin from that location
180
223
PluginManager .runPlugin (tempFile );
@@ -232,6 +275,7 @@ public void save()
232
275
DiskWriter .replaceFile (savePath .getAbsolutePath (), area .getText (), false );
233
276
addRecentPlugin (savePath );
234
277
}, "Plugin Editor Save" );
278
+
235
279
exportThread .start ();
236
280
}
237
281
0 commit comments