Skip to content

Commit 32f66ca

Browse files
committed
Adding plotting functionality to the editor
1 parent 0ae4f0b commit 32f66ca

10 files changed

+722
-142
lines changed

Diff for: app/src/processing/app/AbstractMonitor.java

+12-127
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static processing.app.I18n._;
44

55
import java.awt.BorderLayout;
6+
import java.awt.Container;
67
import java.awt.Dimension;
78
import java.awt.Font;
89
import java.awt.Rectangle;
@@ -15,15 +16,10 @@
1516
import javax.swing.AbstractAction;
1617
import javax.swing.Box;
1718
import javax.swing.BoxLayout;
18-
import javax.swing.JButton;
19-
import javax.swing.JCheckBox;
20-
import javax.swing.JComboBox;
2119
import javax.swing.JComponent;
2220
import javax.swing.JFrame;
2321
import javax.swing.JLabel;
2422
import javax.swing.JPanel;
25-
import javax.swing.JScrollPane;
26-
import javax.swing.JTextField;
2723
import javax.swing.KeyStroke;
2824
import javax.swing.SwingUtilities;
2925
import javax.swing.Timer;
@@ -37,19 +33,11 @@
3733
@SuppressWarnings("serial")
3834
public abstract class AbstractMonitor extends JFrame implements ActionListener {
3935

40-
protected final JLabel noLineEndingAlert;
41-
protected TextAreaFIFO textArea;
42-
protected JScrollPane scrollPane;
43-
protected JTextField textField;
44-
protected JButton sendButton;
45-
protected JCheckBox autoscrollBox;
46-
protected JComboBox lineEndings;
47-
protected JComboBox serialRates;
4836
private boolean monitorEnabled;
4937
private boolean closed;
5038

51-
private Timer updateTimer;
5239
private StringBuffer updateBuffer;
40+
private Timer updateTimer;
5341

5442
private BoardPort boardPort;
5543

@@ -82,84 +70,10 @@ public void actionPerformed(ActionEvent event) {
8270
}
8371
}));
8472

85-
getContentPane().setLayout(new BorderLayout());
86-
87-
Font consoleFont = Theme.getFont("console.font");
88-
Font editorFont = PreferencesData.getFont("editor.font");
89-
Font font = new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize());
90-
91-
textArea = new TextAreaFIFO(8000000);
92-
textArea.setRows(16);
93-
textArea.setColumns(40);
94-
textArea.setEditable(false);
95-
textArea.setFont(font);
96-
97-
// don't automatically update the caret. that way we can manually decide
98-
// whether or not to do so based on the autoscroll checkbox.
99-
((DefaultCaret) textArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
100-
101-
scrollPane = new JScrollPane(textArea);
102-
103-
getContentPane().add(scrollPane, BorderLayout.CENTER);
104-
105-
JPanel upperPane = new JPanel();
106-
upperPane.setLayout(new BoxLayout(upperPane, BoxLayout.X_AXIS));
107-
upperPane.setBorder(new EmptyBorder(4, 4, 4, 4));
108-
109-
textField = new JTextField(40);
110-
sendButton = new JButton(_("Send"));
111-
112-
upperPane.add(textField);
113-
upperPane.add(Box.createRigidArea(new Dimension(4, 0)));
114-
upperPane.add(sendButton);
115-
116-
getContentPane().add(upperPane, BorderLayout.NORTH);
117-
118-
final JPanel pane = new JPanel();
119-
pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
120-
pane.setBorder(new EmptyBorder(4, 4, 4, 4));
121-
122-
autoscrollBox = new JCheckBox(_("Autoscroll"), true);
123-
124-
noLineEndingAlert = new JLabel(I18n.format(_("You've pressed {0} but nothing was sent. Should you select a line ending?"), _("Send")));
125-
noLineEndingAlert.setToolTipText(noLineEndingAlert.getText());
126-
noLineEndingAlert.setForeground(pane.getBackground());
127-
Dimension minimumSize = new Dimension(noLineEndingAlert.getMinimumSize());
128-
minimumSize.setSize(minimumSize.getWidth() / 3, minimumSize.getHeight());
129-
noLineEndingAlert.setMinimumSize(minimumSize);
130-
131-
lineEndings = new JComboBox(new String[]{_("No line ending"), _("Newline"), _("Carriage return"), _("Both NL & CR")});
132-
lineEndings.addActionListener(new ActionListener() {
133-
public void actionPerformed(ActionEvent event) {
134-
PreferencesData.setInteger("serial.line_ending", lineEndings.getSelectedIndex());
135-
noLineEndingAlert.setForeground(pane.getBackground());
136-
}
137-
});
138-
if (PreferencesData.get("serial.line_ending") != null) {
139-
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
140-
}
141-
lineEndings.setMaximumSize(lineEndings.getMinimumSize());
142-
143-
String[] serialRateStrings = {"300", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400", "250000"};
144-
145-
serialRates = new JComboBox();
146-
for (String rate : serialRateStrings) {
147-
serialRates.addItem(rate + " " + _("baud"));
148-
}
149-
150-
serialRates.setMaximumSize(serialRates.getMinimumSize());
151-
152-
pane.add(autoscrollBox);
153-
pane.add(Box.createHorizontalGlue());
154-
pane.add(noLineEndingAlert);
155-
pane.add(Box.createRigidArea(new Dimension(8, 0)));
156-
pane.add(lineEndings);
157-
pane.add(Box.createRigidArea(new Dimension(8, 0)));
158-
pane.add(serialRates);
15973

160-
this.setMinimumSize(new Dimension(pane.getMinimumSize().width, this.getPreferredSize().height));
74+
onCreateWindow(getContentPane());
16175

162-
getContentPane().add(pane, BorderLayout.SOUTH);
76+
this.setMinimumSize(new Dimension(getContentPane().getMinimumSize().width, this.getPreferredSize().height));
16377

16478
pack();
16579

@@ -185,18 +99,15 @@ public void actionPerformed(ActionEvent event) {
18599
monitorEnabled = true;
186100
closed = false;
187101
}
102+
103+
protected abstract void onCreateWindow(Container mainPane);
188104

189105
public void enableWindow(boolean enable) {
190-
textArea.setEnabled(enable);
191-
scrollPane.setEnabled(enable);
192-
textField.setEnabled(enable);
193-
sendButton.setEnabled(enable);
194-
autoscrollBox.setEnabled(enable);
195-
lineEndings.setEnabled(enable);
196-
serialRates.setEnabled(enable);
197-
106+
onEnableWindow(enable);
198107
monitorEnabled = enable;
199108
}
109+
110+
protected abstract void onEnableWindow(boolean enable);
200111

201112
// Puts the window in suspend state, closing the serial port
202113
// to allow other entity (the programmer) to use it
@@ -220,15 +131,6 @@ public void resume(BoardPort boardPort) throws Exception {
220131
open();
221132
}
222133

223-
public void onSerialRateChange(ActionListener listener) {
224-
serialRates.addActionListener(listener);
225-
}
226-
227-
public void onSendCommand(ActionListener listener) {
228-
textField.addActionListener(listener);
229-
sendButton.addActionListener(listener);
230-
}
231-
232134
protected void setPlacement(int[] location) {
233135
setBounds(location[0], location[1], location[2], location[3]);
234136
}
@@ -246,16 +148,7 @@ protected int[] getPlacement() {
246148
return location;
247149
}
248150

249-
public void message(final String s) {
250-
SwingUtilities.invokeLater(new Runnable() {
251-
public void run() {
252-
textArea.append(s);
253-
if (autoscrollBox.isSelected()) {
254-
textArea.setCaretPosition(textArea.getDocument().getLength());
255-
}
256-
}
257-
});
258-
}
151+
public abstract void message(final String s);
259152

260153
public boolean requiresAuthorization() {
261154
return false;
@@ -295,21 +188,13 @@ private synchronized String consumeUpdateBuffer() {
295188
updateBuffer.setLength(0);
296189
return s;
297190
}
298-
191+
299192
public void actionPerformed(ActionEvent e) {
300193
String s = consumeUpdateBuffer();
301-
302194
if (s.isEmpty()) {
303195
return;
304-
}
305-
306-
//System.out.println("gui append " + s.length());
307-
if (autoscrollBox.isSelected()) {
308-
textArea.appendTrim(s);
309-
textArea.setCaretPosition(textArea.getDocument().getLength());
310196
} else {
311-
textArea.appendNoTrim(s);
197+
message(s);
312198
}
313199
}
314-
315200
}

Diff for: app/src/processing/app/AbstractTextMonitor.java

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package processing.app;
2+
3+
import static processing.app.I18n._;
4+
5+
import java.awt.BorderLayout;
6+
import java.awt.Container;
7+
import java.awt.Dimension;
8+
import java.awt.Font;
9+
import java.awt.Rectangle;
10+
import java.awt.Toolkit;
11+
import java.awt.event.ActionEvent;
12+
import java.awt.event.ActionListener;
13+
import java.awt.event.WindowAdapter;
14+
import java.awt.event.WindowEvent;
15+
16+
import javax.swing.AbstractAction;
17+
import javax.swing.Box;
18+
import javax.swing.BoxLayout;
19+
import javax.swing.JButton;
20+
import javax.swing.JCheckBox;
21+
import javax.swing.JComboBox;
22+
import javax.swing.JComponent;
23+
import javax.swing.JFrame;
24+
import javax.swing.JLabel;
25+
import javax.swing.JPanel;
26+
import javax.swing.JScrollPane;
27+
import javax.swing.JTextField;
28+
import javax.swing.KeyStroke;
29+
import javax.swing.SwingUtilities;
30+
import javax.swing.border.EmptyBorder;
31+
import javax.swing.text.DefaultCaret;
32+
33+
import cc.arduino.packages.BoardPort;
34+
import processing.app.debug.TextAreaFIFO;
35+
import processing.app.legacy.PApplet;
36+
37+
@SuppressWarnings("serial")
38+
public abstract class AbstractTextMonitor extends AbstractMonitor {
39+
40+
protected JLabel noLineEndingAlert;
41+
protected TextAreaFIFO textArea;
42+
protected JScrollPane scrollPane;
43+
protected JTextField textField;
44+
protected JButton sendButton;
45+
protected JCheckBox autoscrollBox;
46+
protected JComboBox lineEndings;
47+
protected JComboBox serialRates;
48+
49+
public AbstractTextMonitor(BoardPort boardPort) {
50+
super(boardPort);
51+
}
52+
53+
protected void onCreateWindow(Container mainPane) {
54+
Font consoleFont = Theme.getFont("console.font");
55+
Font editorFont = PreferencesData.getFont("editor.font");
56+
Font font = new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize());
57+
58+
mainPane.setLayout(new BorderLayout());
59+
60+
textArea = new TextAreaFIFO(8000000);
61+
textArea.setRows(16);
62+
textArea.setColumns(40);
63+
textArea.setEditable(false);
64+
textArea.setFont(font);
65+
66+
// don't automatically update the caret. that way we can manually decide
67+
// whether or not to do so based on the autoscroll checkbox.
68+
((DefaultCaret) textArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
69+
70+
scrollPane = new JScrollPane(textArea);
71+
72+
mainPane.add(scrollPane, BorderLayout.CENTER);
73+
74+
JPanel upperPane = new JPanel();
75+
upperPane.setLayout(new BoxLayout(upperPane, BoxLayout.X_AXIS));
76+
upperPane.setBorder(new EmptyBorder(4, 4, 4, 4));
77+
78+
textField = new JTextField(40);
79+
sendButton = new JButton(_("Send"));
80+
81+
upperPane.add(textField);
82+
upperPane.add(Box.createRigidArea(new Dimension(4, 0)));
83+
upperPane.add(sendButton);
84+
85+
mainPane.add(upperPane, BorderLayout.NORTH);
86+
87+
final JPanel pane = new JPanel();
88+
pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
89+
pane.setBorder(new EmptyBorder(4, 4, 4, 4));
90+
91+
autoscrollBox = new JCheckBox(_("Autoscroll"), true);
92+
93+
noLineEndingAlert = new JLabel(I18n.format(_("You've pressed {0} but nothing was sent. Should you select a line ending?"), _("Send")));
94+
noLineEndingAlert.setToolTipText(noLineEndingAlert.getText());
95+
noLineEndingAlert.setForeground(pane.getBackground());
96+
Dimension minimumSize = new Dimension(noLineEndingAlert.getMinimumSize());
97+
minimumSize.setSize(minimumSize.getWidth() / 3, minimumSize.getHeight());
98+
noLineEndingAlert.setMinimumSize(minimumSize);
99+
100+
lineEndings = new JComboBox(new String[]{_("No line ending"), _("Newline"), _("Carriage return"), _("Both NL & CR")});
101+
lineEndings.addActionListener(new ActionListener() {
102+
public void actionPerformed(ActionEvent event) {
103+
PreferencesData.setInteger("serial.line_ending", lineEndings.getSelectedIndex());
104+
noLineEndingAlert.setForeground(pane.getBackground());
105+
}
106+
});
107+
if (PreferencesData.get("serial.line_ending") != null) {
108+
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
109+
}
110+
lineEndings.setMaximumSize(lineEndings.getMinimumSize());
111+
112+
String[] serialRateStrings = {
113+
"300", "1200", "2400", "4800", "9600",
114+
"19200", "38400", "57600", "115200", "230400", "250000"
115+
};
116+
117+
serialRates = new JComboBox();
118+
for (String rate : serialRateStrings) {
119+
serialRates.addItem(rate + " " + _("baud"));
120+
}
121+
122+
serialRates.setMaximumSize(serialRates.getMinimumSize());
123+
124+
pane.add(autoscrollBox);
125+
pane.add(Box.createHorizontalGlue());
126+
pane.add(noLineEndingAlert);
127+
pane.add(Box.createRigidArea(new Dimension(8, 0)));
128+
pane.add(lineEndings);
129+
pane.add(Box.createRigidArea(new Dimension(8, 0)));
130+
pane.add(serialRates);
131+
132+
mainPane.add(pane, BorderLayout.SOUTH);
133+
}
134+
135+
protected void onEnableWindow(boolean enable)
136+
{
137+
textArea.setEnabled(enable);
138+
scrollPane.setEnabled(enable);
139+
textField.setEnabled(enable);
140+
sendButton.setEnabled(enable);
141+
autoscrollBox.setEnabled(enable);
142+
lineEndings.setEnabled(enable);
143+
serialRates.setEnabled(enable);
144+
}
145+
146+
public void onSendCommand(ActionListener listener) {
147+
textField.addActionListener(listener);
148+
sendButton.addActionListener(listener);
149+
}
150+
151+
public void onSerialRateChange(ActionListener listener) {
152+
serialRates.addActionListener(listener);
153+
}
154+
155+
public void message(final String s) {
156+
SwingUtilities.invokeLater(new Runnable() {
157+
public void run() {
158+
textArea.append(s);
159+
if (autoscrollBox.isSelected()) {
160+
textArea.setCaretPosition(textArea.getDocument().getLength());
161+
}
162+
}
163+
});
164+
}
165+
}

0 commit comments

Comments
 (0)