minor fix: always show start of lines

This commit is contained in:
fros4943 2009-06-02 09:34:12 +00:00
parent 16a9656abd
commit 9ea673f3be

View file

@ -1,339 +1,346 @@
/* /*
* Copyright (c) 2007, Swedish Institute of Computer Science. * Copyright (c) 2007, Swedish Institute of Computer Science.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors * 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software * may be used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: SerialUI.java,v 1.1 2009/03/19 18:55:44 joxe Exp $ * $Id: SerialUI.java,v 1.2 2009/06/02 09:34:12 fros4943 Exp $
*/ */
package se.sics.cooja.dialogs; package se.sics.cooja.dialogs;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.awt.event.ActionEvent; import java.awt.Rectangle;
import java.awt.event.ActionListener; import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter; import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyAdapter;
import java.util.Collection; import java.awt.event.KeyEvent;
import java.util.Observable; import java.util.Collection;
import java.util.Observer; import java.util.Observable;
import java.util.Observer;
import org.jdom.Element;
import javax.swing.JButton; import org.jdom.Element;
import javax.swing.JCheckBox; import javax.swing.JButton;
import javax.swing.JLabel; import javax.swing.JCheckBox;
import javax.swing.JOptionPane; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JOptionPane;
import javax.swing.JScrollPane; import javax.swing.JPanel;
import javax.swing.JTextArea; import javax.swing.JScrollPane;
import javax.swing.JTextField; import javax.swing.JTextArea;
import org.apache.log4j.Logger; import javax.swing.JTextField;
import org.apache.log4j.Logger;
import se.sics.cooja.Mote;
import se.sics.cooja.interfaces.Log; import se.sics.cooja.Mote;
import se.sics.cooja.interfaces.SerialPort; import se.sics.cooja.interfaces.Log;
import se.sics.cooja.plugins.SLIP; import se.sics.cooja.interfaces.SerialPort;
import se.sics.cooja.plugins.SLIP;
public abstract class SerialUI extends Log implements SerialPort {
private static Logger logger = Logger.getLogger(SerialUI.class); public abstract class SerialUI extends Log implements SerialPort {
private static Logger logger = Logger.getLogger(SerialUI.class);
private String lastLogMessage = "";
private StringBuilder newMessage = new StringBuilder(); private String lastLogMessage = "";
private StringBuilder newMessage = new StringBuilder();
private JTextArea logTextPane = null;
private JTextField commandField; private JTextArea logTextPane = null;
private JCheckBox slipCheckbox; private JTextField commandField;
private String[] history = new String[50]; private JCheckBox slipCheckbox;
private int historyPos = 0; private String[] history = new String[50];
private int historyCount = 0; private int historyPos = 0;
private int historyCount = 0;
private class SerialDataObservable extends Observable {
private void notifyNewData() { private class SerialDataObservable extends Observable {
if (this.countObservers() == 0) { private void notifyNewData() {
return; if (this.countObservers() == 0) {
} return;
setChanged(); }
notifyObservers(SerialUI.this); setChanged();
} notifyObservers(SerialUI.this);
} }
}
private SerialDataObservable serialDataObservable = new SerialDataObservable();
private byte lastSerialData = 0; private SerialDataObservable serialDataObservable = new SerialDataObservable();
private byte lastSerialData = 0;
public String getLastLogMessage() {
return lastLogMessage; public String getLastLogMessage() {
} return lastLogMessage;
}
public abstract Mote getMote();
public abstract Mote getMote();
public void addSerialDataObserver(Observer o) {
serialDataObservable.addObserver(o); public void addSerialDataObserver(Observer o) {
} serialDataObservable.addObserver(o);
}
public void deleteSerialDataObserver(Observer o) {
serialDataObservable.deleteObserver(o); public void deleteSerialDataObserver(Observer o) {
} serialDataObservable.deleteObserver(o);
}
public byte getLastSerialData() {
return lastSerialData; public byte getLastSerialData() {
} return lastSerialData;
}
public JPanel getInterfaceVisualizer() {
JPanel panel = new JPanel(); public JPanel getInterfaceVisualizer() {
panel.setLayout(new BorderLayout()); JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
if (logTextPane == null) {
logTextPane = new JTextArea(); if (logTextPane == null) {
} logTextPane = new JTextArea();
}
// Send RS232 data visualizer
JPanel sendPane = new JPanel(new BorderLayout()); // Send RS232 data visualizer
commandField = new JTextField(15); JPanel sendPane = new JPanel(new BorderLayout());
JButton sendButton = new JButton("Send data"); commandField = new JTextField(15);
ActionListener action = new ActionListener() { JButton sendButton = new JButton("Send data");
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command = trim(commandField.getText()); public void actionPerformed(ActionEvent e) {
if (command != null) { String command = trim(commandField.getText());
try { if (command != null) {
int previous = historyCount - 1; try {
if (previous < 0) { int previous = historyCount - 1;
previous += history.length; if (previous < 0) {
} previous += history.length;
if (!command.equals(history[previous])) { }
history[historyCount] = command; if (!command.equals(history[previous])) {
historyCount = (historyCount + 1) % history.length; history[historyCount] = command;
} historyCount = (historyCount + 1) % history.length;
historyPos = historyCount; }
historyPos = historyCount;
if (slipCheckbox.isSelected()) {
addToLog("SLIP> " + command); if (slipCheckbox.isSelected()) {
command += "\n"; addToLog("SLIP> " + command);
writeArray(SLIP.asSlip(command.getBytes())); command += "\n";
} else { writeArray(SLIP.asSlip(command.getBytes()));
addToLog("> " + command); } else {
writeString(command); addToLog("> " + command);
} writeString(command);
commandField.setText(""); }
} catch (Exception ex) { commandField.setText("");
System.err.println("could not send '" + command + "':"); } catch (Exception ex) {
ex.printStackTrace(); System.err.println("could not send '" + command + "':");
JOptionPane.showMessageDialog(logTextPane, ex.printStackTrace();
"could not send '" + command + "':\n" JOptionPane.showMessageDialog(logTextPane,
+ ex, "ERROR", "could not send '" + command + "':\n"
JOptionPane.ERROR_MESSAGE); + ex, "ERROR",
} JOptionPane.ERROR_MESSAGE);
} else { }
commandField.getToolkit().beep(); } else {
} commandField.getToolkit().beep();
} }
}
};
commandField.addKeyListener(new KeyAdapter() { };
@Override commandField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) { @Override
switch (e.getKeyCode()) { public void keyPressed(KeyEvent e) {
case KeyEvent.VK_UP: { switch (e.getKeyCode()) {
int nextPos = (historyPos + history.length - 1) % history.length; case KeyEvent.VK_UP: {
if (nextPos == historyCount || history[nextPos] == null) { int nextPos = (historyPos + history.length - 1) % history.length;
commandField.getToolkit().beep(); if (nextPos == historyCount || history[nextPos] == null) {
} else { commandField.getToolkit().beep();
String cmd = trim(commandField.getText()); } else {
if (cmd != null) { String cmd = trim(commandField.getText());
history[historyPos] = cmd; if (cmd != null) {
} history[historyPos] = cmd;
historyPos = nextPos; }
commandField.setText(history[historyPos]); historyPos = nextPos;
} commandField.setText(history[historyPos]);
break; }
} break;
case KeyEvent.VK_DOWN: { }
int nextPos = (historyPos + 1) % history.length; case KeyEvent.VK_DOWN: {
if (nextPos == historyCount) { int nextPos = (historyPos + 1) % history.length;
historyPos = nextPos; if (nextPos == historyCount) {
commandField.setText(""); historyPos = nextPos;
} else if (historyPos == historyCount || history[nextPos] == null) { commandField.setText("");
commandField.getToolkit().beep(); } else if (historyPos == historyCount || history[nextPos] == null) {
} else { commandField.getToolkit().beep();
String cmd = trim(commandField.getText()); } else {
if (cmd != null) { String cmd = trim(commandField.getText());
history[historyPos] = cmd; if (cmd != null) {
} history[historyPos] = cmd;
historyPos = nextPos; }
commandField.setText(history[historyPos]); historyPos = nextPos;
} commandField.setText(history[historyPos]);
break; }
} break;
} }
} }
}
});
});
slipCheckbox = new JCheckBox("", false);
slipCheckbox.setToolTipText("Wrap data as SLIP"); slipCheckbox = new JCheckBox("", false);
slipCheckbox.setToolTipText("Wrap data as SLIP");
commandField.addActionListener(action);
sendButton.addActionListener(action); commandField.addActionListener(action);
sendPane.add(BorderLayout.WEST, slipCheckbox); sendButton.addActionListener(action);
sendPane.add(BorderLayout.CENTER, commandField); sendPane.add(BorderLayout.WEST, slipCheckbox);
sendPane.add(BorderLayout.EAST, sendButton); sendPane.add(BorderLayout.CENTER, commandField);
sendPane.add(BorderLayout.EAST, sendButton);
// Receive RS232 data visualizer
logTextPane.setOpaque(false); // Receive RS232 data visualizer
logTextPane.setEditable(false); logTextPane.setOpaque(false);
logTextPane.setEditable(false);
if (getLastLogMessage() == null) {
logTextPane.setText(""); if (getLastLogMessage() == null) {
} else { logTextPane.setText("");
logTextPane.append(getLastLogMessage()); } else {
} logTextPane.append(getLastLogMessage());
}
Observer observer;
this.addObserver(observer = new Observer() { Observer observer;
public void update(Observable obs, Object obj) { this.addObserver(observer = new Observer() {
final String logMessage = getLastLogMessage(); public void update(Observable obs, Object obj) {
EventQueue.invokeLater(new Runnable() { final String logMessage = getLastLogMessage();
public void run() { EventQueue.invokeLater(new Runnable() {
addToLog(logMessage); public void run() {
} addToLog(logMessage);
}); }
} });
}); }
});
// Saving observer reference for releaseInterfaceVisualizer
panel.putClientProperty("intf_obs", observer); // Saving observer reference for releaseInterfaceVisualizer
panel.putClientProperty("intf_obs", observer);
JScrollPane scrollPane = new JScrollPane(logTextPane);
scrollPane.setPreferredSize(new Dimension(100, 100)); JScrollPane scrollPane = new JScrollPane(logTextPane);
panel.add(BorderLayout.NORTH, new JLabel("Last serial data:")); scrollPane.setPreferredSize(new Dimension(100, 100));
panel.add(BorderLayout.CENTER, scrollPane); panel.add(BorderLayout.NORTH, new JLabel("Last serial data:"));
panel.add(BorderLayout.SOUTH, sendPane); panel.add(BorderLayout.CENTER, scrollPane);
return panel; panel.add(BorderLayout.SOUTH, sendPane);
} return panel;
}
protected void addToLog(String text) {
String current = logTextPane.getText(); protected void addToLog(String text) {
int len = current.length(); String current = logTextPane.getText();
if (len > 8192) { int len = current.length();
current = current.substring(len - 8192); if (len > 8192) {
} current = current.substring(len - 8192);
current = len > 0 ? (current + '\n' + text) : text; }
logTextPane.setText(current); current = len > 0 ? (current + '\n' + text) : text;
logTextPane.setCaretPosition(current.length()); logTextPane.setText(current);
} logTextPane.setCaretPosition(current.length());
public void releaseInterfaceVisualizer(JPanel panel) { Rectangle visRect = logTextPane.getVisibleRect();
Observer observer = (Observer) panel.getClientProperty("intf_obs"); if (visRect.x > 0) {
if (observer == null) { visRect.x = 0;
logger.fatal("Error when releasing panel, observer is null"); logTextPane.scrollRectToVisible(visRect);
return; }
} }
this.deleteObserver(observer); public void releaseInterfaceVisualizer(JPanel panel) {
} Observer observer = (Observer) panel.getClientProperty("intf_obs");
if (observer == null) {
public double energyConsumption() { logger.fatal("Error when releasing panel, observer is null");
return 0; return;
} }
public Collection<Element> getConfigXML() { this.deleteObserver(observer);
return null; }
}
public double energyConsumption() {
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) { return 0;
} }
private int tosChars = 0; public Collection<Element> getConfigXML() {
boolean tosMode = false; return null;
private int[] tosData = new int[255]; }
private int tosPos = 0;
private int tosLen = 0; public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
}
public void dataReceived(int data) {
if (tosMode) { private int tosChars = 0;
/* needs to add checks to CRC */ boolean tosMode = false;
// System.out.println("Received: " + Integer.toString(data, 16) + " = " + (char) data + " tosPos: " + tosPos); private int[] tosData = new int[255];
if (data == 0x7e) { private int tosPos = 0;
if (tosPos > 6) { private int tosLen = 0;
lastLogMessage = "TinyOS: " + newMessage.toString();
newMessage.setLength(0); public void dataReceived(int data) {
this.setChanged(); if (tosMode) {
this.notifyObservers(getMote()); /* needs to add checks to CRC */
// System.out.println("*** Printing TOS String: " + lastLogMessage); // System.out.println("Received: " + Integer.toString(data, 16) + " = " + (char) data + " tosPos: " + tosPos);
tosPos = 0; if (data == 0x7e) {
tosLen = 0; if (tosPos > 6) {
} else { lastLogMessage = "TinyOS: " + newMessage.toString();
/* start of new message! */ newMessage.setLength(0);
tosPos = 0; this.setChanged();
tosLen = 0; this.notifyObservers(getMote());
} // System.out.println("*** Printing TOS String: " + lastLogMessage);
} tosPos = 0;
if (tosPos == 7) { tosLen = 0;
tosLen = data; } else {
// System.out.println("TOS Payload len: " + tosLen); /* start of new message! */
} tosPos = 0;
if (tosPos > 9 && tosPos < 10 + tosLen) { tosLen = 0;
if (data < 32) { }
data = 32; }
} if (tosPos == 7) {
newMessage.append((char) data); tosLen = data;
} // System.out.println("TOS Payload len: " + tosLen);
tosData[tosPos++] = data; }
} else { if (tosPos > 9 && tosPos < 10 + tosLen) {
if (data == 0x7e) { if (data < 32) {
tosChars++; data = 32;
if (tosChars == 2) { }
tosMode = true; newMessage.append((char) data);
/* already read one char here */ }
tosPos = 1; tosData[tosPos++] = data;
} } else {
} else { if (data == 0x7e) {
tosChars = 0; tosChars++;
} if (tosChars == 2) {
if (data == '\n') { tosMode = true;
lastLogMessage = newMessage.toString(); /* already read one char here */
newMessage.setLength(0); tosPos = 1;
this.setChanged(); }
this.notifyObservers(getMote()); } else {
} else { tosChars = 0;
newMessage.append((char) data); }
} if (data == '\n') {
lastSerialData = (byte) data; lastLogMessage = newMessage.toString();
serialDataObservable.notifyNewData(); newMessage.setLength(0);
} this.setChanged();
} this.notifyObservers(getMote());
} else {
public void close() { newMessage.append((char) data);
} }
lastSerialData = (byte) data;
public void flushInput() { serialDataObservable.notifyNewData();
} }
}
private String trim(String text) {
return (text != null) && ((text = text.trim()).length() > 0) ? text : null; public void close() {
} }
}
public void flushInput() {
}
private String trim(String text) {
return (text != null) && ((text = text.trim()).length() > 0) ? text : null;
}
}