📄 gui.java
字号:
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(); 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(frame, "Loading", true); final File fileToLoad = configFile; final Thread loadThread = new Thread(new Runnable() { public void run() { Simulation newSim = null; try { newSim = loadSimulationConfig(fileToLoad, quick); addToFileHistory(fileToLoad); if (progressDialog != null && progressDialog.isDisplayable()) { progressDialog.dispose(); } } catch (UnsatisfiedLinkError e) { JOptionPane.showMessageDialog(frame, e.getMessage(), "Simulation load error", JOptionPane.ERROR_MESSAGE); if (progressDialog != null && progressDialog.isDisplayable()) progressDialog.dispose(); newSim = null; } catch (SimulationCreationException e) { JOptionPane.showMessageDialog(frame, e.getMessage(), "Simulation load error", JOptionPane.ERROR_MESSAGE); if (progressDialog != null && progressDialog.isDisplayable()) progressDialog.dispose(); newSim = null; } if (newSim != null) { myGUI.setSimulation(newSim); } } }); JPanel progressPanel = new JPanel(new BorderLayout()); JProgressBar progressBar; JButton button; progressBar = new JProgressBar(0, 100); progressBar.setValue(0); progressBar.setIndeterminate(true); button = new JButton("Cancel"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (loadThread != null && loadThread.isAlive()) { loadThread.interrupt(); doRemoveSimulation(false); } if (progressDialog != null && progressDialog.isDisplayable()) { progressDialog.dispose(); } } }); progressPanel.add(BorderLayout.CENTER, progressBar); progressPanel.add(BorderLayout.SOUTH, button); progressPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); progressPanel.setVisible(true); progressDialog.getContentPane().add(progressPanel); progressDialog.pack(); progressDialog.getRootPane().setDefaultButton(button); progressDialog.setLocationRelativeTo(frame); progressDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); loadThread.start(); if (quick) progressDialog.setVisible(true); } /** * Reloads current simulation. * This may include recompiling libraries and renaming mote type identifiers. */ private void reloadCurrentSimulation() { if (getSimulation() == null) { logger.fatal("No simulation to reload"); return; } final JDialog progressDialog = new JDialog(frame, "Reloading", true); final Thread loadThread = new Thread(new Runnable() { public void run() { // Create mappings to new mote type identifiers Properties moteTypeNames = new Properties(); for (MoteType moteType: getSimulation().getMoteTypes()) { if (moteType.getClass().equals(ContikiMoteType.class)) { // Suggest new identifier int counter = 0; String testIdentifier = ""; boolean identifierOK = false; while (!identifierOK) { counter++; testIdentifier = ContikiMoteTypeDialog.ID_PREFIX + counter; identifierOK = true; // Check if identifier already reserved for some other type if (moteTypeNames.containsValue(testIdentifier)) { identifierOK = false; } // Check if identifier is already used by some other type for (MoteType existingMoteType : getSimulation().getMoteTypes()) { if (existingMoteType != moteType && existingMoteType.getIdentifier().equals(testIdentifier)) { identifierOK = false; break; } } // Check if library file with given identifier has already been loaded if (identifierOK && CoreComm.hasLibraryFileBeenLoaded(new File( ContikiMoteType.tempOutputDirectory, testIdentifier + ContikiMoteType.librarySuffix))) { identifierOK = false; } } moteTypeNames.setProperty(moteType.getIdentifier(), testIdentifier); } } // Get
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -