33import java .awt .EventQueue ;
44import java .awt .event .WindowEvent ;
55import java .awt .event .WindowFocusListener ;
6- import java .nio .file .Files ;
76import java .io .File ;
87import java .io .IOException ;
98import java .lang .reflect .InvocationTargetException ;
@@ -30,7 +29,7 @@ public class ChangeDetector implements WindowFocusListener {
3029 private final Sketch sketch ;
3130 private final Editor editor ;
3231
33- private List <String > ignoredAdditions = new ArrayList <>();
32+ // private List<String> ignoredAdditions = new ArrayList<>();
3433 private List <SketchCode > ignoredRemovals = new ArrayList <>();
3534 private List <SketchCode > ignoredModifications = new ArrayList <>();
3635
@@ -103,12 +102,12 @@ private synchronized void checkFiles() {
103102 .collect (Collectors .toList ());
104103
105104 // Added files that are actually candidates for a new tab
106- List <String > addedTabsFinal = addedFilenames .stream ()
107- .filter (f -> !ignoredAdditions .contains (f ))
108- .collect (Collectors .toList ());
105+ // List<String> addedTabsFinal = addedFilenames.stream()
106+ // .filter(f -> !ignoredAdditions.contains(f))
107+ // .collect(Collectors.toList());
109108
110109 // Take action if there are any added files which were not previously ignored
111- boolean added = !addedTabsFinal .isEmpty ();
110+ boolean added = !addedFilenames .isEmpty ();
112111
113112
114113 // REMOVED FILES
@@ -127,24 +126,30 @@ private synchronized void checkFiles() {
127126 /// MODIFIED FILES
128127
129128 // Get codes which have file with different modification time
130- List <SketchCode > modifiedCodes = Optional .ofNullable (existsMap .get (Boolean .TRUE ))
131- .orElse (Collections .emptyList ())
132- .stream ()
133- .filter (code -> {
134- if (ignoredModifications .contains (code )) return false ;
135- long fileLastModified = code .getFile ().lastModified ();
136- long codeLastModified = code .getLastModified ();
137- long diff = fileLastModified - codeLastModified ;
138- return fileLastModified == 0L || diff > MODIFICATION_WINDOW_MILLIS ;
139- })
140- .collect (Collectors .toList ());
129+ List <SketchCode > modifiedCodes = existsMap .containsKey (Boolean .TRUE ) ?
130+ existsMap .get (Boolean .TRUE ) : Collections .emptyList ();
131+ List <SketchCode > modifiedCodesFinal = new ArrayList <>();
132+ for (SketchCode code : modifiedCodes ) {
133+ if (ignoredModifications .contains (code )) continue ;
134+ long fileLastModified = code .getFile ().lastModified ();
135+ long codeLastModified = code .getLastModified ();
136+ long diff = fileLastModified - codeLastModified ;
137+ if (fileLastModified == 0L || diff > MODIFICATION_WINDOW_MILLIS ) {
138+ modifiedCodesFinal .add (code );
139+ }
140+ }
141141
142142 // Show prompt if any open codes were modified
143- boolean modified = !modifiedCodes .isEmpty ();
143+ boolean modified = !modifiedCodesFinal .isEmpty ();
144+
145+ // Clean ignore lists
146+ ignoredModifications .retainAll (modifiedCodes );
147+ ignoredRemovals .retainAll (removedCodes );
148+
144149
145150 boolean changes = added || removed || modified ;
146151 // Do both PDE and disk change for any one file?
147- List <SketchCode > mergeConflicts = modifiedCodes .stream ()
152+ List <SketchCode > mergeConflicts = modifiedCodesFinal .stream ()
148153 .filter (SketchCode ::isModified )
149154 .collect (Collectors .toList ());
150155 boolean ask = !mergeConflicts .isEmpty () || removed ;
@@ -153,11 +158,11 @@ private synchronized void checkFiles() {
153158 System .out .println ("ask: " + ask + "\n " +
154159 "merge conflicts: " + mergeConflicts + ",\n " +
155160 "added filenames: " + addedFilenames + ",\n " +
156- "added final: " + addedTabsFinal + ",\n " +
157- "ignored added: " + ignoredAdditions + ",\n " +
161+ // "added final: " + addedTabsFinal + ",\n" +
162+ // "ignored added: " + ignoredAdditions + ",\n" +
158163 "removed codes: " + removedCodes + ",\n " +
159164 "ignored removed: " + ignoredRemovals + ",\n " +
160- "modified codes: " + modifiedCodes + "\n " );
165+ "modified codes: " + modifiedCodesFinal + "\n " );
161166 }
162167
163168
@@ -171,18 +176,16 @@ private synchronized void checkFiles() {
171176 // No prompt yet.
172177 if (changes ) {
173178 for (int i = 0 ; i < filenames .size (); i ++) {
174- for (String addedTab : addedTabsFinal ) {
179+ for (String addedTab : addedFilenames ) {
175180 if (filenames .get (i ).equals (addedTab )) {
176181 sketch .loadNewTab (filenames .get (i ), extensions .get (i ), true );
177- break ;
178182 }
179183 }
180184 }
181- for (SketchCode modifiedCode : modifiedCodes ) {
185+ for (SketchCode modifiedCode : modifiedCodesFinal ) {
182186 if (!mergeConflicts .contains (modifiedCode )) {
183187 sketch .loadNewTab (modifiedCode .getFileName (),
184188 modifiedCode .getExtension (), false );
185- break ;
186189 }
187190 }
188191
@@ -209,32 +212,8 @@ private synchronized void checkFiles() {
209212 sketch .loadNewTab (scReload .getFileName (), scReload .getExtension (), false );
210213 },
211214 scKeep -> {
212- try {
213- File file = scKeep .getFile ();
214- File autosave = File .createTempFile (scKeep .getPrettyName (),
215- ".autosave" , file .getParentFile ());
216- // It is platform-dependent whether File.renameTo would let
217- // you overwrite the dummy file autosave, which is used to
218- // make sure all autosave files are unique.
219- Files .move (file .toPath (), autosave .toPath (),
220- java .nio .file .StandardCopyOption .REPLACE_EXISTING );
221- } catch (IOException e ) {
222- Messages .showWarning ("Move failed" ,
223- "Could not move the external editor's"
224- + " version of " + scKeep .getPrettyName () + " to a safe"
225- + " location; make a copy of it before saving the sketch"
226- + " if you need it." , e );
227- ignoredModifications .add (scKeep ); // No infinite loops.
228- return ;
229- }
230- try {
231- scKeep .save ();
232- } catch (IOException e ) {
233- Messages .showWarning ("Save failed" ,
234- "Did not save " + scKeep .getPrettyName () + " after"
235- + " the file was changed." , e );
236- ignoredModifications .add (scKeep ); // No infinite loops.
237- }
215+ scKeep .setLastModified ();
216+ scKeep .setModified (true );
238217 },
239218 scDelete -> sketch .removeCode (scDelete ),
240219 scResave -> {
@@ -274,12 +253,12 @@ private synchronized void checkFiles() {
274253 }
275254
276255 // If something changed, set modified flags and modification times
277- if (!removedCodes .isEmpty () || !modifiedCodes .isEmpty ()) {
278- Stream .concat (removedCodes .stream (), modifiedCodes .stream ())
279- .forEach (code -> {
280- code .setModified (true );
281- code .setLastModified ();
282- });
256+ if (!removedCodes .isEmpty () || !modifiedCodesFinal .isEmpty ()) {
257+ // Stream.concat(removedCodes.stream(), modifiedCodesFinal .stream())
258+ // .forEach(code -> {
259+ // code.setModified(true);
260+ // code.setLastModified();
261+ // });
283262
284263 // Not sure if this is needed
285264 editor .rebuildHeader ();
0 commit comments