Skip to content

Commit 850ced6

Browse files
committed
Run change detector on EDT
1 parent 014c9c5 commit 850ced6

1 file changed

Lines changed: 79 additions & 102 deletions

File tree

app/src/processing/app/ui/ChangeDetector.java

Lines changed: 79 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
package processing.app.ui;
22

3-
import java.awt.EventQueue;
43
import java.awt.event.WindowEvent;
54
import java.awt.event.WindowFocusListener;
65
import java.io.File;
76
import java.io.IOException;
8-
import java.lang.reflect.InvocationTargetException;
97
import java.util.ArrayList;
108
import java.util.Arrays;
119
import java.util.Collections;
1210
import java.util.List;
1311
import java.util.Map;
1412
import java.util.Optional;
1513
import java.util.function.Consumer;
16-
import java.util.concurrent.ForkJoinPool;
1714
import java.util.stream.Collectors;
18-
import java.util.stream.Stream;
1915

2016
import processing.app.Language;
2117
import processing.app.Messages;
22-
import processing.app.Platform;
2318
import processing.app.Preferences;
2419
import processing.app.Sketch;
2520
import processing.app.SketchCode;
@@ -59,12 +54,7 @@ public void windowGainedFocus(WindowEvent e) {
5954
// if it does not, it will be re-saved, and no changes will be detected
6055
sketch.ensureExistence(); // <- touches UI, stay on EDT
6156

62-
// TODO: Not sure if we even need to run this async. Usually takes
63-
// just a few ms and we probably want to prevent any changes from
64-
// users until the external changes are sorted out. [jv 2016-12-05]
65-
66-
// Run task in common pool, starting threads directly is so Java 6
67-
ForkJoinPool.commonPool().execute(this::checkFiles);
57+
checkFiles();
6858
}
6959
}
7060
}
@@ -77,8 +67,7 @@ public void windowLostFocus(WindowEvent e) {
7767
}
7868

7969

80-
// Synchronize, we are running async and touching fields
81-
private synchronized void checkFiles() {
70+
private void checkFiles() {
8271
List<String> filenames = new ArrayList<>();
8372
List<String> extensions = new ArrayList<>();
8473
sketch.getSketchCodeFiles(filenames, extensions);
@@ -166,113 +155,101 @@ private synchronized void checkFiles() {
166155
}
167156

168157

169-
// This has to happen in one go and also touches UI everywhere. It has to
170-
// run on EDT, otherwise windowGainedFocus callback runs again right after
171-
// dismissing the prompt and we get another prompt before we even finished.
172-
try {
173-
// Wait for EDT to finish its business
174-
// We need to stay in synchronized scope because of ignore lists
175-
EventQueue.invokeAndWait(() -> {
176-
// No prompt yet.
177-
if (changes) {
178-
for (int i = 0; i < filenames.size(); i++) {
179-
for (String addedTab : addedFilenames) {
180-
if (filenames.get(i).equals(addedTab)) {
181-
sketch.loadNewTab(filenames.get(i), extensions.get(i), true);
182-
}
183-
}
184-
}
185-
for (SketchCode modifiedCode : modifiedCodesFinal) {
186-
if (!mergeConflicts.contains(modifiedCode)) {
187-
sketch.loadNewTab(modifiedCode.getFileName(),
188-
modifiedCode.getExtension(), false);
189-
}
158+
// No prompt yet.
159+
if (changes) {
160+
for (int i = 0; i < filenames.size(); i++) {
161+
for (String addedTab : addedFilenames) {
162+
if (filenames.get(i).equals(addedTab)) {
163+
sketch.loadNewTab(filenames.get(i), extensions.get(i), true);
190164
}
165+
}
166+
}
167+
for (SketchCode modifiedCode : modifiedCodesFinal) {
168+
if (!mergeConflicts.contains(modifiedCode)) {
169+
sketch.loadNewTab(modifiedCode.getFileName(),
170+
modifiedCode.getExtension(), false);
171+
}
172+
}
191173

192-
// Destructive actions, so prompt.
193-
if (ask) {
194-
sketch.updateSketchCodes();
195-
196-
showReloadPrompt(mergeConflicts, removedCodesFinal,
197-
scReload -> {
198-
try {
199-
File file = scReload.getFile();
200-
File autosave = File.createTempFile(scReload.getPrettyName(),
201-
".autosave", file.getParentFile());
202-
scReload.copyTo(autosave);
203-
} catch (IOException e) {
204-
Messages.showWarning("Could not autosave modified tab",
205-
"Your changes to " + scReload.getPrettyName() +
206-
" have not been saved, so we won't load the new version.", e);
207-
scReload.setModified(true); // So we'll have another go at saving
208-
// it later,
209-
ignoredModifications.add(scReload); // but not create a loop.
210-
return;
211-
}
212-
sketch.loadNewTab(scReload.getFileName(), scReload.getExtension(), false);
213-
},
214-
scKeep -> {
215-
scKeep.setLastModified();
216-
scKeep.setModified(true);
217-
},
218-
scDelete -> sketch.removeCode(scDelete),
219-
scResave -> {
220-
try {
221-
scResave.save();
222-
} catch (IOException e) {
223-
if (sketch.getCode(0).equals(scResave)) {
224-
// Not a fatal error; the sketch has to stay open if
225-
// they're going to save the code that's in it.
226-
Messages.showWarning(
227-
scResave.getFileName() + " deleted and not re-saved",
228-
"Your main tab was deleted, and Processing couldn't " +
229-
"resave it.\nYour sketch won't work without the " +
230-
"main tab.", e);
231-
} else {
232-
Messages.showWarning("Could not re-save deleted tab",
233-
"Your copy of " + scResave.getPrettyName() +
234-
" will stay in the editor.", e);
235-
}
236-
ignoredRemovals.add(scResave);
237-
scResave.setModified(true); // So we'll have another go at
238-
// saving it later.
239-
}
174+
// Destructive actions, so prompt.
175+
if (ask) {
176+
sketch.updateSketchCodes();
177+
178+
showReloadPrompt(mergeConflicts, removedCodesFinal,
179+
scReload -> {
180+
try {
181+
File file = scReload.getFile();
182+
File autosave = File.createTempFile(scReload.getPrettyName(),
183+
".autosave", file.getParentFile());
184+
scReload.copyTo(autosave);
185+
} catch (IOException e) {
186+
Messages.showWarning("Could not autosave modified tab",
187+
"Your changes to " + scReload.getPrettyName() +
188+
" have not been saved, so we won't load the new version.", e);
189+
scReload.setModified(true); // So we'll have another go at saving
190+
// it later,
191+
ignoredModifications.add(scReload); // but not create a loop.
192+
return;
193+
}
194+
sketch.loadNewTab(scReload.getFileName(), scReload.getExtension(), false);
195+
},
196+
scKeep -> {
197+
scKeep.setLastModified();
198+
scKeep.setModified(true);
199+
},
200+
scDelete -> sketch.removeCode(scDelete),
201+
scResave -> {
202+
try {
203+
scResave.save();
204+
} catch (IOException e) {
205+
if (sketch.getCode(0).equals(scResave)) {
206+
// Not a fatal error; the sketch has to stay open if
207+
// they're going to save the code that's in it.
208+
Messages.showWarning(
209+
scResave.getFileName() + " deleted and not re-saved",
210+
"Your main tab was deleted, and Processing couldn't " +
211+
"resave it.\nYour sketch won't work without the " +
212+
"main tab.", e);
213+
} else {
214+
Messages.showWarning("Could not re-save deleted tab",
215+
"Your copy of " + scResave.getPrettyName() +
216+
" will stay in the editor.", e);
240217
}
241-
);
218+
ignoredRemovals.add(scResave);
219+
scResave.setModified(true); // So we'll have another go at
220+
// saving it later.
221+
}
242222
}
243-
editor.rebuildHeader();
244-
sketch.handleNextCode();
245-
sketch.handlePrevCode();
246-
editor.repaintHeader();
223+
);
224+
}
225+
editor.rebuildHeader();
226+
sketch.handleNextCode();
227+
sketch.handlePrevCode();
228+
editor.repaintHeader();
247229

248-
// Sketch was reloaded, clear ignore lists
249-
//ignoredAdditions.clear();
250-
//ignoredRemovals.clear();
230+
// Sketch was reloaded, clear ignore lists
231+
//ignoredAdditions.clear();
232+
//ignoredRemovals.clear();
251233

252-
return;
253-
}
234+
return;
235+
}
254236

255-
// If something changed, set modified flags and modification times
256-
if (!removedCodes.isEmpty() || !modifiedCodesFinal.isEmpty()) {
237+
// If something changed, set modified flags and modification times
238+
if (!removedCodes.isEmpty() || !modifiedCodesFinal.isEmpty()) {
257239
// Stream.concat(removedCodes.stream(), modifiedCodesFinal.stream())
258240
// .forEach(code -> {
259241
// code.setModified(true);
260242
// code.setLastModified();
261243
// });
262244

263-
// Not sure if this is needed
264-
editor.rebuildHeader();
265-
}
266-
});
267-
} catch (InterruptedException ignore) {
268-
} catch (InvocationTargetException e) {
269-
Messages.loge("exception in ChangeDetector", e);
245+
// Not sure if this is needed
246+
editor.rebuildHeader();
270247
}
271248
}
272249

273250

274251
/**
275-
* Prompt the user what do do about each tab. Passes the tab to the user's
252+
* Prompt the user what to do about each tab. Passes the tab to the user's
276253
* choice of Consumer. Won't let you delete the main tab.
277254
*/
278255
private void showReloadPrompt(

0 commit comments

Comments
 (0)