| 42 | |
| 43 | |
| 44 | public class ChangeDetector implements WindowFocusListener { |
| 45 | private final Sketch sketch; |
| 46 | private final Editor editor; |
| 47 | |
| 48 | private List<SketchCode> ignoredRemovals = new ArrayList<>(); |
| 49 | private List<SketchCode> ignoredModifications = new ArrayList<>(); |
| 50 | |
| 51 | // Windows and others seem to have a few hundred ms difference in reported |
| 52 | // times, so we're arbitrarily setting a gap in time here. |
| 53 | // Mac OS X has an (exactly) one second difference. Not sure if it's a Java |
| 54 | // bug or something else about how OS X is writing files. |
| 55 | static private final int MODIFICATION_WINDOW_MILLIS = |
| 56 | Preferences.getInteger("editor.watcher.window"); |
| 57 | |
| 58 | // Debugging this feature is particularly difficult, adding an option for it |
| 59 | static private final boolean DEBUG = |
| 60 | Preferences.getBoolean("editor.watcher.debug"); |
| 61 | |
| 62 | |
| 63 | public ChangeDetector(Editor editor) { |
| 64 | this.sketch = editor.sketch; |
| 65 | this.editor = editor; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | @Override |
| 70 | public void windowGainedFocus(WindowEvent e) { |
| 71 | if (Preferences.getBoolean("editor.watcher")) { |
| 72 | if (sketch != null) { |
| 73 | // make sure the sketch folder exists at all. |
| 74 | // if it does not, it will be re-saved, and no changes will be detected |
| 75 | sketch.ensureExistence(); |
| 76 | |
| 77 | checkFiles(); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | @Override |
| 84 | public void windowLostFocus(WindowEvent e) { |
| 85 | // Shouldn't need to do anything here, and not storing anything here b/c we |
| 86 | // don't want to assume a loss of focus is required before change detection |
| 87 | } |
| 88 | |
| 89 | |
| 90 | private void checkFiles() { |
| 91 | List<String> filenames = new ArrayList<>(); |
| 92 | List<String> extensions = new ArrayList<>(); |
| 93 | sketch.getSketchCodeFiles(filenames, extensions); |
| 94 | |
| 95 | SketchCode[] codes = sketch.getCode(); |
| 96 | |
| 97 | // Separate codes with and without files |
| 98 | Map<Boolean, List<SketchCode>> existsMap = Arrays.stream(codes) |
| 99 | .collect(Collectors.groupingBy(code -> filenames.contains(code.getFileName()))); |
| 100 | |
| 101 |
nothing calls this directly
no test coverage detected