Skip to content

Commit bd66184

Browse files
author
Federico Fissore
committed
closes #278 Text marker should follow undo actions
1 parent 6d3ec1f commit bd66184

File tree

4 files changed

+121
-5
lines changed

4 files changed

+121
-5
lines changed

Diff for: app/src/processing/app/CaretAwareUndoableEdit.java

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package processing.app;
2+
3+
import processing.app.syntax.JEditTextArea;
4+
5+
import javax.swing.undo.CannotRedoException;
6+
import javax.swing.undo.CannotUndoException;
7+
import javax.swing.undo.UndoableEdit;
8+
9+
public class CaretAwareUndoableEdit implements UndoableEdit {
10+
11+
private final UndoableEdit undoableEdit;
12+
private final int caretPosition;
13+
14+
public CaretAwareUndoableEdit(UndoableEdit undoableEdit, JEditTextArea textArea) {
15+
this.undoableEdit = undoableEdit;
16+
this.caretPosition = textArea.getCaretPosition();
17+
}
18+
19+
@Override
20+
public void undo() throws CannotUndoException {
21+
undoableEdit.undo();
22+
}
23+
24+
@Override
25+
public boolean canUndo() {
26+
return undoableEdit.canUndo();
27+
}
28+
29+
@Override
30+
public void redo() throws CannotRedoException {
31+
undoableEdit.redo();
32+
}
33+
34+
@Override
35+
public boolean canRedo() {
36+
return undoableEdit.canRedo();
37+
}
38+
39+
@Override
40+
public void die() {
41+
undoableEdit.die();
42+
}
43+
44+
@Override
45+
public boolean addEdit(UndoableEdit undoableEdit) {
46+
return this.undoableEdit.addEdit(undoableEdit);
47+
}
48+
49+
@Override
50+
public boolean replaceEdit(UndoableEdit undoableEdit) {
51+
return this.undoableEdit.replaceEdit(undoableEdit);
52+
}
53+
54+
@Override
55+
public boolean isSignificant() {
56+
return undoableEdit.isSignificant();
57+
}
58+
59+
@Override
60+
public String getPresentationName() {
61+
return undoableEdit.getPresentationName();
62+
}
63+
64+
@Override
65+
public String getUndoPresentationName() {
66+
return undoableEdit.getUndoPresentationName();
67+
}
68+
69+
@Override
70+
public String getRedoPresentationName() {
71+
return undoableEdit.getRedoPresentationName();
72+
}
73+
74+
public int getCaretPosition() {
75+
return caretPosition;
76+
}
77+
}

Diff for: app/src/processing/app/Editor.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public class Editor extends JFrame implements RunnerListener {
137137
JMenuItem undoItem, redoItem;
138138
protected UndoAction undoAction;
139139
protected RedoAction redoAction;
140-
UndoManager undo;
140+
LastUndoableEditAwareUndoManager undo;
141141
// used internally, and only briefly
142142
CompoundEdit compoundEdit;
143143

@@ -1344,6 +1344,10 @@ public void actionPerformed(ActionEvent e) {
13441344
//System.out.println("Unable to undo: " + ex);
13451345
//ex.printStackTrace();
13461346
}
1347+
if (undo.getLastUndoableEdit() != null && undo.getLastUndoableEdit() instanceof CaretAwareUndoableEdit) {
1348+
CaretAwareUndoableEdit undoableEdit = (CaretAwareUndoableEdit) undo.getLastUndoableEdit();
1349+
textarea.setCaretPosition(undoableEdit.getCaretPosition() - 1);
1350+
}
13471351
updateUndoState();
13481352
redoAction.updateRedoState();
13491353
}
@@ -1383,6 +1387,10 @@ public void actionPerformed(ActionEvent e) {
13831387
//System.out.println("Unable to redo: " + ex);
13841388
//ex.printStackTrace();
13851389
}
1390+
if (undo.getLastUndoableEdit() != null && undo.getLastUndoableEdit() instanceof CaretAwareUndoableEdit) {
1391+
CaretAwareUndoableEdit undoableEdit = (CaretAwareUndoableEdit) undo.getLastUndoableEdit();
1392+
textarea.setCaretPosition(undoableEdit.getCaretPosition());
1393+
}
13861394
updateRedoState();
13871395
undoAction.updateUndoState();
13881396
}
@@ -1664,7 +1672,7 @@ public void undoableEditHappened(UndoableEditEvent e) {
16641672
compoundEdit.addEdit(e.getEdit());
16651673

16661674
} else if (undo != null) {
1667-
undo.addEdit(e.getEdit());
1675+
undo.addEdit(new CaretAwareUndoableEdit(e.getEdit(), textarea));
16681676
undoAction.updateUndoState();
16691677
redoAction.updateRedoState();
16701678
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package processing.app;
2+
3+
import javax.swing.undo.CannotRedoException;
4+
import javax.swing.undo.CannotUndoException;
5+
import javax.swing.undo.UndoManager;
6+
import javax.swing.undo.UndoableEdit;
7+
8+
public class LastUndoableEditAwareUndoManager extends UndoManager {
9+
10+
private UndoableEdit lastUndoableEdit;
11+
12+
public LastUndoableEditAwareUndoManager() {
13+
this.lastUndoableEdit = null;
14+
}
15+
16+
@Override
17+
public synchronized void undo() throws CannotUndoException {
18+
lastUndoableEdit = super.editToBeUndone();
19+
super.undo();
20+
}
21+
22+
@Override
23+
public synchronized void redo() throws CannotRedoException {
24+
lastUndoableEdit = super.editToBeRedone();
25+
super.redo();
26+
}
27+
28+
public UndoableEdit getLastUndoableEdit() {
29+
return lastUndoableEdit;
30+
}
31+
}

Diff for: app/src/processing/app/SketchCode.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.io.*;
2828

2929
import javax.swing.text.Document;
30-
import javax.swing.undo.*;
30+
3131
import static processing.app.I18n._;
3232

3333

@@ -55,7 +55,7 @@ public class SketchCode {
5555
* Editor.undo will be set to this object when this code is the tab
5656
* that's currently the front.
5757
*/
58-
private UndoManager undo = new UndoManager();
58+
private LastUndoableEditAwareUndoManager undo = new LastUndoableEditAwareUndoManager();
5959

6060
// saved positions from last time this tab was used
6161
private int selectionStart;
@@ -221,7 +221,7 @@ public void setDocument(Document d) {
221221
}
222222

223223

224-
public UndoManager getUndo() {
224+
public LastUndoableEditAwareUndoManager getUndo() {
225225
return undo;
226226
}
227227

0 commit comments

Comments
 (0)