📄 gui.java
字号:
return null; } newPlugin = 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; } newPlugin = pluginClass.getConstructor(new Class[] { GUI.class }) .newInstance(gui); } } catch (Exception e) { logger.fatal("Exception thrown when starting plugin: " + e); e.printStackTrace(); return null; } if (newPlugin == null) { return null; } // Add to active plugins list startedPlugins.add(newPlugin); // Show plugin if visualizer type if (newPlugin instanceof VisPlugin) { myGUI.showPlugin((VisPlugin) newPlugin); } return newPlugin; } /** * Register a plugin to be included in the GUI. The plugin will be visible in * the menubar. * * @param newPluginClass * New plugin to register * @return True if this plugin was registered ok, false otherwise */ public boolean registerPlugin(Class<? extends Plugin> newPluginClass) { return registerPlugin(newPluginClass, true); } /** * Register a temporary plugin to be included in the GUI. The plugin will be * visible in the menubar. This plugin will automatically be unregistered if * the current simulation is removed. * * @param newPluginClass * New plugin to register * @return True if this plugin was registered ok, false otherwise */ public boolean registerTemporaryPlugin(Class<? extends Plugin> newPluginClass) { if (pluginClasses.contains(newPluginClass)) { return false; } boolean returnVal = registerPlugin(newPluginClass, true); if (!returnVal) { return false; } pluginClassesTemporary.add(newPluginClass); return true; } /** * Unregister a plugin class. Removes any plugin menu items links as well. * * @param pluginClass * Plugin class to unregister */ public void unregisterPlugin(Class<? extends Plugin> pluginClass) { // Remove (if existing) plugin class menu items for (Component menuComponent : menuPlugins.getMenuComponents()) { if (menuComponent.getClass().isAssignableFrom(JMenuItem.class)) { JMenuItem menuItem = (JMenuItem) menuComponent; if (menuItem.getClientProperty("class").equals(pluginClass)) { menuPlugins.remove(menuItem); } } } if (menuMotePluginClasses.contains(pluginClass)) { menuMotePluginClasses.remove(pluginClass); } // Remove from plugin vectors (including temporary) if (pluginClasses.contains(pluginClass)) { pluginClasses.remove(pluginClass); } if (pluginClassesTemporary.contains(pluginClass)) { pluginClassesTemporary.remove(pluginClass); } } /** * Register a plugin to be included in the GUI. * * @param newPluginClass * New plugin to register * @param addToMenu * Should this plugin be added to the dedicated plugins menubar? * @return True if this plugin was registered ok, false otherwise */ private boolean registerPlugin(Class<? extends Plugin> newPluginClass, boolean addToMenu) { // Get description annotation (if any) String description = getDescriptionOf(newPluginClass); // Get plugin type annotation (required) int pluginType; if (newPluginClass.isAnnotationPresent(PluginType.class)) { pluginType = newPluginClass.getAnnotation(PluginType.class).value(); } else { pluginType = PluginType.UNDEFINED_PLUGIN; } // Check that plugin type is valid and constructor exists try { if (pluginType == PluginType.MOTE_PLUGIN) { newPluginClass.getConstructor(new Class[] { Mote.class, Simulation.class, GUI.class }); } else if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN) { newPluginClass .getConstructor(new Class[] { Simulation.class, GUI.class }); } else if (pluginType == PluginType.COOJA_PLUGIN || pluginType == PluginType.COOJA_STANDARD_PLUGIN) { newPluginClass.getConstructor(new Class[] { GUI.class }); } else { logger.fatal("Could not find valid plugin type annotation in class " + newPluginClass); return false; } } catch (NoSuchMethodException e) { logger.fatal("Could not find valid constructor in class " + newPluginClass + ": " + e); return false; } if (addToMenu && menuPlugins != null) { // Create 'start plugin'-menu item JMenuItem menuItem = new JMenuItem(description); menuItem.setActionCommand("start plugin"); menuItem.putClientProperty("class", newPluginClass); menuItem.addActionListener(guiEventHandler); menuPlugins.add(menuItem); if (pluginType == PluginType.MOTE_PLUGIN) { // Disable previous menu item and add new item to mote plugins menu menuItem.setEnabled(false); menuItem.setToolTipText("Mote plugin"); menuMotePluginClasses.add(newPluginClass); } } pluginClasses.add(newPluginClass); return true; } /** * Unregister all plugin classes, including temporary plugins. */ public void unregisterPlugins() { if (menuPlugins != null) { menuPlugins.removeAll(); } if (menuMotePluginClasses != null) { menuMotePluginClasses.clear(); } pluginClasses.clear(); pluginClassesTemporary.clear(); } /** * Return a mote plugins submenu for given mote. * * @param mote Mote * @return Mote plugins menu */ public JMenu createMotePluginsSubmenu(Mote mote) { JMenu menuMotePlugins = new JMenu("Open mote plugin for " + mote); for (Class<? extends Plugin> motePluginClass: menuMotePluginClasses) { JMenuItem menuItem = new JMenuItem(getDescriptionOf(motePluginClass)); menuItem.setActionCommand("start plugin"); menuItem.putClientProperty("class", motePluginClass); menuItem.putClientProperty("mote", mote); menuItem.addActionListener(guiEventHandler); menuMotePlugins.add(menuItem); } return menuMotePlugins; } // // GUI CONTROL METHODS //// /** * @return Current simulation */ public Simulation getSimulation() { return mySimulation; } public void setSimulation(Simulation sim) { if (sim != null) { doRemoveSimulation(false); } mySimulation = sim; // Set frame title if (frame != null) { frame.setTitle("COOJA Simulator" + " - " + sim.getTitle()); } // Open standard plugins (if none opened already) if (startedPlugins.size() == 0) { for (Class<? extends Plugin> pluginClass : pluginClasses) { int pluginType = pluginClass.getAnnotation(PluginType.class).value(); if (pluginType == PluginType.SIM_STANDARD_PLUGIN) { startPlugin(pluginClass, this, sim, null); } } } } /** * Creates a new mote type of the given mote type class. * * @param moteTypeClass * Mote type class */ public void doCreateMoteType(Class<? extends MoteType> moteTypeClass) { if (mySimulation == null) { logger.fatal("Can't create mote type (no simulation)"); return; } // Stop simulation (if running) mySimulation.stopSimulation(); // Create mote type MoteType newMoteType = null; boolean moteTypeOK = false; try { newMoteType = moteTypeClass.newInstance(); moteTypeOK = newMoteType.configureAndInit(frame, mySimulation, isVisualized()); } catch (InstantiationException e) { logger.fatal("Exception when creating mote type: " + e); return; } catch (IllegalAccessException e) { logger.fatal("Exception when creating mote type: " + e); return; } catch (MoteTypeCreationException e) { logger.fatal("Exception when creating mote type: " + e); return; } // Add mote type to simulation if (newMoteType != null && moteTypeOK) { mySimulation.addMoteType(newMoteType); } } /** * Remove current simulation * * @param askForConfirmation * Should we ask for confirmation if a simulation is already active? */ public void doRemoveSimulation(boolean askForConfirmation) { if (mySimulation != null) { if (askForConfirmation) { String s1 = "Remove"; String s2 = "Cancel"; Object[] options = { s1, s2 }; int n = JOptionPane.showOptionDialog(frame, "You have an active simulation.\nDo you want to remove it?", "Remove current simulation?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, s1); if (n != JOptionPane.YES_OPTION) { return; } } // Close all started non-GUI plugins for (Object startedPlugin : startedPlugins.toArray()) { int pluginType = startedPlugin.getClass().getAnnotation( PluginType.class).value(); if (pluginType != PluginType.COOJA_PLUGIN && pluginType != PluginType.COOJA_STANDARD_PLUGIN) { removePlugin((Plugin) startedPlugin, false); } } // Delete simulation mySimulation.deleteObservers(); mySimulation.stopSimulation(); mySimulation = null; // Unregister temporary plugin classes Enumeration<Class<? extends Plugin>> pluginClasses = pluginClassesTemporary .elements(); while (pluginClasses.hasMoreElements()) { unregisterPlugin(pluginClasses.nextElement()); } // Reset frame title frame.setTitle("COOJA Simulator"); } } /** * Load a simulation configuration file from disk * * @param askForConfirmation Ask for confirmation before removing any current simulation * @param quick Quick-load simulation * @param configFile Configuration file to load, if null a dialog will appear */ public void doLoadConfig(boolean askForConfirmation, final boolean quick, File configFile) { if (CoreComm.hasLibraryBeenLoaded()) { JOptionPane .showMessageDialog( frame, "Shared libraries has already been loaded.\nYou need to restart the simulator!", "Can't load simulation", JOptionPane.ERROR_MESSAGE); return; } if (askForConfirmation && mySimulation != null) { String s1 = "Remove"; String s2 = "Cancel"; Object[] options = { s1, s2 }; int n = JOptionPane.showOptionDialog(frame, "You have an active simulation.\nDo you want to remove it?", "Remove current simulation?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, s1); if (n != JOptionPane.YES_OPTION) { return; } } doRemoveSimulation(false); // Check already selected file, or select file using filechooser if (configFile != null) { if (!configFile.exists() || !configFile.canRead()) { logger.fatal("No read access to file"); return; } } else { JFileChooser fc = new JFileChooser(); fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES); // Suggest file using history Vector<File> history = getFileHistory(); if (history != null && history.size() > 0) { File suggestedFile = getFileHistory().firstElement(); fc.setSelectedFile(suggestedFile); } int returnVal = fc.showOpenDialog(frame); if (returnVal == JFileChooser.APPROVE_OPTION) { configFile = fc.getSelectedFile(); // Try adding extension if not founds if (!configFile.exists()) { configFile = new File(configFile.getParent(), configFile.getName() + SAVED_SIMULATIONS_FILES); } if (!configFile.exists() || !configFile.canRead()) { logger.fatal("No read access to file"); return; } } else { logger.info("Load command cancelled by user..."); return; } } // Load simulation in separate thread, while showing progress monitor final JDialog progressDialog = new JDialog(f
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -