📄 mantismotetypedialog.java
字号:
libraryCreatedOK = false; progressBar.setBackground(Color.ORANGE); if (e.getMessage() != null) progressBar.setString("source file generation failed: " + e.getMessage()); else progressBar.setString("source file generation failed"); progressBar.setIndeterminate(false); progressBar.setValue(0); createButton.setEnabled(libraryCreatedOK); return; } // Test compile shared library progressBar.setString("..compiling.."); if (libFile.exists()) { libFile.delete(); } compilationThread = new Thread(new Runnable() { public void run() { compilationSucceded = MantisMoteTypeDialog.compileLibrary( libFile, objFile, srcFile, workingDir, taskOutput.getInputStream(MessageList.NORMAL), taskOutput.getInputStream(MessageList.ERROR)); } }, "compilation thread"); compilationThread.start(); while (compilationThread.isAlive()) { try { Thread.sleep(100); } catch (InterruptedException e) { // NOP } } if (!compilationSucceded) { if (libFile.exists()) { libFile.delete(); } libraryCreatedOK = false; } else { libraryCreatedOK = true; if (!libFile.exists()) libraryCreatedOK = false; } if (libraryCreatedOK) { progressBar.setBackground(Color.GREEN); progressBar.setString("compilation succeded"); button.grabFocus(); myDialog.getRootPane().setDefaultButton(createButton); } else { progressBar.setBackground(Color.ORANGE); progressBar.setString("compilation failed"); myDialog.getRootPane().setDefaultButton(compileButton); } progressBar.setIndeterminate(false); progressBar.setValue(0); createButton.setEnabled(libraryCreatedOK); } /** * Generates new source file by reading default source template and replacing * certain field in order to be loadable from given Java class. * * @param outputFile Source file to create * @throws Exception */ public static void generateSourceFile(File outputFile) throws Exception { // CHECK JNI CLASS AVAILABILITY String libString = CoreComm.getAvailableClassName(); if (libString == null) { logger.fatal("No more libraries can be loaded!"); throw new Exception("Maximum number of mote types already exist"); } // GENERATE NEW FILE BufferedWriter destFile = null; BufferedReader sourceFile = null; try { Reader reader; String mainTemplate = GUI .getExternalToolsSetting("MANTIS_MAIN_TEMPLATE_FILENAME"); if ((new File(mainTemplate)).exists()) { reader = new FileReader(mainTemplate); } else { InputStream input = MantisMoteTypeDialog.class .getResourceAsStream('/' + mainTemplate); if (input == null) { throw new FileNotFoundException(mainTemplate + " not found"); } reader = new InputStreamReader(input); } sourceFile = new BufferedReader(reader); destFile = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outputFile))); // Replace fields in template String line; while ((line = sourceFile.readLine()) != null) { line = line.replaceFirst("\\[CLASS_NAME\\]", libString); destFile.write(line + "\n"); } destFile.close(); sourceFile.close(); } catch (Exception e) { try { if (destFile != null) destFile.close(); if (sourceFile != null) sourceFile.close(); } catch (Exception e2) { } // Forward exception throw e; } } /** * Compiles a mote type shared library using the standard Mantis makefile. * * @param libFile Library file to create * @param binFile Binary file to link against * @param sourceFile Source file to compile * @param workingDir Working directory * @param outputStream * Output stream from compilation (optional) * @param errorStream * Error stream from compilation (optional) * @return True if compilation succeeded, false otherwise */ public static boolean compileLibrary(File libFile, File binFile, File sourceFile, File workingDir, final PrintStream outputStream, final PrintStream errorStream) { // Check needed files if (!workingDir.exists()) { if (errorStream != null) errorStream.println("Bad paths"); logger.fatal("Working directory does not exist"); return false; } if (!workingDir.isDirectory()) { if (errorStream != null) errorStream.println("Bad paths"); logger.fatal("Working directory is not a directory"); return false; } if (libFile.exists()) { if (errorStream != null) errorStream.println("Bad output filenames"); logger.fatal("Library already exists"); return false; } if (!sourceFile.exists()) { if (errorStream != null) errorStream.println("Bad dependency files"); logger.fatal("Source file not found"); return false; } if (!binFile.exists()) { if (errorStream != null) errorStream.println("Bad dependency files"); logger.fatal("Link object file not found"); return false; } if (CoreComm.hasLibraryFileBeenLoaded(libFile)) { if (errorStream != null) errorStream.println("Bad output filenames"); logger.fatal("A library has already been loaded with the same name before"); return false; } try { // Call make file String[] cmd = new String[]{ GUI.getExternalToolsSetting("PATH_MAKE"), libFile.getName()}; String[] env = new String[]{ "COOJA_LINKFILE=" + binFile.getName(), "COOJA_SOURCE=" + sourceFile.getName(), "PATH=" + System.getenv("PATH")}; Process p = Runtime.getRuntime().exec(cmd, env, workingDir); final BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream())); Thread readInput = new Thread(new Runnable() { public void run() { String readLine; try { while ((readLine = input.readLine()) != null) { if (outputStream != null && readLine != null) outputStream.println(readLine); } } catch (IOException e) { logger.warn("Error while reading from process"); } } }, "read input stream thread"); Thread readError = new Thread(new Runnable() { public void run() { String readLine; try { while ((readLine = err.readLine()) != null) { if (errorStream != null && readLine != null) errorStream.println(readLine); } } catch (IOException e) { logger.warn("Error while reading from process"); } } }, "read input stream thread"); readInput.start(); readError.start(); while (readInput.isAlive() || readError.isAlive()) { Thread.sleep(100); } input.close(); err.close(); p.waitFor(); if (p.exitValue() != 0) { logger.fatal("Make file returned error: " + p.exitValue()); return false; } } catch (Exception e) { logger.fatal("Error while compiling library: " + e); return false; } return true; } private void pathsWereUpdated() { updateVisualFields(); } private void updateVisualFields() { settingsOK = true; // Check for non-unique identifier textID.setBackground(Color.WHITE); textID.setToolTipText(null); for (MoteType otherType : allOtherTypes) { if (otherType != myMoteType && otherType.getIdentifier().equalsIgnoreCase(textID.getText())) { textID.setBackground(Color.RED); textID.setToolTipText("Conflicting name - must be unique"); settingsOK = false; break; } } // Check for non-unique description textDescription.setBackground(Color.WHITE); textDescription.setToolTipText(null); for (MoteType otherType : allOtherTypes) { if (otherType != myMoteType && otherType.getDescription().equals(textDescription.getText())) { textDescription.setBackground(Color.RED); textDescription.setToolTipText("Conflicting name - must be unique"); settingsOK = false; break; } } // Check that binary exists textMantisBinary.setBackground(Color.WHITE); textMantisBinary.setToolTipText(null); objFile = new File(textMantisBinary.getText()); workingDir = objFile.getParentFile(); libFile = new File(workingDir, textID.getText() + ".library"); srcFile = new File(workingDir, textID.getText() + ".c"); // TODO Check that file is correct type (.o or something) if (objFile == null || !objFile.exists()) { textMantisBinary.setBackground(Color.RED); textMantisBinary.setToolTipText("Incorrect object file"); objFile = null; libFile = null; srcFile = null; workingDir = null; settingsOK = false; } // Update output text field if (settingsOK) { textOutputFiles.setText(libFile.getName() + ", " + srcFile.getName() + ", " + textID.getText() + ".o"); } else { textOutputFiles.setText(""); } createButton.setEnabled(libraryCreatedOK = false); compileButton.setEnabled(settingsOK); } private class MoteTypeEventHandler implements ActionListener, DocumentListener { public void insertUpdate(DocumentEvent e) { if (myDialog.isVisible()) javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { pathsWereUpdated(); } }); } public void removeUpdate(DocumentEvent e) { if (myDialog.isVisible()) javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { pathsWereUpdated(); } }); } public void changedUpdate(DocumentEvent e) { if (myDialog.isVisible()) javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { pathsWereUpdated(); } }); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("cancel")) { // Cancel creation of mote type myMoteType = null; dispose(); } else if (e.getActionCommand().equals("clean")) { // Delete any created intermediate files // TODO Not implemented logger.fatal("Clean functionality not implemented"); } else if (e.getActionCommand().equals("create")) { // Create mote type and set related fields boolean ret = myMoteType.doInit(libFile, objFile, moteInterfaceClasses); myMoteType.setDescription(textDescription.getText()); myMoteType.setIdentifier(textID.getText()); if (ret) { dispose(); } else { logger.fatal("Mote type creation failed."); } } else if (e.getActionCommand().equals("compile")) { compileButton.requestFocus(); Thread testSettingsThread = new Thread(new Runnable() { public void run() { doCompileCurrentSettings(); } }, "test settings thread"); testSettingsThread.start(); } else if (e.getActionCommand().equals("browsemantis")) { JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(new java.io.File(".")); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); fc.setDialogTitle("Mantis binary to link against"); if (fc.showOpenDialog(myDialog) == JFileChooser.APPROVE_OPTION) { textMantisBinary.setText(fc.getSelectedFile().getPath()); } createButton.setEnabled(libraryCreatedOK = false); pathsWereUpdated(); } else logger.warn("Unhandled action: " + e.getActionCommand()); createButton.setEnabled(libraryCreatedOK = false); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -