From a437d06ba364aa282ef617a5b3f65bd5eaec9967 Mon Sep 17 00:00:00 2001 From: Fredrik Osterlind Date: Thu, 24 May 2012 14:02:45 +0200 Subject: [PATCH] disable plugin menu items if not supported --- tools/cooja/java/se/sics/cooja/GUI.java | 63 +++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/GUI.java b/tools/cooja/java/se/sics/cooja/GUI.java index a14d5d184..c081652d9 100644 --- a/tools/cooja/java/se/sics/cooja/GUI.java +++ b/tools/cooja/java/se/sics/cooja/GUI.java @@ -898,6 +898,61 @@ public class GUI extends Observable { } menuPlugins.setMnemonic(KeyEvent.VK_P); menuBar.add(menuPlugins); + menuPlugins.addMenuListener(new MenuListener() { + public void menuSelected(MenuEvent e) { + for (Component menuComponent: menuPlugins.getMenuComponents()) { + if (!(menuComponent instanceof JMenuItem)) { + continue; + } + JMenuItem menuItem = (JMenuItem) menuComponent; + Class pluginClass = (Class) menuItem.getClientProperty("class"); + int pluginType; + if (pluginClass.isAnnotationPresent(PluginType.class)) { + pluginType = pluginClass.getAnnotation(PluginType.class).value(); + } else { + pluginType = PluginType.UNDEFINED_PLUGIN; + } + + /* No simulation -> deactivate non-GUI plugins */ + if (pluginType == PluginType.COOJA_PLUGIN || pluginType == PluginType.COOJA_STANDARD_PLUGIN) { + menuItem.setEnabled(true); + continue; + } + /* Mote plugin -> not accessed from this menu */ + if (pluginType == PluginType.MOTE_PLUGIN) { + menuItem.setEnabled(false); + continue; + } + if (pluginType != PluginType.SIM_PLUGIN && pluginType != PluginType.SIM_STANDARD_PLUGIN) { + /* Unknown */ + menuItem.setEnabled(false); + continue; + } + if (getSimulation() == null) { + menuItem.setEnabled(false); + continue; + } + + /* Check if simulation plugin depends on any particular radio medium */ + if (pluginClass.getAnnotation(SupportedArguments.class) != null) { + menuItem.setEnabled(false); + Class[] radioMediums = pluginClass.getAnnotation(SupportedArguments.class).radioMediums(); + for (Class o: radioMediums) { + if (o.isAssignableFrom(getSimulation().getRadioMedium().getClass())) { + menuItem.setEnabled(true); + break; + } + } + } else { + menuItem.setEnabled(true); + } + } + } + public void menuDeselected(MenuEvent e) { + } + public void menuCanceled(MenuEvent e) { + } + }); // Settings menu menu = new JMenu("Settings"); @@ -1806,7 +1861,7 @@ public class GUI extends Observable { public Boolean work() { // Create 'start plugin'-menu item JMenuItem menuItem; - String tooltip = ""; + String tooltip = "
";
 
           /* Sort menu according to plugin type */
           int itemIndex=0;
@@ -1844,7 +1899,7 @@ public class GUI extends Observable {
             menuItem = new JMenuItem(description);
             menuItem.setEnabled(false);
             tooltip += "Mote plugin: " + newPluginClass.getName();
-            tooltip += "
Start mote plugins by right-clicking a mote in the simulation visualizer"; + tooltip += "\nStart mote plugins by right-clicking a mote in the simulation visualizer"; menuMotePluginClasses.add(newPluginClass); itemIndex = menuPlugins.getItemCount(); } else { @@ -1852,13 +1907,11 @@ public class GUI extends Observable { return false; } - /* TODO Check if plugin specifies supported arguments here */ - /* Check if plugin was imported by a project directory */ File project = getProjectConfig().getUserProjectDefining(GUI.class, "PLUGINS", newPluginClass.getName()); if (project != null) { - tooltip += "
Loaded by project: " + project.getPath(); + tooltip += "\nLoaded by project: " + project.getPath(); } tooltip += "";