Skip to content

Commit 46677c2

Browse files
committed
Add watcher on sketch files
Reloads sketch content if the Editor is not in foreground and isomething happens in the backing storage files. Note that no confirmation dialog is displayed (same behaviour as SublimeText, differs from other IDEs) Fixes arduino#4551 and arduino#5345
1 parent cba0435 commit 46677c2

File tree

2 files changed

+223
-1
lines changed

2 files changed

+223
-1
lines changed

app/src/processing/app/EditorTab.java

+54-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.awt.Font;
3131
import java.awt.event.ActionEvent;
3232
import java.awt.event.ActionListener;
33+
import java.awt.event.FocusEvent;
34+
import java.awt.event.FocusListener;
3335
import java.io.IOException;
3436

3537
import javax.swing.Action;
@@ -47,6 +49,14 @@
4749
import javax.swing.text.DefaultCaret;
4850
import javax.swing.text.Document;
4951

52+
import static java.nio.file.StandardWatchEventKinds.*;
53+
import java.nio.file.WatchService;
54+
import java.nio.file.WatchKey;
55+
import java.nio.file.WatchEvent;
56+
import java.nio.file.FileSystems;
57+
import java.nio.file.Path;
58+
import java.io.File;
59+
5060
import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
5161
import org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit;
5262
import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities;
@@ -60,18 +70,21 @@
6070
import processing.app.syntax.SketchTextArea;
6171
import processing.app.syntax.SketchTextAreaEditorKit;
6272
import processing.app.tools.DiscourseFormat;
73+
import processing.app.tools.WatchDir;
6374

6475
/**
6576
* Single tab, editing a single file, in the main window.
6677
*/
67-
public class EditorTab extends JPanel implements SketchFile.TextStorage {
78+
public class EditorTab extends JPanel implements SketchFile.TextStorage, FocusListener {
6879
protected Editor editor;
6980
protected SketchTextArea textarea;
7081
protected RTextScrollPane scrollPane;
7182
protected SketchFile file;
7283
protected boolean modified;
7384
/** Is external editing mode currently enabled? */
7485
protected boolean external;
86+
protected Thread watcher = null;
87+
protected Runnable task = null;
7588

7689
/**
7790
* Create a new EditorTab
@@ -106,6 +119,27 @@ public EditorTab(Editor editor, SketchFile file, String contents)
106119
file.setStorage(this);
107120
applyPreferences();
108121
add(scrollPane, BorderLayout.CENTER);
122+
addFocusListener(this);
123+
textarea.addFocusListener(this);
124+
scrollPane.addFocusListener(this);
125+
setFocusable(true);
126+
setRequestFocusEnabled(true);
127+
}
128+
129+
@Override
130+
public void focusGained(FocusEvent fe){
131+
if (watcher != null) {
132+
watcher.interrupt();
133+
watcher = null;
134+
}
135+
}
136+
137+
@Override
138+
public void focusLost(FocusEvent fe){
139+
if (watcher == null) {
140+
watcher = new Thread(task);
141+
watcher.start();
142+
}
109143
}
110144

111145
private RSyntaxDocument createDocument(String contents) {
@@ -120,6 +154,25 @@ private RSyntaxDocument createDocument(String contents) {
120154
}
121155
document.addDocumentListener(new DocumentTextChangeListener(
122156
() -> setModified(true)));
157+
158+
// Add FS watcher for modification
159+
File dir_s = this.file.getFile();
160+
Path dir = dir_s.toPath().getParent();
161+
EditorTab tab = this;
162+
163+
task = new Runnable() {
164+
public void run() {
165+
try {
166+
new WatchDir(dir, true).processEvents(tab);
167+
} catch (IOException x) {
168+
System.err.println(x);
169+
}
170+
}
171+
};
172+
173+
watcher = new Thread(task);
174+
watcher.start();
175+
123176
return document;
124177
}
125178

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of Oracle nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package processing.app.tools;
33+
34+
import java.nio.file.*;
35+
import static java.nio.file.StandardWatchEventKinds.*;
36+
import static java.nio.file.LinkOption.*;
37+
import java.nio.file.attribute.*;
38+
import java.io.*;
39+
import java.util.*;
40+
import processing.app.EditorTab;
41+
42+
/**
43+
* Example to watch a directory (or tree) for changes to files.
44+
*/
45+
46+
public class WatchDir {
47+
48+
private final WatchService watcher;
49+
private final Map<WatchKey,Path> keys;
50+
private final boolean recursive;
51+
private boolean trace = false;
52+
53+
@SuppressWarnings("unchecked")
54+
static <T> WatchEvent<T> cast(WatchEvent<?> event) {
55+
return (WatchEvent<T>)event;
56+
}
57+
58+
/**
59+
* Register the given directory with the WatchService
60+
*/
61+
private void register(Path dir) throws IOException {
62+
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
63+
if (trace) {
64+
Path prev = keys.get(key);
65+
if (prev == null) {
66+
} else {
67+
if (!dir.equals(prev)) {
68+
}
69+
}
70+
}
71+
keys.put(key, dir);
72+
}
73+
74+
/**
75+
* Register the given directory, and all its sub-directories, with the
76+
* WatchService.
77+
*/
78+
private void registerAll(final Path start) throws IOException {
79+
// register directory and sub-directories
80+
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
81+
@Override
82+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
83+
throws IOException
84+
{
85+
register(dir);
86+
return FileVisitResult.CONTINUE;
87+
}
88+
});
89+
}
90+
91+
/**
92+
* Creates a WatchService and registers the given directory
93+
*/
94+
public WatchDir(Path dir, boolean recursive) throws IOException {
95+
this.watcher = FileSystems.getDefault().newWatchService();
96+
this.keys = new HashMap<WatchKey,Path>();
97+
this.recursive = recursive;
98+
99+
if (recursive) {
100+
registerAll(dir);
101+
} else {
102+
register(dir);
103+
}
104+
105+
// enable trace after initial registration
106+
this.trace = true;
107+
}
108+
109+
/**
110+
* Process all events for keys queued to the watcher
111+
*/
112+
public void processEvents(EditorTab tab) {
113+
for (;;) {
114+
115+
// wait for key to be signalled
116+
WatchKey key;
117+
try {
118+
key = watcher.take();
119+
} catch (InterruptedException x) {
120+
return;
121+
}
122+
123+
Path dir = keys.get(key);
124+
if (dir == null) {
125+
continue;
126+
}
127+
128+
for (WatchEvent<?> event: key.pollEvents()) {
129+
WatchEvent.Kind kind = event.kind();
130+
131+
// TBD - provide example of how OVERFLOW event is handled
132+
if (kind == OVERFLOW) {
133+
continue;
134+
}
135+
136+
// Context for directory entry event is the file name of entry
137+
WatchEvent<Path> ev = cast(event);
138+
Path name = ev.context();
139+
Path child = dir.resolve(name);
140+
141+
// reload the tab content
142+
tab.reload();
143+
144+
// if directory is created, and watching recursively, then
145+
// register it and its sub-directories
146+
if (recursive && (kind == ENTRY_CREATE)) {
147+
try {
148+
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
149+
registerAll(child);
150+
}
151+
} catch (IOException x) {
152+
// ignore to keep sample readbale
153+
}
154+
}
155+
}
156+
157+
// reset key and remove from set if directory no longer accessible
158+
boolean valid = key.reset();
159+
if (!valid) {
160+
keys.remove(key);
161+
162+
// all directories are inaccessible
163+
if (keys.isEmpty()) {
164+
break;
165+
}
166+
}
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)