forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractMonitor.java
More file actions
216 lines (171 loc) · 5.26 KB
/
Copy pathAbstractMonitor.java
File metadata and controls
216 lines (171 loc) · 5.26 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
package processing.app;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.DiscoveryManager;
import processing.app.legacy.PApplet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
@SuppressWarnings("serial")
public abstract class AbstractMonitor extends JFrame implements ActionListener {
private boolean closed;
private StringBuffer updateBuffer;
private Timer updateTimer;
private Timer portExistsTimer;
private BoardPort boardPort;
protected String[] serialRateStrings = {"300", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "74880", "115200", "230400", "250000", "500000", "1000000", "2000000"};
public AbstractMonitor(BoardPort boardPort) {
super(boardPort.getLabel());
this.boardPort = boardPort;
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent event) {
try {
closed = true;
close();
} catch (Exception e) {
// ignore
}
}
});
// obvious, no?
KeyStroke wc = Editor.WINDOW_CLOSE_KEYSTROKE;
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(wc, "close");
getRootPane().getActionMap().put("close", (new AbstractAction() {
@Override
public void actionPerformed(ActionEvent event) {
try {
close();
} catch (Exception e) {
// ignore
}
setVisible(false);
}
}));
onCreateWindow(getContentPane());
this.setMinimumSize(new Dimension(getContentPane().getMinimumSize().width, this.getPreferredSize().height));
pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
String locationStr = PreferencesData.get("last.serial.location");
if (locationStr != null) {
int[] location = PApplet.parseInt(PApplet.split(locationStr, ','));
if (location[0] + location[2] <= screen.width && location[1] + location[3] <= screen.height) {
setPlacement(location);
}
}
updateBuffer = new StringBuffer(1048576);
updateTimer = new Timer(33, this); // redraw serial monitor at 30 Hz
updateTimer.start();
ActionListener portExists = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
if (Base.getDiscoveryManager().find(boardPort.getAddress()) == null) {
if (!closed) {
suspend();
}
} else {
if (closed && (Editor.isUploading() == false)) {
resume(boardPort);
}
}
} catch (Exception e) {}
}
};
portExistsTimer = new Timer(1000, portExists); // check if the port is still there every second
portExistsTimer.start();
closed = false;
}
protected abstract void onCreateWindow(Container mainPane);
public void enableWindow(boolean enable) {
onEnableWindow(enable);
}
protected abstract void onEnableWindow(boolean enable);
// Puts the window in suspend state, closing the serial port
// to allow other entity (the programmer) to use it
public void suspend() throws Exception {
enableWindow(false);
close();
}
public void dispose() {
super.dispose();
portExistsTimer.stop();
}
public void resume(BoardPort boardPort) throws Exception {
setBoardPort(boardPort);
// Enable the window
enableWindow(true);
// If the window is visible, try to open the serial port
if (!isVisible()) {
return;
}
open();
}
protected void setPlacement(int[] location) {
setBounds(location[0], location[1], location[2], location[3]);
}
protected int[] getPlacement() {
int[] location = new int[4];
// Get the dimensions of the Frame
Rectangle bounds = getBounds();
location[0] = bounds.x;
location[1] = bounds.y;
location[2] = bounds.width;
location[3] = bounds.height;
return location;
}
public abstract void message(final String s);
public boolean requiresAuthorization() {
return false;
}
public String getAuthorizationKey() {
return null;
}
public boolean isClosed() {
return closed;
}
public void open() throws Exception {
closed = false;
}
public void close() throws Exception {
closed = true;
}
public BoardPort getBoardPort() {
return boardPort;
}
public void setBoardPort(BoardPort boardPort) {
if (boardPort == null) {
return;
}
setTitle(boardPort.getLabel());
this.boardPort = boardPort;
}
public synchronized void addToUpdateBuffer(char buff[], int n) {
updateBuffer.append(buff, 0, n);
}
private synchronized String consumeUpdateBuffer() {
String s = updateBuffer.toString();
updateBuffer.setLength(0);
return s;
}
@Override
public void actionPerformed(ActionEvent e) {
String s = consumeUpdateBuffer();
if (s.isEmpty()) {
return;
} else {
message(s);
}
}
/**
* Read and apply new values from the preferences, either because
* the app is just starting up, or the user just finished messing
* with things in the Preferences window.
*/
public void applyPreferences() {
// Empty.
};
}