11package processing .app ;
22
3+ import java .awt .EventQueue ;
4+ import java .awt .Frame ;
35import java .awt .event .WindowEvent ;
46import java .awt .event .WindowFocusListener ;
57import java .io .File ;
68import java .io .FilenameFilter ;
79import java .io .IOException ;
10+ import java .lang .reflect .InvocationTargetException ;
811
912public class ChangeDetector implements WindowFocusListener {
1013 private Sketch sketch ;
@@ -46,20 +49,84 @@ public void run() {
4649 th .start ();
4750 }
4851
52+ private void showErrorAsync (final String title , final String message ,
53+ final Exception e ) {
54+ EventQueue .invokeLater (new Runnable () {
55+ @ Override
56+ public void run () {
57+ Base .showError (title , message , e );
58+ }
59+ });
60+ }
61+
62+ private void showWarningAsync (final String title , final String message ) {
63+ EventQueue .invokeLater (new Runnable () {
64+ @ Override
65+ public void run () {
66+ Base .showWarning (title , message );
67+ }
68+ });
69+ }
70+
71+ private void showWarningTieredAsync (final String title ,
72+ final String message1 ,
73+ final String message2 , final Exception e ) {
74+ EventQueue .invokeLater (new Runnable () {
75+ @ Override
76+ public void run () {
77+ Base .showWarningTiered (title , message1 , message2 , e );
78+ }
79+ });
80+ }
81+
82+ private int showYesNoQuestionAsync (final Frame editor , final String title ,
83+ final String message1 ,
84+ final String message2 ) {
85+ final int [] res = { -1 };
86+ try {
87+ //have to wait for a response on this one
88+ EventQueue .invokeAndWait (new Runnable () {
89+ @ Override
90+ public void run () {
91+ res [0 ] = Base .showYesNoQuestion (editor , title , message1 , message2 );
92+ }
93+ });
94+ } catch (InvocationTargetException e ) {
95+ //occurs if Base.showYesNoQuestion throws an error, so, shouldn't happen
96+ e .printStackTrace ();
97+ } catch (InterruptedException e ) {
98+ //occurs if the EDT is interrupted, so, shouldn't happen
99+ e .printStackTrace ();
100+ }
101+ return res [0 ];
102+ }
103+
104+ private void rebuildHeaderAsync () {
105+ EventQueue .invokeLater (new Runnable () {
106+ @ Override
107+ public void run () {
108+ editor .header .rebuild ();
109+ }
110+ });
111+ }
112+
49113 private void checkFileChange () {
50114 //check that the content of each of the files in sketch matches what is in memory
51115 if (sketch == null ) {
52116 return ;
53117 }
54118
119+ //make sure the sketch folder exists at all. if it does not, it will be re-saved, and no changes will be detected
120+ //
121+ sketch .ensureExistence ();
55122 //check file count first
56123 File sketchFolder = sketch .getFolder ();
57124 int fileCount = sketchFolder .list (new FilenameFilter () {
58125 //return true if the file is a code file for this mode
59126 @ Override
60127 public boolean accept (File dir , String name ) {
61128 for (String s : editor .getMode ().getExtensions ()) {
62- if (name .endsWith (s )) {
129+ if (name .toLowerCase (). endsWith (s . toLowerCase () )) {
63130 return true ;
64131 }
65132 }
@@ -68,14 +135,14 @@ public boolean accept(File dir, String name) {
68135 }).length ;
69136
70137 if (fileCount != sketch .getCodeCount ()) {
71- reloadSketch ( null );
72- if (fileCount < 1 ) {
138+ //if they chose to reload and there aren't any files left
139+ if (reloadSketch ( null ) && fileCount < 1 ) {
73140 try {
74141 //make a blank file
75142 sketch .getMainFile ().createNewFile ();
76143 } catch (Exception e1 ) {
77144 //if that didn't work, tell them it's un-recoverable
78- Base . showError ("Reload failed" , "The sketch contains no code files." ,
145+ showErrorAsync ("Reload failed" , "The sketch contains no code files." ,
79146 e1 );
80147 //don't try to reload again after the double fail
81148 //this editor is probably trashed by this point, but a save-as might be possible
@@ -84,11 +151,9 @@ public boolean accept(File dir, String name) {
84151 }
85152 //it's okay to do this without confirmation, because they already confirmed to deleting the unsaved changes above
86153 sketch .reload ();
87- editor .header .rebuild ();
88- Base
89- .showWarning ("Modified Reload" ,
90- "You cannot delete the last code file in a sketch.\n "
91- + "A new blank sketch file has been generated for you." );
154+ showWarningAsync ("Modified Reload" ,
155+ "You cannot delete the last code file in a sketch.\n "
156+ + "A new blank sketch file has been generated for you." );
92157
93158 }
94159 return ;
@@ -108,10 +173,9 @@ public boolean accept(File dir, String name) {
108173 try {
109174 onDisk = Base .loadFile (sketchFile );
110175 } catch (IOException e1 ) {
111- Base
112- .showWarningTiered ("File Change Detection Failed" ,
113- "Checking for changed files for this sketch has failed." ,
114- "The file change detector will be disabled." , e1 );
176+ showWarningTieredAsync ("File Change Detection Failed" ,
177+ "Checking for changed files for this sketch has failed." ,
178+ "The file change detector will be disabled." , e1 );
115179 enabled = false ;
116180 return ;
117181 }
@@ -130,18 +194,17 @@ private void setSketchCodeModified(SketchCode sc) {
130194 }
131195
132196 //returns true if the files in the sketch have been reloaded
133- private void reloadSketch (SketchCode changed ) {
134- int response = Base
135- .showYesNoQuestion (editor ,
136- "File Modified" ,
137- "Your sketch has been modified externally.<br>Would you like to reload the sketch?" ,
138- "If you reload the sketch, any unsaved changes will be lost!" );
197+ private boolean reloadSketch (SketchCode changed ) {
198+ int response = showYesNoQuestionAsync (editor ,
199+ "File Modified" ,
200+ "Your sketch has been modified externally.<br>Would you like to reload the sketch?" ,
201+ "If you reload the sketch, any unsaved changes will be lost!" );
139202 if (response == 0 ) {
140203 //reload the sketch
141204
142205 sketch .reload ();
143- editor . header . rebuild ();
144-
206+ rebuildHeaderAsync ();
207+ return true ;
145208 } else {
146209 //they said no, make it possible for them to stop the errors by saving
147210 if (changed != null ) {
@@ -157,11 +220,10 @@ private void reloadSketch(SketchCode changed) {
157220 }
158221 //if files were simply added, then nothing needs done
159222 }
160- editor . header . rebuild ();
223+ rebuildHeaderAsync ();
161224 skip = true ;
162- return ;
225+ return false ;
163226 }
164- return ;
165227 }
166228
167229 @ Override
0 commit comments