save command history with configs

This commit is contained in:
Fredrik Osterlind 2012-01-26 16:16:02 +01:00
parent 207fddddf0
commit 3a02e43e09

View File

@ -1,33 +1,31 @@
/*
* Copyright (c) 2007, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 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
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* 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
* SUCH DAMAGE.
*
* $Id: SerialUI.java,v 1.7 2010/10/07 13:09:28 joxe Exp $
*/
/*
* Copyright (c) 2007, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 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
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* 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
* SUCH DAMAGE.
*/
package se.sics.cooja.dialogs;
@ -39,11 +37,12 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Observable;
import java.util.Observer;
import org.jdom.Element;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
@ -52,7 +51,9 @@ import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.Mote;
import se.sics.cooja.interfaces.Log;
@ -67,7 +68,7 @@ public abstract class SerialUI extends Log implements SerialPort {
private final static byte SLIP_ESC_END = (byte)0334;
private final static byte SLIP_ESC_ESC = (byte)0335;
private final static int MAX_LENGTH = 1024;
private String lastLogMessage = "";
@ -81,15 +82,15 @@ public abstract class SerialUI extends Log implements SerialPort {
private int historyCount = 0;
private class SerialDataObservable extends Observable {
private void notifyNewData() {
if (this.countObservers() == 0) {
return;
}
setChanged();
notifyObservers(SerialUI.this);
private void notifyNewData() {
if (this.countObservers() == 0) {
return;
}
setChanged();
notifyObservers(SerialUI.this);
}
}
private SerialDataObservable serialDataObservable = new SerialDataObservable();
private byte lastSerialData = 0;
@ -98,11 +99,11 @@ public abstract class SerialUI extends Log implements SerialPort {
}
public abstract Mote getMote();
public void addSerialDataObserver(Observer o) {
serialDataObservable.addObserver(o);
}
public void deleteSerialDataObserver(Observer o) {
serialDataObservable.deleteObserver(o);
}
@ -214,7 +215,14 @@ public abstract class SerialUI extends Log implements SerialPort {
// Receive RS232 data visualizer
logTextPane.setOpaque(false);
logTextPane.setEditable(false);
logTextPane.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if ((e.getModifiers() & (MouseEvent.SHIFT_MASK|MouseEvent.CTRL_MASK)) != 0) {
return;
}
commandField.requestFocusInWindow();
}
});
if (getLastLogMessage() == null) {
logTextPane.setText("");
} else {
@ -253,7 +261,7 @@ public abstract class SerialUI extends Log implements SerialPort {
current = len > 0 ? (current + '\n' + text) : text;
logTextPane.setText(current);
logTextPane.setCaretPosition(current.length());
Rectangle visRect = logTextPane.getVisibleRect();
if (visRect.x > 0) {
visRect.x = 0;
@ -272,10 +280,34 @@ public abstract class SerialUI extends Log implements SerialPort {
}
public Collection<Element> getConfigXML() {
return null;
StringBuilder sb = new StringBuilder();
for (String s: history) {
if (s == null) {
continue;
}
sb.append(s + "~;");
}
if (sb.length() == 0) {
return null;
}
ArrayList<Element> config = new ArrayList<Element>();
Element element = new Element("history");
element.setText(sb.toString());
config.add(element);
return config;
}
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
for (Element element : configXML) {
if (element.getName().equals("history")) {
String[] history = element.getText().split("~;");
System.arraycopy(history, 0, this.history, 0, history.length);
historyCount = history.length;
historyPos = historyCount;
}
}
}
private int tosChars = 0;
@ -285,10 +317,10 @@ public abstract class SerialUI extends Log implements SerialPort {
private int slipCounter = 0;
private int totalTOSChars = 0;
public void dataReceived(int data) {
if (data == SLIP_END || data == SLIP_ESC) {
slipCounter++;
slipCounter++;
}
if (tosMode) {
int tmpData = data;
@ -326,12 +358,12 @@ public abstract class SerialUI extends Log implements SerialPort {
totalTOSChars++;
if (tosChars == 2) {
if (totalTOSChars > slipCounter) {
tosMode = true;
tosMode = false; /* XXX Disabled TOS mode due to error */
/* already read one char here */
tosPos = 1;
tosMode = true;
tosMode = false; /* XXX Disabled TOS mode due to error */
/* already read one char here */
tosPos = 1;
} else {
tosChars = 0;
tosChars = 0;
}
}
} else {
@ -347,7 +379,7 @@ public abstract class SerialUI extends Log implements SerialPort {
newMessage.append((char) data);
if (newMessage.length() > MAX_LENGTH) {
/*logger.warn("Dropping too large log message (>" + MAX_LENGTH + " bytes).");*/
lastLogMessage = "# [1024 bytes binary data]";
lastLogMessage = "# [1024 bytes binary data]";
this.setChanged();
this.notifyObservers(getMote());
newMessage.setLength(0);