removed dependency on visplugin class, instead referencing plugin visualizers via new plugin method getGUI()

+ sanity-check when restoring last cooja location
This commit is contained in:
fros4943 2008-12-16 15:10:49 +00:00
parent 55e5f12503
commit 4d399119a4

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.95 2008/12/08 10:26:21 fros4943 Exp $ * $Id: GUI.java,v 1.96 2008/12/16 15:10:49 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -367,10 +367,10 @@ public class GUI extends Observable {
} }
// Start all standard GUI plugins // Start all standard GUI plugins
for (Class<? extends Plugin> visPluginClass : pluginClasses) { for (Class<? extends Plugin> pluginClass : pluginClasses) {
int pluginType = visPluginClass.getAnnotation(PluginType.class).value(); int pluginType = pluginClass.getAnnotation(PluginType.class).value();
if (pluginType == PluginType.COOJA_STANDARD_PLUGIN) { if (pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
startPlugin(visPluginClass, this, null, null); startPlugin(pluginClass, this, null, null);
} }
} }
} }
@ -871,8 +871,17 @@ public class GUI extends Observable {
frame.setLocation(device.getDefaultConfiguration().getBounds().getLocation()); frame.setLocation(device.getDefaultConfiguration().getBounds().getLocation());
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
} else if (frameWidth > 0 && frameHeight > 0) { } else if (frameWidth > 0 && frameHeight > 0) {
frame.setLocation(framePosX, framePosY);
frame.setSize(frameWidth, frameHeight); /* Sanity-check: will Cooja be visible on screen? */
boolean intersects =
device.getDefaultConfiguration().getBounds().intersects(
new Rectangle(framePosX, framePosY, frameWidth, frameHeight));
if (intersects) {
frame.setLocation(framePosX, framePosY);
frame.setSize(frameWidth, frameHeight);
}
} }
} }
@ -1339,12 +1348,15 @@ public class GUI extends Observable {
// Start plugins and try to place them wisely // Start plugins and try to place them wisely
logger.info("> Starting plugin and showing GUI"); logger.info("> Starting plugin and showing GUI");
VisPlugin plugin = (VisPlugin) gui.startPlugin(VisState.class, gui, simulation, null); Plugin plugin = gui.startPlugin(VisState.class, gui, simulation, null);
plugin.setLocation(350, 20); JInternalFrame pluginGUI = plugin.getGUI();
plugin = (VisPlugin) gui.startPlugin(VisTraffic.class, gui, simulation, null); pluginGUI.setLocation(350, 20);
plugin.setLocation(350, 340); plugin = gui.startPlugin(VisTraffic.class, gui, simulation, null);
plugin = (VisPlugin) gui.startPlugin(LogListener.class, gui, simulation, null); pluginGUI = plugin.getGUI();
plugin.setLocation(20, 420); pluginGUI.setLocation(350, 340);
plugin = gui.startPlugin(LogListener.class, gui, simulation, null);
pluginGUI = plugin.getGUI();
pluginGUI.setLocation(20, 420);
frame.setJMenuBar(gui.createMenuBar()); frame.setJMenuBar(gui.createMenuBar());
// Finally show GUI // Finally show GUI
@ -1694,41 +1706,42 @@ public class GUI extends Observable {
/** /**
* Show a started plugin in working area. * Show a started plugin in working area.
* *
* @param plugin * @param plugin Plugin
* Internal frame to add
*/ */
public void showPlugin(final VisPlugin plugin) { public void showPlugin(final Plugin plugin) {
new RunnableInEDT<Boolean>() { new RunnableInEDT<Boolean>() {
public Boolean work() { public Boolean work() {
int nrFrames = myDesktopPane.getAllFrames().length; JInternalFrame pluginFrame = plugin.getGUI();
myDesktopPane.add(plugin); if (pluginFrame == null) {
logger.fatal("Failed trying to show plugin without visualizer!");
// Set standard size if not specified by plugin itself return false;
if (plugin.getWidth() <= 0 || plugin.getHeight() <= 0) {
plugin.setSize(FRAME_STANDARD_WIDTH, FRAME_STANDARD_HEIGHT);
} }
// Set location if not already visible int nrFrames = myDesktopPane.getAllFrames().length;
if (plugin.getLocation().x <= 0 && plugin.getLocation().y <= 0) { myDesktopPane.add(pluginFrame);
plugin.setLocation(
/* Set size if not already specified by plugin */
if (pluginFrame.getWidth() <= 0 || pluginFrame.getHeight() <= 0) {
pluginFrame.setSize(FRAME_STANDARD_WIDTH, FRAME_STANDARD_HEIGHT);
}
/* Set location if not already visible */
if (pluginFrame.getLocation().x <= 0 && pluginFrame.getLocation().y <= 0) {
pluginFrame.setLocation(
nrFrames * FRAME_NEW_OFFSET, nrFrames * FRAME_NEW_OFFSET,
nrFrames * FRAME_NEW_OFFSET); nrFrames * FRAME_NEW_OFFSET);
} }
plugin.setVisible(true); pluginFrame.setVisible(true);
// Deselect all other plugins before selecting the new one /* Select plugin */
try { try {
for (JInternalFrame existingPlugin : myDesktopPane.getAllFrames()) { for (JInternalFrame existingPlugin : myDesktopPane.getAllFrames()) {
existingPlugin.setSelected(false); existingPlugin.setSelected(false);
} }
plugin.setSelected(true); pluginFrame.setSelected(true);
} catch (Exception e) { } catch (Exception e) { }
// Ignore myDesktopPane.moveToFront(pluginFrame);
}
// Mote plugin to front
myDesktopPane.moveToFront(plugin);
return true; return true;
} }
@ -1778,8 +1791,8 @@ public class GUI extends Observable {
startedPlugins.remove(plugin); startedPlugins.remove(plugin);
/* Dispose visualized components */ /* Dispose visualized components */
if (plugin instanceof VisPlugin) { if (plugin.getGUI() != null) {
((VisPlugin) plugin).dispose(); plugin.getGUI().dispose();
} }
/* (OPTIONAL) Remove simulation if all plugins are closed */ /* (OPTIONAL) Remove simulation if all plugins are closed */
@ -1814,79 +1827,61 @@ public class GUI extends Observable {
return null; return null;
} }
// Check that visualizer plugin is not started without GUI
if (!isVisualized()) {
try {
pluginClass.asSubclass(VisPlugin.class);
// Cast succeded, plugin is visualizer plugin!
/*logger.warn("Can't start visualizer plugin (no GUI): " + pluginClass);*/
return null;
} catch (ClassCastException e) {
}
}
// Construct plugin depending on plugin type // Construct plugin depending on plugin type
Plugin newPlugin = new RunnableInEDT<Plugin>() { int pluginType = pluginClass.getAnnotation(PluginType.class).value();
public Plugin work() { Plugin plugin = null;
int pluginType = pluginClass.getAnnotation(PluginType.class).value();
Plugin plugin = null;
try { try {
if (pluginType == PluginType.MOTE_PLUGIN) { if (pluginType == PluginType.MOTE_PLUGIN) {
if (mote == null) { if (mote == null) {
logger.fatal("Can't start mote plugin (no mote selected)"); logger.fatal("Can't start mote plugin (no mote selected)");
return null;
}
plugin = pluginClass.getConstructor(
new Class[] { Mote.class, Simulation.class, GUI.class })
.newInstance(mote, simulation, gui);
// Tag plugin with mote
plugin.tagWithObject(mote);
} else if (pluginType == PluginType.SIM_PLUGIN
|| pluginType == PluginType.SIM_STANDARD_PLUGIN) {
if (simulation == null) {
logger.fatal("Can't start simulation plugin (no simulation)");
return null;
}
plugin = pluginClass.getConstructor(
new Class[] { Simulation.class, GUI.class }).newInstance(
simulation, gui);
} else if (pluginType == PluginType.COOJA_PLUGIN
|| pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
if (gui == null) {
logger.fatal("Can't start COOJA plugin (no GUI)");
return null;
}
plugin = pluginClass.getConstructor(new Class[] { GUI.class }).newInstance(gui);
}
} catch (Exception e) {
logger.fatal("Exception thrown when starting plugin: " + e);
e.printStackTrace();
return null; return null;
} }
return plugin; plugin = pluginClass.getConstructor(
} new Class[] { Mote.class, Simulation.class, GUI.class })
}.invokeAndWait(); .newInstance(mote, simulation, gui);
if (newPlugin == null) { // Tag plugin with mote
plugin.tagWithObject(mote);
} else if (pluginType == PluginType.SIM_PLUGIN
|| pluginType == PluginType.SIM_STANDARD_PLUGIN) {
if (simulation == null) {
logger.fatal("Can't start simulation plugin (no simulation)");
return null;
}
plugin = pluginClass.getConstructor(
new Class[] { Simulation.class, GUI.class }).newInstance(
simulation, gui);
} else if (pluginType == PluginType.COOJA_PLUGIN
|| pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
if (gui == null) {
logger.fatal("Can't start COOJA plugin (no GUI)");
return null;
}
plugin = pluginClass.getConstructor(new Class[] { GUI.class }).newInstance(gui);
}
} catch (Exception e) {
logger.fatal("Exception thrown when starting plugin: " + e);
e.printStackTrace();
return null;
}
if (plugin == null) {
return null; return null;
} }
// Add to active plugins list // Add to active plugins list
startedPlugins.add(newPlugin); startedPlugins.add(plugin);
// Show plugin if visualizer type // Show plugin if visualizer type
if (newPlugin instanceof VisPlugin) { if (plugin.getGUI() != null) {
myGUI.showPlugin((VisPlugin) newPlugin); myGUI.showPlugin(plugin);
} }
return newPlugin; return plugin;
} }
/** /**
@ -2883,8 +2878,8 @@ public class GUI extends Observable {
} }
} }
} else if (e.getActionCommand().equals("start plugin")) { } else if (e.getActionCommand().equals("start plugin")) {
Class<? extends VisPlugin> pluginClass = (Class<? extends VisPlugin>) ((JMenuItem) e Class<Plugin> pluginClass =
.getSource()).getClientProperty("class"); (Class<Plugin>) ((JMenuItem) e.getSource()).getClientProperty("class");
Mote mote = (Mote) ((JMenuItem) e.getSource()).getClientProperty("mote"); Mote mote = (Mote) ((JMenuItem) e.getSource()).getClientProperty("mote");
startPlugin(pluginClass, myGUI, mySimulation, mote); startPlugin(pluginClass, myGUI, mySimulation, mote);
} else { } else {
@ -3522,7 +3517,7 @@ public class GUI extends Observable {
} }
// Create plugin specific configuration // Create plugin specific configuration
Collection pluginXML = startedPlugin.getConfigXML(); Collection<Element> pluginXML = startedPlugin.getConfigXML();
if (pluginXML != null) { if (pluginXML != null) {
pluginSubElement = new Element("plugin_config"); pluginSubElement = new Element("plugin_config");
pluginSubElement.addContent(pluginXML); pluginSubElement.addContent(pluginXML);
@ -3530,33 +3525,31 @@ public class GUI extends Observable {
} }
// If plugin is visualizer plugin, create visualization arguments // If plugin is visualizer plugin, create visualization arguments
if (startedPlugin instanceof VisPlugin) { if (startedPlugin.getGUI() != null) {
VisPlugin startedVisPlugin = (VisPlugin) startedPlugin; JInternalFrame pluginFrame = startedPlugin.getGUI();
pluginSubElement = new Element("width"); pluginSubElement = new Element("width");
pluginSubElement.setText("" + startedVisPlugin.getSize().width); pluginSubElement.setText("" + pluginFrame.getSize().width);
pluginElement.addContent(pluginSubElement); pluginElement.addContent(pluginSubElement);
pluginSubElement = new Element("z"); pluginSubElement = new Element("z");
pluginSubElement.setText("" pluginSubElement.setText("" + getDesktopPane().getComponentZOrder(pluginFrame));
+ getDesktopPane().getComponentZOrder(startedVisPlugin));
pluginElement.addContent(pluginSubElement); pluginElement.addContent(pluginSubElement);
pluginSubElement = new Element("height"); pluginSubElement = new Element("height");
pluginSubElement.setText("" + startedVisPlugin.getSize().height); pluginSubElement.setText("" + pluginFrame.getSize().height);
pluginElement.addContent(pluginSubElement); pluginElement.addContent(pluginSubElement);
pluginSubElement = new Element("location_x"); pluginSubElement = new Element("location_x");
pluginSubElement.setText("" + startedVisPlugin.getLocation().x); pluginSubElement.setText("" + pluginFrame.getLocation().x);
pluginElement.addContent(pluginSubElement); pluginElement.addContent(pluginSubElement);
pluginSubElement = new Element("location_y"); pluginSubElement = new Element("location_y");
pluginSubElement.setText("" + startedVisPlugin.getLocation().y); pluginSubElement.setText("" + pluginFrame.getLocation().y);
pluginElement.addContent(pluginSubElement); pluginElement.addContent(pluginSubElement);
pluginSubElement = new Element("minimized"); pluginSubElement = new Element("minimized");
pluginSubElement.setText(new Boolean(startedVisPlugin.isIcon()) pluginSubElement.setText(new Boolean(pluginFrame.isIcon()).toString());
.toString());
pluginElement.addContent(pluginSubElement); pluginElement.addContent(pluginSubElement);
} }
@ -3610,8 +3603,8 @@ public class GUI extends Observable {
// Read plugin class // Read plugin class
String pluginClassName = pluginElement.getText().trim(); String pluginClassName = pluginElement.getText().trim();
Class<? extends Plugin> pluginClass = tryLoadClass(this, Class<? extends Plugin> pluginClass =
Plugin.class, pluginClassName); tryLoadClass(this, Plugin.class, pluginClassName);
if (pluginClass == null) { if (pluginClass == null) {
logger.fatal("Could not load plugin class: " + pluginClassName); logger.fatal("Could not load plugin class: " + pluginClassName);
return false; return false;
@ -3628,78 +3621,70 @@ public class GUI extends Observable {
} }
} }
// Start plugin (before applying rest of config) /* Start plugin */
Plugin startedPlugin = startPlugin(pluginClass, this, simulation, mote); final Plugin startedPlugin = startPlugin(pluginClass, this, simulation, mote);
if (startedPlugin == null) {
logger.warn("Could not start plugin of class: " + pluginClass);
continue;
}
/* Ignore visualized plugins if Cooja not visualized */ /* Apply plugin specific configuration */
try {
if (!visAvailable && startedPlugin == null && pluginClass.asSubclass(VisPlugin.class) != null) {
continue;
}
} catch (ClassCastException e) { }
// Apply plugin specific configuration
for (Element pluginSubElement : (List<Element>) pluginElement.getChildren()) { for (Element pluginSubElement : (List<Element>) pluginElement.getChildren()) {
if (pluginSubElement.getName().equals("plugin_config")) { if (pluginSubElement.getName().equals("plugin_config")) {
startedPlugin.setConfigXML(pluginSubElement.getChildren(), visAvailable); startedPlugin.setConfigXML(pluginSubElement.getChildren(), visAvailable);
} }
} }
// If plugin is visualizer plugin, parse visualization arguments /* If Cooja not visualized, ignore window configuration */
if (startedPlugin instanceof VisPlugin) { if (startedPlugin.getGUI() == null) {
final VisPlugin startedVisPlugin = (VisPlugin) startedPlugin; continue;
new RunnableInEDT<Boolean>() {
public Boolean work() {
Dimension size = new Dimension(100, 100);
Point location = new Point(100, 100);
for (Element pluginSubElement : (List<Element>) pluginElement.getChildren()) {
if (pluginSubElement.getName().equals("width")) {
size.width = Integer.parseInt(pluginSubElement.getText());
startedVisPlugin.setSize(size);
} else if (pluginSubElement.getName().equals("height")) {
size.height = Integer.parseInt(pluginSubElement.getText());
startedVisPlugin.setSize(size);
} else if (pluginSubElement.getName().equals("z")) {
int zOrder = Integer.parseInt(pluginSubElement.getText());
// Save z order as temporary client property
startedVisPlugin.putClientProperty("zorder", zOrder);
} else if (pluginSubElement.getName().equals("location_x")) {
location.x = Integer.parseInt(pluginSubElement.getText());
startedVisPlugin.setLocation(location);
} else if (pluginSubElement.getName().equals("location_y")) {
location.y = Integer.parseInt(pluginSubElement.getText());
startedVisPlugin.setLocation(location);
} else if (pluginSubElement.getName().equals("minimized")) {
try {
startedVisPlugin.setIcon(Boolean.parseBoolean(pluginSubElement.getText()));
} catch (PropertyVetoException e) {
// Ignoring
}
}
}
// For all started visplugins, check if they have a zorder property
try {
for (JInternalFrame plugin : getDesktopPane().getAllFrames()) {
if (plugin.getClientProperty("zorder") != null) {
getDesktopPane().setComponentZOrder(plugin,
((Integer) plugin.getClientProperty("zorder")).intValue());
plugin.putClientProperty("zorder", null);
}
}
} catch (Exception e) {
// Ignore errors
}
return true;
}
}.invokeAndWait();
} }
// If plugin is visualizer plugin, parse visualization arguments
new RunnableInEDT<Boolean>() {
public Boolean work() {
Dimension size = new Dimension(100, 100);
Point location = new Point(100, 100);
for (Element pluginSubElement : (List<Element>) pluginElement.getChildren()) {
if (pluginSubElement.getName().equals("width")) {
size.width = Integer.parseInt(pluginSubElement.getText());
startedPlugin.getGUI().setSize(size);
} else if (pluginSubElement.getName().equals("height")) {
size.height = Integer.parseInt(pluginSubElement.getText());
startedPlugin.getGUI().setSize(size);
} else if (pluginSubElement.getName().equals("z")) {
int zOrder = Integer.parseInt(pluginSubElement.getText());
// Save z order as temporary client property
startedPlugin.getGUI().putClientProperty("zorder", zOrder);
} else if (pluginSubElement.getName().equals("location_x")) {
location.x = Integer.parseInt(pluginSubElement.getText());
startedPlugin.getGUI().setLocation(location);
} else if (pluginSubElement.getName().equals("location_y")) {
location.y = Integer.parseInt(pluginSubElement.getText());
startedPlugin.getGUI().setLocation(location);
} else if (pluginSubElement.getName().equals("minimized")) {
try {
startedPlugin.getGUI().setIcon(Boolean.parseBoolean(pluginSubElement.getText()));
} catch (PropertyVetoException e) { }
}
}
// For all visualized plugins, check if they have a zorder property
try {
for (JInternalFrame plugin : getDesktopPane().getAllFrames()) {
if (plugin.getClientProperty("zorder") != null) {
getDesktopPane().setComponentZOrder(plugin,
((Integer) plugin.getClientProperty("zorder")).intValue());
plugin.putClientProperty("zorder", null);
}
}
} catch (Exception e) { }
return true;
}
}.invokeAndWait();
} }
} }