Skip to content

Commit 76bc293

Browse files
Use RTextArea.getActon for some actions
Previously, custom Actions and ActionHandlers were used to handle undo, redo, cut, copy, paste and select all. However, RTextArea.getAction() exposes action objects for these things already. This commit makes use of them. This allows slightly shrinking buildEditMenu(), but the real gain is that RTextArea automatically takes care of keeping the state these actions correct (based on the most recently focused RTextArea), which allows removing some custom event handlers and other overhead. In particular, this helps to removes the Arduino-specific changes for RSyntaxTextArea later. As a side effect, this breaks these actions when the text area is not in focus. This will be fixed in a subsequent commit.
1 parent 7434443 commit 76bc293

File tree

2 files changed

+27
-136
lines changed

2 files changed

+27
-136
lines changed

app/src/processing/app/Editor.java

+27-132
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
import javax.swing.*;
4545
import javax.swing.event.*;
4646
import javax.swing.text.BadLocationException;
47-
import javax.swing.undo.CannotRedoException;
48-
import javax.swing.undo.CannotUndoException;
49-
import javax.swing.undo.UndoManager;
47+
import org.fife.ui.rtextarea.RTextArea;
48+
import org.fife.ui.rtextarea.RecordableTextAction;
49+
5050
import java.awt.*;
5151
import java.awt.datatransfer.DataFlavor;
5252
import java.awt.datatransfer.Transferable;
@@ -182,12 +182,6 @@ public boolean test(SketchController sketch) {
182182
//boolean presenting;
183183
private boolean uploading;
184184

185-
// undo fellers
186-
private JMenuItem undoItem;
187-
private JMenuItem redoItem;
188-
protected UndoAction undoAction;
189-
protected RedoAction redoAction;
190-
191185
private FindReplace find;
192186

193187
Runnable runHandler;
@@ -1265,43 +1259,40 @@ public void actionPerformed(ActionEvent e) {
12651259
return menu;
12661260
}
12671261

1268-
12691262
private JMenu buildEditMenu() {
12701263
JMenu menu = new JMenu(tr("Edit"));
12711264
menu.setName("menuEdit");
12721265
menu.setMnemonic(KeyEvent.VK_E);
12731266

1274-
undoItem = newJMenuItem(tr("Undo"), 'Z');
1267+
// Create a dummy RTextArea, to force it to create the static list
1268+
// of actions accessible through RTextArea.getAction().
1269+
new RTextArea();
1270+
1271+
RecordableTextAction undoAction = RTextArea.getAction(RTextArea.UNDO_ACTION);
1272+
JMenuItem undoItem = new JMenuItem(undoAction);
12751273
undoItem.setName("menuEditUndo");
1276-
undoItem.addActionListener(undoAction = new UndoAction());
12771274
menu.add(undoItem);
12781275

1279-
if (!OSUtils.isMacOS()) {
1280-
redoItem = newJMenuItem(tr("Redo"), 'Y');
1281-
} else {
1282-
redoItem = newJMenuItemShift(tr("Redo"), 'Z');
1283-
}
1276+
RecordableTextAction redoAction = RTextArea.getAction(RTextArea.REDO_ACTION);
1277+
JMenuItem redoItem = new JMenuItem(redoAction);
12841278
redoItem.setName("menuEditRedo");
1285-
redoItem.addActionListener(redoAction = new RedoAction());
12861279
menu.add(redoItem);
12871280

1281+
// RSTA defaults to Ctrl-Y for redo, but convention on OSX is Ctrl-Shift-Z
1282+
// This might not be the best place, but it works.
1283+
if (OSUtils.isMacOS()) {
1284+
int mods = SHORTCUT_KEY_MASK | ActionEvent.SHIFT_MASK;
1285+
redoAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, mods));
1286+
}
1287+
12881288
menu.addSeparator();
12891289

1290-
JMenuItem cutItem = newJMenuItem(tr("Cut"), 'X');
1291-
cutItem.addActionListener(new ActionListener() {
1292-
public void actionPerformed(ActionEvent e) {
1293-
getCurrentTab().handleCut();
1294-
}
1295-
});
1296-
menu.add(cutItem);
1290+
RecordableTextAction cutAction = RTextArea.getAction(RTextArea.CUT_ACTION);
1291+
menu.add(new JMenuItem(cutAction));
1292+
1293+
RecordableTextAction copyAction = RTextArea.getAction(RTextArea.COPY_ACTION);
1294+
menu.add(new JMenuItem(copyAction));
12971295

1298-
JMenuItem copyItem = newJMenuItem(tr("Copy"), 'C');
1299-
copyItem.addActionListener(new ActionListener() {
1300-
public void actionPerformed(ActionEvent e) {
1301-
getCurrentTab().getTextArea().copy();
1302-
}
1303-
});
1304-
menu.add(copyItem);
13051296

13061297
JMenuItem copyForumItem = newJMenuItemShift(tr("Copy for Forum"), 'C');
13071298
copyForumItem.addActionListener(new ActionListener() {
@@ -1319,21 +1310,11 @@ public void actionPerformed(ActionEvent e) {
13191310
});
13201311
menu.add(copyHTMLItem);
13211312

1322-
JMenuItem pasteItem = newJMenuItem(tr("Paste"), 'V');
1323-
pasteItem.addActionListener(new ActionListener() {
1324-
public void actionPerformed(ActionEvent e) {
1325-
getCurrentTab().handlePaste();
1326-
}
1327-
});
1328-
menu.add(pasteItem);
1313+
RecordableTextAction pasteAction = RTextArea.getAction(RTextArea.PASTE_ACTION);
1314+
menu.add(new JMenuItem(pasteAction));
13291315

1330-
JMenuItem selectAllItem = newJMenuItem(tr("Select All"), 'A');
1331-
selectAllItem.addActionListener(new ActionListener() {
1332-
public void actionPerformed(ActionEvent e) {
1333-
getCurrentTab().handleSelectAll();
1334-
}
1335-
});
1336-
menu.add(selectAllItem);
1316+
RecordableTextAction selectAllAction = RTextArea.getAction(RTextArea.SELECT_ALL_ACTION);
1317+
menu.add(new JMenuItem(selectAllAction));
13371318

13381319
JMenuItem gotoLine = newJMenuItem(tr("Go to line..."), 'L');
13391320
gotoLine.addActionListener(e -> {
@@ -1422,21 +1403,6 @@ public void actionPerformed(ActionEvent e) {
14221403
menu.add(useSelectionForFindItem);
14231404
}
14241405

1425-
menu.addMenuListener(new MenuListener() {
1426-
@Override
1427-
public void menuSelected(MenuEvent e) {
1428-
boolean enabled = getCurrentTab().getSelectedText() != null;
1429-
cutItem.setEnabled(enabled);
1430-
copyItem.setEnabled(enabled);
1431-
}
1432-
1433-
@Override
1434-
public void menuDeselected(MenuEvent e) {}
1435-
1436-
@Override
1437-
public void menuCanceled(MenuEvent e) {}
1438-
});
1439-
14401406
return menu;
14411407
}
14421408

@@ -1474,75 +1440,6 @@ private static JMenuItem newJMenuItemAlt(String title, int what) {
14741440
return menuItem;
14751441
}
14761442

1477-
1478-
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1479-
1480-
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-
}
1543-
}
1544-
1545-
15461443
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15471444

15481445

@@ -1610,8 +1507,6 @@ public List<EditorTab> getTabs() {
16101507
*/
16111508
public void selectTab(final int index) {
16121509
currentTabIndex = index;
1613-
undoAction.updateUndoState();
1614-
redoAction.updateRedoState();
16151510
updateTitle();
16161511
header.rebuild();
16171512
getCurrentTab().activated();

app/src/processing/app/LastUndoableEditAwareUndoManager.java

-4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,5 @@ public synchronized void redo() throws CannotRedoException {
2929
@Override
3030
public void updateActions() {
3131
super.updateActions();
32-
editor.undoAction.updateUndoState();
33-
editor.redoAction.updateRedoState();
3432
}
35-
36-
3733
}

0 commit comments

Comments
 (0)