From c7954c8a5bfc3412e7c5b26be5744b1e00a4dd39 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Tue, 22 Apr 2008 13:04:43 +0000 Subject: [PATCH] added functionality for observing GUI (added/removed simulations) --- tools/cooja/java/se/sics/cooja/GUI.java | 72 ++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/GUI.java b/tools/cooja/java/se/sics/cooja/GUI.java index ba3236670..9b0c8b644 100644 --- a/tools/cooja/java/se/sics/cooja/GUI.java +++ b/tools/cooja/java/se/sics/cooja/GUI.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: GUI.java,v 1.76 2008/04/03 13:59:37 fros4943 Exp $ + * $Id: GUI.java,v 1.77 2008/04/22 13:04:43 fros4943 Exp $ */ package se.sics.cooja; @@ -71,7 +71,7 @@ import se.sics.cooja.plugins.*; * * @author Fredrik Osterlind */ -public class GUI { +public class GUI extends Observable { /** * External tools default Win32 settings filename. @@ -501,7 +501,7 @@ public class GUI { menuItem = new JMenuItem("Reload simulation"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - reloadCurrentSimulation(); + reloadCurrentSimulation(false); } }); menu.add(menuItem); @@ -1998,6 +1998,9 @@ public class GUI { } } } + + setChanged(); + notifyObservers(); } /** @@ -2086,6 +2089,9 @@ public class GUI { if (isVisualizedInFrame()) { frame.setTitle("COOJA Simulator"); } + + setChanged(); + notifyObservers(); } } @@ -2252,7 +2258,7 @@ public class GUI { * Reloads current simulation. * This may include recompiling libraries and renaming mote type identifiers. */ - private void reloadCurrentSimulation() { + public void reloadCurrentSimulation(final boolean autoStart) { if (getSimulation() == null) { logger.fatal("No simulation to reload"); return; @@ -2280,6 +2286,9 @@ public class GUI { myGUI.doRemoveSimulation(false); Simulation newSim = loadSimulationConfig(root, true); myGUI.setSimulation(newSim); + if (autoStart) { + newSim.startSimulation(); + } } catch (UnsatisfiedLinkError e) { shouldRetry = showErrorDialog(frame, "Simulation reload error", e, true); @@ -3047,12 +3056,62 @@ public class GUI { } else if (args.length > 0 && args[0].startsWith("-nogui")) { - // No GUI start-up + /* Parse optional script argument */ + String tmpTest=null; + for (int i=1; i < args.length; i++) { + if (args[i].startsWith("-test=")) { + tmpTest = args[i].substring("-test=".length()); + } else { + logger.fatal("Unknown argument: " + args[i]); + System.exit(1); + } + + } + + final File scriptFile; + final File configFile; + final File logFile; + if (tmpTest != null) { + /* Locate script and simulation config files */ + scriptFile = new File(tmpTest + ".js"); + configFile = new File(tmpTest + ".csc"); + logFile = new File(tmpTest + ".log"); + if (!scriptFile.exists()) { + logger.fatal("Can't locate script: " + scriptFile); + System.exit(1); + } + if (!configFile.exists()) { + logger.fatal("Can't locate simulation config: " + configFile); + System.exit(1); + } + if (logFile.exists()) { + logFile.delete(); + } + if (logFile.exists() && !logFile.canWrite()) { + logger.fatal("Can't write to log file: " + logFile); + System.exit(1); + } + } else { + scriptFile = null; + configFile = null; + logFile = null; + } + + /* No GUI start-up */ javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { JDesktopPane desktop = new JDesktopPane(); desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); - new GUI(desktop); + GUI gui = new GUI(desktop); + + if (scriptFile != null && configFile != null) { + /* Load and start script plugin (no-GUI version) */ + gui.registerPlugin(ScriptRunnerNoGUI.class, false); + ScriptRunnerNoGUI scriptPlugin = (ScriptRunnerNoGUI) gui.startPlugin(ScriptRunnerNoGUI.class, gui, null, null); + + /* Activate test */ + scriptPlugin.activateTest(configFile, scriptFile, logFile); + } } }); @@ -3208,6 +3267,7 @@ public class GUI { if (((Element) element).getName().equals("simulation")) { Collection config = ((Element) element).getChildren(); newSim = new Simulation(this); + System.gc(); boolean createdOK = newSim.setConfigXML(config, !quick); if (!createdOK) { logger.info("Simulation not loaded");