10
10
import java .awt .event .ActionListener ;
11
11
import java .awt .event .WindowAdapter ;
12
12
import java .awt .event .WindowEvent ;
13
+ import java .text .SimpleDateFormat ;
14
+ import java .util .Date ;
13
15
14
16
import javax .swing .Box ;
15
17
import javax .swing .BoxLayout ;
22
24
import javax .swing .JTextField ;
23
25
import javax .swing .SwingUtilities ;
24
26
import javax .swing .border .EmptyBorder ;
27
+ import javax .swing .text .BadLocationException ;
25
28
import javax .swing .text .DefaultCaret ;
29
+ import javax .swing .text .Document ;
26
30
27
31
import cc .arduino .packages .BoardPort ;
28
32
@@ -36,11 +40,15 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
36
40
protected JButton sendButton ;
37
41
protected JButton clearButton ;
38
42
protected JCheckBox autoscrollBox ;
43
+ protected JCheckBox addTimeStampBox ;
39
44
protected JComboBox lineEndings ;
40
45
protected JComboBox serialRates ;
41
46
47
+ private SimpleDateFormat logDateFormat ;
48
+
42
49
public AbstractTextMonitor (BoardPort boardPort ) {
43
50
super (boardPort );
51
+ logDateFormat = new SimpleDateFormat ("HH:mm:ss.SSS" );
44
52
}
45
53
46
54
protected void onCreateWindow (Container mainPane ) {
@@ -90,6 +98,7 @@ public void windowGainedFocus(WindowEvent e) {
90
98
pane .setBorder (new EmptyBorder (4 , 4 , 4 , 4 ));
91
99
92
100
autoscrollBox = new JCheckBox (tr ("Autoscroll" ), true );
101
+ addTimeStampBox = new JCheckBox (tr ("Show timestamp" ), false );
93
102
94
103
noLineEndingAlert = new JLabel (I18n .format (tr ("You've pressed {0} but nothing was sent. Should you select a line ending?" ), tr ("Send" )));
95
104
noLineEndingAlert .setToolTipText (noLineEndingAlert .getText ());
@@ -108,6 +117,15 @@ public void actionPerformed(ActionEvent event) {
108
117
if (PreferencesData .get ("serial.line_ending" ) != null ) {
109
118
lineEndings .setSelectedIndex (PreferencesData .getInteger ("serial.line_ending" ));
110
119
}
120
+ if (PreferencesData .get ("serial.show_timestamp" ) != null ) {
121
+ addTimeStampBox .setSelected (PreferencesData .getBoolean ("serial.show_timestamp" ));
122
+ }
123
+ addTimeStampBox .addActionListener (new ActionListener () {
124
+ public void actionPerformed (ActionEvent e ) {
125
+ PreferencesData .setBoolean ("serial.show_timestamp" , addTimeStampBox .isSelected ());
126
+ }
127
+ });
128
+
111
129
lineEndings .setMaximumSize (lineEndings .getMinimumSize ());
112
130
113
131
serialRates = new JComboBox ();
@@ -118,6 +136,7 @@ public void actionPerformed(ActionEvent event) {
118
136
serialRates .setMaximumSize (serialRates .getMinimumSize ());
119
137
120
138
pane .add (autoscrollBox );
139
+ pane .add (addTimeStampBox );
121
140
pane .add (Box .createHorizontalGlue ());
122
141
pane .add (noLineEndingAlert );
123
142
pane .add (Box .createRigidArea (new Dimension (8 , 0 )));
@@ -138,6 +157,7 @@ protected void onEnableWindow(boolean enable)
138
157
textField .setEnabled (enable );
139
158
sendButton .setEnabled (enable );
140
159
autoscrollBox .setEnabled (enable );
160
+ addTimeStampBox .setEnabled (enable );
141
161
lineEndings .setEnabled (enable );
142
162
serialRates .setEnabled (enable );
143
163
}
@@ -158,7 +178,24 @@ public void onSerialRateChange(ActionListener listener) {
158
178
public void message (final String s ) {
159
179
SwingUtilities .invokeLater (new Runnable () {
160
180
public void run () {
161
- textArea .append (s );
181
+ if (addTimeStampBox .isSelected ()) {
182
+ String [] lines = s .split ("(?<=\\ n)" );
183
+ Document doc = textArea .getDocument ();
184
+ for (String currentLine : lines ) {
185
+ try {
186
+ if (doc .getLength () == 0 || ((int ) doc .getText (doc .getLength () - 1 , 1 ).charAt (0 ) == 10 )) {
187
+ textArea .append (logDateFormat .format (new Date ()) + " -> " + currentLine );
188
+ } else {
189
+ textArea .append (currentLine );
190
+ }
191
+ } catch (BadLocationException e ) {
192
+ e .printStackTrace ();
193
+ }
194
+ }
195
+ } else {
196
+ textArea .append (s );
197
+ }
198
+
162
199
if (autoscrollBox .isSelected ()) {
163
200
textArea .setCaretPosition (textArea .getDocument ().getLength ());
164
201
}
0 commit comments