forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractTextMonitor.java
More file actions
253 lines (206 loc) · 7.97 KB
/
Copy pathAbstractTextMonitor.java
File metadata and controls
253 lines (206 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package processing.app;
import static processing.app.I18n.tr;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseWheelListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultEditorKit;
import cc.arduino.packages.BoardPort;
@SuppressWarnings("serial")
public abstract class AbstractTextMonitor extends AbstractMonitor {
protected JLabel noLineEndingAlert;
protected TextAreaFIFO textArea;
protected JScrollPane scrollPane;
protected JTextField textField;
protected JButton sendButton;
protected JButton clearButton;
protected JCheckBox autoscrollBox;
protected JCheckBox addTimeStampBox;
protected JComboBox<String> lineEndings;
protected JComboBox<String> serialRates;
public AbstractTextMonitor(BoardPort boardPort) {
super(boardPort);
}
@Override
public synchronized void addMouseWheelListener(MouseWheelListener l) {
super.addMouseWheelListener(l);
textArea.addMouseWheelListener(l);
}
@Override
public synchronized void addKeyListener(KeyListener l) {
super.addKeyListener(l);
textArea.addKeyListener(l);
textField.addKeyListener(l);
}
@Override
protected void onCreateWindow(Container mainPane) {
mainPane.setLayout(new BorderLayout());
textArea = new TextAreaFIFO(8_000_000);
textArea.setRows(16);
textArea.setColumns(40);
textArea.setEditable(false);
// don't automatically update the caret. that way we can manually decide
// whether or not to do so based on the autoscroll checkbox.
((DefaultCaret) textArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
scrollPane = new JScrollPane(textArea);
mainPane.add(scrollPane, BorderLayout.CENTER);
JPanel upperPane = new JPanel();
upperPane.setLayout(new BoxLayout(upperPane, BoxLayout.X_AXIS));
upperPane.setBorder(new EmptyBorder(4, 4, 4, 4));
textField = new JTextField(40);
// textField is selected every time the window is focused
addWindowFocusListener(new WindowAdapter() {
@Override
public void windowGainedFocus(WindowEvent e) {
textField.requestFocusInWindow();
}
});
// Add cut/copy/paste contextual menu to the text input field.
JPopupMenu menu = new JPopupMenu();
Action cut = new DefaultEditorKit.CutAction();
cut.putValue(Action.NAME, tr("Cut"));
menu.add(cut);
Action copy = new DefaultEditorKit.CopyAction();
copy.putValue(Action.NAME, tr("Copy"));
menu.add(copy);
Action paste = new DefaultEditorKit.PasteAction();
paste.putValue(Action.NAME, tr("Paste"));
menu.add(paste);
textField.setComponentPopupMenu(menu);
sendButton = new JButton(tr("Send"));
clearButton = new JButton(tr("Clear output"));
upperPane.add(textField);
upperPane.add(Box.createRigidArea(new Dimension(4, 0)));
upperPane.add(sendButton);
mainPane.add(upperPane, BorderLayout.NORTH);
final JPanel pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
pane.setBorder(new EmptyBorder(4, 4, 4, 4));
autoscrollBox = new JCheckBox(tr("Autoscroll"), true);
addTimeStampBox = new JCheckBox(tr("Show timestamp"), false);
noLineEndingAlert = new JLabel(I18n.format(tr("You've pressed {0} but nothing was sent. Should you select a line ending?"), tr("Send")));
noLineEndingAlert.setToolTipText(noLineEndingAlert.getText());
noLineEndingAlert.setForeground(pane.getBackground());
Dimension minimumSize = new Dimension(noLineEndingAlert.getMinimumSize());
minimumSize.setSize(minimumSize.getWidth() / 3, minimumSize.getHeight());
noLineEndingAlert.setMinimumSize(minimumSize);
lineEndings = new JComboBox<>(new String[]{tr("No line ending"), tr("Newline"), tr("Carriage return"), tr("Both NL & CR")});
lineEndings.addActionListener((ActionEvent event) -> {
PreferencesData.setInteger("serial.line_ending", lineEndings.getSelectedIndex());
noLineEndingAlert.setForeground(pane.getBackground());
});
addTimeStampBox.addActionListener((ActionEvent event) ->
PreferencesData.setBoolean("serial.show_timestamp", addTimeStampBox.isSelected()));
lineEndings.setMaximumSize(lineEndings.getMinimumSize());
serialRates = new JComboBox<>();
for (String rate : serialRateStrings) {
serialRates.addItem(rate + " " + tr("baud"));
}
serialRates.setMaximumSize(serialRates.getMinimumSize());
pane.add(autoscrollBox);
pane.add(addTimeStampBox);
pane.add(Box.createHorizontalGlue());
pane.add(noLineEndingAlert);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(lineEndings);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(serialRates);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(clearButton);
applyPreferences();
mainPane.add(pane, BorderLayout.SOUTH);
}
@Override
protected void onEnableWindow(boolean enable)
{
textArea.setEnabled(enable);
clearButton.setEnabled(enable);
scrollPane.setEnabled(enable);
textField.setEnabled(enable);
sendButton.setEnabled(enable);
autoscrollBox.setEnabled(enable);
addTimeStampBox.setEnabled(enable);
lineEndings.setEnabled(enable);
serialRates.setEnabled(enable);
}
public void onSendCommand(ActionListener listener) {
textField.addActionListener(listener);
sendButton.addActionListener(listener);
}
public void onClearCommand(ActionListener listener) {
clearButton.addActionListener(listener);
}
public void onSerialRateChange(ActionListener listener) {
serialRates.addActionListener(listener);
}
@Override
public void message(String msg) {
SwingUtilities.invokeLater(() -> updateTextArea(msg));
}
private static final String LINE_SEPARATOR = "\n";
private boolean isStartingLine = true;
protected void updateTextArea(String msg) {
if (addTimeStampBox.isSelected()) {
textArea.append(addTimestamps(msg));
} else {
textArea.append(msg);
}
if (autoscrollBox.isSelected()) {
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
@Override
public void applyPreferences() {
// Apply font.
Font consoleFont = Theme.getFont("console.font");
Font editorFont = PreferencesData.getFont("editor.font");
textArea.setFont(Theme.scale(new Font(
consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize())));
// Apply line endings.
if (PreferencesData.get("serial.line_ending") != null) {
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
}
// Apply timestamp visibility.
if (PreferencesData.get("serial.show_timestamp") != null) {
addTimeStampBox.setSelected(PreferencesData.getBoolean("serial.show_timestamp"));
}
}
private String addTimestamps(String text) {
String now = new SimpleDateFormat("HH:mm:ss.SSS -> ").format(new Date());
final StringBuilder sb = new StringBuilder(text.length() + now.length());
StringTokenizer tokenizer = new StringTokenizer(text, LINE_SEPARATOR, true);
while (tokenizer.hasMoreTokens()) {
if (isStartingLine) {
sb.append(now);
}
String token = tokenizer.nextToken();
sb.append(token);
// tokenizer returns "\n" as a single token
isStartingLine = token.equals(LINE_SEPARATOR);
}
return sb.toString();
}
}