explicitly calling startPlugin and showPlugin methods, instead of relying on the plugin constructor to configure the plugin. if a simulation is loaded, startPlugin() is called after the simulation configuration is set

This commit is contained in:
fros4943 2009-12-14 13:29:35 +00:00
parent 32936c3df8
commit 9ddc25527d
3 changed files with 49 additions and 13 deletions

View file

@ -24,7 +24,7 @@
* (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: GUI.java,v 1.155 2009/11/25 20:47:18 fros4943 Exp $ * $Id: GUI.java,v 1.156 2009/12/14 13:29:35 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -1640,10 +1640,10 @@ public class GUI extends Observable {
* @param argMote Plugin mote argument * @param argMote Plugin mote argument
* @return Started plugin * @return Started plugin
*/ */
public Plugin tryStartPlugin(final Class<? extends Plugin> pluginClass, private Plugin tryStartPlugin(final Class<? extends Plugin> pluginClass,
final GUI argGUI, final Simulation argSimulation, final Mote argMote) { final GUI argGUI, final Simulation argSimulation, final Mote argMote, boolean activate) {
try { try {
return startPlugin(pluginClass, argGUI, argSimulation, argMote); return startPlugin(pluginClass, argGUI, argSimulation, argMote, activate);
} catch (PluginConstructionException ex) { } catch (PluginConstructionException ex) {
if (GUI.isVisualized()) { if (GUI.isVisualized()) {
GUI.showErrorDialog(GUI.getTopParentContainer(), "Error when starting plugin", ex, false); GUI.showErrorDialog(GUI.getTopParentContainer(), "Error when starting plugin", ex, false);
@ -1663,6 +1663,18 @@ public class GUI extends Observable {
return null; return null;
} }
public Plugin tryStartPlugin(final Class<? extends Plugin> pluginClass,
final GUI argGUI, final Simulation argSimulation, final Mote argMote) {
return tryStartPlugin(pluginClass, argGUI, argSimulation, argMote, true);
}
public Plugin startPlugin(final Class<? extends Plugin> pluginClass,
final GUI argGUI, final Simulation argSimulation, final Mote argMote)
throws PluginConstructionException
{
return startPlugin(pluginClass, argGUI, argSimulation, argMote, true);
}
/** /**
* Starts given plugin. If visualized, the plugin is also shown. * Starts given plugin. If visualized, the plugin is also shown.
* *
@ -1674,8 +1686,8 @@ public class GUI extends Observable {
* @return Started plugin * @return Started plugin
* @throws PluginConstructionException At errors * @throws PluginConstructionException At errors
*/ */
public Plugin startPlugin(final Class<? extends Plugin> pluginClass, private Plugin startPlugin(final Class<? extends Plugin> pluginClass,
final GUI argGUI, final Simulation argSimulation, final Mote argMote) final GUI argGUI, final Simulation argSimulation, final Mote argMote, boolean activate)
throws PluginConstructionException throws PluginConstructionException
{ {
@ -1743,12 +1755,16 @@ public class GUI extends Observable {
throw ex; throw ex;
} }
if (activate) {
plugin.startPlugin();
}
// Add to active plugins list // Add to active plugins list
startedPlugins.add(plugin); startedPlugins.add(plugin);
updateGUIComponentState(); updateGUIComponentState();
// Show plugin if visualizer type // Show plugin if visualizer type
if (plugin.getGUI() != null) { if (activate && plugin.getGUI() != null) {
myGUI.showPlugin(plugin); myGUI.showPlugin(plugin);
} }
@ -3515,7 +3531,7 @@ public class GUI extends Observable {
} }
/* Start plugin */ /* Start plugin */
final Plugin startedPlugin = tryStartPlugin(pluginClass, this, simulation, mote); final Plugin startedPlugin = tryStartPlugin(pluginClass, this, simulation, mote, false);
if (startedPlugin == null) { if (startedPlugin == null) {
continue; continue;
} }
@ -3527,6 +3543,9 @@ public class GUI extends Observable {
} }
} }
/* Activate plugin */
startedPlugin.startPlugin();
/* If Cooja not visualized, ignore window configuration */ /* If Cooja not visualized, ignore window configuration */
if (startedPlugin.getGUI() == null) { if (startedPlugin.getGUI() == null) {
continue; continue;
@ -3573,6 +3592,7 @@ public class GUI extends Observable {
} }
} catch (Exception e) { } } catch (Exception e) { }
showPlugin(startedPlugin);
return true; return true;
} }
}.invokeAndWait(); }.invokeAndWait();

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: Plugin.java,v 1.3 2008/12/16 15:07:14 fros4943 Exp $ * $Id: Plugin.java,v 1.4 2009/12/14 13:29:35 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -54,6 +54,16 @@ public interface Plugin {
*/ */
public JInternalFrame getGUI(); public JInternalFrame getGUI();
/**
* This method is called to activate a new plugin, after constructing it.
* If a simulation is loaded, this method is called after {@link #setConfigXML(Collection, boolean)}.
*
* @see #setConfigXML(Collection, boolean)
* @see #closePlugin()
*/
public void startPlugin();
/** /**
* This method is called when an opened plugin is about to close. * This method is called when an opened plugin is about to close.
* It should release any resources such as registered observers or * It should release any resources such as registered observers or

View file

@ -26,16 +26,17 @@
* 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: VisPlugin.java,v 1.10 2009/06/30 12:46:26 fros4943 Exp $ * $Id: VisPlugin.java,v 1.11 2009/12/14 13:29:35 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
import java.util.Collection; import java.util.Collection;
import javax.swing.JInternalFrame; import javax.swing.JInternalFrame;
import javax.swing.event.InternalFrameAdapter; import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent; import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.plugins.SimControl; import se.sics.cooja.plugins.SimControl;
@ -103,6 +104,11 @@ public abstract class VisPlugin extends JInternalFrame implements Plugin {
return tag; return tag;
} }
public class PluginRequiresVisualizationException extends RuntimeException { public void startPlugin() {
}
public void closePlugin() {
}
public static class PluginRequiresVisualizationException extends RuntimeException {
} }
} }