📄 mspmotetype.java
字号:
private String target; private JDialog myDialog; private String customizedCompileCommand = null; private Process compileProcess; static enum DialogState { NO_SOURCE, SELECTED_SOURCE, IS_COMPILING, COMPILED_SOURCE } public MspELFCompiler(String target) { this.target = target; } private String getCompileCommand(String filename) { if (customizedCompileCommand != null) { return customizedCompileCommand; } return GUI.getExternalToolsSetting("PATH_MAKE") + " " + filename + getTargetFileExtension(target) + " TARGET=" + target; } private void setCompileCommand(String command) { if (command == null || command.isEmpty()) { customizedCompileCommand = null; return; } customizedCompileCommand = command; } /** * @return Compiler output */ public File getOutputFile() { return ELFFile; } public File getSourceFile() { return sourceFile; } public String getLastCompileCommand() { return lastCompileCommand; } private void updateDialog(DialogState dialogState) { switch (dialogState) { case NO_SOURCE: compileButton.setEnabled(false); createButton.setEnabled(false); compileCommandTextField.setText(""); break; case IS_COMPILING: compileButton.setEnabled(false); createButton.setEnabled(false); break; case SELECTED_SOURCE: File sourceFile = new File(sourceTextField.getText()); if (!sourceFile.exists()) { updateDialog(DialogState.NO_SOURCE); break; } File parentDirectory = sourceFile.getParentFile(); if (!parentDirectory.exists()) { updateDialog(DialogState.NO_SOURCE); break; } if (!sourceFile.getName().endsWith(".c")) { updateDialog(DialogState.NO_SOURCE); break; } String name = sourceFile.getName().substring(0, sourceFile.getName().length() - 2); compileButton.setEnabled(true); createButton.setEnabled(false); compileCommandTextField.setText(getCompileCommand(name)); compileButton.requestFocusInWindow(); break; case COMPILED_SOURCE: compileButton.setEnabled(true); createButton.setEnabled(true); createButton.requestFocusInWindow(); myDialog.getRootPane().setDefaultButton(createButton); break; default: break; } } protected void compileFirmware(final File sourceFile, final Action successAction, final Action failAction, final MessageList compilationOutput, boolean synchronous) throws Exception { final File parentDirectory = sourceFile.getParentFile(); if (!sourceFile.getName().endsWith(".c")) { logger.fatal("Source file does not end with '.c'"); return; } final String filenameNoExtension = sourceFile.getName().substring(0, sourceFile.getName().length() - 2); final String command = getCompileCommand(filenameNoExtension); logger.info("-- Compiling MSP430 Firmware --"); compileFirmware(command, sourceFile, filenameNoExtension + getTargetFileExtension(target), parentDirectory, successAction, failAction, compilationOutput, synchronous); } protected void compileFirmware( final String command, final File sourceFile, final String firmware, final File parentDirectory, final Action successAction, final Action failAction, final MessageList compilationOutput, boolean synchronous) throws Exception { if (compilationOutput != null) { compilationOutput.clearMessages(); } try { logger.info("Compilation command: " + command); String[] cmd = command.split(" "); compileProcess = Runtime.getRuntime().exec(cmd, null, parentDirectory); final BufferedReader processNormal = new BufferedReader( new InputStreamReader(compileProcess.getInputStream())); final BufferedReader processError = new BufferedReader( new InputStreamReader(compileProcess.getErrorStream())); final File ELFFile = new File(parentDirectory, firmware); if (firmware != null) { if (ELFFile.exists()) { ELFFile.delete(); if (ELFFile.exists()) { if (compilationOutput != null) { compilationOutput.addMessage("Error when deleting old " + ELFFile.getName(), MessageList.ERROR); } if (failAction != null) { failAction.actionPerformed(null); } throw new MoteTypeCreationException("Error when deleting old " + ELFFile.getName()); } } } Thread readInput = new Thread(new Runnable() { public void run() { try { String readLine; while ((readLine = processNormal.readLine()) != null) { if (compilationOutput != null) { compilationOutput.addMessage(readLine, MessageList.NORMAL); } } } catch (IOException e) { logger.warn("Error while reading from process"); } } }, "read input stream thread"); Thread readError = new Thread(new Runnable() { public void run() { try { String readLine; while ((readLine = processError.readLine()) != null) { if (compilationOutput != null) { compilationOutput.addMessage(readLine, MessageList.ERROR); } } } catch (IOException e) { logger.warn("Error while reading from process"); } } }, "read input stream thread"); final MoteTypeCreationException syncException = new MoteTypeCreationException(""); Thread handleCompilationResultThread = new Thread(new Runnable() { public void run() { /* Wait for compilation to end */ try { compileProcess.waitFor(); } catch (Exception e) { if (compilationOutput != null) { compilationOutput.addMessage(e.getMessage(), MessageList.ERROR); } syncException.setCompilationOutput(new MessageList()); syncException.fillInStackTrace(); return; } /* Check return value */ if (compileProcess.exitValue() != 0) { if (compilationOutput != null) { compilationOutput.addMessage("Process returned error code " + compileProcess.exitValue(), MessageList.ERROR); } if (failAction != null) { failAction.actionPerformed(null); } syncException.setCompilationOutput(new MessageList()); syncException.fillInStackTrace(); return; } if (firmware == null) { return; } if (!ELFFile.exists()) { if (compilationOutput != null) { compilationOutput.addMessage("Can't locate output file " + ELFFile, MessageList.ERROR); } if (failAction != null) { failAction.actionPerformed(null); } syncException.setCompilationOutput(new MessageList()); syncException.fillInStackTrace(); return; } if (compilationOutput != null) { compilationOutput.addMessage("", MessageList.NORMAL); compilationOutput.addMessage("Compilation succeded", MessageList.NORMAL); } MspELFCompiler.this.lastCompileCommand = command; MspELFCompiler.this.sourceFile = sourceFile; MspELFCompiler.this.ELFFile = ELFFile; if (successAction != null) { successAction.actionPerformed(null); } } }, "handle compilation results"); readInput.start(); readError.start(); handleCompilationResultThread.start(); if (synchronous) { try { handleCompilationResultThread.join(); } catch (Exception e) { throw (MoteTypeCreationException) new MoteTypeCreationException( "Compilation error: " + e.getMessage()).initCause(e); } /* Detect error manually */ if (syncException.hasCompilationOutput()) { throw (MoteTypeCreationException) new MoteTypeCreationException( "Bad return value").initCause(syncException); } } else { } } catch (IOException ex) { if (failAction != null) { failAction.actionPerformed(null); } throw (MoteTypeCreationException) new MoteTypeCreationException( "Compilation error: " + ex.getMessage()).initCause(ex); } } public boolean showDialog(Container parentContainer, final MspMoteType moteType) { if (parentContainer instanceof Window) { myDialog = new JDialog((Window)parentContainer, "Compile firmware file", ModalityType.APPLICATION_MODAL); } else if (parentContainer instanceof Dialog) { myDialog = new JDialog((Dialog)parentContainer, "Compile firmware file", ModalityType.APPLICATION_MODAL); } else if (parentContainer instanceof Frame) { myDialog = new JDialog((Frame)parentContainer, "Compile firmware file", ModalityType.APPLICATION_MODAL); } else { logger.fatal("Unknown parent container type: " + parentContainer); return false; } final MessageList taskOutput = new MessageList(); // BOTTOM BUTTON PART Box buttonBox = Box.createHorizontalBox(); buttonBox.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); buttonBox.add(Box.createHorizontalGlue()); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sourceFile = null; ELFFile = null; if (compileProcess != null) { compileProcess.destroy(); } myDialog.dispose(); } }); cleanButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new Thread(new Runnable() { public void run() { try { File parentDir = new File(sourceTextField.getText()).getParentFile(); compileFirmware( "make clean TARGET=" + target, new File(sourceTextField.getText()), null, parentDir, null, null, taskOutput, true); } catch (Exception e2) { } } }).start(); } }); compileButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { final File selectedSourceFile = new File(sourceTextField.getText()); /* Strip .c file extension */ if (!selectedSourceFile.getName().endsWith(".c")) { logger.fatal("Source file does not end with '.c'"); return; } final String filenameNoExtension = selectedSourceFile.getName() .substring(0, selectedSourceFile.getName().length() - 2); final Action successAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { updateDialog(DialogState.COMPILED_SOURCE); File parentFile = selectedSourceFile.getParentFile(); sourceFile = selectedSourceFile; ELFFile = new File(parentFile, filenameNoExtension + getTargetFileExtension(target)); } }; final Action failAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { updateDialog(DialogState.SELECTED_SOURCE); } }; updateDialog(DialogState.IS_COMPILING); try { new Thread(new Runnable() { public void run() { try { compileFirmware(selectedSourceFile, successAction, failAction, taskOutput, false); } catch (Exception e) { e.printStackTrace(); } } }).start();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -