|
1 | 1 | package processing.app; |
2 | 2 |
|
3 | | -import processing.app.debug.MessageConsumer; |
4 | | -import processing.app.legacy.PApplet; |
| 3 | +import static processing.app.I18n._; |
5 | 4 |
|
6 | | -import javax.swing.*; |
7 | | -import javax.swing.border.EmptyBorder; |
8 | | -import javax.swing.text.DefaultCaret; |
9 | | -import java.awt.*; |
| 5 | +import java.awt.BorderLayout; |
| 6 | +import java.awt.Dimension; |
| 7 | +import java.awt.Font; |
| 8 | +import java.awt.Rectangle; |
| 9 | +import java.awt.Toolkit; |
10 | 10 | import java.awt.event.ActionEvent; |
11 | 11 | import java.awt.event.ActionListener; |
12 | 12 | import java.awt.event.WindowAdapter; |
13 | 13 | import java.awt.event.WindowEvent; |
14 | 14 |
|
15 | | -import static processing.app.I18n._; |
| 15 | +import javax.swing.AbstractAction; |
| 16 | +import javax.swing.Box; |
| 17 | +import javax.swing.BoxLayout; |
| 18 | +import javax.swing.JButton; |
| 19 | +import javax.swing.JCheckBox; |
| 20 | +import javax.swing.JComboBox; |
| 21 | +import javax.swing.JComponent; |
| 22 | +import javax.swing.JFrame; |
| 23 | +import javax.swing.JLabel; |
| 24 | +import javax.swing.JPanel; |
| 25 | +import javax.swing.JScrollPane; |
| 26 | +import javax.swing.JTextField; |
| 27 | +import javax.swing.KeyStroke; |
| 28 | +import javax.swing.SwingUtilities; |
| 29 | +import javax.swing.Timer; |
| 30 | +import javax.swing.border.EmptyBorder; |
| 31 | +import javax.swing.text.DefaultCaret; |
| 32 | + |
| 33 | +import processing.app.debug.TextAreaFIFO; |
| 34 | +import processing.app.legacy.PApplet; |
16 | 35 |
|
17 | 36 | @SuppressWarnings("serial") |
18 | | -public abstract class AbstractMonitor extends JFrame implements MessageConsumer { |
| 37 | +public abstract class AbstractMonitor extends JFrame implements ActionListener { |
19 | 38 |
|
20 | 39 | protected final JLabel noLineEndingAlert; |
21 | | - protected JTextArea textArea; |
| 40 | + protected TextAreaFIFO textArea; |
22 | 41 | protected JScrollPane scrollPane; |
23 | 42 | protected JTextField textField; |
24 | 43 | protected JButton sendButton; |
25 | 44 | protected JCheckBox autoscrollBox; |
26 | 45 | protected JComboBox lineEndings; |
27 | 46 | protected JComboBox serialRates; |
28 | 47 |
|
| 48 | + private Timer updateTimer; |
| 49 | + private StringBuffer updateBuffer; |
| 50 | + |
29 | 51 | public AbstractMonitor(String title) { |
30 | 52 | super(title); |
31 | 53 |
|
@@ -59,7 +81,9 @@ public void actionPerformed(ActionEvent event) { |
59 | 81 | Font editorFont = Preferences.getFont("editor.font"); |
60 | 82 | Font font = new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize()); |
61 | 83 |
|
62 | | - textArea = new JTextArea(16, 40); |
| 84 | + textArea = new TextAreaFIFO(8000000); |
| 85 | + textArea.setRows(16); |
| 86 | + textArea.setColumns(40); |
63 | 87 | textArea.setEditable(false); |
64 | 88 | textArea.setFont(font); |
65 | 89 |
|
@@ -149,6 +173,10 @@ public void actionPerformed(ActionEvent event) { |
149 | 173 | } |
150 | 174 | } |
151 | 175 | } |
| 176 | + |
| 177 | + updateBuffer = new StringBuffer(1048576); |
| 178 | + updateTimer = new Timer(33, this); // redraw serial monitor at 30 Hz |
| 179 | + updateTimer.start(); |
152 | 180 | } |
153 | 181 |
|
154 | 182 | public void onSerialRateChange(ActionListener listener) { |
@@ -199,4 +227,28 @@ public String getAuthorizationKey() { |
199 | 227 | public abstract void open() throws Exception; |
200 | 228 |
|
201 | 229 | public abstract void close() throws Exception; |
| 230 | + |
| 231 | + public synchronized void addToUpdateBuffer(char buff[], int n) { |
| 232 | + updateBuffer.append(buff, 0, n); |
| 233 | + } |
| 234 | + |
| 235 | + private synchronized String consumeUpdateBuffer() { |
| 236 | + String s = updateBuffer.toString(); |
| 237 | + updateBuffer.setLength(0); |
| 238 | + return s; |
| 239 | + } |
| 240 | + |
| 241 | + public void actionPerformed(ActionEvent e) { |
| 242 | + final String s = consumeUpdateBuffer(); |
| 243 | + if (s.length() > 0) { |
| 244 | + //System.out.println("gui append " + s.length()); |
| 245 | + if (autoscrollBox.isSelected()) { |
| 246 | + textArea.appendTrim(s); |
| 247 | + textArea.setCaretPosition(textArea.getDocument().getLength()); |
| 248 | + } else { |
| 249 | + textArea.appendNoTrim(s); |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
202 | 254 | } |
0 commit comments