Skip to content

Add serial input text field to plotter #8929

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

Merged
merged 1 commit into from
Oct 29, 2019
Merged
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
112 changes: 112 additions & 0 deletions app/src/processing/app/SerialPlotter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultEditorKit;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

Expand All @@ -40,6 +44,11 @@ public class SerialPlotter extends AbstractMonitor {
private Serial serial;
private int serialRate, xCount;

private JLabel noLineEndingAlert;
private JTextField textField;
private JButton sendButton;
private JComboBox<String> lineEndings;

private ArrayList<Graph> graphs;
private final static int BUFFER_CAPACITY = 500;

Expand Down Expand Up @@ -254,10 +263,113 @@ protected void onCreateWindow(Container mainPane) {
pane.add(serialRates);

mainPane.add(pane, BorderLayout.SOUTH);

textField = new JTextField(40);
// textField is selected every time the window is focused
addWindowFocusListener(new WindowAdapter() {
@Override
public void windowGainedFocus(WindowEvent e) {
textField.requestFocusInWindow();
}
});

// Add cut/copy/paste contextual menu to the text input field.
JPopupMenu menu = new JPopupMenu();

Action cut = new DefaultEditorKit.CutAction();
cut.putValue(Action.NAME, tr("Cut"));
menu.add(cut);

Action copy = new DefaultEditorKit.CopyAction();
copy.putValue(Action.NAME, tr("Copy"));
menu.add(copy);

Action paste = new DefaultEditorKit.PasteAction();
paste.putValue(Action.NAME, tr("Paste"));
menu.add(paste);

textField.setComponentPopupMenu(menu);

sendButton = new JButton(tr("Send"));

JPanel lowerPane = new JPanel();
lowerPane.setLayout(new BoxLayout(lowerPane, BoxLayout.X_AXIS));
lowerPane.setBorder(new EmptyBorder(4, 4, 4, 4));

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());
noLineEndingAlert.setForeground(pane.getBackground());
Dimension minimumSize = new Dimension(noLineEndingAlert.getMinimumSize());
minimumSize.setSize(minimumSize.getWidth() / 3, minimumSize.getHeight());
noLineEndingAlert.setMinimumSize(minimumSize);


lineEndings = new JComboBox<String>(new String[]{tr("No line ending"), tr("Newline"), tr("Carriage return"), tr("Both NL & CR")});
lineEndings.addActionListener((ActionEvent event) -> {
PreferencesData.setInteger("serial.line_ending", lineEndings.getSelectedIndex());
noLineEndingAlert.setForeground(pane.getBackground());
});
lineEndings.setMaximumSize(lineEndings.getMinimumSize());

lowerPane.add(textField);
lowerPane.add(Box.createRigidArea(new Dimension(4, 0)));
lowerPane.add(sendButton);

pane.add(lowerPane);
pane.add(noLineEndingAlert);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(lineEndings);

applyPreferences();

onSendCommand((ActionEvent event) -> {
send(textField.getText());
textField.setText("");
});

}

private void send(String string) {
String s = string;
if (serial != null) {
switch (lineEndings.getSelectedIndex()) {
case 1:
s += "\n";
break;
case 2:
s += "\r";
break;
case 3:
s += "\r\n";
break;
default:
break;
}
if ("".equals(s) && lineEndings.getSelectedIndex() == 0 && !PreferencesData.has("runtime.line.ending.alert.notified")) {
noLineEndingAlert.setForeground(Color.RED);
PreferencesData.set("runtime.line.ending.alert.notified", "true");
}
serial.write(s);
}
}

public void onSendCommand(ActionListener listener) {
textField.addActionListener(listener);
sendButton.addActionListener(listener);
}

public void appyPreferences() {
// Apply line endings.
if (PreferencesData.get("serial.line_ending") != null) {
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
}
}

protected void onEnableWindow(boolean enable) {
serialRates.setEnabled(enable);
textField.setEnabled(enable);
sendButton.setEnabled(enable);
lineEndings.setEnabled(enable);
}

private void onSerialRateChange(ActionListener listener) {
Expand Down