1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-20 14:54:31 +01:00

Serial Monitor: added "show timestamp" checkbox

This commit is contained in:
Cristian Maglie 2017-04-10 16:17:34 +02:00 committed by Martino Facchin
parent 6023459dad
commit 8e0d668344

View File

@ -10,6 +10,8 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Box;
import javax.swing.BoxLayout;
@ -22,7 +24,9 @@ import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Document;
import cc.arduino.packages.BoardPort;
@ -36,11 +40,15 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
protected JButton sendButton;
protected JButton clearButton;
protected JCheckBox autoscrollBox;
protected JCheckBox addTimeStampBox;
protected JComboBox lineEndings;
protected JComboBox serialRates;
private SimpleDateFormat logDateFormat;
public AbstractTextMonitor(BoardPort boardPort) {
super(boardPort);
logDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
}
protected void onCreateWindow(Container mainPane) {
@ -90,6 +98,7 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
pane.setBorder(new EmptyBorder(4, 4, 4, 4));
autoscrollBox = new JCheckBox(tr("Autoscroll"), true);
addTimeStampBox = new JCheckBox(tr("Show timestamp"), false);
noLineEndingAlert = new JLabel(I18n.format(tr("You've pressed {0} but nothing was sent. Should you select a line ending?"), tr("Send")));
noLineEndingAlert.setToolTipText(noLineEndingAlert.getText());
@ -108,6 +117,15 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
if (PreferencesData.get("serial.line_ending") != null) {
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
}
if (PreferencesData.get("serial.show_timestamp") != null) {
addTimeStampBox.setSelected(PreferencesData.getBoolean("serial.show_timestamp"));
}
addTimeStampBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PreferencesData.setBoolean("serial.show_timestamp", addTimeStampBox.isSelected());
}
});
lineEndings.setMaximumSize(lineEndings.getMinimumSize());
serialRates = new JComboBox();
@ -118,6 +136,7 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
serialRates.setMaximumSize(serialRates.getMinimumSize());
pane.add(autoscrollBox);
pane.add(addTimeStampBox);
pane.add(Box.createHorizontalGlue());
pane.add(noLineEndingAlert);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
@ -138,6 +157,7 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
textField.setEnabled(enable);
sendButton.setEnabled(enable);
autoscrollBox.setEnabled(enable);
addTimeStampBox.setEnabled(enable);
lineEndings.setEnabled(enable);
serialRates.setEnabled(enable);
}
@ -158,7 +178,24 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
public void message(final String s) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(s);
if (addTimeStampBox.isSelected()) {
String[] lines = s.split("(?<=\\n)");
Document doc = textArea.getDocument();
for (String currentLine : lines) {
try {
if (doc.getLength() == 0 || ((int) doc.getText(doc.getLength() - 1, 1).charAt(0) == 10)) {
textArea.append(logDateFormat.format(new Date()) + " -> " + currentLine);
} else {
textArea.append(currentLine);
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
} else {
textArea.append(s);
}
if (autoscrollBox.isSelected()) {
textArea.setCaretPosition(textArea.getDocument().getLength());
}