save command history with configs

This commit is contained in:
Fredrik Osterlind 2012-01-26 16:16:02 +01:00
parent 207fddddf0
commit 3a02e43e09
1 changed files with 84 additions and 52 deletions

View File

@ -1,33 +1,31 @@
/* /*
* 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.7 2010/10/07 13:09:28 joxe Exp $
*/
package se.sics.cooja.dialogs; package se.sics.cooja.dialogs;
@ -39,11 +37,12 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import org.jdom.Element;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
import javax.swing.JLabel; import javax.swing.JLabel;
@ -52,7 +51,9 @@ import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import javax.swing.JTextField; import javax.swing.JTextField;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.Mote; import se.sics.cooja.Mote;
import se.sics.cooja.interfaces.Log; import se.sics.cooja.interfaces.Log;
@ -81,14 +82,14 @@ public abstract class SerialUI extends Log implements SerialPort {
private int historyCount = 0; private int historyCount = 0;
private class SerialDataObservable extends Observable { private class SerialDataObservable extends Observable {
private void notifyNewData() { private void notifyNewData() {
if (this.countObservers() == 0) { if (this.countObservers() == 0) {
return; return;
}
setChanged();
notifyObservers(SerialUI.this);
} }
setChanged();
notifyObservers(SerialUI.this);
} }
}
private SerialDataObservable serialDataObservable = new SerialDataObservable(); private SerialDataObservable serialDataObservable = new SerialDataObservable();
private byte lastSerialData = 0; private byte lastSerialData = 0;
@ -214,7 +215,14 @@ public abstract class SerialUI extends Log implements SerialPort {
// Receive RS232 data visualizer // Receive RS232 data visualizer
logTextPane.setOpaque(false); logTextPane.setOpaque(false);
logTextPane.setEditable(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) { if (getLastLogMessage() == null) {
logTextPane.setText(""); logTextPane.setText("");
} else { } else {
@ -272,10 +280,34 @@ public abstract class SerialUI extends Log implements SerialPort {
} }
public Collection<Element> getConfigXML() { 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) { 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; private int tosChars = 0;
@ -288,7 +320,7 @@ public abstract class SerialUI extends Log implements SerialPort {
public void dataReceived(int data) { public void dataReceived(int data) {
if (data == SLIP_END || data == SLIP_ESC) { if (data == SLIP_END || data == SLIP_ESC) {
slipCounter++; slipCounter++;
} }
if (tosMode) { if (tosMode) {
int tmpData = data; int tmpData = data;
@ -326,12 +358,12 @@ public abstract class SerialUI extends Log implements SerialPort {
totalTOSChars++; totalTOSChars++;
if (tosChars == 2) { if (tosChars == 2) {
if (totalTOSChars > slipCounter) { if (totalTOSChars > slipCounter) {
tosMode = true; tosMode = true;
tosMode = false; /* XXX Disabled TOS mode due to error */ tosMode = false; /* XXX Disabled TOS mode due to error */
/* already read one char here */ /* already read one char here */
tosPos = 1; tosPos = 1;
} else { } else {
tosChars = 0; tosChars = 0;
} }
} }
} else { } else {
@ -347,7 +379,7 @@ public abstract class SerialUI extends Log implements SerialPort {
newMessage.append((char) data); newMessage.append((char) data);
if (newMessage.length() > MAX_LENGTH) { if (newMessage.length() > MAX_LENGTH) {
/*logger.warn("Dropping too large log message (>" + MAX_LENGTH + " bytes).");*/ /*logger.warn("Dropping too large log message (>" + MAX_LENGTH + " bytes).");*/
lastLogMessage = "# [1024 bytes binary data]"; lastLogMessage = "# [1024 bytes binary data]";
this.setChanged(); this.setChanged();
this.notifyObservers(getMote()); this.notifyObservers(getMote());
newMessage.setLength(0); newMessage.setLength(0);