outsourced 'scan for autostart processes' to static method

This commit is contained in:
fros4943 2006-09-05 14:57:57 +00:00
parent 8c16d29d5e
commit 0490cb7c35

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: ContikiMoteTypeDialog.java,v 1.6 2006/08/30 14:59:35 fros4943 Exp $ * $Id: ContikiMoteTypeDialog.java,v 1.7 2006/09/05 14:57:57 fros4943 Exp $
*/ */
package se.sics.cooja.contikimote; package se.sics.cooja.contikimote;
@ -1506,113 +1506,147 @@ public class ContikiMoteTypeDialog extends JDialog {
return interfaces; return interfaces;
} }
private void autoSelectDependencyProcesses(String processName, /**
String sourceFilename, boolean confirmSelection) { * Scans given file for an autostart expression and returns all process names
* found.
/* *
* Try to autofind and select dependency processes (via * @param sourceFile
* AUTOSTART_PROCESSES()) If this procedure fails, nothing will be reported * Source file to scan
* to user * @return Autostart process names or null
* @throws FileNotFoundException
* Given file not found
* @throws IOException
* IO Exception
*/ */
public static Vector<String> parseAutostartProcesses(File sourceFile)
// Open source code file throws FileNotFoundException, IOException {
FileReader reader = null; // Open file for reading
try { BufferedReader sourceFileReader = new BufferedReader(new FileReader(
reader = new FileReader(textCoreDir.getText() + File.separatorChar sourceFile));
+ sourceFilename);
} catch (Exception ex) {
}
for (File userPlatform : GUI.currentGUI.getUserPlatforms()) {
if (reader == null) {
try {
reader = new FileReader(userPlatform.getPath() + File.separatorChar
+ sourceFilename);
} catch (Exception ex) {
}
}
}
for (File userPlatform : moteTypeUserPlatforms) {
if (reader == null) {
try {
reader = new FileReader(userPlatform.getPath() + File.separatorChar
+ sourceFilename);
} catch (Exception ex) {
}
}
}
if (reader == null) {
// Could not locate source file
return;
}
// Find which processes were set to autostart // Find which processes were set to autostart
BufferedReader sourceFile = new BufferedReader(reader);
String line; String line;
try {
String autostartExpression = "^AUTOSTART_PROCESSES([^$]*)$"; String autostartExpression = "^AUTOSTART_PROCESSES([^$]*)$";
Pattern pattern = Pattern.compile(autostartExpression); Pattern pattern = Pattern.compile(autostartExpression);
Vector<String> foundProcesses = new Vector<String>();
while ((line = sourceFile.readLine()) != null) { while ((line = sourceFileReader.readLine()) != null) {
Matcher matcher = pattern.matcher(line); Matcher matcher = pattern.matcher(line);
if (matcher.find()) { if (matcher.find()) {
// Parse autostart processes // Parse autostart processes
String[] allProcesses = matcher.group(1).split(","); String[] allProcesses = matcher.group(1).split(",");
for (String autostartProcess : allProcesses) { for (String autostartProcess : allProcesses) {
autostartProcess = autostartProcess.replaceAll("[&; \\(\\)]", ""); foundProcesses.add(autostartProcess.replaceAll("[&; \\(\\)]", ""));
}
}
}
return foundProcesses;
}
private boolean autoSelectDependencyProcesses(String processName,
String sourceFilename, boolean confirmSelection) {
// Locate source file
File sourceFile = new File(textCoreDir.getText(), sourceFilename);
boolean foundFile = sourceFile.exists();
if (!foundFile)
for (File userPlatform : GUI.currentGUI.getUserPlatforms()) {
sourceFile = new File(userPlatform, sourceFilename);
if (foundFile = sourceFile.exists())
break;
}
if (!foundFile)
for (File userPlatform : moteTypeUserPlatforms) {
sourceFile = new File(userPlatform, sourceFilename);
if (foundFile = sourceFile.exists())
break;
}
if (!foundFile) {
// Die quietly
return false;
}
Vector<String> autostartProcesses = null;
try {
autostartProcesses = parseAutostartProcesses(sourceFile);
} catch (Exception e) {
return false;
}
if (autostartProcesses == null || autostartProcesses.isEmpty()) {
// Die quietly
return true;
}
for (String autostartProcess : autostartProcesses) {
// Does this process already exist? // Does this process already exist?
boolean processAlreadyExists = false; boolean processAlreadySelected = false;
for (Component checkBox : processPanel.getComponents()) { for (Component checkBox : processPanel.getComponents()) {
String existingProcess = ((JCheckBox) checkBox).getText(); JCheckBox checkBox2 = (JCheckBox) checkBox;
if (existingProcess.equals(autostartProcess)) { String existingProcess = checkBox2.getText();
processAlreadyExists = true; boolean selected = checkBox2.isSelected();
if (existingProcess.equals(autostartProcess) && selected) {
processAlreadySelected = true;
} }
} }
if (!processAlreadyExists) { if (!processAlreadySelected) {
boolean processShouldBeSelected = false;
if (confirmSelection) { if (confirmSelection) {
// Let user choose whether to add process
Object[] options = { "Create", "Cancel" }; Object[] options = { "Create", "Cancel" };
String question = "The process " + processName String question = "The process " + processName
+ " depends on the following process: " + autostartProcess + " depends on the following process: " + autostartProcess
+ "\nDo you want to add this as well?"; + "\nDo you want to select this as well?";
String title = "Add dependency process?"; String title = "Select dependency process?";
int answer = JOptionPane.showOptionDialog(myDialog, question, int answer = JOptionPane.showOptionDialog(myDialog, question, title,
title, JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
JOptionPane.QUESTION_MESSAGE, null, options, options[0]); options, options[0]);
if (answer == JOptionPane.YES_OPTION) { if (answer == JOptionPane.YES_OPTION) {
JCheckBox processCheckBox = new JCheckBox(autostartProcess, processShouldBeSelected = true;
true);
processCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT);
processCheckBox.setActionCommand("process_clicked: "
+ autostartProcess);
processCheckBox.addActionListener(myEventHandler);
processPanel.add(processCheckBox);
} }
} else { } else {
JCheckBox processCheckBox = new JCheckBox(autostartProcess, // Add process
true); processShouldBeSelected = true;
processCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT); }
processCheckBox.setActionCommand("process_clicked: " if (processShouldBeSelected) {
// Get checkbox to select
JCheckBox processToSelectCheckBox = null;
for (Component checkBox : processPanel.getComponents()) {
JCheckBox checkBox2 = (JCheckBox) checkBox;
if (checkBox2.getText().equals(autostartProcess)) {
processToSelectCheckBox = checkBox2;
break;
}
}
if (processToSelectCheckBox == null) {
// Create new check box
processToSelectCheckBox = new JCheckBox(autostartProcess, true);
processToSelectCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT);
processToSelectCheckBox.setActionCommand("process_clicked: "
+ autostartProcess); + autostartProcess);
processCheckBox.addActionListener(myEventHandler); processToSelectCheckBox.addActionListener(myEventHandler);
processPanel.add(processCheckBox); processPanel.add(processToSelectCheckBox);
}
processToSelectCheckBox.setSelected(true);
processPanel.invalidate();
processPanel.revalidate();
} }
} }
} }
} return true;
}
} catch (Exception ex) {
return;
}
} }
private void pathsWereUpdated() { private void pathsWereUpdated() {