@@ -43,21 +43,18 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
43
43
protected JComboBox lineEndings ;
44
44
protected JComboBox serialRates ;
45
45
46
- private SimpleDateFormat logDateFormat ;
47
-
48
46
public AbstractTextMonitor (BoardPort boardPort ) {
49
47
super (boardPort );
50
- logDateFormat = new SimpleDateFormat ("HH:mm:ss.SSS -> " );
51
48
}
52
-
49
+
53
50
protected void onCreateWindow (Container mainPane ) {
54
51
Font consoleFont = Theme .getFont ("console.font" );
55
52
Font editorFont = PreferencesData .getFont ("editor.font" );
56
53
Font font = Theme .scale (new Font (consoleFont .getName (), consoleFont .getStyle (), editorFont .getSize ()));
57
54
58
55
mainPane .setLayout (new BorderLayout ());
59
56
60
- textArea = new TextAreaFIFO (8000000 );
57
+ textArea = new TextAreaFIFO (8_000_000 );
61
58
textArea .setRows (16 );
62
59
textArea .setColumns (40 );
63
60
textArea .setEditable (false );
@@ -70,7 +67,7 @@ protected void onCreateWindow(Container mainPane) {
70
67
scrollPane = new JScrollPane (textArea );
71
68
72
69
mainPane .add (scrollPane , BorderLayout .CENTER );
73
-
70
+
74
71
JPanel upperPane = new JPanel ();
75
72
upperPane .setLayout (new BoxLayout (upperPane , BoxLayout .X_AXIS ));
76
73
upperPane .setBorder (new EmptyBorder (4 , 4 , 4 , 4 ));
@@ -165,49 +162,65 @@ public void onSendCommand(ActionListener listener) {
165
162
textField .addActionListener (listener );
166
163
sendButton .addActionListener (listener );
167
164
}
168
-
165
+
169
166
public void onClearCommand (ActionListener listener ) {
170
167
clearButton .addActionListener (listener );
171
168
}
172
169
173
170
public void onSerialRateChange (ActionListener listener ) {
174
171
serialRates .addActionListener (listener );
175
172
}
176
-
177
- public void message (final String s ) {
178
- SwingUtilities .invokeLater (new Runnable () {
179
- // Pre-allocate all objects used for streaming data
180
- Date t = new Date ();
181
- String now ;
182
- StringBuilder out = new StringBuilder (16384 );
183
- boolean isStartingLine = false ;
184
-
185
- public void run () {
186
- if (addTimeStampBox .isSelected ()) {
187
- t .setTime (System .currentTimeMillis ());
188
- now = logDateFormat .format (t );
189
- out .setLength (0 );
190
-
191
- StringTokenizer tokenizer = new StringTokenizer (s , "\n " , true );
192
- while (tokenizer .hasMoreTokens ()) {
193
- if (isStartingLine ) {
194
- out .append (now );
195
- }
196
- String token = tokenizer .nextToken ();
197
- out .append (token );
198
- // tokenizer returns "\n" as a single token
199
- isStartingLine = token .charAt (0 ) == '\n' ;
200
- }
201
-
202
- textArea .append (out .toString ());
203
- } else {
204
- textArea .append (s );
205
- }
206
173
207
- if (autoscrollBox .isSelected ()) {
208
- textArea .setCaretPosition (textArea .getDocument ().getLength ());
174
+ public void message (final String msg ) {
175
+ SwingUtilities .invokeLater (new UpdateTextAreaAction (textArea ,
176
+ addTimeStampBox .isSelected (),
177
+ autoscrollBox .isSelected (),
178
+ msg ));
179
+ }
180
+
181
+ static class UpdateTextAreaAction implements Runnable {
182
+
183
+ private static final String LINE_SEPARATOR = "\n " ;
184
+
185
+ private String msg ;
186
+ private boolean addTimeStamp ;
187
+ private boolean doAutoscroll ;
188
+ private TextAreaFIFO textArea ;
189
+
190
+ UpdateTextAreaAction (TextAreaFIFO textArea , boolean addTimeStamp ,
191
+ boolean doAutoscroll , String msg ) {
192
+ this .msg = msg ;
193
+ this .textArea = textArea ;
194
+ this .addTimeStamp = addTimeStamp ;
195
+ this .doAutoscroll = doAutoscroll ;
196
+ }
197
+
198
+ public void run () {
199
+ if (addTimeStamp ) {
200
+ textArea .append (addTimestamps (msg ));
201
+ } else {
202
+ textArea .append (msg );
203
+ }
204
+ if (doAutoscroll ) {
205
+ textArea .setCaretPosition (textArea .getDocument ().getLength ());
206
+ }
207
+ }
208
+
209
+ private String addTimestamps (String text ) {
210
+ String now = new SimpleDateFormat ("HH:mm:ss.SSS -> " ).format (new Date ());
211
+ final StringBuilder sb = new StringBuilder (text .length () + now .length ());
212
+ boolean isStartingLine = true ;
213
+ StringTokenizer tokenizer = new StringTokenizer (text , LINE_SEPARATOR , true );
214
+ while (tokenizer .hasMoreTokens ()) {
215
+ if (isStartingLine ) {
216
+ sb .append (now );
209
217
}
218
+ String token = tokenizer .nextToken ();
219
+ sb .append (token );
220
+ // tokenizer returns "\n" as a single token
221
+ isStartingLine = token .equals (LINE_SEPARATOR );
210
222
}
211
- });
223
+ return sb .toString ();
224
+ }
212
225
}
213
226
}
0 commit comments