JPanel progressPanel = new JPanel(new BorderLayout());
final JDialog progressDialog = new JDialog(myDialog, (String) null);
JProgressBar progressBar;
JButton button;
final MessageList taskOutput;
progressDialog.setLocationRelativeTo(myDialog);
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
taskOutput = new MessageList();
final Thread compilationThreadCopy = compilationThread;
button = new JButton("Close/Abort");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (compilationThreadCopy != null && compilationThreadCopy.isAlive()) {
compilationThreadCopy.interrupt();
}
if (progressDialog != null && progressDialog.isDisplayable()) {
progressDialog.dispose();
}
}
});
final JPopupMenu popup = new JPopupMenu();
JMenuItem headerMenuItem = new JMenuItem("Compilation output:");
headerMenuItem.setEnabled(false);
popup.add(headerMenuItem);
popup.add(new JSeparator());
JMenuItem consoleOutputMenuItem = new JMenuItem("Output to console");
consoleOutputMenuItem.setEnabled(true);
consoleOutputMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int nrRows = taskOutput.getModel().getSize();
System.out.println("\nCOMPILATION OUTPUT:\n");
for (int n=0; n < nrRows; n++) {
System.out.println(taskOutput.getModel().getElementAt(n));
}
System.out.println();
}
});
popup.add(consoleOutputMenuItem);
JMenuItem clipboardMenuItem = new JMenuItem("Copy to clipboard");
clipboardMenuItem.setEnabled(true);
clipboardMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
String output = "";
int nrRows = taskOutput.getModel().getSize();
for (int n=0; n < nrRows; n++) {
output += taskOutput.getModel().getElementAt(n) + "\n";
}
StringSelection stringSelection = new StringSelection(output);
clipboard.setContents(stringSelection, null);
logger.info("Output copied to clipboard");
}
});
popup.add(clipboardMenuItem);
taskOutput.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.isPopupTrigger() || SwingUtilities.isRightMouseButton(e)) {
popup.show(taskOutput, e.getX(), e.getY());
}
}
});
progressPanel.add(BorderLayout.CENTER, new JScrollPane(taskOutput));
progressPanel.add(BorderLayout.NORTH, progressBar);
progressPanel.add(BorderLayout.SOUTH, button);
progressPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
progressPanel.setVisible(true);
progressDialog.getContentPane().add(progressPanel);
progressDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
progressDialog.pack();
progressDialog.getRootPane().setDefaultButton(button);
progressDialog.setVisible(true);
// Create temp output directory if not already exists
if (!ContikiMoteType.tempOutputDirectory.exists()) {
ContikiMoteType.tempOutputDirectory.mkdir();
}
// Parse selected sensors
Vector<String> sensors = new Vector<String>();
for (Component checkBox : sensorPanel.getComponents()) {
if (((JCheckBox) checkBox).isSelected()) {
sensors.add(((JCheckBox) checkBox).getText());
}
}
// Parse selected core interfaces
Vector<String> coreInterfaces = new Vector<String>();
for (Component checkBox : coreInterfacePanel.getComponents()) {
if (((JCheckBox) checkBox).isSelected()) {
coreInterfaces.add(((JCheckBox) checkBox).getText());
}
}
// Parse selected user processes
Vector<String> userProcesses = new Vector<String>();
for (Component checkBox : processPanel.getComponents()) {
if (((JCheckBox) checkBox).isSelected()) {
userProcesses.add(((JCheckBox) checkBox).getText());
}
}
// Generate main contiki source file
String filename = null;
try {
filename = generateSourceFile(textID.getText(), sensors, coreInterfaces,
userProcesses);
} catch (Exception e) {
libraryCreatedOK = false;
progressBar.setBackground(Color.ORANGE);
progressBar.setString(e.getMessage());
progressBar.setIndeterminate(false);
progressBar.setValue(0);
createButton.setEnabled(libraryCreatedOK);
return;
}
// Test compile shared library
progressBar.setString("..compiling..");
final File contikiDir = new File(textContikiDir.getText());
final String identifier = textID.getText();
File libFile = new File(ContikiMoteType.tempOutputDirectory,
identifier + ContikiMoteType.librarySuffix);
File mapFile = new File(ContikiMoteType.tempOutputDirectory,
identifier + ContikiMoteType.mapSuffix);
File depFile = new File(ContikiMoteType.tempOutputDirectory,
identifier + ContikiMoteType.dependSuffix);
if (libFile.exists()) {
libFile.delete();
}
if (depFile.exists()) {
depFile.delete();
}
if (mapFile.exists()) {
mapFile.delete();
}
compilationThread = new Thread(new Runnable() {
public void run() {
// Add all project directories
compilationFiles = (Vector<File>) myGUI
.getProjectDirs().clone();
if (moteTypeProjectDirs == null || moteTypeProjectDirs.isEmpty()) {
compilationFiles.add(new File(textCoreDir.getText(), "testapps"));
} else {
compilationFiles.addAll(moteTypeProjectDirs);
}
// Add source files from project configs
String[] projectSourceFiles = newMoteTypeConfig.getStringArrayValue(
ContikiMoteType.class, "C_SOURCES");
for (String projectSourceFile : projectSourceFiles) {
if (!projectSourceFile.equals("")) {
File file = new File(projectSourceFile);
if (file.getParent() != null) {
// Find which project directory added this file
File projectDir = newMoteTypeConfig.getUserProjectDefining(
ContikiMoteType.class, "C_SOURCES", projectSourceFile);
if (projectDir != null) {
// We found a project directory; add it to path
compilationFiles.add(new File(projectDir.getPath(), file
.getParent()));
}
}
compilationFiles.add(new File(file.getName()));
}
}
// Add selected process source files
for (Component checkBox : processPanel.getComponents()) {
if (((JCheckBox) checkBox).isSelected()) {
String fileName = ((JCheckBox) checkBox).getToolTipText();
if (fileName != null) {
compilationFiles.add(new File(fileName));
}
}
}
compilationSucceded = ContikiMoteTypeDialog.compileLibrary(identifier,
contikiDir, compilationFiles, symbolsCheckBox.isSelected(),
(ContikiMoteType.CommunicationStack) commStackComboBox.getSelectedItem(),
taskOutput.getInputStream(MessageList.NORMAL),
taskOutput.getInputStream(MessageList.ERROR));
}
}, "compilation thread");
compilationThread.start();
while (compilationThread.isAlive()) {