Skip to content

Commit af45788

Browse files
committed
move watcher to Editor instance
1 parent 46677c2 commit af45788

File tree

3 files changed

+81
-48
lines changed

3 files changed

+81
-48
lines changed

app/src/processing/app/Editor.java

+49-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import processing.app.syntax.SketchTextArea;
4545
import processing.app.tools.MenuScroller;
4646
import processing.app.tools.Tool;
47+
import processing.app.tools.WatchDir;
4748

4849
import javax.swing.*;
4950
import javax.swing.event.*;
@@ -72,11 +73,19 @@
7273
import static processing.app.I18n.tr;
7374
import static processing.app.Theme.scale;
7475

76+
import static java.nio.file.StandardWatchEventKinds.*;
77+
import java.nio.file.WatchService;
78+
import java.nio.file.WatchKey;
79+
import java.nio.file.WatchEvent;
80+
import java.nio.file.FileSystems;
81+
import java.nio.file.Path;
82+
import java.io.File;
83+
7584
/**
7685
* Main editor panel for the Processing Development Environment.
7786
*/
7887
@SuppressWarnings("serial")
79-
public class Editor extends JFrame implements RunnerListener {
88+
public class Editor extends JFrame implements RunnerListener, FocusListener {
8089

8190
public static final int MAX_TIME_AWAITING_FOR_RESUMING_SERIAL_MONITOR = 10000;
8291

@@ -198,6 +207,9 @@ public boolean test(SketchController sketch) {
198207
private Runnable exportAppHandler;
199208
private Runnable timeoutUploadHandler;
200209

210+
protected Thread watcher = null;
211+
protected Runnable task = null;
212+
201213
public Editor(Base ibase, File file, int[] storedLocation, int[] defaultLocation, Platform platform) throws Exception {
202214
super("Arduino");
203215
this.base = ibase;
@@ -341,6 +353,21 @@ public void windowDeactivated(WindowEvent e) {
341353
if (!loaded) sketchController = null;
342354
}
343355

356+
@Override
357+
public void focusGained(FocusEvent fe){
358+
if (watcher != null) {
359+
watcher.interrupt();
360+
watcher = null;
361+
}
362+
}
363+
364+
@Override
365+
public void focusLost(FocusEvent fe){
366+
if (watcher == null) {
367+
watcher = new Thread(task);
368+
watcher.start();
369+
}
370+
}
344371

345372
/**
346373
* Handles files dragged & dropped from the desktop and into the editor
@@ -1649,7 +1676,7 @@ public void reorderTabs() {
16491676
* the given file.
16501677
* @throws IOException
16511678
*/
1652-
protected void addTab(SketchFile file, String contents) throws IOException {
1679+
public void addTab(SketchFile file, String contents) throws IOException {
16531680
EditorTab tab = new EditorTab(this, file, contents);
16541681
tab.getTextArea().getDocument()
16551682
.addDocumentListener(new DocumentTextChangeListener(
@@ -1658,7 +1685,7 @@ protected void addTab(SketchFile file, String contents) throws IOException {
16581685
reorderTabs();
16591686
}
16601687

1661-
protected void removeTab(SketchFile file) throws IOException {
1688+
public void removeTab(SketchFile file) throws IOException {
16621689
int index = findTabIndex(file);
16631690
tabs.remove(index);
16641691
}
@@ -1927,6 +1954,25 @@ protected boolean handleOpenInternal(File sketchFile) {
19271954
// Disable untitled setting from previous document, if any
19281955
untitled = false;
19291956

1957+
// Add FS watcher for current Editor instance
1958+
Path dir = file.toPath().getParent();
1959+
1960+
Editor instance = this;
1961+
1962+
task = new Runnable() {
1963+
public void run() {
1964+
try {
1965+
new WatchDir(dir, true).processEvents(instance);
1966+
} catch (IOException x) {
1967+
System.err.println(x);
1968+
}
1969+
}
1970+
};
1971+
1972+
addFocusListener(this);
1973+
getTabs().forEach(tab -> tab.getScrollPane().addFocusListener(this));
1974+
getTabs().forEach(tab -> tab.getTextArea().addFocusListener(this));
1975+
19301976
// opening was successful
19311977
return true;
19321978
}

app/src/processing/app/EditorTab.java

+6-41
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,14 @@
7575
/**
7676
* Single tab, editing a single file, in the main window.
7777
*/
78-
public class EditorTab extends JPanel implements SketchFile.TextStorage, FocusListener {
78+
public class EditorTab extends JPanel implements SketchFile.TextStorage {
7979
protected Editor editor;
8080
protected SketchTextArea textarea;
8181
protected RTextScrollPane scrollPane;
8282
protected SketchFile file;
8383
protected boolean modified;
8484
/** Is external editing mode currently enabled? */
8585
protected boolean external;
86-
protected Thread watcher = null;
87-
protected Runnable task = null;
8886

8987
/**
9088
* Create a new EditorTab
@@ -119,29 +117,10 @@ public EditorTab(Editor editor, SketchFile file, String contents)
119117
file.setStorage(this);
120118
applyPreferences();
121119
add(scrollPane, BorderLayout.CENTER);
122-
addFocusListener(this);
123-
textarea.addFocusListener(this);
124-
scrollPane.addFocusListener(this);
125120
setFocusable(true);
126121
setRequestFocusEnabled(true);
127122
}
128123

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-
}
143-
}
144-
145124
private RSyntaxDocument createDocument(String contents) {
146125
RSyntaxDocument document = new RSyntaxDocument(new ArduinoTokenMakerFactory(editor.base.getPdeKeywords()), RSyntaxDocument.SYNTAX_STYLE_CPLUSPLUS);
147126
document.putProperty(PlainDocument.tabSizeAttribute, PreferencesData.getInteger("editor.tabs.size"));
@@ -155,24 +134,6 @@ private RSyntaxDocument createDocument(String contents) {
155134
document.addDocumentListener(new DocumentTextChangeListener(
156135
() -> setModified(true)));
157136

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-
176137
return document;
177138
}
178139

@@ -509,7 +470,11 @@ public void setSelection(int start, int stop) {
509470
public int getScrollPosition() {
510471
return scrollPane.getVerticalScrollBar().getValue();
511472
}
512-
473+
474+
public RTextScrollPane getScrollPane() {
475+
return scrollPane;
476+
}
477+
513478
public void setScrollPosition(int pos) {
514479
scrollPane.getVerticalScrollBar().setValue(pos);
515480
}

app/src/processing/app/tools/WatchDir.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
import java.nio.file.attribute.*;
3838
import java.io.*;
3939
import java.util.*;
40-
import processing.app.EditorTab;
40+
import processing.app.Editor;
41+
import processing.app.Sketch;
42+
import processing.app.SketchFile;
43+
import processing.app.helpers.FileUtils;
4144

4245
/**
4346
* Example to watch a directory (or tree) for changes to files.
@@ -109,7 +112,7 @@ public WatchDir(Path dir, boolean recursive) throws IOException {
109112
/**
110113
* Process all events for keys queued to the watcher
111114
*/
112-
public void processEvents(EditorTab tab) {
115+
public void processEvents(Editor editor) {
113116
for (;;) {
114117

115118
// wait for key to be signalled
@@ -139,7 +142,26 @@ public void processEvents(EditorTab tab) {
139142
Path child = dir.resolve(name);
140143

141144
// reload the tab content
142-
tab.reload();
145+
if (kind == ENTRY_CREATE) {
146+
try {
147+
String filename = name.toString();
148+
FileUtils.SplitFile split = FileUtils.splitFilename(filename);
149+
if (Sketch.EXTENSIONS.contains(split.extension.toLowerCase())) {
150+
SketchFile sketch = editor.getSketch().addFile(filename);
151+
editor.addTab(sketch, null);
152+
}
153+
} catch (IOException e) {}
154+
} else if (kind == ENTRY_DELETE) {
155+
editor.getTabs().forEach(tab -> {
156+
try {
157+
if (name.getFileName().toString() == tab.getSketchFile().getFileName()) {
158+
editor.removeTab(tab.getSketchFile());
159+
}
160+
} catch (IOException x) {}
161+
});
162+
} else {
163+
editor.getTabs().forEach(tab -> tab.reload());
164+
}
143165

144166
// if directory is created, and watching recursively, then
145167
// register it and its sub-directories
@@ -166,4 +188,4 @@ public void processEvents(EditorTab tab) {
166188
}
167189
}
168190
}
169-
}
191+
}

0 commit comments

Comments
 (0)