From 5f12bb9a3ea04c3b726536bdd3f069b5bdd8afe7 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 22 Feb 2017 16:46:36 +0100 Subject: [PATCH] Removed the need for get/setUndoManager() This dramatically simplifies undo/redo handling and allows to use unpatched RSyntaxTextArea library. --- app/src/processing/app/Editor.java | 83 +++---------------- app/src/processing/app/EditorTab.java | 14 +--- .../app/LastUndoableEditAwareUndoManager.java | 37 --------- 3 files changed, 14 insertions(+), 120 deletions(-) delete mode 100644 app/src/processing/app/LastUndoableEditAwareUndoManager.java diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 439dcdf18..287d9fe4e 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -33,20 +33,19 @@ import com.jcraft.jsch.JSchException; import jssc.SerialPortException; import processing.app.debug.RunnerException; import processing.app.forms.PasswordAuthorizationDialog; +import processing.app.helpers.DocumentTextChangeListener; import processing.app.helpers.Keys; import processing.app.helpers.OSUtils; import processing.app.helpers.PreferencesMapException; import processing.app.legacy.PApplet; import processing.app.syntax.PdeKeywords; +import processing.app.syntax.SketchTextArea; import processing.app.tools.MenuScroller; import processing.app.tools.Tool; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.BadLocationException; -import javax.swing.undo.CannotRedoException; -import javax.swing.undo.CannotUndoException; -import javax.swing.undo.UndoManager; import java.awt.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; @@ -185,8 +184,6 @@ public class Editor extends JFrame implements RunnerListener { // undo fellers private JMenuItem undoItem; private JMenuItem redoItem; - protected UndoAction undoAction; - protected RedoAction redoAction; private FindReplace find; @@ -1273,7 +1270,7 @@ public class Editor extends JFrame implements RunnerListener { undoItem = newJMenuItem(tr("Undo"), 'Z'); undoItem.setName("menuEditUndo"); - undoItem.addActionListener(undoAction = new UndoAction()); + undoItem.addActionListener(e -> getCurrentTab().handleUndo()); menu.add(undoItem); if (!OSUtils.isMacOS()) { @@ -1282,7 +1279,7 @@ public class Editor extends JFrame implements RunnerListener { redoItem = newJMenuItemShift(tr("Redo"), 'Z'); } redoItem.setName("menuEditRedo"); - redoItem.addActionListener(redoAction = new RedoAction()); + redoItem.addActionListener(e -> getCurrentTab().handleRedo()); menu.add(redoItem); menu.addSeparator(); @@ -1478,68 +1475,10 @@ public class Editor extends JFrame implements RunnerListener { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - class UndoAction extends AbstractAction { - public UndoAction() { - super("Undo"); - this.setEnabled(false); - } - - public void actionPerformed(ActionEvent e) { - try { - getCurrentTab().handleUndo(); - } catch (CannotUndoException ex) { - //System.out.println("Unable to undo: " + ex); - //ex.printStackTrace(); - } - } - - protected void updateUndoState() { - UndoManager undo = getCurrentTab().getUndoManager(); - - if (undo.canUndo()) { - this.setEnabled(true); - undoItem.setEnabled(true); - undoItem.setText(undo.getUndoPresentationName()); - putValue(Action.NAME, undo.getUndoPresentationName()); - } else { - this.setEnabled(false); - undoItem.setEnabled(false); - undoItem.setText(tr("Undo")); - putValue(Action.NAME, "Undo"); - } - } - } - - - class RedoAction extends AbstractAction { - public RedoAction() { - super("Redo"); - this.setEnabled(false); - } - - public void actionPerformed(ActionEvent e) { - try { - getCurrentTab().handleRedo(); - } catch (CannotRedoException ex) { - //System.out.println("Unable to redo: " + ex); - //ex.printStackTrace(); - } - } - - protected void updateRedoState() { - UndoManager undo = getCurrentTab().getUndoManager(); - - if (undo.canRedo()) { - redoItem.setEnabled(true); - redoItem.setText(undo.getRedoPresentationName()); - putValue(Action.NAME, undo.getRedoPresentationName()); - } else { - this.setEnabled(false); - redoItem.setEnabled(false); - redoItem.setText(tr("Redo")); - putValue(Action.NAME, "Redo"); - } - } + protected void updateUndoRedoState() { + SketchTextArea textArea = getCurrentTab().getTextArea(); + undoItem.setEnabled(textArea.canUndo()); + redoItem.setEnabled(textArea.canRedo()); } @@ -1610,8 +1549,7 @@ public class Editor extends JFrame implements RunnerListener { */ public void selectTab(final int index) { currentTabIndex = index; - undoAction.updateUndoState(); - redoAction.updateRedoState(); + updateUndoRedoState(); updateTitle(); header.rebuild(); getCurrentTab().activated(); @@ -1710,6 +1648,9 @@ public class Editor extends JFrame implements RunnerListener { */ protected void addTab(SketchFile file, String contents) throws IOException { EditorTab tab = new EditorTab(this, file, contents); + tab.getTextArea().getDocument() + .addDocumentListener(new DocumentTextChangeListener( + () -> updateUndoRedoState())); tabs.add(tab); reorderTabs(); } diff --git a/app/src/processing/app/EditorTab.java b/app/src/processing/app/EditorTab.java index 3ca4b22bb..7e84fa001 100644 --- a/app/src/processing/app/EditorTab.java +++ b/app/src/processing/app/EditorTab.java @@ -44,7 +44,6 @@ import javax.swing.event.PopupMenuListener; import javax.swing.text.BadLocationException; import javax.swing.text.Element; import javax.swing.text.PlainDocument; -import javax.swing.undo.UndoManager; import javax.swing.text.DefaultCaret; import javax.swing.text.Document; @@ -53,7 +52,6 @@ import org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit; import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities; import org.fife.ui.rtextarea.Gutter; import org.fife.ui.rtextarea.RTextScrollPane; -import org.fife.ui.rtextarea.RUndoManager; import cc.arduino.UpdatableBoardsLibsFakeURLsHandler; import processing.app.helpers.DocumentTextChangeListener; @@ -108,10 +106,6 @@ public class EditorTab extends JPanel implements SketchFile.TextStorage { file.setStorage(this); applyPreferences(); add(scrollPane, BorderLayout.CENTER); - - RUndoManager undo = new LastUndoableEditAwareUndoManager(this.textarea, this.editor); - document.addUndoableEditListener(undo); - textarea.setUndoManager(undo); } private RSyntaxDocument createDocument(String contents) { @@ -407,14 +401,14 @@ public class EditorTab extends JPanel implements SketchFile.TextStorage { int oldLength = doc.getLength(); // The undo manager already seems to group the insert and remove together // automatically, but better be explicit about it. - textarea.getUndoManager().beginInternalAtomicEdit(); + textarea.beginAtomicEdit(); try { doc.insertString(oldLength, what, null); doc.remove(0, oldLength); } catch (BadLocationException e) { System.err.println("Unexpected failure replacing text"); } finally { - textarea.getUndoManager().endInternalAtomicEdit(); + textarea.endAtomicEdit(); } } finally { caret.setUpdatePolicy(policy); @@ -554,10 +548,6 @@ public class EditorTab extends JPanel implements SketchFile.TextStorage { textarea.redoLastAction(); } - public UndoManager getUndoManager() { - return textarea.getUndoManager(); - } - public String getCurrentKeyword() { String text = ""; if (textarea.getSelectedText() != null) diff --git a/app/src/processing/app/LastUndoableEditAwareUndoManager.java b/app/src/processing/app/LastUndoableEditAwareUndoManager.java deleted file mode 100644 index 736be42d3..000000000 --- a/app/src/processing/app/LastUndoableEditAwareUndoManager.java +++ /dev/null @@ -1,37 +0,0 @@ -package processing.app; - -import javax.swing.undo.CannotRedoException; -import javax.swing.undo.CannotUndoException; - -import org.fife.ui.rtextarea.RUndoManager; - -import processing.app.syntax.SketchTextArea; - -public class LastUndoableEditAwareUndoManager extends RUndoManager { - - private Editor editor; - - public LastUndoableEditAwareUndoManager(SketchTextArea textarea, Editor editor) { - super(textarea); - this.editor = editor; - } - - @Override - public synchronized void undo() throws CannotUndoException { - super.undo(); - } - - @Override - public synchronized void redo() throws CannotRedoException { - super.redo(); - } - - @Override - public void updateActions() { - super.updateActions(); - editor.undoAction.updateUndoState(); - editor.redoAction.updateRedoState(); - } - - -}