Skip to content

Commit

Permalink
Adding plotting functionality to the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
henningpohl committed Jun 1, 2015
1 parent e55d414 commit 0f5eaac
Show file tree
Hide file tree
Showing 10 changed files with 722 additions and 142 deletions.
139 changes: 12 additions & 127 deletions app/src/processing/app/AbstractMonitor.java
Expand Up @@ -3,6 +3,7 @@
import static processing.app.I18n._;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
Expand All @@ -15,15 +16,10 @@
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
Expand All @@ -37,19 +33,11 @@
@SuppressWarnings("serial")
public abstract class AbstractMonitor extends JFrame implements ActionListener {

protected final JLabel noLineEndingAlert;
protected TextAreaFIFO textArea;
protected JScrollPane scrollPane;
protected JTextField textField;
protected JButton sendButton;
protected JCheckBox autoscrollBox;
protected JComboBox lineEndings;
protected JComboBox serialRates;
private boolean monitorEnabled;
private boolean closed;

private Timer updateTimer;
private StringBuffer updateBuffer;
private Timer updateTimer;

private BoardPort boardPort;

Expand Down Expand Up @@ -82,84 +70,10 @@ public void actionPerformed(ActionEvent event) {
}
}));

getContentPane().setLayout(new BorderLayout());

Font consoleFont = Theme.getFont("console.font");
Font editorFont = PreferencesData.getFont("editor.font");
Font font = new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize());

textArea = new TextAreaFIFO(8000000);
textArea.setRows(16);
textArea.setColumns(40);
textArea.setEditable(false);
textArea.setFont(font);

// don't automatically update the caret. that way we can manually decide
// whether or not to do so based on the autoscroll checkbox.
((DefaultCaret) textArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

scrollPane = new JScrollPane(textArea);

getContentPane().add(scrollPane, BorderLayout.CENTER);

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

textField = new JTextField(40);
sendButton = new JButton(_("Send"));

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

getContentPane().add(upperPane, BorderLayout.NORTH);

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

autoscrollBox = new JCheckBox(_("Autoscroll"), true);

noLineEndingAlert = new JLabel(I18n.format(_("You've pressed {0} but nothing was sent. Should you select a line ending?"), _("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(new String[]{_("No line ending"), _("Newline"), _("Carriage return"), _("Both NL & CR")});
lineEndings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
PreferencesData.setInteger("serial.line_ending", lineEndings.getSelectedIndex());
noLineEndingAlert.setForeground(pane.getBackground());
}
});
if (PreferencesData.get("serial.line_ending") != null) {
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
}
lineEndings.setMaximumSize(lineEndings.getMinimumSize());

String[] serialRateStrings = {"300", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400", "250000"};

serialRates = new JComboBox();
for (String rate : serialRateStrings) {
serialRates.addItem(rate + " " + _("baud"));
}

serialRates.setMaximumSize(serialRates.getMinimumSize());

pane.add(autoscrollBox);
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);

this.setMinimumSize(new Dimension(pane.getMinimumSize().width, this.getPreferredSize().height));
onCreateWindow(getContentPane());

getContentPane().add(pane, BorderLayout.SOUTH);
this.setMinimumSize(new Dimension(getContentPane().getMinimumSize().width, this.getPreferredSize().height));

pack();

Expand All @@ -185,18 +99,15 @@ public void actionPerformed(ActionEvent event) {
monitorEnabled = true;
closed = false;
}

protected abstract void onCreateWindow(Container mainPane);

public void enableWindow(boolean enable) {
textArea.setEnabled(enable);
scrollPane.setEnabled(enable);
textField.setEnabled(enable);
sendButton.setEnabled(enable);
autoscrollBox.setEnabled(enable);
lineEndings.setEnabled(enable);
serialRates.setEnabled(enable);

onEnableWindow(enable);
monitorEnabled = enable;
}

protected abstract void onEnableWindow(boolean enable);

// Puts the window in suspend state, closing the serial port
// to allow other entity (the programmer) to use it
Expand All @@ -220,15 +131,6 @@ public void resume(BoardPort boardPort) throws Exception {
open();
}

public void onSerialRateChange(ActionListener listener) {
serialRates.addActionListener(listener);
}

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

protected void setPlacement(int[] location) {
setBounds(location[0], location[1], location[2], location[3]);
}
Expand All @@ -246,16 +148,7 @@ protected int[] getPlacement() {
return location;
}

public void message(final String s) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(s);
if (autoscrollBox.isSelected()) {
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
});
}
public abstract void message(final String s);

public boolean requiresAuthorization() {
return false;
Expand Down Expand Up @@ -295,21 +188,13 @@ private synchronized String consumeUpdateBuffer() {
updateBuffer.setLength(0);
return s;
}

public void actionPerformed(ActionEvent e) {
String s = consumeUpdateBuffer();

if (s.isEmpty()) {
return;
}

//System.out.println("gui append " + s.length());
if (autoscrollBox.isSelected()) {
textArea.appendTrim(s);
textArea.setCaretPosition(textArea.getDocument().getLength());
} else {
textArea.appendNoTrim(s);
message(s);
}
}

}
165 changes: 165 additions & 0 deletions app/src/processing/app/AbstractTextMonitor.java
@@ -0,0 +1,165 @@
package processing.app;

import static processing.app.I18n._;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;

import cc.arduino.packages.BoardPort;
import processing.app.debug.TextAreaFIFO;
import processing.app.legacy.PApplet;

@SuppressWarnings("serial")
public abstract class AbstractTextMonitor extends AbstractMonitor {

protected JLabel noLineEndingAlert;
protected TextAreaFIFO textArea;
protected JScrollPane scrollPane;
protected JTextField textField;
protected JButton sendButton;
protected JCheckBox autoscrollBox;
protected JComboBox lineEndings;
protected JComboBox serialRates;

public AbstractTextMonitor(BoardPort boardPort) {
super(boardPort);
}

protected void onCreateWindow(Container mainPane) {
Font consoleFont = Theme.getFont("console.font");
Font editorFont = PreferencesData.getFont("editor.font");
Font font = new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize());

mainPane.setLayout(new BorderLayout());

textArea = new TextAreaFIFO(8000000);
textArea.setRows(16);
textArea.setColumns(40);
textArea.setEditable(false);
textArea.setFont(font);

// don't automatically update the caret. that way we can manually decide
// whether or not to do so based on the autoscroll checkbox.
((DefaultCaret) textArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

scrollPane = new JScrollPane(textArea);

mainPane.add(scrollPane, BorderLayout.CENTER);

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

textField = new JTextField(40);
sendButton = new JButton(_("Send"));

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

mainPane.add(upperPane, BorderLayout.NORTH);

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

autoscrollBox = new JCheckBox(_("Autoscroll"), true);

noLineEndingAlert = new JLabel(I18n.format(_("You've pressed {0} but nothing was sent. Should you select a line ending?"), _("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(new String[]{_("No line ending"), _("Newline"), _("Carriage return"), _("Both NL & CR")});
lineEndings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
PreferencesData.setInteger("serial.line_ending", lineEndings.getSelectedIndex());
noLineEndingAlert.setForeground(pane.getBackground());
}
});
if (PreferencesData.get("serial.line_ending") != null) {
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
}
lineEndings.setMaximumSize(lineEndings.getMinimumSize());

String[] serialRateStrings = {
"300", "1200", "2400", "4800", "9600",
"19200", "38400", "57600", "115200", "230400", "250000"
};

serialRates = new JComboBox();
for (String rate : serialRateStrings) {
serialRates.addItem(rate + " " + _("baud"));
}

serialRates.setMaximumSize(serialRates.getMinimumSize());

pane.add(autoscrollBox);
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);

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

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

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

public void onSerialRateChange(ActionListener listener) {
serialRates.addActionListener(listener);
}

public void message(final String s) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(s);
if (autoscrollBox.isSelected()) {
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
});
}
}

0 comments on commit 0f5eaac

Please sign in to comment.