[cooja/serialsocket] SerialSocketClient: New visual appearance with

input for server hostname and address including connect button
This commit is contained in:
Enrico Joerns 2014-04-12 03:07:21 +02:00
parent 404fd82399
commit d90aec2376

View file

@ -31,21 +31,34 @@ package org.contikios.cooja.serialsocket;
*/
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.NumberFormatter;
import org.apache.log4j.Logger;
import org.jdom.Element;
@ -94,26 +107,55 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
/* GUI components */
if (Cooja.isVisualized()) {
Box northBox = Box.createHorizontalBox();
northBox.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
statusLabel = configureLabel(northBox, "", "");
Box mainBox = Box.createHorizontalBox();
mainBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
inLabel = configureLabel(mainBox, "socket -> mote:", "0 bytes");
outLabel = configureLabel(mainBox, "mote -> socket", "0 bytes");
setResizable(false);
setLayout(new BorderLayout());
getContentPane().add(BorderLayout.NORTH, northBox);
getContentPane().add(BorderLayout.CENTER, mainBox);
GridBagConstraints c = new GridBagConstraints();
JPanel serverSelectPanel = new JPanel(new GridBagLayout());
pack();
}
c.gridx = 0;
c.gridy = 0;
serverSelectPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
/* Mote serial port */
serialPort = (SerialPort) mote.getInterfaces().getLog();
if (serialPort == null) {
throw new RuntimeException("No mote serial port");
}
label = new JLabel("Host:");
c.gridx++;
serverSelectPanel.add(label, c);
final JTextField serverHostField = new JTextField("localhost");
serverHostField.setColumns(10);
c.gridx++;
c.weightx = 1.0;
serverSelectPanel.add(serverHostField, c);
label = new JLabel("Port:");
c.gridx++;
c.weightx = 0.0;
serverSelectPanel.add(label, c);
final JFormattedTextField serverPort = new JFormattedTextField(
new NumberFormatter(NumberFormat.getIntegerInstance()));
serverPort.setColumns(5);
serverPort.setText("1234");
c.gridx++;
serverSelectPanel.add(serverPort, c);
final JButton serverSelectButton = new JButton("Connect");
c.gridx++;
serverSelectPanel.add(serverSelectButton, c);
c.gridx = 0;
c.gridy++;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
serverSelectPanel.add(new JSeparator(JSeparator.HORIZONTAL), c);
add(BorderLayout.NORTH, serverSelectPanel);
serverSelectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
logger.info("Connecting: " + SERVER_HOST + ":" + SERVER_PORT);
socket = new Socket(SERVER_HOST, SERVER_PORT);
@ -121,10 +163,60 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
out = new DataOutputStream(socket.getOutputStream());
out.flush();
startSocketReadThread(in);
} catch (Exception e) {
} catch (Exception ex) {
throw (RuntimeException) new RuntimeException(
"Connection error: " + e.getMessage()).initCause(e);
"Connection error: " + ex.getMessage()).initCause(ex);
}
}
});
JPanel connectionInfoPanel = new JPanel(new GridBagLayout());
connectionInfoPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
c = new GridBagConstraints();
label = new JLabel("Status:");
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.EAST;
c.ipadx = 5;
connectionInfoPanel.add(label, c);
final JLabel socketStatusLabel = new JLabel("disconnected");
c.gridx++;
c.anchor = GridBagConstraints.WEST;
connectionInfoPanel.add(socketStatusLabel, c);
label = new JLabel("socket -> mote:");
c.gridx = 0;
c.gridy++;
c.anchor = GridBagConstraints.EAST;
connectionInfoPanel.add(label, c);
final JLabel socketToMoteLabel = new JLabel("0 bytes");
c.gridx++;
c.anchor = GridBagConstraints.WEST;
connectionInfoPanel.add(socketToMoteLabel, c);
label = new JLabel("mote -> socket:");
c.gridx = 0;
c.gridy++;
c.anchor = GridBagConstraints.EAST;
connectionInfoPanel.add(label, c);
final JLabel moteToSocketLabel = new JLabel("0 bytes");
c.gridx++;
c.anchor = GridBagConstraints.WEST;
connectionInfoPanel.add(moteToSocketLabel, c);
add(BorderLayout.CENTER, connectionInfoPanel);
/* Mote serial port */
serialPort = (SerialPort) mote.getInterfaces().getLog();
if (serialPort == null) {
throw new RuntimeException("No mote serial port");
}
/* Observe serial port for outgoing data */
serialPort.addSerialDataObserver(serialDataObserver = new Observer() {
@ -146,6 +238,7 @@ public class SerialSocketClient extends VisPlugin implements MotePlugin {
}
});
}
}
private void startSocketReadThread(final DataInputStream in) {
/* Forward data: virtual port -> mote */