3
3
import java .awt .EventQueue ;
4
4
import java .awt .event .WindowEvent ;
5
5
import java .awt .event .WindowFocusListener ;
6
- import java .nio .file .Files ;
7
6
import java .io .File ;
8
7
import java .io .IOException ;
9
8
import java .lang .reflect .InvocationTargetException ;
@@ -30,7 +29,7 @@ public class ChangeDetector implements WindowFocusListener {
30
29
private final Sketch sketch ;
31
30
private final Editor editor ;
32
31
33
- private List <String > ignoredAdditions = new ArrayList <>();
32
+ // private List<String> ignoredAdditions = new ArrayList<>();
34
33
private List <SketchCode > ignoredRemovals = new ArrayList <>();
35
34
private List <SketchCode > ignoredModifications = new ArrayList <>();
36
35
@@ -103,12 +102,12 @@ private synchronized void checkFiles() {
103
102
.collect (Collectors .toList ());
104
103
105
104
// 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());
109
108
110
109
// Take action if there are any added files which were not previously ignored
111
- boolean added = !addedTabsFinal .isEmpty ();
110
+ boolean added = !addedFilenames .isEmpty ();
112
111
113
112
114
113
// REMOVED FILES
@@ -127,24 +126,30 @@ private synchronized void checkFiles() {
127
126
/// MODIFIED FILES
128
127
129
128
// 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
+ }
141
141
142
142
// 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
+
144
149
145
150
boolean changes = added || removed || modified ;
146
151
// Do both PDE and disk change for any one file?
147
- List <SketchCode > mergeConflicts = modifiedCodes .stream ()
152
+ List <SketchCode > mergeConflicts = modifiedCodesFinal .stream ()
148
153
.filter (SketchCode ::isModified )
149
154
.collect (Collectors .toList ());
150
155
boolean ask = !mergeConflicts .isEmpty () || removed ;
@@ -153,11 +158,11 @@ private synchronized void checkFiles() {
153
158
System .out .println ("ask: " + ask + "\n " +
154
159
"merge conflicts: " + mergeConflicts + ",\n " +
155
160
"added filenames: " + addedFilenames + ",\n " +
156
- "added final: " + addedTabsFinal + ",\n " +
157
- "ignored added: " + ignoredAdditions + ",\n " +
161
+ // "added final: " + addedTabsFinal + ",\n" +
162
+ // "ignored added: " + ignoredAdditions + ",\n" +
158
163
"removed codes: " + removedCodes + ",\n " +
159
164
"ignored removed: " + ignoredRemovals + ",\n " +
160
- "modified codes: " + modifiedCodes + "\n " );
165
+ "modified codes: " + modifiedCodesFinal + "\n " );
161
166
}
162
167
163
168
@@ -171,18 +176,16 @@ private synchronized void checkFiles() {
171
176
// No prompt yet.
172
177
if (changes ) {
173
178
for (int i = 0 ; i < filenames .size (); i ++) {
174
- for (String addedTab : addedTabsFinal ) {
179
+ for (String addedTab : addedFilenames ) {
175
180
if (filenames .get (i ).equals (addedTab )) {
176
181
sketch .loadNewTab (filenames .get (i ), extensions .get (i ), true );
177
- break ;
178
182
}
179
183
}
180
184
}
181
- for (SketchCode modifiedCode : modifiedCodes ) {
185
+ for (SketchCode modifiedCode : modifiedCodesFinal ) {
182
186
if (!mergeConflicts .contains (modifiedCode )) {
183
187
sketch .loadNewTab (modifiedCode .getFileName (),
184
188
modifiedCode .getExtension (), false );
185
- break ;
186
189
}
187
190
}
188
191
@@ -209,32 +212,8 @@ private synchronized void checkFiles() {
209
212
sketch .loadNewTab (scReload .getFileName (), scReload .getExtension (), false );
210
213
},
211
214
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 );
238
217
},
239
218
scDelete -> sketch .removeCode (scDelete ),
240
219
scResave -> {
@@ -274,12 +253,12 @@ private synchronized void checkFiles() {
274
253
}
275
254
276
255
// 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
+ // });
283
262
284
263
// Not sure if this is needed
285
264
editor .rebuildHeader ();
0 commit comments