Skip to content

Commit 5f12bb9

Browse files
committed
Removed the need for get/setUndoManager()
This dramatically simplifies undo/redo handling and allows to use unpatched RSyntaxTextArea library.
1 parent 1062307 commit 5f12bb9

File tree

3 files changed

+14
-120
lines changed

3 files changed

+14
-120
lines changed

app/src/processing/app/Editor.java

+12-71
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,19 @@
3333
import jssc.SerialPortException;
3434
import processing.app.debug.RunnerException;
3535
import processing.app.forms.PasswordAuthorizationDialog;
36+
import processing.app.helpers.DocumentTextChangeListener;
3637
import processing.app.helpers.Keys;
3738
import processing.app.helpers.OSUtils;
3839
import processing.app.helpers.PreferencesMapException;
3940
import processing.app.legacy.PApplet;
4041
import processing.app.syntax.PdeKeywords;
42+
import processing.app.syntax.SketchTextArea;
4143
import processing.app.tools.MenuScroller;
4244
import processing.app.tools.Tool;
4345

4446
import javax.swing.*;
4547
import javax.swing.event.*;
4648
import javax.swing.text.BadLocationException;
47-
import javax.swing.undo.CannotRedoException;
48-
import javax.swing.undo.CannotUndoException;
49-
import javax.swing.undo.UndoManager;
5049
import java.awt.*;
5150
import java.awt.datatransfer.DataFlavor;
5251
import java.awt.datatransfer.Transferable;
@@ -185,8 +184,6 @@ public boolean test(SketchController sketch) {
185184
// undo fellers
186185
private JMenuItem undoItem;
187186
private JMenuItem redoItem;
188-
protected UndoAction undoAction;
189-
protected RedoAction redoAction;
190187

191188
private FindReplace find;
192189

@@ -1273,7 +1270,7 @@ private JMenu buildEditMenu() {
12731270

12741271
undoItem = newJMenuItem(tr("Undo"), 'Z');
12751272
undoItem.setName("menuEditUndo");
1276-
undoItem.addActionListener(undoAction = new UndoAction());
1273+
undoItem.addActionListener(e -> getCurrentTab().handleUndo());
12771274
menu.add(undoItem);
12781275

12791276
if (!OSUtils.isMacOS()) {
@@ -1282,7 +1279,7 @@ private JMenu buildEditMenu() {
12821279
redoItem = newJMenuItemShift(tr("Redo"), 'Z');
12831280
}
12841281
redoItem.setName("menuEditRedo");
1285-
redoItem.addActionListener(redoAction = new RedoAction());
1282+
redoItem.addActionListener(e -> getCurrentTab().handleRedo());
12861283
menu.add(redoItem);
12871284

12881285
menu.addSeparator();
@@ -1478,68 +1475,10 @@ private static JMenuItem newJMenuItemAlt(String title, int what) {
14781475
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14791476

14801477

1481-
class UndoAction extends AbstractAction {
1482-
public UndoAction() {
1483-
super("Undo");
1484-
this.setEnabled(false);
1485-
}
1486-
1487-
public void actionPerformed(ActionEvent e) {
1488-
try {
1489-
getCurrentTab().handleUndo();
1490-
} catch (CannotUndoException ex) {
1491-
//System.out.println("Unable to undo: " + ex);
1492-
//ex.printStackTrace();
1493-
}
1494-
}
1495-
1496-
protected void updateUndoState() {
1497-
UndoManager undo = getCurrentTab().getUndoManager();
1498-
1499-
if (undo.canUndo()) {
1500-
this.setEnabled(true);
1501-
undoItem.setEnabled(true);
1502-
undoItem.setText(undo.getUndoPresentationName());
1503-
putValue(Action.NAME, undo.getUndoPresentationName());
1504-
} else {
1505-
this.setEnabled(false);
1506-
undoItem.setEnabled(false);
1507-
undoItem.setText(tr("Undo"));
1508-
putValue(Action.NAME, "Undo");
1509-
}
1510-
}
1511-
}
1512-
1513-
1514-
class RedoAction extends AbstractAction {
1515-
public RedoAction() {
1516-
super("Redo");
1517-
this.setEnabled(false);
1518-
}
1519-
1520-
public void actionPerformed(ActionEvent e) {
1521-
try {
1522-
getCurrentTab().handleRedo();
1523-
} catch (CannotRedoException ex) {
1524-
//System.out.println("Unable to redo: " + ex);
1525-
//ex.printStackTrace();
1526-
}
1527-
}
1528-
1529-
protected void updateRedoState() {
1530-
UndoManager undo = getCurrentTab().getUndoManager();
1531-
1532-
if (undo.canRedo()) {
1533-
redoItem.setEnabled(true);
1534-
redoItem.setText(undo.getRedoPresentationName());
1535-
putValue(Action.NAME, undo.getRedoPresentationName());
1536-
} else {
1537-
this.setEnabled(false);
1538-
redoItem.setEnabled(false);
1539-
redoItem.setText(tr("Redo"));
1540-
putValue(Action.NAME, "Redo");
1541-
}
1542-
}
1478+
protected void updateUndoRedoState() {
1479+
SketchTextArea textArea = getCurrentTab().getTextArea();
1480+
undoItem.setEnabled(textArea.canUndo());
1481+
redoItem.setEnabled(textArea.canRedo());
15431482
}
15441483

15451484

@@ -1610,8 +1549,7 @@ public List<EditorTab> getTabs() {
16101549
*/
16111550
public void selectTab(final int index) {
16121551
currentTabIndex = index;
1613-
undoAction.updateUndoState();
1614-
redoAction.updateRedoState();
1552+
updateUndoRedoState();
16151553
updateTitle();
16161554
header.rebuild();
16171555
getCurrentTab().activated();
@@ -1710,6 +1648,9 @@ public void reorderTabs() {
17101648
*/
17111649
protected void addTab(SketchFile file, String contents) throws IOException {
17121650
EditorTab tab = new EditorTab(this, file, contents);
1651+
tab.getTextArea().getDocument()
1652+
.addDocumentListener(new DocumentTextChangeListener(
1653+
() -> updateUndoRedoState()));
17131654
tabs.add(tab);
17141655
reorderTabs();
17151656
}

app/src/processing/app/EditorTab.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import javax.swing.text.BadLocationException;
4545
import javax.swing.text.Element;
4646
import javax.swing.text.PlainDocument;
47-
import javax.swing.undo.UndoManager;
4847
import javax.swing.text.DefaultCaret;
4948
import javax.swing.text.Document;
5049

@@ -53,7 +52,6 @@
5352
import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities;
5453
import org.fife.ui.rtextarea.Gutter;
5554
import org.fife.ui.rtextarea.RTextScrollPane;
56-
import org.fife.ui.rtextarea.RUndoManager;
5755

5856
import cc.arduino.UpdatableBoardsLibsFakeURLsHandler;
5957
import processing.app.helpers.DocumentTextChangeListener;
@@ -108,10 +106,6 @@ public EditorTab(Editor editor, SketchFile file, String contents)
108106
file.setStorage(this);
109107
applyPreferences();
110108
add(scrollPane, BorderLayout.CENTER);
111-
112-
RUndoManager undo = new LastUndoableEditAwareUndoManager(this.textarea, this.editor);
113-
document.addUndoableEditListener(undo);
114-
textarea.setUndoManager(undo);
115109
}
116110

117111
private RSyntaxDocument createDocument(String contents) {
@@ -407,14 +401,14 @@ public void setText(String what) {
407401
int oldLength = doc.getLength();
408402
// The undo manager already seems to group the insert and remove together
409403
// automatically, but better be explicit about it.
410-
textarea.getUndoManager().beginInternalAtomicEdit();
404+
textarea.beginAtomicEdit();
411405
try {
412406
doc.insertString(oldLength, what, null);
413407
doc.remove(0, oldLength);
414408
} catch (BadLocationException e) {
415409
System.err.println("Unexpected failure replacing text");
416410
} finally {
417-
textarea.getUndoManager().endInternalAtomicEdit();
411+
textarea.endAtomicEdit();
418412
}
419413
} finally {
420414
caret.setUpdatePolicy(policy);
@@ -554,10 +548,6 @@ void handleRedo() {
554548
textarea.redoLastAction();
555549
}
556550

557-
public UndoManager getUndoManager() {
558-
return textarea.getUndoManager();
559-
}
560-
561551
public String getCurrentKeyword() {
562552
String text = "";
563553
if (textarea.getSelectedText() != null)

app/src/processing/app/LastUndoableEditAwareUndoManager.java

-37
This file was deleted.

0 commit comments

Comments
 (0)