added support for external script file, which currently requires manually editing the .csc

This commit is contained in:
fros4943 2010-02-12 09:28:28 +00:00
parent f1db103777
commit c56d04e2bb

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: ScriptRunner.java,v 1.26 2010/01/25 07:49:42 fros4943 Exp $ * $Id: ScriptRunner.java,v 1.27 2010/02/12 09:28:28 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
@ -46,10 +46,10 @@ import java.io.FileNotFoundException;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
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 java.util.Vector;
import javax.script.ScriptException; import javax.script.ScriptException;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
@ -80,6 +80,7 @@ import se.sics.cooja.util.StringUtils;
@ClassDescription("Contiki Test Editor") @ClassDescription("Contiki Test Editor")
@PluginType(PluginType.SIM_PLUGIN) @PluginType(PluginType.SIM_PLUGIN)
public class ScriptRunner extends VisPlugin { public class ScriptRunner extends VisPlugin {
private static final long serialVersionUID = 7614358340336799109L;
private static Logger logger = Logger.getLogger(ScriptRunner.class); private static Logger logger = Logger.getLogger(ScriptRunner.class);
final String[] EXAMPLE_SCRIPTS = new String[] { final String[] EXAMPLE_SCRIPTS = new String[] {
@ -550,12 +551,19 @@ public class ScriptRunner extends VisPlugin {
} }
public Collection<Element> getConfigXML() { public Collection<Element> getConfigXML() {
Vector<Element> config = new Vector<Element>(); ArrayList<Element> config = new ArrayList<Element>();
Element element; Element element;
element = new Element("script"); if (scriptFile != null) {
element.setText(scriptTextArea.getText()); element = new Element("scriptfile");
config.add(element); element.setText(simulation.getGUI().createPortablePath(scriptFile).getPath().replace('\\', '/'));
config.add(element);
StringUtils.saveToFile(scriptFile, scriptTextArea.getText());
} else {
element = new Element("script");
element.setText(scriptTextArea.getText());
config.add(element);
}
element = new Element("active"); element = new Element("active");
element.setText("" + (engine != null)); element.setText("" + (engine != null));
@ -568,6 +576,7 @@ public class ScriptRunner extends VisPlugin {
setScriptActive(false); setScriptActive(false);
} }
private File scriptFile = null;
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) { public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
for (Element element : configXML) { for (Element element : configXML) {
String name = element.getName(); String name = element.getName();
@ -575,6 +584,15 @@ public class ScriptRunner extends VisPlugin {
if (!element.getText().isEmpty()) { if (!element.getText().isEmpty()) {
updateScript(element.getText()); updateScript(element.getText());
} }
} else if ("scriptfile".equals(name)) {
File file = simulation.getGUI().restorePortablePath(new File(element.getText().trim()));
String script = StringUtils.loadFromFile(file);
if (script == null) {
logger.fatal("Failed to load script from: " + file.getAbsolutePath());
} else {
updateScript(script);
}
scriptFile = file;
} else if ("active".equals(name)) { } else if ("active".equals(name)) {
boolean active = Boolean.parseBoolean(element.getText()); boolean active = Boolean.parseBoolean(element.getText());
if (GUI.isVisualized()) { if (GUI.isVisualized()) {