3
3
import static processing .app .I18n ._ ;
4
4
5
5
import java .awt .BorderLayout ;
6
+ import java .awt .Container ;
6
7
import java .awt .Dimension ;
7
8
import java .awt .Font ;
8
9
import java .awt .Rectangle ;
15
16
import javax .swing .AbstractAction ;
16
17
import javax .swing .Box ;
17
18
import javax .swing .BoxLayout ;
18
- import javax .swing .JButton ;
19
- import javax .swing .JCheckBox ;
20
- import javax .swing .JComboBox ;
21
19
import javax .swing .JComponent ;
22
20
import javax .swing .JFrame ;
23
21
import javax .swing .JLabel ;
24
22
import javax .swing .JPanel ;
25
- import javax .swing .JScrollPane ;
26
- import javax .swing .JTextField ;
27
23
import javax .swing .KeyStroke ;
28
24
import javax .swing .SwingUtilities ;
29
25
import javax .swing .Timer ;
37
33
@ SuppressWarnings ("serial" )
38
34
public abstract class AbstractMonitor extends JFrame implements ActionListener {
39
35
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 ;
48
36
private boolean monitorEnabled ;
49
37
private boolean closed ;
50
38
51
- private Timer updateTimer ;
52
39
private StringBuffer updateBuffer ;
40
+ private Timer updateTimer ;
53
41
54
42
private BoardPort boardPort ;
55
43
@@ -82,84 +70,10 @@ public void actionPerformed(ActionEvent event) {
82
70
}
83
71
}));
84
72
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" , "74880" , "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 );
159
73
160
- this . setMinimumSize ( new Dimension ( pane . getMinimumSize (). width , this . getPreferredSize (). height ));
74
+ onCreateWindow ( getContentPane ( ));
161
75
162
- getContentPane ().add ( pane , BorderLayout . SOUTH );
76
+ this . setMinimumSize ( new Dimension ( getContentPane ().getMinimumSize (). width , this . getPreferredSize (). height ) );
163
77
164
78
pack ();
165
79
@@ -185,18 +99,15 @@ public void actionPerformed(ActionEvent event) {
185
99
monitorEnabled = true ;
186
100
closed = false ;
187
101
}
102
+
103
+ protected abstract void onCreateWindow (Container mainPane );
188
104
189
105
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 );
198
107
monitorEnabled = enable ;
199
108
}
109
+
110
+ protected abstract void onEnableWindow (boolean enable );
200
111
201
112
// Puts the window in suspend state, closing the serial port
202
113
// to allow other entity (the programmer) to use it
@@ -220,15 +131,6 @@ public void resume(BoardPort boardPort) throws Exception {
220
131
open ();
221
132
}
222
133
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
-
232
134
protected void setPlacement (int [] location ) {
233
135
setBounds (location [0 ], location [1 ], location [2 ], location [3 ]);
234
136
}
@@ -246,16 +148,7 @@ protected int[] getPlacement() {
246
148
return location ;
247
149
}
248
150
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 );
259
152
260
153
public boolean requiresAuthorization () {
261
154
return false ;
@@ -295,21 +188,13 @@ private synchronized String consumeUpdateBuffer() {
295
188
updateBuffer .setLength (0 );
296
189
return s ;
297
190
}
298
-
191
+
299
192
public void actionPerformed (ActionEvent e ) {
300
193
String s = consumeUpdateBuffer ();
301
-
302
194
if (s .isEmpty ()) {
303
195
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 ());
310
196
} else {
311
- textArea . appendNoTrim (s );
197
+ message (s );
312
198
}
313
199
}
314
-
315
200
}
0 commit comments