1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

Removed the need for get/setUndoManager()

This dramatically simplifies undo/redo handling and allows to use
unpatched RSyntaxTextArea library.
This commit is contained in:
Cristian Maglie 2017-02-22 16:46:36 +01:00 committed by Cristian Maglie
parent 1062307c52
commit 5f12bb9a3e
3 changed files with 14 additions and 120 deletions

View File

@ -33,20 +33,19 @@ import com.jcraft.jsch.JSchException;
import jssc.SerialPortException; import jssc.SerialPortException;
import processing.app.debug.RunnerException; import processing.app.debug.RunnerException;
import processing.app.forms.PasswordAuthorizationDialog; import processing.app.forms.PasswordAuthorizationDialog;
import processing.app.helpers.DocumentTextChangeListener;
import processing.app.helpers.Keys; import processing.app.helpers.Keys;
import processing.app.helpers.OSUtils; import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMapException; import processing.app.helpers.PreferencesMapException;
import processing.app.legacy.PApplet; import processing.app.legacy.PApplet;
import processing.app.syntax.PdeKeywords; import processing.app.syntax.PdeKeywords;
import processing.app.syntax.SketchTextArea;
import processing.app.tools.MenuScroller; import processing.app.tools.MenuScroller;
import processing.app.tools.Tool; import processing.app.tools.Tool;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.*; import javax.swing.event.*;
import javax.swing.text.BadLocationException; 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.*;
import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
@ -185,8 +184,6 @@ public class Editor extends JFrame implements RunnerListener {
// undo fellers // undo fellers
private JMenuItem undoItem; private JMenuItem undoItem;
private JMenuItem redoItem; private JMenuItem redoItem;
protected UndoAction undoAction;
protected RedoAction redoAction;
private FindReplace find; private FindReplace find;
@ -1273,7 +1270,7 @@ public class Editor extends JFrame implements RunnerListener {
undoItem = newJMenuItem(tr("Undo"), 'Z'); undoItem = newJMenuItem(tr("Undo"), 'Z');
undoItem.setName("menuEditUndo"); undoItem.setName("menuEditUndo");
undoItem.addActionListener(undoAction = new UndoAction()); undoItem.addActionListener(e -> getCurrentTab().handleUndo());
menu.add(undoItem); menu.add(undoItem);
if (!OSUtils.isMacOS()) { if (!OSUtils.isMacOS()) {
@ -1282,7 +1279,7 @@ public class Editor extends JFrame implements RunnerListener {
redoItem = newJMenuItemShift(tr("Redo"), 'Z'); redoItem = newJMenuItemShift(tr("Redo"), 'Z');
} }
redoItem.setName("menuEditRedo"); redoItem.setName("menuEditRedo");
redoItem.addActionListener(redoAction = new RedoAction()); redoItem.addActionListener(e -> getCurrentTab().handleRedo());
menu.add(redoItem); menu.add(redoItem);
menu.addSeparator(); menu.addSeparator();
@ -1478,68 +1475,10 @@ public class Editor extends JFrame implements RunnerListener {
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
class UndoAction extends AbstractAction { protected void updateUndoRedoState() {
public UndoAction() { SketchTextArea textArea = getCurrentTab().getTextArea();
super("Undo"); undoItem.setEnabled(textArea.canUndo());
this.setEnabled(false); redoItem.setEnabled(textArea.canRedo());
}
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");
}
}
} }
@ -1610,8 +1549,7 @@ public class Editor extends JFrame implements RunnerListener {
*/ */
public void selectTab(final int index) { public void selectTab(final int index) {
currentTabIndex = index; currentTabIndex = index;
undoAction.updateUndoState(); updateUndoRedoState();
redoAction.updateRedoState();
updateTitle(); updateTitle();
header.rebuild(); header.rebuild();
getCurrentTab().activated(); getCurrentTab().activated();
@ -1710,6 +1648,9 @@ public class Editor extends JFrame implements RunnerListener {
*/ */
protected void addTab(SketchFile file, String contents) throws IOException { protected void addTab(SketchFile file, String contents) throws IOException {
EditorTab tab = new EditorTab(this, file, contents); EditorTab tab = new EditorTab(this, file, contents);
tab.getTextArea().getDocument()
.addDocumentListener(new DocumentTextChangeListener(
() -> updateUndoRedoState()));
tabs.add(tab); tabs.add(tab);
reorderTabs(); reorderTabs();
} }

View File

@ -44,7 +44,6 @@ import javax.swing.event.PopupMenuListener;
import javax.swing.text.BadLocationException; import javax.swing.text.BadLocationException;
import javax.swing.text.Element; import javax.swing.text.Element;
import javax.swing.text.PlainDocument; import javax.swing.text.PlainDocument;
import javax.swing.undo.UndoManager;
import javax.swing.text.DefaultCaret; import javax.swing.text.DefaultCaret;
import javax.swing.text.Document; 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.rsyntaxtextarea.RSyntaxUtilities;
import org.fife.ui.rtextarea.Gutter; import org.fife.ui.rtextarea.Gutter;
import org.fife.ui.rtextarea.RTextScrollPane; import org.fife.ui.rtextarea.RTextScrollPane;
import org.fife.ui.rtextarea.RUndoManager;
import cc.arduino.UpdatableBoardsLibsFakeURLsHandler; import cc.arduino.UpdatableBoardsLibsFakeURLsHandler;
import processing.app.helpers.DocumentTextChangeListener; import processing.app.helpers.DocumentTextChangeListener;
@ -108,10 +106,6 @@ public class EditorTab extends JPanel implements SketchFile.TextStorage {
file.setStorage(this); file.setStorage(this);
applyPreferences(); applyPreferences();
add(scrollPane, BorderLayout.CENTER); add(scrollPane, BorderLayout.CENTER);
RUndoManager undo = new LastUndoableEditAwareUndoManager(this.textarea, this.editor);
document.addUndoableEditListener(undo);
textarea.setUndoManager(undo);
} }
private RSyntaxDocument createDocument(String contents) { private RSyntaxDocument createDocument(String contents) {
@ -407,14 +401,14 @@ public class EditorTab extends JPanel implements SketchFile.TextStorage {
int oldLength = doc.getLength(); int oldLength = doc.getLength();
// The undo manager already seems to group the insert and remove together // The undo manager already seems to group the insert and remove together
// automatically, but better be explicit about it. // automatically, but better be explicit about it.
textarea.getUndoManager().beginInternalAtomicEdit(); textarea.beginAtomicEdit();
try { try {
doc.insertString(oldLength, what, null); doc.insertString(oldLength, what, null);
doc.remove(0, oldLength); doc.remove(0, oldLength);
} catch (BadLocationException e) { } catch (BadLocationException e) {
System.err.println("Unexpected failure replacing text"); System.err.println("Unexpected failure replacing text");
} finally { } finally {
textarea.getUndoManager().endInternalAtomicEdit(); textarea.endAtomicEdit();
} }
} finally { } finally {
caret.setUpdatePolicy(policy); caret.setUpdatePolicy(policy);
@ -554,10 +548,6 @@ public class EditorTab extends JPanel implements SketchFile.TextStorage {
textarea.redoLastAction(); textarea.redoLastAction();
} }
public UndoManager getUndoManager() {
return textarea.getUndoManager();
}
public String getCurrentKeyword() { public String getCurrentKeyword() {
String text = ""; String text = "";
if (textarea.getSelectedText() != null) if (textarea.getSelectedText() != null)

View File

@ -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();
}
}