Skip to content

Commit 6e576d8

Browse files
committed
Serial Monitor: added "show timestamp" checkbox
1 parent 7e2f798 commit 6e576d8

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

app/src/processing/app/AbstractTextMonitor.java

+38-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.awt.event.ActionListener;
1111
import java.awt.event.WindowAdapter;
1212
import java.awt.event.WindowEvent;
13+
import java.text.SimpleDateFormat;
14+
import java.util.Date;
1315

1416
import javax.swing.Box;
1517
import javax.swing.BoxLayout;
@@ -22,7 +24,9 @@
2224
import javax.swing.JTextField;
2325
import javax.swing.SwingUtilities;
2426
import javax.swing.border.EmptyBorder;
27+
import javax.swing.text.BadLocationException;
2528
import javax.swing.text.DefaultCaret;
29+
import javax.swing.text.Document;
2630

2731
import cc.arduino.packages.BoardPort;
2832

@@ -36,11 +40,15 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
3640
protected JButton sendButton;
3741
protected JButton clearButton;
3842
protected JCheckBox autoscrollBox;
43+
protected JCheckBox addTimeStampBox;
3944
protected JComboBox lineEndings;
4045
protected JComboBox serialRates;
4146

47+
private SimpleDateFormat logDateFormat;
48+
4249
public AbstractTextMonitor(BoardPort boardPort) {
4350
super(boardPort);
51+
logDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
4452
}
4553

4654
protected void onCreateWindow(Container mainPane) {
@@ -90,6 +98,7 @@ public void windowGainedFocus(WindowEvent e) {
9098
pane.setBorder(new EmptyBorder(4, 4, 4, 4));
9199

92100
autoscrollBox = new JCheckBox(tr("Autoscroll"), true);
101+
addTimeStampBox = new JCheckBox(tr("Show timestamp"), false);
93102

94103
noLineEndingAlert = new JLabel(I18n.format(tr("You've pressed {0} but nothing was sent. Should you select a line ending?"), tr("Send")));
95104
noLineEndingAlert.setToolTipText(noLineEndingAlert.getText());
@@ -108,6 +117,15 @@ public void actionPerformed(ActionEvent event) {
108117
if (PreferencesData.get("serial.line_ending") != null) {
109118
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
110119
}
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+
111129
lineEndings.setMaximumSize(lineEndings.getMinimumSize());
112130

113131
serialRates = new JComboBox();
@@ -118,6 +136,7 @@ public void actionPerformed(ActionEvent event) {
118136
serialRates.setMaximumSize(serialRates.getMinimumSize());
119137

120138
pane.add(autoscrollBox);
139+
pane.add(addTimeStampBox);
121140
pane.add(Box.createHorizontalGlue());
122141
pane.add(noLineEndingAlert);
123142
pane.add(Box.createRigidArea(new Dimension(8, 0)));
@@ -138,6 +157,7 @@ protected void onEnableWindow(boolean enable)
138157
textField.setEnabled(enable);
139158
sendButton.setEnabled(enable);
140159
autoscrollBox.setEnabled(enable);
160+
addTimeStampBox.setEnabled(enable);
141161
lineEndings.setEnabled(enable);
142162
serialRates.setEnabled(enable);
143163
}
@@ -158,7 +178,24 @@ public void onSerialRateChange(ActionListener listener) {
158178
public void message(final String s) {
159179
SwingUtilities.invokeLater(new Runnable() {
160180
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+
162199
if (autoscrollBox.isSelected()) {
163200
textArea.setCaretPosition(textArea.getDocument().getLength());
164201
}

0 commit comments

Comments
 (0)