Skip to content

More useability for serial monitor / editor #5921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion app/src/processing/app/AbstractTextMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.Box;
import javax.swing.BoxLayout;
Expand All @@ -20,7 +22,9 @@
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;

Expand All @@ -32,12 +36,17 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
protected JScrollPane scrollPane;
protected JTextField textField;
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) {
Expand Down Expand Up @@ -67,6 +76,7 @@ protected void onCreateWindow(Container mainPane) {

textField = new JTextField(40);
sendButton = new JButton(tr("Send"));
clearButton = new JButton(tr("Clear output"));

upperPane.add(textField);
upperPane.add(Box.createRigidArea(new Dimension(4, 0)));
Expand All @@ -79,6 +89,7 @@ protected void onCreateWindow(Container mainPane) {
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());
Expand All @@ -97,6 +108,15 @@ public void actionPerformed(ActionEvent event) {
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();
Expand All @@ -107,23 +127,28 @@ public void actionPerformed(ActionEvent event) {
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)));
pane.add(lineEndings);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(serialRates);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(clearButton);

mainPane.add(pane, BorderLayout.SOUTH);
}

protected void onEnableWindow(boolean enable)
{
textArea.setEnabled(enable);
clearButton.setEnabled(enable);
scrollPane.setEnabled(enable);
textField.setEnabled(enable);
sendButton.setEnabled(enable);
autoscrollBox.setEnabled(enable);
addTimeStampBox.setEnabled(enable);
lineEndings.setEnabled(enable);
serialRates.setEnabled(enable);
}
Expand All @@ -132,6 +157,10 @@ public void onSendCommand(ActionListener listener) {
textField.addActionListener(listener);
sendButton.addActionListener(listener);
}

public void onClearCommand(ActionListener listener) {
clearButton.addActionListener(listener);
}

public void onSerialRateChange(ActionListener listener) {
serialRates.addActionListener(listener);
Expand All @@ -140,7 +169,25 @@ public void onSerialRateChange(ActionListener listener) {
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());
}
Expand Down
55 changes: 46 additions & 9 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import processing.app.helpers.Keys;
import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMapException;
import processing.app.helpers.StringReplacer;
import processing.app.legacy.PApplet;
import processing.app.syntax.PdeKeywords;
import processing.app.tools.MenuScroller;
Expand Down Expand Up @@ -197,6 +198,8 @@ public boolean test(SketchController sketch) {
Runnable exportHandler;
private Runnable exportAppHandler;
private Runnable timeoutUploadHandler;

private Map<String, Tool> internalToolCache = new HashMap<String, Tool>();

public Editor(Base ibase, File file, int[] storedLocation, int[] defaultLocation, Platform platform) throws Exception {
super("Arduino");
Expand Down Expand Up @@ -962,8 +965,7 @@ public void updateKeywords(PdeKeywords keywords) {

JMenuItem createToolMenuItem(String className) {
try {
Class<?> toolClass = Class.forName(className);
final Tool tool = (Tool) toolClass.newInstance();
final Tool tool = getOrCreateToolInstance(className);

JMenuItem item = new JMenuItem(tool.getMenuTitle());

Expand All @@ -982,6 +984,20 @@ public void actionPerformed(ActionEvent e) {
}
}

private Tool getOrCreateToolInstance(String className) {
Tool internalTool = internalToolCache.get(className);
if (internalTool == null) {
try {
Class<?> toolClass = Class.forName(className);
internalTool = (Tool) toolClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
return null;
}
internalToolCache.put(className, internalTool);
}
return internalTool;
}

private void addInternalTools(JMenu menu) {
JMenuItem item;
Expand Down Expand Up @@ -1344,8 +1360,8 @@ public void actionPerformed(ActionEvent e) {
menu.add(gotoLine);

menu.addSeparator();

JMenuItem commentItem = newJMenuItem(tr("Comment/Uncomment"), '/');
JMenuItem commentItem = newJMenuItem(tr("Comment/Uncomment"), PreferencesData.get("editor.keys.shortcut_comment", "/").charAt(0));
commentItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getCurrentTab().handleCommentUncomment();
Expand Down Expand Up @@ -1992,12 +2008,27 @@ private void updateTitle() {
return;
}
SketchFile current = getCurrentTab().getSketchFile();
if (current.isPrimary()) {
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(),
BaseNoGui.VERSION_NAME_LONG));

String customFormat = PreferencesData.get("editor.custom_title_format");
if (customFormat != null && !customFormat.trim().isEmpty()) {

Map<String, String> titleMap = new HashMap<String, String>();
titleMap.put("file", current.getFileName());
String path = sketch.getFolder().getAbsolutePath();
titleMap.put("folder", path);
titleMap.put("path", path);
titleMap.put("project", sketch.getName());
titleMap.put("version", BaseNoGui.VERSION_NAME_LONG);

setTitle(StringReplacer.replaceFromMapping(customFormat, titleMap));
} else {
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(),
current.getFileName(), BaseNoGui.VERSION_NAME_LONG));
if (current.isPrimary()) {
setTitle(I18n.format(tr("{0} | Arduino {1}"), sketch.getName(),
BaseNoGui.VERSION_NAME_LONG));
} else {
setTitle(I18n.format(tr("{0} - {1} | Arduino {2}"), sketch.getName(),
current.getFileName(), BaseNoGui.VERSION_NAME_LONG));
}
}
}

Expand Down Expand Up @@ -2039,6 +2070,12 @@ private boolean handleSave2() {
statusNotice(tr("Saving..."));
boolean saved = false;
try {

if (PreferencesData.getBoolean("editor.autoformat_currentfile_before_saving")) {
Tool formatTool = getOrCreateToolInstance("cc.arduino.packages.formatter.AStyle");
formatTool.run();
}

boolean wasReadOnly = sketchController.isReadOnly(BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath());
String previousMainFilePath = sketch.getMainFilePath();
saved = sketchController.save();
Expand Down
6 changes: 6 additions & 0 deletions app/src/processing/app/SerialMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});

onClearCommand(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});
}

private void send(String s) {
Expand Down
2 changes: 1 addition & 1 deletion arduino-core/src/processing/app/SketchFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public String getFileName() {
* others.
*/
public String getPrettyName() {
if (isExtension(Sketch.SKETCH_EXTENSIONS))
if (!PreferencesData.getBoolean("editor.show_always_extensions") && isExtension(Sketch.SKETCH_EXTENSIONS))
return getBaseName();
else
return getFileName();
Expand Down