Skip to content

Commit 9cb8ab2

Browse files
Let EditorTab listen for changes to the text area
Previously, EditorTab set the Document on the SketchCodeDocument, and the latter would listen for changes, only forwarding the modified status to SketchCode. This commit cuts out a step and lets EditorTab call SketchCode::setModified directly. Additionally, the DocumentTextChangedListener helper class is added, which wraps a simple (lambda) function to be called whenever anything about the document text is modified. This hides the verbosity of having to handle both insertion and deletion, and instead suffices with just having a single lambda function instead.
1 parent c9da9bd commit 9cb8ab2

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

app/src/processing/app/EditorTab.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.fife.ui.rtextarea.Gutter;
5454
import org.fife.ui.rtextarea.RTextScrollPane;
5555

56+
import processing.app.helpers.DocumentTextChangeListener;
5657
import processing.app.syntax.ArduinoTokenMakerFactory;
5758
import processing.app.syntax.PdeKeywords;
5859
import processing.app.syntax.SketchTextArea;
@@ -91,8 +92,8 @@ private RSyntaxDocument createDocument() {
9192
} catch (BadLocationException bl) {
9293
bl.printStackTrace();
9394
}
94-
((SketchCodeDocument) code.getMetadata()).setDocument(document);
95-
95+
document.addDocumentListener(new DocumentTextChangeListener(
96+
() -> code.setModified(true)));
9697
return document;
9798
}
9899

app/src/processing/app/SketchCodeDocument.java

+1-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import javax.swing.event.DocumentListener;
77
import javax.swing.text.Document;
88

9-
public class SketchCodeDocument implements DocumentListener {
9+
public class SketchCodeDocument {
1010

1111
private SketchCode code;
1212
private Sketch sketch;
@@ -29,24 +29,5 @@ public void setCode(SketchCode code) {
2929
this.code = code;
3030
}
3131

32-
public void setDocument(Document document) {
33-
document.addDocumentListener(this);
34-
}
35-
36-
@Override
37-
public void insertUpdate(DocumentEvent e) {
38-
if(!code.isModified()) sketch.setModified(true);
39-
}
40-
41-
42-
@Override
43-
public void removeUpdate(DocumentEvent e) {
44-
if(!code.isModified()) sketch.setModified(true);
45-
}
46-
47-
@Override
48-
public void changedUpdate(DocumentEvent e) {
49-
// ignore
50-
}
5132

5233
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package processing.app.helpers;
2+
3+
import javax.swing.event.DocumentEvent;
4+
import javax.swing.event.DocumentListener;
5+
6+
/**
7+
* Helper class that create a document listener that calls the given
8+
* TextChangeListener on any change to the document text (but not changes to
9+
* document attributes).
10+
*
11+
* The TextChangeListener to be passed is intended to be a lambda function, for
12+
* easy definition of a callback.
13+
*/
14+
public class DocumentTextChangeListener implements DocumentListener {
15+
public interface TextChangeListener {
16+
public void textChanged();
17+
}
18+
19+
private TextChangeListener onChange;
20+
21+
public DocumentTextChangeListener(TextChangeListener onChange) {
22+
this.onChange = onChange;
23+
}
24+
25+
@Override
26+
public void changedUpdate(DocumentEvent arg0) {
27+
/* Attributes changed, do nothing */
28+
}
29+
30+
@Override
31+
public void insertUpdate(DocumentEvent arg0) {
32+
onChange.textChanged();
33+
}
34+
35+
@Override
36+
public void removeUpdate(DocumentEvent arg0) {
37+
onChange.textChanged();
38+
}
39+
}

0 commit comments

Comments
 (0)