📄 gui.java
字号:
frame.setSize(frameWidth, frameHeight); } } frame.setVisible(true); if (createSimDialog) { SwingUtilities.invokeLater(new Runnable() { public void run() { gui.doCreateSimulation(true); } }); } } private static void configureApplet(final GUI gui, boolean createSimDialog) { applet = CoojaApplet.applet; // Add menu bar JMenuBar menuBar = gui.createMenuBar(); applet.setJMenuBar(menuBar); JComponent newContentPane = gui.getDesktopPane(); newContentPane.setOpaque(true); applet.setContentPane(newContentPane); applet.setSize(700, 700); if (createSimDialog) { SwingUtilities.invokeLater(new Runnable() { public void run() { gui.doCreateSimulation(true); } }); } } /** * @return Current desktop pane (simulator visualizer) */ public JDesktopPane getDesktopPane() { return myDesktopPane; } private static void setLookAndFeel() { try { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); logger.info("Nimbus Look And Feel loaded"); } catch (Exception e) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } } catch (UnsupportedLookAndFeelException e) { logger.warn("LookAndFeel: " + e); } catch (ClassNotFoundException e) { logger.warn("LookAndFeel: " + e); } catch (InstantiationException e) { logger.warn("LookAndFeel: " + e); } catch (IllegalAccessException e) { logger.warn("LookAndFeel: " + e); } } /** * Quick-starts a simulation using given parameters. TODO Experimental code * * @param moteTypeID * Mote type ID (if null "mtype1" will be used) * @param projectDirs * GUI project directories * @param sensors * Contiki sensors (if null sensors will be scanned for) * @param coreInterfaces * COOJA core interfaces (if null interfaces will be scanned for) * @param userProcesses * Contiki user processes (if null processes all in given main file * will be added) * @param addAutostartProcesses * Should autostart processes automatically be added? * @param numberOfNodes * Number of nodes to add * @param areaSideLength * Side of node positioning square * @param delayTime * Initial delay time * @param simulationStartinge * Simulation automatically started? * @param filename * Main Contiki process file * @param contikiPath * Contiki path * @return True if simulation was quickstarted correctly */ private static boolean quickStartSimulation(String moteTypeID, Vector<String> projectDirs, Vector<String> sensors, Vector<String> coreInterfaces, Vector<String> userProcesses, boolean addAutostartProcesses, int numberOfNodes, double areaSideLength, int delayTime, boolean simulationStarting, String filename, String contikiPath) { logger.info("> Creating GUI and main frame (invisible)"); frame = new JFrame("COOJA Simulator"); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // Create and set up the content pane. JDesktopPane desktop = new JDesktopPane(); desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); GUI gui = new GUI(desktop); // loads external settings and creates initial project config // Add menu bar frame.setSize(700, 700); frame.addWindowListener(gui.guiEventHandler); JComponent newContentPane = gui.getDesktopPane(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.setLocationRelativeTo(null); // Set manual Contiki path if specified if (contikiPath != null) { setExternalToolsSetting("PATH_CONTIKI", contikiPath); } // Parse project directories and create config if (projectDirs == null) { projectDirs = new Vector<String>(); projectDirs.add("."); } // TODO Should add user prop projects as well here... logger.info("> Reparsing project directories and creating config"); for (String projectDir : projectDirs) { logger.info(">> Adding: " + projectDir); gui.currentProjectDirs.add(new File(projectDir)); } try { gui.reparseProjectConfig(); } catch (ParseProjectsException e) { logger.fatal(">> Error when parsing project directories: " + e.getMessage()); return false; } // Check file permissions and paths logger.info("> Checking paths and file permissions"); if (moteTypeID == null) { moteTypeID = "mtype1"; } File contikiBaseDir = new File(getExternalToolsSetting("PATH_CONTIKI")); File contikiCoreDir = new File(contikiBaseDir, getExternalToolsSetting("PATH_COOJA_CORE_RELATIVE")); File libFile = new File(ContikiMoteType.tempOutputDirectory, moteTypeID + ContikiMoteType.librarySuffix); File mapFile = new File(ContikiMoteType.tempOutputDirectory, moteTypeID + ContikiMoteType.mapSuffix); File depFile = new File(ContikiMoteType.tempOutputDirectory, moteTypeID + ContikiMoteType.dependSuffix); if (libFile.exists()) { libFile.delete(); } if (depFile.exists()) { depFile.delete(); } if (mapFile.exists()) { mapFile.delete(); } if (libFile.exists()) { logger.fatal(">> Can't delete output file, aborting: " + libFile); return false; } if (depFile.exists()) { logger.fatal(">> Can't delete output file, aborting: " + depFile); return false; } if (mapFile.exists()) { logger.fatal(">> Can't delete output file, aborting: " + mapFile); return false; } // Search for main file in current directory (or arg) File mainProcessFile = new File(filename); logger.info(">> Searching main process file: " + mainProcessFile.getAbsolutePath()); if (!mainProcessFile.exists()) { logger.info(">> Searching main process file: " + mainProcessFile.getAbsolutePath()); boolean foundFile = false; for (String projectDir : projectDirs) { mainProcessFile = new File(projectDir, filename); logger.info(">> Searching main process file: " + mainProcessFile.getAbsolutePath()); if (mainProcessFile.exists()) { foundFile = true; break; } } if (!foundFile) { logger.fatal(">> Could not locate main process file, aborting"); return false; } } // Setup compilation arguments logger.info("> Setting up compilation arguments"); Vector<File> filesToCompile = new Vector<File>(); filesToCompile.add(mainProcessFile); // main process file for (String projectDir : projectDirs) { // project directories filesToCompile.add(new File(projectDir)); } String[] projectSources = // project config sources gui.getProjectConfig().getStringArrayValue(ContikiMoteType.class, "C_SOURCES"); for (String projectSource : projectSources) { if (!projectSource.equals("")) { File file = new File(projectSource); if (file.getParent() != null) { // Find which project directory added this file File projectDir = gui.getProjectConfig().getUserProjectDefining( ContikiMoteType.class, "C_SOURCES", projectSource); if (projectDir != null) { // We found a project directory - Add it filesToCompile.add(new File(projectDir.getPath(), file .getParent())); } } filesToCompile.add(new File(file.getName())); } } // Scan for sensors if (sensors == null) { logger.info("> Scanning for sensors"); sensors = new Vector<String>(); Vector<String[]> scannedSensorInfo = ContikiMoteTypeDialog .scanForSensors(contikiCoreDir); for (String projectDir : projectDirs) { // project directories scannedSensorInfo.addAll(ContikiMoteTypeDialog.scanForSensors(new File( projectDir))); } for (String[] sensorInfo : scannedSensorInfo) { // logger.info(">> Found and added: " + sensorInfo[1] + " (" + // sensorInfo[0] + ")"); sensors.add(sensorInfo[1]); } } // Scan for core interfaces if (coreInterfaces == null) { logger.info("> Scanning for core interfaces"); coreInterfaces = new Vector<String>(); Vector<String[]> scannedCoreInterfaceInfo = ContikiMoteTypeDialog .scanForInterfaces(contikiCoreDir); for (String projectDir : projectDirs) { // project directories scannedCoreInterfaceInfo.addAll(ContikiMoteTypeDialog .scanForInterfaces(new File(projectDir))); } for (String[] coreInterfaceInfo : scannedCoreInterfaceInfo) { // logger.info(">> Found and added: " + coreInterfaceInfo[1] + " (" + // coreInterfaceInfo[0] + ")"); coreInterfaces.add(coreInterfaceInfo[1]); } } // Scan for mote interfaces logger.info("> Loading mote interfaces"); String[] moteInterfaces = gui.getProjectConfig().getStringArrayValue( ContikiMoteType.class, "MOTE_INTERFACES"); Vector<Class<? extends MoteInterface>> moteIntfClasses = new Vector<Class<? extends MoteInterface>>(); for (String moteInterface : moteInterfaces) { try { Class<? extends MoteInterface> newMoteInterfaceClass = gui .tryLoadClass(gui, MoteInterface.class, moteInterface); moteIntfClasses.add(newMoteInterfaceClass); // logger.info(">> Loaded mote interface: " + newMoteInterfaceClass); } catch (Exception e) { logger.fatal(">> Failed to load mote interface, aborting: " + moteInterface + ", " + e.getMessage()); return false; } } // Scan for processes if (userProcesses == null) { logger.info("> Scanning for user processes"); userProcesses = new Vector<String>(); Vector<String> autostartProcesses = new Vector<String>(); Vector<ContikiProcess> scannedProcesses = new Vector<ContikiProcess>(); for (String projectDir : projectDirs) { scannedProcesses.addAll(ContikiMoteTypeDialog.scanForProcesses(new File(projectDir))); } for (ContikiProcess processInfo : scannedProcesses) { if (processInfo.getSourceFile().equals(mainProcessFile)) { logger.info(">> Found and added: " + processInfo); userProcesses.add(processInfo.getProcessName()); if (addAutostartProcesses) { // Parse any autostart processes try { // logger.info(">>> Parsing " + processInfo.getProcessName() + " for autostart processes"); Vector<String> autostarters = ContikiMoteTypeDialog.parseAutostartProcesses(mainProcessFile); if (autostarters != null) { autostartProcesses.addAll(autostarters); } } catch (Exception e) { logger.fatal(">>> Error when parsing autostart processes, aborting: " + e); return false; } } } else { // logger.info(">> Found and ignored: " + processInfo[1] + " (" + // processInfo[0] + ")"); } } if (addAutostartProcesses) { // Add autostart process sources if found logger.info("> Adding autostart processes"); for (String autostartProcess : autostartProcesses) { boolean alreadyExists = false; for (String existingProcess : userProcesses) { if (existingProcess.equals(autostartProcess)) { alreadyExists = true; break; } } if (!alreadyExists) { userProcesses.add(autostartProcess); logger.info(">> Added autostart process: " + autostartProcess); } } } } // Generate Contiki main source file logger.info("> Generating Contiki main source file"); if (!ContikiMoteType.tempOutputDirectory.exists()) { ContikiMoteType.tempOutputDirectory.mkdir(); } if (!ContikiMoteType.tempOutputDirectory.exists()) { logger.fatal(">> Could not create output directory: " + ContikiMoteType.tempOutputDirectory); return false; } try { String generatedFilename = ContikiMoteTypeDialog.generateSourceFile( moteTypeID, sensors, coreInterfaces, userProcesses); // logger.info(">> Generated source file: " + generatedFilename); } catch (Exception e) { logger.fatal(">> Error during file generation, aborting: " + e.getMessage()); return false; } // Compile library logger.info("> Compiling library (Rime comm stack)"); // TODO Warning, assuming Rime communication stack boolean compilationSucceded = ContikiMoteTypeDialog.compileLibrary( moteTypeID, contikiBaseDir, filesToCompile, false, ContikiMoteType.CommunicationStack.RIME, null, System.err); if (!libFile.exists() || !depFile.exists() || !mapFile.exists()) { compilationSucceded = false; } if (compilationSucceded) { // logger.info(">> Compilation complete"); } else { logger.fatal(">> Error during compilation, aborting"); return false; } // Create mote type logger.info("> Creating mote type"); ContikiMoteType moteType; try { moteType = new ContikiMoteType(moteTypeID); } catch (MoteTypeCreationException e) { logger.fatal("Exception when creating mote type: " + e); return false; } moteType.setDescription("Mote type: " + filename); moteType.setContikiBaseDir(contikiBaseDir.getPath()); moteType.setContikiCoreDir(contikiCoreDir.getPath()); moteType.setProjectDirs(new Vector<File>()); moteType.setCompilationFiles(filesToCompile);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -