19
19
package the .bytecode .club .bytecodeviewer .plugin ;
20
20
21
21
import com .google .common .io .Files ;
22
+ import com .konloch .taskmanager .Task ;
23
+ import com .konloch .taskmanager .TaskRunnable ;
22
24
import me .konloch .kontainer .io .DiskReader ;
23
25
import me .konloch .kontainer .io .DiskWriter ;
24
26
import org .apache .commons .compress .utils .FileNameUtils ;
36
38
import the .bytecode .club .bytecodeviewer .util .SyntaxLanguage ;
37
39
38
40
import javax .swing .*;
41
+ import javax .swing .text .DefaultCaret ;
39
42
import java .awt .*;
40
43
import java .awt .event .KeyAdapter ;
41
44
import java .awt .event .KeyEvent ;
45
+ import java .awt .event .MouseAdapter ;
46
+ import java .awt .event .MouseEvent ;
42
47
import java .io .File ;
43
48
import java .io .IOException ;
44
49
import java .nio .charset .StandardCharsets ;
@@ -66,13 +71,15 @@ public PluginWriter(PluginTemplate template) throws IOException
66
71
{
67
72
this .content = template .getContents ();
68
73
this .pluginName = "Template." + template .getExtension ();
74
+
69
75
buildGUI ();
70
76
}
71
77
72
78
public PluginWriter (String content , String pluginName )
73
79
{
74
80
this .content = content ;
75
81
this .pluginName = pluginName ;
82
+
76
83
buildGUI ();
77
84
}
78
85
@@ -86,9 +93,13 @@ public void buildGUI()
86
93
area .setOnCtrlS (this ::save );
87
94
area .setText (content );
88
95
area .setCaretPosition (0 );
96
+ DefaultCaret caret = (DefaultCaret )area .getCaret ();
97
+ caret .setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
89
98
SyntaxLanguage .setLanguage (area , pluginName );
90
99
content = null ;
91
100
101
+ lastModifiedPluginWriterPane = System .currentTimeMillis ();
102
+
92
103
area .addKeyListener (new KeyAdapter ()
93
104
{
94
105
@ Override
@@ -98,6 +109,19 @@ public void keyTyped(KeyEvent e)
98
109
}
99
110
});
100
111
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
+
101
125
JButton run = new JButton ("Run" );
102
126
103
127
JMenuBar menuBar = new JMenuBar ();
@@ -182,42 +206,8 @@ public void runPlugin()
182
206
183
207
try
184
208
{
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 );
221
211
222
212
//run plugin from that location
223
213
PluginManager .runPlugin (tempFile );
@@ -286,6 +276,58 @@ public void setSourceFile(File file)
286
276
menuSaveAs .updateUI ();
287
277
menuSave .updateUI ();
288
278
savePath = file ;
279
+
289
280
setPluginName (file .getName ());
290
281
}
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
+ }
291
333
}
0 commit comments