updated plugin example to also save and restore configuration

This commit is contained in:
fros4943 2010-02-03 09:54:54 +00:00
parent c9c9d71831
commit 1e203e1b77
2 changed files with 92 additions and 31 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0"?>
<project name="Plugin example" default="compile" basedir=".">
<project name="COOJA Plugin example" default="compile" basedir=".">
<property name="src" location="java"/>
<property name="build" location="java"/>

View File

@ -26,73 +26,134 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: MyDummyPlugin.java,v 1.3 2009/05/26 14:34:43 fros4943 Exp $
* $Id: MyDummyPlugin.java,v 1.4 2010/02/03 09:54:54 fros4943 Exp $
*/
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import org.apache.log4j.Logger;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Observable;
import java.util.Observer;
import se.sics.cooja.*;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;
import org.jdom.Element;
import se.sics.cooja.ClassDescription;
import se.sics.cooja.GUI;
import se.sics.cooja.PluginType;
import se.sics.cooja.Simulation;
import se.sics.cooja.TimeEvent;
import se.sics.cooja.VisPlugin;
/**
* This is a simple example plugin.
* This is a simple example COOJA plugin.
* It is a simulation plugin (SIM_PLUGIN): it depends on a single simulation.
*
* @author Fredrik Österlind
*
* This plugin demonstrates user interaction, scheduling time events,
* and saving plugin configurations.
*
* This project must be loaded in COOJA before the plugin can be used:
* Menu>Settings>Manage project directories>Browse>..>OK
*
* @author Fredrik Osterlind
*/
@ClassDescription("Dummy Plugin") /* Description shown in menu */
@ClassDescription("Example Plugin") /* Description shown in menu */
@PluginType(PluginType.SIM_PLUGIN)
public class MyDummyPlugin extends VisPlugin {
private static final long serialVersionUID = 4368807123350830772L;
private static Logger logger = Logger.getLogger(MyDummyPlugin.class);
private Simulation mySimulation;
private Observer millisecondObserver;
private Simulation sim;
private Observer msObserver;
private JTextField textField;
/**
* Creates a new dummy plugin.
*
* @param simulationToVisualize Simulation to visualize
* @param simulation Simulation object
* @param gui GUI object
*/
public MyDummyPlugin(Simulation simulationToVisualize, GUI gui) {
public MyDummyPlugin(Simulation simulation, GUI gui) {
super("Example plugin title", gui);
mySimulation = simulationToVisualize;
this.sim = simulation;
// Create and add a button
JButton button = new JButton("dummy button");
/* Button */
JButton button = new JButton("button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
logger.info("Button clicked");
}
});
add(button);
add(BorderLayout.NORTH, button);
/* Register as millisecond observer */
mySimulation.addMillisecondObserver(millisecondObserver = new Observer() {
/* Text field */
textField = new JTextField("enter text to save");
add(BorderLayout.SOUTH, textField);
/* Register as millisecond observer: print time every millisecond */
simulation.addMillisecondObserver(msObserver = new Observer() {
public void update(Observable obs, Object obj) {
logger.info("Another millisecond passed - simulation time is now " + mySimulation.getSimulationTimeMillis() + " ms");
logger.info("Millisecond observer: simulation time is now: " + sim.getSimulationTimeMillis() + " ms");
}
});
/* Register self-repeating event */
repeatEvent.execute(mySimulation.getSimulationTime());
setSize(300,100); // Set an initial size of this plugin
/* Register self-repeating event in simulation thread */
simulation.invokeSimulationThread(new Runnable() {
public void run() {
/* This is called from the simulation thread; we can safely schedule events */
sim.scheduleEvent(repeatEvent, sim.getSimulationTime());
}
});
setSize(300,100);
}
private TimeEvent repeatEvent = new TimeEvent(0) {
public void execute(long t) {
logger.info("Event executed - simulation time is now " + mySimulation.getSimulationTimeMillis() + " ms");
logger.info("Repeat event: simulation time is now: " + sim.getSimulationTimeMillis() + " ms");
mySimulation.scheduleEvent(this, t+10*Simulation.MILLISECOND);
/* This is called from the simulation thread; we can safely schedule events */
sim.scheduleEvent(this, t + 10*Simulation.MILLISECOND);
}
};
public void closePlugin() {
/* Clean up plugin resources */
mySimulation.deleteMillisecondObserver(millisecondObserver);
logger.info("Deleting millisecond observer");
sim.deleteMillisecondObserver(msObserver);
logger.info("Unschedule repeat event");
repeatEvent.remove();
}
public Collection<Element> getConfigXML() {
ArrayList<Element> config = new ArrayList<Element>();
Element element;
/* Save text field */
element = new Element("textfield");
element.setText(textField.getText());
config.add(element);
return config;
}
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
for (Element element : configXML) {
if (element.getName().equals("textfield")) {
final String text = element.getText();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textField.setText(text);
}
});
}
}
return true;
}
}