-
-
Notifications
You must be signed in to change notification settings - Fork 7k
/
Copy pathAbstractTextMonitor.java
262 lines (223 loc) · 8.66 KB
/
AbstractTextMonitor.java
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
254
255
256
257
258
259
260
261
262
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.WindowAdapter;
import java.awt.event.WindowEvent;
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.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.Document;
import cc.arduino.packages.BoardPort;
@SuppressWarnings("serial")
public abstract class AbstractTextMonitor extends AbstractMonitor {
protected JLabel noLineEndingAlert;
protected TextAreaFIFO textArea;
protected HTMLTextAreaFIFO htmlTextArea;
protected JScrollPane scrollPane;
protected JScrollPane htmlScrollPane;
protected JTextField textField;
protected JButton sendButton;
protected JButton clearButton;
protected JCheckBox autoscrollBox;
protected JComboBox lineEndings;
protected JComboBox serialRates;
protected Container mainPane;
private long lastMessage;
private javax.swing.Timer updateTimer;
private boolean htmlView = true;
public AbstractTextMonitor(BoardPort boardPort) {
super(boardPort);
}
protected void onCreateWindow(Container mainPane) {
Font consoleFont = Theme.getFont("console.font");
Font editorFont = PreferencesData.getFont("editor.font");
Font font = Theme.scale(new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize()));
this.mainPane = mainPane;
mainPane.setLayout(new BorderLayout());
textArea = new TextAreaFIFO(8000000);
textArea.setRows(16);
textArea.setColumns(40);
textArea.setEditable(false);
textArea.setFont(font);
htmlTextArea = new HTMLTextAreaFIFO(8000000);
htmlTextArea.setEditable(false);
htmlTextArea.setFont(font);
htmlTextArea.setOpaque(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);
((DefaultCaret) htmlTextArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
Document doc = textArea.getDocument();
if (doc instanceof AbstractDocument)
{
UndoableEditListener[] undoListeners =
( (AbstractDocument) doc).getUndoableEditListeners();
if (undoListeners.length > 0)
{
for (UndoableEditListener undoListener : undoListeners)
{
doc.removeUndoableEditListener(undoListener);
}
}
}
doc = htmlTextArea.getDocument();
if (doc instanceof AbstractDocument)
{
UndoableEditListener[] undoListeners =
( (AbstractDocument) doc).getUndoableEditListeners();
if (undoListeners.length > 0)
{
for (UndoableEditListener undoListener : undoListeners)
{
doc.removeUndoableEditListener(undoListener);
}
}
}
scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
htmlScrollPane = new JScrollPane(htmlTextArea);
htmlScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
ActionListener checkIfSteady = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (System.currentTimeMillis() - lastMessage > 200) {
if (htmlView == false && textArea.getLength() < 1000) {
htmlTextArea.setText("");
boolean res = htmlTextArea.append(textArea.getText());
if (res) {
htmlView = true;
mainPane.remove(scrollPane);
if (textArea.getCaretPosition() > htmlTextArea.getDocument().getLength()) {
htmlTextArea.setCaretPosition(htmlTextArea.getDocument().getLength());
} else {
htmlTextArea.setCaretPosition(textArea.getCaretPosition());
}
mainPane.add(htmlScrollPane, BorderLayout.CENTER);
scrollPane.setVisible(false);
mainPane.validate();
mainPane.repaint();
}
}
} else {
if (htmlView == true) {
htmlView = false;
mainPane.remove(htmlScrollPane);
mainPane.add(scrollPane, BorderLayout.CENTER);
scrollPane.setVisible(true);
mainPane.validate();
mainPane.repaint();
}
}
}
};
updateTimer = new javax.swing.Timer(33, checkIfSteady);
mainPane.add(scrollPane, BorderLayout.CENTER);
htmlTextArea.setVisible(true);
htmlScrollPane.setVisible(true);
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() {
public void windowGainedFocus(WindowEvent e) {
textField.requestFocusInWindow();
}
});
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);
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(new ActionListener() {
public void actionPerformed(ActionEvent event) {
PreferencesData.setInteger("serial.line_ending", lineEndings.getSelectedIndex());
noLineEndingAlert.setForeground(pane.getBackground());
}
});
if (PreferencesData.get("serial.line_ending") != null) {
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
}
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(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);
mainPane.add(pane, BorderLayout.SOUTH);
updateTimer.start();
}
protected void onEnableWindow(boolean enable)
{
textArea.setEnabled(enable);
htmlTextArea.setEnabled(enable);
clearButton.setEnabled(enable);
scrollPane.setEnabled(enable);
htmlScrollPane.setEnabled(enable);
textField.setEnabled(enable);
sendButton.setEnabled(enable);
autoscrollBox.setEnabled(enable);
lineEndings.setEnabled(enable);
serialRates.setEnabled(enable);
if (enable == false) {
htmlTextArea.setText("");
}
}
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);
}
public void message(final String s) {
lastMessage = System.currentTimeMillis();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(s);
if (autoscrollBox.isSelected()) {
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
});
}
}