mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-01 12:24:14 +01:00
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.
This commit is contained in:
parent
8f1ae9ba0b
commit
422c111d81
@ -53,6 +53,7 @@ import org.fife.ui.rtextarea.Gutter;
|
||||
import org.fife.ui.rtextarea.RTextScrollPane;
|
||||
|
||||
import cc.arduino.UpdatableBoardsLibsFakeURLsHandler;
|
||||
import processing.app.helpers.DocumentTextChangeListener;
|
||||
import processing.app.syntax.ArduinoTokenMakerFactory;
|
||||
import processing.app.syntax.PdeKeywords;
|
||||
import processing.app.syntax.SketchTextArea;
|
||||
@ -91,8 +92,8 @@ public class EditorTab extends JPanel {
|
||||
} catch (BadLocationException bl) {
|
||||
bl.printStackTrace();
|
||||
}
|
||||
((SketchCodeDocument) code.getMetadata()).setDocument(document);
|
||||
|
||||
document.addDocumentListener(new DocumentTextChangeListener(
|
||||
() -> code.setModified(true)));
|
||||
return document;
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
import javax.swing.text.Document;
|
||||
|
||||
public class SketchCodeDocument implements DocumentListener {
|
||||
public class SketchCodeDocument {
|
||||
|
||||
private SketchCode code;
|
||||
private Sketch sketch;
|
||||
@ -29,24 +29,5 @@ public class SketchCodeDocument implements DocumentListener {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public void setDocument(Document document) {
|
||||
document.addDocumentListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
if(!code.isModified()) sketch.setModified(true);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
if(!code.isModified()) sketch.setModified(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,39 @@
|
||||
package processing.app.helpers;
|
||||
|
||||
import javax.swing.event.DocumentEvent;
|
||||
import javax.swing.event.DocumentListener;
|
||||
|
||||
/**
|
||||
* Helper class that create a document listener that calls the given
|
||||
* TextChangeListener on any change to the document text (but not changes to
|
||||
* document attributes).
|
||||
*
|
||||
* The TextChangeListener to be passed is intended to be a lambda function, for
|
||||
* easy definition of a callback.
|
||||
*/
|
||||
public class DocumentTextChangeListener implements DocumentListener {
|
||||
public interface TextChangeListener {
|
||||
public void textChanged();
|
||||
}
|
||||
|
||||
private TextChangeListener onChange;
|
||||
|
||||
public DocumentTextChangeListener(TextChangeListener onChange) {
|
||||
this.onChange = onChange;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changedUpdate(DocumentEvent arg0) {
|
||||
/* Attributes changed, do nothing */
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertUpdate(DocumentEvent arg0) {
|
||||
onChange.textChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeUpdate(DocumentEvent arg0) {
|
||||
onChange.textChanged();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user