Skip to content

Commit 6f4b03a

Browse files
Fix edit actions when the text area is not focused
Since some edit actions are handled by RTextArea Action objects directly, they no longer worked when the main text area was not focused. This is because RecordableTextAction uses TextAction.getTextComponent to figure out what component to act on, which resolves to the event source (if it is a JTextComponent) or the most recently JTextComponent otherwise. In this case, it meant that if the error output component is focused, actions will not work. To fix that, the actions are wrapped to always act on the text area in the current tab, instead of relying on getTextComponent().
1 parent ca284fb commit 6f4b03a

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

app/src/processing/app/Editor.java

+25-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import jssc.SerialPortException;
3434
import processing.app.debug.RunnerException;
3535
import processing.app.forms.PasswordAuthorizationDialog;
36+
import processing.app.helpers.ActionWrapper;
3637
import processing.app.helpers.Keys;
3738
import processing.app.helpers.OSUtils;
3839
import processing.app.helpers.PreferencesMapException;
@@ -1259,6 +1260,24 @@ public void actionPerformed(ActionEvent e) {
12591260
return menu;
12601261
}
12611262

1263+
/**
1264+
* Wrapper around RSyntaxTextArea's RecordableTextActions. Normally,
1265+
* these actions use the event source, or the most recently focused
1266+
* component to find the text area to work on, but this does not
1267+
* always work. This wrapper makes them always work on the currently
1268+
* selected tab instead.
1269+
*/
1270+
class RstaActionWrapper extends ActionWrapper {
1271+
RstaActionWrapper(RecordableTextAction action) {
1272+
super(action);
1273+
}
1274+
1275+
@Override
1276+
public void actionPerformed(ActionEvent e) {
1277+
((RecordableTextAction) getWrappedAction()).actionPerformedImpl(e, getCurrentTab().getTextArea());
1278+
}
1279+
}
1280+
12621281
private JMenu buildEditMenu() {
12631282
JMenu menu = new JMenu(tr("Edit"));
12641283
menu.setName("menuEdit");
@@ -1269,12 +1288,12 @@ private JMenu buildEditMenu() {
12691288
new RTextArea();
12701289

12711290
RecordableTextAction undoAction = RTextArea.getAction(RTextArea.UNDO_ACTION);
1272-
JMenuItem undoItem = new JMenuItem(undoAction);
1291+
JMenuItem undoItem = new JMenuItem(new RstaActionWrapper(undoAction));
12731292
undoItem.setName("menuEditUndo");
12741293
menu.add(undoItem);
12751294

12761295
RecordableTextAction redoAction = RTextArea.getAction(RTextArea.REDO_ACTION);
1277-
JMenuItem redoItem = new JMenuItem(redoAction);
1296+
JMenuItem redoItem = new JMenuItem(new RstaActionWrapper(redoAction));
12781297
redoItem.setName("menuEditRedo");
12791298
menu.add(redoItem);
12801299

@@ -1288,10 +1307,10 @@ private JMenu buildEditMenu() {
12881307
menu.addSeparator();
12891308

12901309
RecordableTextAction cutAction = RTextArea.getAction(RTextArea.CUT_ACTION);
1291-
menu.add(new JMenuItem(cutAction));
1310+
menu.add(new JMenuItem(new RstaActionWrapper(cutAction)));
12921311

12931312
RecordableTextAction copyAction = RTextArea.getAction(RTextArea.COPY_ACTION);
1294-
menu.add(new JMenuItem(copyAction));
1313+
menu.add(new JMenuItem(new RstaActionWrapper(copyAction)));
12951314

12961315

12971316
JMenuItem copyForumItem = newJMenuItemShift(tr("Copy for Forum"), 'C');
@@ -1311,10 +1330,10 @@ public void actionPerformed(ActionEvent e) {
13111330
menu.add(copyHTMLItem);
13121331

13131332
RecordableTextAction pasteAction = RTextArea.getAction(RTextArea.PASTE_ACTION);
1314-
menu.add(new JMenuItem(pasteAction));
1333+
menu.add(new JMenuItem(new RstaActionWrapper(pasteAction)));
13151334

13161335
RecordableTextAction selectAllAction = RTextArea.getAction(RTextArea.SELECT_ALL_ACTION);
1317-
menu.add(new JMenuItem(selectAllAction));
1336+
menu.add(new JMenuItem(new RstaActionWrapper(selectAllAction)));
13181337

13191338
JMenuItem gotoLine = newJMenuItem(tr("Go to line..."), 'L');
13201339
gotoLine.addActionListener(e -> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package processing.app.helpers;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.beans.PropertyChangeListener;
5+
6+
import javax.swing.Action;
7+
8+
public class ActionWrapper implements Action {
9+
protected Action action;
10+
11+
public ActionWrapper(Action wrapped) {
12+
this.action = wrapped;
13+
}
14+
15+
public Action getWrappedAction() {
16+
return this.action;
17+
}
18+
19+
@Override
20+
public void actionPerformed(ActionEvent e) {
21+
this.action.actionPerformed(e);
22+
}
23+
24+
@Override
25+
public Object getValue(String key) {
26+
return this.action.getValue(key);
27+
}
28+
29+
@Override
30+
public void putValue(String key, Object value) {
31+
this.action.putValue(key, value);
32+
}
33+
34+
@Override
35+
public void setEnabled(boolean b) {
36+
this.action.setEnabled(b);
37+
}
38+
39+
@Override
40+
public boolean isEnabled() {
41+
return this.action.isEnabled();
42+
}
43+
44+
@Override
45+
public void addPropertyChangeListener(PropertyChangeListener listener) {
46+
this.action.addPropertyChangeListener(listener);
47+
}
48+
49+
@Override
50+
public void removePropertyChangeListener(PropertyChangeListener listener) {
51+
this.action.removePropertyChangeListener(listener);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)