catching unknown variable exceptions + using integer size

This commit is contained in:
fros4943 2008-02-11 14:03:19 +00:00
parent 47165456ca
commit afebaa9e97

View File

@ -26,7 +26,7 @@
* 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: VariableWatcher.java,v 1.5 2007/03/22 11:13:19 fros4943 Exp $ * $Id: VariableWatcher.java,v 1.6 2008/02/11 14:03:19 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -39,13 +39,13 @@ import java.util.Collection;
import java.util.Vector; import java.util.Vector;
import javax.swing.*; import javax.swing.*;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.AddressMemory.UnknownVariableException;
/** /**
* Variable Watcher enables a user to watch mote variables during a simulation. * Variable Watcher enables a user to watch mote variables during a simulation.
* Variables can be read or written either as bytes, integers (4 bytes) or byte arrays. * Variables can be read or written either as bytes, integers or byte arrays.
* *
* User can also see which variables seems to be available on the selected node. * User can also see which variables seems to be available on the selected node.
* *
* @author Fredrik Osterlind * @author Fredrik Osterlind
@ -63,7 +63,7 @@ public class VariableWatcher extends VisPlugin {
private final static int BYTE_INDEX = 0; private final static int BYTE_INDEX = 0;
private final static int INT_INDEX = 1; private final static int INT_INDEX = 1;
private final static int ARRAY_INDEX = 2; private final static int ARRAY_INDEX = 2;
private JPanel lengthPane; private JPanel lengthPane;
private JPanel valuePane; private JPanel valuePane;
private JComboBox varName; private JComboBox varName;
@ -102,9 +102,10 @@ public class VariableWatcher extends VisPlugin {
varName.setSelectedItem("[enter or pick name]"); varName.setSelectedItem("[enter or pick name]");
String[] allPotentialVarNames = moteMemory.getVariableNames(); String[] allPotentialVarNames = moteMemory.getVariableNames();
for (String aVarName: allPotentialVarNames) for (String aVarName: allPotentialVarNames) {
varName.addItem(aVarName); varName.addItem(aVarName);
}
varName.addKeyListener(new KeyListener() { varName.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
writeButton.setEnabled(false); writeButton.setEnabled(false);
@ -128,9 +129,9 @@ public class VariableWatcher extends VisPlugin {
varType = new JComboBox(); varType = new JComboBox();
varType.addItem("Byte (1 byte)"); // BYTE_INDEX = 0 varType.addItem("Byte (1 byte)"); // BYTE_INDEX = 0
varType.addItem("Integer (4 bytes)"); // INT_INDEX = 1 varType.addItem("Integer (" + moteMemory.getIntegerLength() + " bytes)"); // INT_INDEX = 1
varType.addItem("Byte array (x bytes)"); // ARRAY_INDEX = 2 varType.addItem("Byte array (x bytes)"); // ARRAY_INDEX = 2
varType.addActionListener(new ActionListener() { varType.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (varType.getSelectedIndex() == ARRAY_INDEX) { if (varType.getSelectedIndex() == ARRAY_INDEX) {
@ -143,7 +144,7 @@ public class VariableWatcher extends VisPlugin {
pack(); pack();
} }
}); });
smallPane.add(BorderLayout.EAST, varType); smallPane.add(BorderLayout.EAST, varType);
mainPane.add(smallPane); mainPane.add(smallPane);
@ -161,37 +162,37 @@ public class VariableWatcher extends VisPlugin {
setNumberOfValues(((Number) varLength.getValue()).intValue()); setNumberOfValues(((Number) varLength.getValue()).intValue());
} }
}); });
lengthPane.add(BorderLayout.EAST, varLength); lengthPane.add(BorderLayout.EAST, varLength);
mainPane.add(lengthPane); mainPane.add(lengthPane);
mainPane.add(Box.createRigidArea(new Dimension(0,25))); mainPane.add(Box.createRigidArea(new Dimension(0,25)));
lengthPane.setVisible(false); lengthPane.setVisible(false);
// Variable value label // Variable value label
label = new JLabel("Variable value"); label = new JLabel("Variable value");
label.setAlignmentX(JLabel.CENTER_ALIGNMENT); label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label.setPreferredSize(new Dimension(LABEL_WIDTH,LABEL_HEIGHT)); label.setPreferredSize(new Dimension(LABEL_WIDTH,LABEL_HEIGHT));
mainPane.add(label); mainPane.add(label);
// Variable value(s) // Variable value(s)
valuePane = new JPanel(); valuePane = new JPanel();
valuePane.setLayout(new BoxLayout(valuePane, BoxLayout.X_AXIS)); valuePane.setLayout(new BoxLayout(valuePane, BoxLayout.X_AXIS));
varValues = new JFormattedTextField[1]; varValues = new JFormattedTextField[1];
varValues[0] = new JFormattedTextField(integerFormat); varValues[0] = new JFormattedTextField(integerFormat);
varValues[0].setValue(new Integer(0)); varValues[0].setValue(new Integer(0));
varValues[0].setColumns(3); varValues[0].setColumns(3);
varValues[0].setText("?"); varValues[0].setText("?");
for (JFormattedTextField varValue: varValues) { for (JFormattedTextField varValue: varValues) {
valuePane.add(varValue); valuePane.add(varValue);
} }
mainPane.add(valuePane); mainPane.add(valuePane);
mainPane.add(Box.createRigidArea(new Dimension(0,25))); mainPane.add(Box.createRigidArea(new Dimension(0,25)));
// Read/write buttons // Read/write buttons
smallPane = new JPanel(new BorderLayout()); smallPane = new JPanel(new BorderLayout());
JButton button = new JButton("Read"); JButton button = new JButton("Read");
@ -203,7 +204,7 @@ public class VariableWatcher extends VisPlugin {
varValues[0].setValue(new Integer(val)); varValues[0].setValue(new Integer(val));
varName.setBackground(Color.WHITE); varName.setBackground(Color.WHITE);
writeButton.setEnabled(true); writeButton.setEnabled(true);
} catch (Exception ex) { } catch (UnknownVariableException ex) {
varName.setBackground(Color.RED); varName.setBackground(Color.RED);
writeButton.setEnabled(false); writeButton.setEnabled(false);
} }
@ -213,7 +214,7 @@ public class VariableWatcher extends VisPlugin {
varValues[0].setValue(new Integer(val)); varValues[0].setValue(new Integer(val));
varName.setBackground(Color.WHITE); varName.setBackground(Color.WHITE);
writeButton.setEnabled(true); writeButton.setEnabled(true);
} catch (Exception ex) { } catch (UnknownVariableException ex) {
varName.setBackground(Color.RED); varName.setBackground(Color.RED);
writeButton.setEnabled(false); writeButton.setEnabled(false);
} }
@ -221,11 +222,12 @@ public class VariableWatcher extends VisPlugin {
try { try {
int length = ((Number) varLength.getValue()).intValue(); int length = ((Number) varLength.getValue()).intValue();
byte[] vals = moteMemory.getByteArray((String) varName.getSelectedItem(), length); byte[] vals = moteMemory.getByteArray((String) varName.getSelectedItem(), length);
for (int i=0; i < length; i++) for (int i=0; i < length; i++) {
varValues[i].setValue(new Integer(vals[i])); varValues[i].setValue(new Integer(vals[i]));
}
varName.setBackground(Color.WHITE); varName.setBackground(Color.WHITE);
writeButton.setEnabled(true); writeButton.setEnabled(true);
} catch (Exception ex) { } catch (UnknownVariableException ex) {
varName.setBackground(Color.RED); varName.setBackground(Color.RED);
writeButton.setEnabled(false); writeButton.setEnabled(false);
} }
@ -233,7 +235,7 @@ public class VariableWatcher extends VisPlugin {
} }
}); });
smallPane.add(BorderLayout.WEST, button); smallPane.add(BorderLayout.WEST, button);
button = new JButton("Write"); button = new JButton("Write");
button.addActionListener(new ActionListener() { button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -242,7 +244,7 @@ public class VariableWatcher extends VisPlugin {
byte val = (byte) ((Number) varValues[0].getValue()).intValue(); byte val = (byte) ((Number) varValues[0].getValue()).intValue();
moteMemory.setByteValueOf((String) varName.getSelectedItem(), val); moteMemory.setByteValueOf((String) varName.getSelectedItem(), val);
varName.setBackground(Color.WHITE); varName.setBackground(Color.WHITE);
} catch (Exception ex) { } catch (UnknownVariableException ex) {
varName.setBackground(Color.RED); varName.setBackground(Color.RED);
} }
} else if (varType.getSelectedIndex() == INT_INDEX) { } else if (varType.getSelectedIndex() == INT_INDEX) {
@ -250,7 +252,7 @@ public class VariableWatcher extends VisPlugin {
int val = ((Number) varValues[0].getValue()).intValue(); int val = ((Number) varValues[0].getValue()).intValue();
moteMemory.setIntValueOf((String) varName.getSelectedItem(), val); moteMemory.setIntValueOf((String) varName.getSelectedItem(), val);
varName.setBackground(Color.WHITE); varName.setBackground(Color.WHITE);
} catch (Exception ex) { } catch (UnknownVariableException ex) {
varName.setBackground(Color.RED); varName.setBackground(Color.RED);
} }
} else if (varType.getSelectedIndex() == ARRAY_INDEX) { } else if (varType.getSelectedIndex() == ARRAY_INDEX) {
@ -260,11 +262,11 @@ public class VariableWatcher extends VisPlugin {
for (int i=0; i < length; i++) { for (int i=0; i < length; i++) {
vals[i] = (byte) ((Number) varValues[i].getValue()).intValue(); vals[i] = (byte) ((Number) varValues[i].getValue()).intValue();
} }
moteMemory.setByteArray((String) varName.getSelectedItem(), vals); moteMemory.setByteArray((String) varName.getSelectedItem(), vals);
varName.setBackground(Color.WHITE); varName.setBackground(Color.WHITE);
writeButton.setEnabled(true); writeButton.setEnabled(true);
} catch (Exception ex) { } catch (UnknownVariableException ex) {
varName.setBackground(Color.RED); varName.setBackground(Color.RED);
writeButton.setEnabled(false); writeButton.setEnabled(false);
} }
@ -275,7 +277,7 @@ public class VariableWatcher extends VisPlugin {
button.setEnabled(false); button.setEnabled(false);
writeButton = button; writeButton = button;
mainPane.add(smallPane); mainPane.add(smallPane);
mainPane.add(Box.createRigidArea(new Dimension(0,25))); mainPane.add(Box.createRigidArea(new Dimension(0,25)));
@ -294,7 +296,7 @@ public class VariableWatcher extends VisPlugin {
private void setNumberOfValues(int nr) { private void setNumberOfValues(int nr) {
valuePane.removeAll(); valuePane.removeAll();
if (nr > 0) { if (nr > 0) {
varValues = new JFormattedTextField[nr]; varValues = new JFormattedTextField[nr];
for (int i=0; i < nr; i++) { for (int i=0; i < nr; i++) {
@ -314,9 +316,9 @@ public class VariableWatcher extends VisPlugin {
public Collection<Element> getConfigXML() { public Collection<Element> getConfigXML() {
// Return currently watched variable and type // Return currently watched variable and type
Vector<Element> config = new Vector<Element>(); Vector<Element> config = new Vector<Element>();
Element element; Element element;
// Selected variable name // Selected variable name
element = new Element("varname"); element = new Element("varname");
element.setText((String) varName.getSelectedItem()); element.setText((String) varName.getSelectedItem());
@ -339,7 +341,7 @@ public class VariableWatcher extends VisPlugin {
element.setText(varLength.getValue().toString()); element.setText(varLength.getValue().toString());
config.add(element); config.add(element);
} }
return config; return config;
} }
@ -347,7 +349,7 @@ public class VariableWatcher extends VisPlugin {
lengthPane.setVisible(false); lengthPane.setVisible(false);
setNumberOfValues(1); setNumberOfValues(1);
varLength.setValue(1); varLength.setValue(1);
for (Element element : configXML) { for (Element element : configXML) {
if (element.getName().equals("varname")) { if (element.getName().equals("varname")) {
varName.setSelectedItem(element.getText()); varName.setSelectedItem(element.getText());
@ -366,8 +368,8 @@ public class VariableWatcher extends VisPlugin {
varLength.setValue(nrValues); varLength.setValue(nrValues);
} }
} }
return true; return true;
} }
} }