Skip to content

Cid test #11887

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 16 commits into from
122 changes: 64 additions & 58 deletions app/src/processing/app/AbstractTextMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,29 @@
import static processing.app.I18n.tr;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseWheelListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultEditorKit;

import cc.arduino.packages.BoardPort;

Expand All @@ -46,24 +40,17 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
protected JButton clearButton;
protected JCheckBox autoscrollBox;
protected JCheckBox addTimeStampBox;
protected JComboBox<String> sendEncoding;
protected JComboBox<String> receiveEncoding;
protected JComboBox<String> lineEndings;
protected JComboBox<String> serialRates;

public AbstractTextMonitor(BoardPort boardPort) {
public AbstractTextMonitor(Base base, BoardPort boardPort) {
super(boardPort);
}

@Override
public synchronized void addMouseWheelListener(MouseWheelListener l) {
super.addMouseWheelListener(l);
textArea.addMouseWheelListener(l);
}

@Override
public synchronized void addKeyListener(KeyListener l) {
super.addKeyListener(l);
textArea.addKeyListener(l);
textField.addKeyListener(l);
// Add font size adjustment listeners. This has to be done here due to
// super(boardPort) invoking onCreateWindow(...) before we can store base.
base.addEditorFontResizeListeners(textArea);
}

@Override
Expand Down Expand Up @@ -97,23 +84,6 @@ public void windowGainedFocus(WindowEvent e) {
}
});

// 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"));
clearButton = new JButton(tr("Clear output"));

Expand All @@ -137,7 +107,45 @@ public void windowGainedFocus(WindowEvent e) {
minimumSize.setSize(minimumSize.getWidth() / 3, minimumSize.getHeight());
noLineEndingAlert.setMinimumSize(minimumSize);

lineEndings = new JComboBox<>(new String[]{tr("No line ending"), tr("Newline"), tr("Carriage return"), tr("Both NL & CR")});
String sendAs = tr("Send as") + " ";
sendEncoding = new JComboBox<>(new String[] {
sendAs + EncodingOption.SYSTEM_DEFAULT,
sendAs + EncodingOption.BYTES,
sendAs + EncodingOption.UTF_8,
sendAs + EncodingOption.UTF_16,
sendAs + EncodingOption.UTF_16BE,
sendAs + EncodingOption.UTF_16LE,
sendAs + EncodingOption.ISO_8859_1,
sendAs + EncodingOption.US_ASCII});
sendEncoding.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
PreferencesData.set("serial.send_encoding", sendEncoding.getItemAt(
sendEncoding.getSelectedIndex()).substring(sendAs.length()));
}
});
String sendEncodingStr = PreferencesData.get("serial.send_encoding");
if (sendEncodingStr != null) {
sendEncoding.setSelectedItem(sendAs + sendEncodingStr);
}
sendEncoding.setMaximumSize(sendEncoding.getMinimumSize());

String receiveAs = tr("Receive as") + " ";
receiveEncoding = new JComboBox<>(new String[] {
receiveAs + EncodingOption.SYSTEM_DEFAULT,
receiveAs + EncodingOption.BYTES,
receiveAs + EncodingOption.UTF_8,
receiveAs + EncodingOption.UTF_16,
receiveAs + EncodingOption.UTF_16BE,
receiveAs + EncodingOption.UTF_16LE,
receiveAs + EncodingOption.ISO_8859_1,
receiveAs + EncodingOption.US_ASCII});
String receiveEncodingStr = PreferencesData.get("serial.receive_encoding");
if (receiveEncodingStr != null) {
receiveEncoding.setSelectedItem(receiveAs + receiveEncodingStr);
}
receiveEncoding.setMaximumSize(receiveEncoding.getMinimumSize());

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());
Expand All @@ -147,7 +155,7 @@ public void windowGainedFocus(WindowEvent e) {

lineEndings.setMaximumSize(lineEndings.getMinimumSize());

serialRates = new JComboBox<>();
serialRates = new JComboBox<String>();
for (String rate : serialRateStrings) {
serialRates.addItem(rate + " " + tr("baud"));
}
Expand All @@ -159,6 +167,10 @@ public void windowGainedFocus(WindowEvent e) {
pane.add(Box.createHorizontalGlue());
pane.add(noLineEndingAlert);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(sendEncoding);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(receiveEncoding);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(lineEndings);
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(serialRates);
Expand All @@ -172,27 +184,17 @@ public void windowGainedFocus(WindowEvent e) {

@Override
protected void onEnableWindow(boolean enable) {
// never actually disable textArea, so people can
// still select & copy text, even when the port
// is closed or disconnected
textArea.setEnabled(true);
if (enable) {
// setting these to null for system default
// gives a wrong gray background on Windows
// so assume black text on white background
textArea.setForeground(Color.BLACK);
textArea.setBackground(Color.WHITE);
} else {
// In theory, UIManager.getDefaults() should
// give us the system's colors for disabled
// windows. But it doesn't seem to work. :(
textArea.setForeground(new Color(64, 64, 64));
textArea.setBackground(new Color(238, 238, 238));
}
textArea.invalidate();
textArea.setEnabled(enable);
clearButton.setEnabled(enable);
scrollPane.setEnabled(enable);
textField.setEnabled(enable);
sendButton.setEnabled(enable);
autoscrollBox.setEnabled(enable);
addTimeStampBox.setEnabled(enable);
sendEncoding.setEnabled(enable);
receiveEncoding.setEnabled(enable);
lineEndings.setEnabled(enable);
serialRates.setEnabled(enable);
}

public void onSendCommand(ActionListener listener) {
Expand All @@ -208,6 +210,10 @@ public void onSerialRateChange(ActionListener listener) {
serialRates.addActionListener(listener);
}

public void onReceiveEncodingChange(ActionListener listener) {
receiveEncoding.addActionListener(listener);
}

@Override
public void message(String msg) {
SwingUtilities.invokeLater(() -> updateTextArea(msg));
Expand All @@ -223,7 +229,7 @@ protected void updateTextArea(String msg) {
textArea.append(msg);
}
if (autoscrollBox.isSelected()) {
textArea.setCaretPosition(textArea.getDocument().getLength());
// dasdasdasd
}
}

Expand Down Expand Up @@ -262,4 +268,4 @@ private String addTimestamps(String text) {
}
return sb.toString();
}
}
}
77 changes: 77 additions & 0 deletions app/src/processing/app/EncodingOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package processing.app;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Represents the encoding option for decoding or encoding bytes that are
* read from or written to a stream.
*/
public enum EncodingOption {

/**
* The system default character set as returned by
* {@link Charset#defaultCharset()}.
*/
SYSTEM_DEFAULT(Charset.defaultCharset()),

/**
* Comma separated unsigned byte representation.
*/
BYTES(null),

UTF_8(StandardCharsets.UTF_8),
UTF_16(StandardCharsets.UTF_16),
UTF_16BE(StandardCharsets.UTF_16BE),
UTF_16LE(StandardCharsets.UTF_16LE),
ISO_8859_1(StandardCharsets.ISO_8859_1),
US_ASCII(StandardCharsets.US_ASCII);

private final Charset charset;

private EncodingOption(Charset charset) {
this.charset = charset;
}

public Charset getCharset() {
return this.charset;
}

@Override
public String toString() {
switch (this) {
case SYSTEM_DEFAULT:
case BYTES:
return this.name().replace('_', ' ').toLowerCase();
default:
return this.charset.name();
}
}

/**
* Gets the {@link EncodingOption} with the given name.
* The name match is case-insensitive and
* whitespaces/dashes are interpreted as '_'.
* @param name - The name of the {@link EncodingOption}.
* @return The matching {@link EncodingOption}
* or null when no such option exists.
*/
public static EncodingOption forName(String name) {
if (name == null) {
return null;
}
try {
return EncodingOption.valueOf(
name.replace(' ', '_').replace('-', '_').toUpperCase());
} catch (IllegalArgumentException e) {
return null;
}
// name = name.replace(' ', '_').replace('-', '_');
// for (EncodingOption option : EncodingOption.values()) {
// if (option.name().equalsIgnoreCase(name)) {
// return option;
// }
// }
// return null;
}
}
Loading