added get/set config for event listener plugin

This commit is contained in:
fros4943 2008-01-08 12:12:15 +00:00
parent df9d43c62d
commit 58215907bb

View file

@ -24,15 +24,17 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: EventListener.java,v 1.4 2007/01/26 15:12:00 fros4943 Exp $ * $Id: EventListener.java,v 1.5 2008/01/08 12:12:15 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
import java.awt.Component;
import java.awt.event.*; import java.awt.event.*;
import java.util.*; import java.util.*;
import javax.swing.*; import javax.swing.*;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMoteType; import se.sics.cooja.contikimote.ContikiMoteType;
@ -61,6 +63,10 @@ public class EventListener extends VisPlugin {
private JButton actionButton = null; private JButton actionButton = null;
private JPanel interfacePanel = null;
private JPanel generalPanel = null;
protected abstract class EventObserver implements Observer { protected abstract class EventObserver implements Observer {
protected Observable myObservation = null; protected Observable myObservation = null;
@ -99,8 +105,9 @@ public class EventListener extends VisPlugin {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
final MoteInterface moteInterface = (MoteInterface) obs; final MoteInterface moteInterface = (MoteInterface) obs;
int moteID = -1; int moteID = -1;
if (myMote.getInterfaces().getMoteID() != null) if (myMote.getInterfaces().getMoteID() != null) {
moteID = myMote.getInterfaces().getMoteID().getMoteID(); moteID = myMote.getInterfaces().getMoteID().getMoteID();
}
myParent.actOnChange("'" + GUI.getDescriptionOf(moteInterface.getClass()) myParent.actOnChange("'" + GUI.getDescriptionOf(moteInterface.getClass())
+ "'" + " of mote '" + (moteID > 0 ? Integer.toString(moteID) : "?") + "'" + " of mote '" + (moteID > 0 ? Integer.toString(moteID) : "?")
@ -131,13 +138,11 @@ public class EventListener extends VisPlugin {
} }
/** /**
* Create a new simulation control panel.
*
* @param simulationToControl * @param simulationToControl
* Simulation to control * Simulation to control
*/ */
public EventListener(Simulation simulationToControl, GUI gui) { public EventListener(Simulation simulationToControl, GUI gui) {
super("Event Breaker", gui); super("Event Listener", gui);
mySimulation = simulationToControl; mySimulation = simulationToControl;
myPlugin = this; myPlugin = this;
@ -165,7 +170,8 @@ public class EventListener extends VisPlugin {
allMoteTypes.add(moteTypeClass); allMoteTypes.add(moteTypeClass);
} }
} }
JPanel interfacePanel = new JPanel();
interfacePanel = new JPanel();
interfacePanel.setLayout(new BoxLayout(interfacePanel, BoxLayout.Y_AXIS)); interfacePanel.setLayout(new BoxLayout(interfacePanel, BoxLayout.Y_AXIS));
interfacePanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); interfacePanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
for (Class<? extends MoteInterface> moteTypeClass : allMoteTypes) { for (Class<? extends MoteInterface> moteTypeClass : allMoteTypes) {
@ -182,7 +188,7 @@ public class EventListener extends VisPlugin {
} }
// Create general selectable list // Create general selectable list
JPanel generalPanel = new JPanel(); generalPanel = new JPanel();
generalPanel.setLayout(new BoxLayout(generalPanel, BoxLayout.Y_AXIS)); generalPanel.setLayout(new BoxLayout(generalPanel, BoxLayout.Y_AXIS));
generalPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); generalPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
@ -226,20 +232,19 @@ public class EventListener extends VisPlugin {
} }
private void actOnChange(final String message, final Action action) { private void actOnChange(final String message, final Action action) {
if (!mySimulation.isRunning()) if (!mySimulation.isRunning()) {
return; return;
}
mySimulation.stopSimulation(); mySimulation.stopSimulation();
// Update plugin in separate thread SwingUtilities.invokeLater(new Runnable() {
Thread t = new Thread(new Runnable() {
public void run() { public void run() {
messageLabel.setText(message); messageLabel.setText(message);
actionButton.setAction(action); actionButton.setAction(action);
actionButton.setVisible(action != null); actionButton.setVisible(action != null);
} }
}); });
t.start();
} }
private ActionListener interfaceCheckBoxListener = new ActionListener() { private ActionListener interfaceCheckBoxListener = new ActionListener() {
@ -300,4 +305,71 @@ public class EventListener extends VisPlugin {
} }
} }
public Collection<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>();
Element element;
/* Save general observers */
for (Component comp: generalPanel.getComponents()) {
if (comp instanceof JCheckBox) {
JCheckBox checkBox = (JCheckBox) comp;
if (checkBox.isSelected()) {
element = new Element("general");
element.setText(checkBox.getText());
config.add(element);
}
}
}
/* Save interface observers */
for (Component comp: interfacePanel.getComponents()) {
if (comp instanceof JCheckBox) {
JCheckBox checkBox = (JCheckBox) comp;
if (checkBox.isSelected()) {
element = new Element("interface");
element.setText(checkBox.getText());
config.add(element);
}
}
}
return config;
}
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
/* Load general observers */
for (Element element : configXML) {
if (element.getName().equals("general")) {
for (Component comp: generalPanel.getComponents()) {
if (comp instanceof JCheckBox) {
JCheckBox checkBox = (JCheckBox) comp;
if (checkBox.getText().equals(element.getText())) {
checkBox.setSelected(true);
}
}
}
}
/* Load interface observers */
else if (element.getName().equals("interface")) {
for (Component comp: interfacePanel.getComponents()) {
if (comp instanceof JCheckBox) {
JCheckBox checkBox = (JCheckBox) comp;
if (checkBox.getText().equals(element.getText())) {
checkBox.setSelected(true);
}
}
}
}
else {
return false;
}
}
return true;
}
} }