mirror of
https://github.com/processing/processing4.git
synced 2026-02-03 21:59:20 +01:00
Added error handling if Base.loadFile throws an IOException (disable
listener) Fully removed the old file change detection code
This commit is contained in:
@@ -11,6 +11,8 @@ public class ChangeDetector implements WindowFocusListener {
|
||||
|
||||
private Editor editor;
|
||||
|
||||
private boolean enabled = true;
|
||||
|
||||
public ChangeDetector(Sketch sketch, Editor editor) {
|
||||
this.sketch = sketch;
|
||||
this.editor = editor;
|
||||
@@ -18,16 +20,15 @@ public class ChangeDetector implements WindowFocusListener {
|
||||
|
||||
@Override
|
||||
public void windowGainedFocus(WindowEvent e) {
|
||||
long start = System.currentTimeMillis();
|
||||
//remove the detector from main if it is disabled during runtime (due to an error?)
|
||||
if (!enabled || !Preferences.getBoolean("editor.watcher")) {
|
||||
editor.removeWindowFocusListener(this);
|
||||
return;
|
||||
}
|
||||
checkFileChange();
|
||||
long end = System.currentTimeMillis();
|
||||
long diff = end - start;
|
||||
System.out.println("Full file change scan took " + (diff / 1000.)
|
||||
+ " seconds");
|
||||
}
|
||||
|
||||
private void checkFileChange() {
|
||||
|
||||
//check that the content of each of the files in sketch matches what is in memory
|
||||
if (sketch == null) {
|
||||
return;
|
||||
@@ -49,8 +50,6 @@ public class ChangeDetector implements WindowFocusListener {
|
||||
}
|
||||
}).length;
|
||||
if (fileCount != sketch.getCodeCount()) {
|
||||
System.out.println("filecount = " + fileCount + "\tsketchCount = "
|
||||
+ sketch.getCodeCount());
|
||||
if (reloadSketch()) {
|
||||
return;
|
||||
}
|
||||
@@ -59,12 +58,10 @@ public class ChangeDetector implements WindowFocusListener {
|
||||
|
||||
SketchCode[] codes = sketch.getCode();
|
||||
for (SketchCode sc : codes) {
|
||||
//REMOVE
|
||||
System.out.println("Checking " + sc.getFileName());
|
||||
String inMemory = sc.getProgram();
|
||||
String onDisk = null;
|
||||
File f = sc.getFile();
|
||||
if (!f.exists()) {
|
||||
File sketchFile = sc.getFile();
|
||||
if (!sketchFile.exists()) {
|
||||
//if a file in the sketch was not found, then it must have been deleted externally
|
||||
//so reload the sketch
|
||||
if (reloadSketch()) {
|
||||
@@ -72,18 +69,20 @@ public class ChangeDetector implements WindowFocusListener {
|
||||
}
|
||||
}
|
||||
try {
|
||||
onDisk = Base.loadFile(f);
|
||||
} catch (Exception e1) {
|
||||
// TODO couldn't access file for some reason
|
||||
// deal with it
|
||||
System.out.println("Loading file failed");
|
||||
e1.printStackTrace();
|
||||
onDisk = Base.loadFile(sketchFile);
|
||||
} catch (IOException e1) {
|
||||
Base
|
||||
.showWarningTiered("File Change Detection Failed",
|
||||
"Checking for changed files for this sketch has failed.",
|
||||
"The file change detector will be disabled.", e1);
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
if (onDisk == null) {
|
||||
//failed
|
||||
} else if (!inMemory.equals(onDisk)) {
|
||||
if (reloadSketch()) {
|
||||
break;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,8 +108,6 @@ public abstract class Editor extends JFrame implements RunnerListener {
|
||||
// maintain caret position during undo operations
|
||||
private final Stack<Integer> caretUndoStack = new Stack<Integer>();
|
||||
private final Stack<Integer> caretRedoStack = new Stack<Integer>();
|
||||
|
||||
private ChangeDetector changeDetector;
|
||||
|
||||
private FindReplace find;
|
||||
JMenu toolsMenu;
|
||||
@@ -290,10 +288,9 @@ public abstract class Editor extends JFrame implements RunnerListener {
|
||||
sketch = null;
|
||||
}
|
||||
|
||||
//TODO new implementation of file change listener here
|
||||
//add a window listener to watch for changes to the files in the sketch
|
||||
if (Preferences.getBoolean("editor.watcher")) {
|
||||
changeDetector = new ChangeDetector(sketch, this);
|
||||
addWindowFocusListener(changeDetector);
|
||||
addWindowFocusListener(new ChangeDetector(sketch, this));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2469,11 +2466,6 @@ public abstract class Editor extends JFrame implements RunnerListener {
|
||||
Base.showWarning("Error", "Could not create the sketch.", e);
|
||||
return false;
|
||||
}
|
||||
//REMOVE delete
|
||||
// // Disabling for 3.0a4
|
||||
// if (false && Preferences.getBoolean("editor.watcher")) {
|
||||
// initFileChangeListener();
|
||||
// }
|
||||
|
||||
header.rebuild();
|
||||
updateTitle();
|
||||
@@ -2494,135 +2486,6 @@ public abstract class Editor extends JFrame implements RunnerListener {
|
||||
// return false;
|
||||
// }
|
||||
}
|
||||
|
||||
//REMOVE delete
|
||||
//used to prevent the fileChangeListener from asking for reloads after internal changes
|
||||
// public void setWatcherSave() {
|
||||
// watcherSave = true;
|
||||
// }
|
||||
//
|
||||
// //set to true when the sketch is saved from inside processing
|
||||
// private boolean watcherSave;
|
||||
//
|
||||
// //the key which is being used to poll the fs for changes
|
||||
// private WatchKey watcherKey = null;
|
||||
//
|
||||
// private void initFileChangeListener() {
|
||||
// try {
|
||||
// WatchService watchService = FileSystems.getDefault().newWatchService();
|
||||
// Path sp = sketch.getFolder().toPath();
|
||||
// watcherKey = sp.register(watchService,
|
||||
// StandardWatchEventKinds.ENTRY_CREATE,
|
||||
// StandardWatchEventKinds.ENTRY_DELETE,
|
||||
// StandardWatchEventKinds.ENTRY_MODIFY);
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// final WatchKey finKey = watcherKey;
|
||||
//
|
||||
// //if the key is null for some reason, don't bother attaching a listener to it
|
||||
// if (finKey != null) {
|
||||
// // the key can now be polled for changes in the files
|
||||
// addWindowFocusListener(new WindowFocusListener() {
|
||||
// @Override
|
||||
// public void windowGainedFocus(WindowEvent arg0) {
|
||||
// //we switched locations (saveAs), ignore old things
|
||||
// if (watcherKey != finKey) {
|
||||
// return;
|
||||
// }
|
||||
// // check preference here for enabled or not?
|
||||
//
|
||||
// //if the directory was deleted, then don't scan
|
||||
// if (finKey.isValid()) {
|
||||
// List<WatchEvent<?>> events = finKey.pollEvents();
|
||||
// if (!watcherSave) {
|
||||
// processFileEvents(events);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// List<WatchEvent<?>> events = finKey.pollEvents();
|
||||
// if (!watcherSave)
|
||||
// processFileEvents(events);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void windowLostFocus(WindowEvent arg0) {
|
||||
// //we switched locations (saveAs), ignore old things
|
||||
// if (watcherKey != finKey) {
|
||||
// return;
|
||||
// }
|
||||
// List<WatchEvent<?>> events = finKey.pollEvents();
|
||||
// //don't ask to reload a file we saved
|
||||
// if (!watcherSave) {
|
||||
// processFileEvents(events);
|
||||
// }
|
||||
// watcherSave = false;
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Called when a file is changed.
|
||||
* @param events the list of events that have occured in the sketch folder
|
||||
*/
|
||||
// private void processFileEvents(List<WatchEvent<?>> events) {
|
||||
// for (WatchEvent<?> e : events) {
|
||||
// boolean sketchFile = false;
|
||||
// Path file = ((Path) e.context()).getFileName();
|
||||
// System.out.println(file);
|
||||
// for (String s : getMode().getExtensions()) {
|
||||
// // if it is a change to a file with a known extension
|
||||
// if (file.toString().endsWith(s)) {
|
||||
// sketchFile = true;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// //if the file is not a known type, then go the the next event
|
||||
// if (!sketchFile) {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// int response =
|
||||
// Base.showYesNoQuestion(Editor.this,
|
||||
// "File Modified",
|
||||
// "Your sketch has been modified externally",
|
||||
// "Would you like to reload the sketch?");
|
||||
// if (response == 0) {
|
||||
// //grab the 'main' code in case this reload tries to delete everything
|
||||
// File sc = sketch.getMainFile();
|
||||
// //reload the sketch
|
||||
// try {
|
||||
// sketch.reload();
|
||||
// header.rebuild();
|
||||
// } catch (Exception f) {
|
||||
// if (sketch.getCodeCount() < 1) {
|
||||
// Base.showWarning("Canceling Reload",
|
||||
// "You cannot delete the last code file in a sketch.");
|
||||
// //if they deleted the last file, re-save the SketchCode
|
||||
// try {
|
||||
// //make a blank file
|
||||
// sc.createNewFile();
|
||||
// } catch (IOException e1) {
|
||||
// //if that didn't work, tell them it's un-recoverable
|
||||
// Base.showError("Reload failed", "The sketch contains no code files", e1);
|
||||
// //don't try to reload again after the double fail
|
||||
// //this editor is probably trashed by this point, but a save-as might be possible
|
||||
// break;
|
||||
// }
|
||||
// //don't ask for another reload after this save
|
||||
// watcherSave = true;
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// //now that we've reloaded once, don't try to reload again
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// watcherSave = false;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Set the title of the PDE window based on the current sketch, i.e.
|
||||
@@ -2654,8 +2517,6 @@ public abstract class Editor extends JFrame implements RunnerListener {
|
||||
public boolean handleSave(boolean immediately) {
|
||||
// handleStop(); // 0136
|
||||
|
||||
//REMOVE
|
||||
//setWatcherSave();
|
||||
if (sketch.isUntitled()) {
|
||||
return handleSaveAs();
|
||||
// need to get the name, user might also cancel here
|
||||
@@ -2699,14 +2560,6 @@ public abstract class Editor extends JFrame implements RunnerListener {
|
||||
statusNotice(Language.text("editor.status.saving"));
|
||||
try {
|
||||
if (sketch.saveAs()) {
|
||||
//REMOVE
|
||||
// // Disabling for 3.0a4
|
||||
// if (false && Preferences.getBoolean("editor.watcher")) {
|
||||
// // "Save As" moves where the files are, so a listener must be
|
||||
// // attached to the new location.
|
||||
// // TODO shouldn't this remove the old listener?
|
||||
// initFileChangeListener();
|
||||
// }
|
||||
// statusNotice("Done Saving.");
|
||||
// status is now printed from Sketch so that "Done Saving."
|
||||
// is only printed after Save As when progress bar is shown.
|
||||
|
||||
@@ -516,8 +516,6 @@ public class EditorHeader extends JComponent {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
editor.getSketch().handleNewCode();
|
||||
//REMOVE
|
||||
//editor.setWatcherSave();
|
||||
}
|
||||
};
|
||||
mapKey = "editor.header.new_tab";
|
||||
@@ -555,8 +553,6 @@ public class EditorHeader extends JComponent {
|
||||
Language.text("editor.header.delete.warning.text"), null);
|
||||
} else {
|
||||
editor.getSketch().handleDeleteCode();
|
||||
//REMOVE
|
||||
//editor.setWatcherSave();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user