Skip to content

Commit 941050e

Browse files
committed
Add contextual menus to input fields
- Add contextual menus to text-based monitors (serial / network monitor). - Add contextual menu to installer dialog search filter fields (library manager / contribution manager). - Make installer dialogs focus the search filter field on window-open. This prevents pastes from ending up elsewhere in the case that they are performed before the field has been focussed at least once. Fixes #8423.
1 parent a87024d commit 941050e

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

app/src/cc/arduino/contributions/ui/FilterJTextField.java

+19
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,23 @@ private void updateStyle() {
124124
setFont(getFont().deriveFont(Font.PLAIN));
125125
}
126126
}
127+
128+
@Override
129+
public void paste() {
130+
131+
// Same precondition check as JTextComponent#paste().
132+
if (!isEditable() || !isEnabled()) {
133+
return;
134+
}
135+
136+
// Disable hint to prevent the focus handler from clearing the pasted text.
137+
if (showingHint) {
138+
showingHint = false;
139+
setText("");
140+
updateStyle();
141+
}
142+
143+
// Perform the paste.
144+
super.paste();
145+
}
127146
}

app/src/cc/arduino/contributions/ui/InstallerJDialog.java

+29
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,18 @@
4545
import java.awt.event.WindowEvent;
4646
import java.awt.event.MouseEvent;
4747
import java.awt.event.MouseMotionListener;
48+
import java.awt.event.WindowAdapter;
4849
import java.util.function.Predicate;
4950

51+
import javax.swing.Action;
5052
import javax.swing.Box;
5153
import javax.swing.BoxLayout;
5254
import javax.swing.JButton;
5355
import javax.swing.JComboBox;
5456
import javax.swing.JDialog;
5557
import javax.swing.JLabel;
5658
import javax.swing.JPanel;
59+
import javax.swing.JPopupMenu;
5760
import javax.swing.JScrollPane;
5861
import javax.swing.JTable;
5962
import javax.swing.ListSelectionModel;
@@ -64,6 +67,7 @@
6467
import javax.swing.table.TableCellRenderer;
6568
import javax.swing.table.TableColumn;
6669
import javax.swing.table.TableColumnModel;
70+
import javax.swing.text.DefaultEditorKit;
6771

6872
import cc.arduino.contributions.ui.listeners.AbstractKeyListener;
6973
import processing.app.Base;
@@ -130,6 +134,31 @@ protected void onFilter(String[] _filters) {
130134
}
131135
};
132136

137+
// Add cut/copy/paste contextual menu to the search filter input field.
138+
JPopupMenu menu = new JPopupMenu();
139+
140+
Action cut = new DefaultEditorKit.CutAction();
141+
cut.putValue(Action.NAME, tr("Cut"));
142+
menu.add(cut);
143+
144+
Action copy = new DefaultEditorKit.CopyAction();
145+
copy.putValue(Action.NAME, tr("Copy"));
146+
menu.add(copy);
147+
148+
Action paste = new DefaultEditorKit.PasteAction();
149+
paste.putValue(Action.NAME, tr("Paste"));
150+
menu.add(paste);
151+
152+
filterField.setComponentPopupMenu(menu);
153+
154+
// Focus the filter field when the window opens.
155+
addWindowListener(new WindowAdapter() {
156+
@Override
157+
public void windowOpened(WindowEvent e) {
158+
filterField.requestFocus();
159+
}
160+
});
161+
133162
filtersContainer = new JPanel();
134163
filtersContainer.setLayout(new BoxLayout(filtersContainer, BoxLayout.X_AXIS));
135164
filtersContainer.add(Box.createHorizontalStrut(5));

app/src/processing/app/AbstractTextMonitor.java

+20
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@
1414
import java.util.Date;
1515
import java.util.StringTokenizer;
1616

17+
import javax.swing.Action;
1718
import javax.swing.Box;
1819
import javax.swing.BoxLayout;
1920
import javax.swing.JButton;
2021
import javax.swing.JCheckBox;
2122
import javax.swing.JComboBox;
2223
import javax.swing.JLabel;
2324
import javax.swing.JPanel;
25+
import javax.swing.JPopupMenu;
2426
import javax.swing.JScrollPane;
2527
import javax.swing.JTextField;
2628
import javax.swing.SwingUtilities;
2729
import javax.swing.border.EmptyBorder;
2830
import javax.swing.text.DefaultCaret;
31+
import javax.swing.text.DefaultEditorKit;
2932

3033
import cc.arduino.packages.BoardPort;
3134

@@ -82,6 +85,23 @@ public void windowGainedFocus(WindowEvent e) {
8285
}
8386
});
8487

88+
// Add cut/copy/paste contextual menu to the text input field.
89+
JPopupMenu menu = new JPopupMenu();
90+
91+
Action cut = new DefaultEditorKit.CutAction();
92+
cut.putValue(Action.NAME, tr("Cut"));
93+
menu.add(cut);
94+
95+
Action copy = new DefaultEditorKit.CopyAction();
96+
copy.putValue(Action.NAME, tr("Copy"));
97+
menu.add(copy);
98+
99+
Action paste = new DefaultEditorKit.PasteAction();
100+
paste.putValue(Action.NAME, tr("Paste"));
101+
menu.add(paste);
102+
103+
textField.setComponentPopupMenu(menu);
104+
85105
sendButton = new JButton(tr("Send"));
86106
clearButton = new JButton(tr("Clear output"));
87107

0 commit comments

Comments
 (0)