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 ;
36
32
@ SuppressWarnings ("serial" )
37
33
public abstract class AbstractMonitor extends JFrame implements ActionListener {
38
34
39
- protected final JLabel noLineEndingAlert ;
40
- protected TextAreaFIFO textArea ;
41
- protected JScrollPane scrollPane ;
42
- protected JTextField textField ;
43
- protected JButton sendButton ;
44
- protected JCheckBox autoscrollBox ;
45
- protected JComboBox lineEndings ;
46
- protected JComboBox serialRates ;
47
35
private boolean monitorEnabled ;
48
36
private boolean closed ;
49
37
50
- private Timer updateTimer ;
51
38
private StringBuffer updateBuffer ;
39
+ private Timer updateTimer ;
52
40
53
41
public AbstractMonitor (String title ) {
54
42
super (title );
@@ -78,87 +66,10 @@ public void actionPerformed(ActionEvent event) {
78
66
}
79
67
}));
80
68
81
- getContentPane ().setLayout (new BorderLayout ());
82
-
83
- Font consoleFont = Theme .getFont ("console.font" );
84
- Font editorFont = PreferencesData .getFont ("editor.font" );
85
- Font font = new Font (consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ());
86
-
87
- textArea = new TextAreaFIFO (8000000 );
88
- textArea .setRows (16 );
89
- textArea .setColumns (40 );
90
- textArea .setEditable (false );
91
- textArea .setFont (font );
92
-
93
- // don't automatically update the caret. that way we can manually decide
94
- // whether or not to do so based on the autoscroll checkbox.
95
- ((DefaultCaret ) textArea .getCaret ()).setUpdatePolicy (DefaultCaret .NEVER_UPDATE );
96
-
97
- scrollPane = new JScrollPane (textArea );
98
-
99
- getContentPane ().add (scrollPane , BorderLayout .CENTER );
100
-
101
- JPanel upperPane = new JPanel ();
102
- upperPane .setLayout (new BoxLayout (upperPane , BoxLayout .X_AXIS ));
103
- upperPane .setBorder (new EmptyBorder (4 , 4 , 4 , 4 ));
104
-
105
- textField = new JTextField (40 );
106
- sendButton = new JButton (_ ("Send" ));
107
-
108
- upperPane .add (textField );
109
- upperPane .add (Box .createRigidArea (new Dimension (4 , 0 )));
110
- upperPane .add (sendButton );
111
-
112
- getContentPane ().add (upperPane , BorderLayout .NORTH );
113
-
114
- final JPanel pane = new JPanel ();
115
- pane .setLayout (new BoxLayout (pane , BoxLayout .X_AXIS ));
116
- pane .setBorder (new EmptyBorder (4 , 4 , 4 , 4 ));
117
-
118
- autoscrollBox = new JCheckBox (_ ("Autoscroll" ), true );
119
-
120
- noLineEndingAlert = new JLabel (I18n .format (_ ("You've pressed {0} but nothing was sent. Should you select a line ending?" ), _ ("Send" )));
121
- noLineEndingAlert .setToolTipText (noLineEndingAlert .getText ());
122
- noLineEndingAlert .setForeground (pane .getBackground ());
123
- Dimension minimumSize = new Dimension (noLineEndingAlert .getMinimumSize ());
124
- minimumSize .setSize (minimumSize .getWidth () / 3 , minimumSize .getHeight ());
125
- noLineEndingAlert .setMinimumSize (minimumSize );
126
-
127
- lineEndings = new JComboBox (new String []{_ ("No line ending" ), _ ("Newline" ), _ ("Carriage return" ), _ ("Both NL & CR" )});
128
- lineEndings .addActionListener (new ActionListener () {
129
- public void actionPerformed (ActionEvent event ) {
130
- PreferencesData .setInteger ("serial.line_ending" , lineEndings .getSelectedIndex ());
131
- noLineEndingAlert .setForeground (pane .getBackground ());
132
- }
133
- });
134
- if (PreferencesData .get ("serial.line_ending" ) != null ) {
135
- lineEndings .setSelectedIndex (PreferencesData .getInteger ("serial.line_ending" ));
136
- }
137
- lineEndings .setMaximumSize (lineEndings .getMinimumSize ());
138
-
139
- String [] serialRateStrings = {
140
- "300" , "1200" , "2400" , "4800" , "9600" ,
141
- "19200" , "38400" , "57600" , "115200" , "230400" , "250000"
142
- };
143
-
144
- serialRates = new JComboBox ();
145
- for (String rate : serialRateStrings ) {
146
- serialRates .addItem (rate + " " + _ ("baud" ));
147
- }
148
-
149
- serialRates .setMaximumSize (serialRates .getMinimumSize ());
150
69
151
- pane .add (autoscrollBox );
152
- pane .add (Box .createHorizontalGlue ());
153
- pane .add (noLineEndingAlert );
154
- pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
155
- pane .add (lineEndings );
156
- pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
157
- pane .add (serialRates );
70
+ onCreateWindow (getContentPane ());
158
71
159
- this .setMinimumSize (new Dimension (pane .getMinimumSize ().width , this .getPreferredSize ().height ));
160
-
161
- getContentPane ().add (pane , BorderLayout .SOUTH );
72
+ this .setMinimumSize (new Dimension (getContentPane ().getMinimumSize ().width , this .getPreferredSize ().height ));
162
73
163
74
pack ();
164
75
@@ -184,19 +95,16 @@ public void actionPerformed(ActionEvent event) {
184
95
monitorEnabled = true ;
185
96
closed = false ;
186
97
}
98
+
99
+ protected abstract void onCreateWindow (Container mainPane );
187
100
188
101
public void enableWindow (boolean enable )
189
102
{
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
-
103
+ onEnableWindow (enable );
198
104
monitorEnabled = enable ;
199
105
}
106
+
107
+ protected abstract void onEnableWindow (boolean enable );
200
108
201
109
// Puts the window in suspend state, closing the serial port
202
110
// to allow other entity (the programmer) to use it
@@ -229,15 +137,6 @@ public void resume() throws SerialException
229
137
230
138
}
231
139
232
- public void onSerialRateChange (ActionListener listener ) {
233
- serialRates .addActionListener (listener );
234
- }
235
-
236
- public void onSendCommand (ActionListener listener ) {
237
- textField .addActionListener (listener );
238
- sendButton .addActionListener (listener );
239
- }
240
-
241
140
protected void setPlacement (int [] location ) {
242
141
setBounds (location [0 ], location [1 ], location [2 ], location [3 ]);
243
142
}
@@ -255,16 +154,7 @@ protected int[] getPlacement() {
255
154
return location ;
256
155
}
257
156
258
- public void message (final String s ) {
259
- SwingUtilities .invokeLater (new Runnable () {
260
- public void run () {
261
- textArea .append (s );
262
- if (autoscrollBox .isSelected ()) {
263
- textArea .setCaretPosition (textArea .getDocument ().getLength ());
264
- }
265
- }
266
- });
267
- }
157
+ public abstract void message (final String s );
268
158
269
159
public boolean requiresAuthorization () {
270
160
return false ;
@@ -291,18 +181,11 @@ private synchronized String consumeUpdateBuffer() {
291
181
updateBuffer .setLength (0 );
292
182
return s ;
293
183
}
294
-
184
+
295
185
public void actionPerformed (ActionEvent e ) {
296
186
final String s = consumeUpdateBuffer ();
297
187
if (s .length () > 0 ) {
298
- //System.out.println("gui append " + s.length());
299
- if (autoscrollBox .isSelected ()) {
300
- textArea .appendTrim (s );
301
- textArea .setCaretPosition (textArea .getDocument ().getLength ());
302
- } else {
303
- textArea .appendNoTrim (s );
304
- }
188
+ message (s );
305
189
}
306
190
}
307
-
308
191
}
0 commit comments