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
// Open source code file * @throws IOException
FileReader reader = null; * IO Exception
try { */
reader = new FileReader(textCoreDir.getText() + File.separatorChar public static Vector<String> parseAutostartProcesses(File sourceFile)
+ sourceFilename); throws FileNotFoundException, IOException {
} catch (Exception ex) { // Open file for reading
} BufferedReader sourceFileReader = new BufferedReader(new FileReader(
for (File userPlatform : GUI.currentGUI.getUserPlatforms()) { sourceFile));
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;
String autostartExpression = "^AUTOSTART_PROCESSES([^$]*)$";
Pattern pattern = Pattern.compile(autostartExpression);
Vector<String> foundProcesses = new Vector<String>();
while ((line = sourceFileReader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
// Parse autostart processes
String[] allProcesses = matcher.group(1).split(",");
for (String autostartProcess : allProcesses) {
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 { try {
String autostartExpression = "^AUTOSTART_PROCESSES([^$]*)$"; autostartProcesses = parseAutostartProcesses(sourceFile);
} catch (Exception e) {
return false;
}
Pattern pattern = Pattern.compile(autostartExpression); if (autostartProcesses == null || autostartProcesses.isEmpty()) {
// Die quietly
return true;
}
while ((line = sourceFile.readLine()) != null) { for (String autostartProcess : autostartProcesses) {
Matcher matcher = pattern.matcher(line); // Does this process already exist?
if (matcher.find()) { boolean processAlreadySelected = false;
// Parse autostart processes for (Component checkBox : processPanel.getComponents()) {
String[] allProcesses = matcher.group(1).split(","); JCheckBox checkBox2 = (JCheckBox) checkBox;
for (String autostartProcess : allProcesses) { String existingProcess = checkBox2.getText();
autostartProcess = autostartProcess.replaceAll("[&; \\(\\)]", ""); boolean selected = checkBox2.isSelected();
if (existingProcess.equals(autostartProcess) && selected) {
processAlreadySelected = true;
}
}
// Does this process already exist? if (!processAlreadySelected) {
boolean processAlreadyExists = false;
for (Component checkBox : processPanel.getComponents()) {
String existingProcess = ((JCheckBox) checkBox).getText();
if (existingProcess.equals(autostartProcess)) {
processAlreadyExists = true;
}
}
if (!processAlreadyExists) { boolean processShouldBeSelected = false;
if (confirmSelection) {
// Let user choose whether to add process
Object[] options = { "Create", "Cancel" };
if (confirmSelection) { String question = "The process " + processName
Object[] options = {"Create", "Cancel"}; + " depends on the following process: " + autostartProcess
+ "\nDo you want to select this as well?";
String title = "Select dependency process?";
int answer = JOptionPane.showOptionDialog(myDialog, question, title,
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);
String question = "The process " + processName if (answer == JOptionPane.YES_OPTION) {
+ " depends on the following process: " + autostartProcess processShouldBeSelected = true;
+ "\nDo you want to add this as well?"; }
String title = "Add dependency process?"; } else {
int answer = JOptionPane.showOptionDialog(myDialog, question, // Add process
title, JOptionPane.DEFAULT_OPTION, processShouldBeSelected = true;
JOptionPane.QUESTION_MESSAGE, null, options, options[0]); }
if (answer == JOptionPane.YES_OPTION) { if (processShouldBeSelected) {
JCheckBox processCheckBox = new JCheckBox(autostartProcess, // Get checkbox to select
true); JCheckBox processToSelectCheckBox = null;
processCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT); for (Component checkBox : processPanel.getComponents()) {
JCheckBox checkBox2 = (JCheckBox) checkBox;
processCheckBox.setActionCommand("process_clicked: " if (checkBox2.getText().equals(autostartProcess)) {
+ autostartProcess); processToSelectCheckBox = checkBox2;
processCheckBox.addActionListener(myEventHandler); break;
processPanel.add(processCheckBox);
}
} else {
JCheckBox processCheckBox = new JCheckBox(autostartProcess,
true);
processCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT);
processCheckBox.setActionCommand("process_clicked: "
+ autostartProcess);
processCheckBox.addActionListener(myEventHandler);
processPanel.add(processCheckBox);
}
} }
} }
if (processToSelectCheckBox == null) {
// Create new check box
processToSelectCheckBox = new JCheckBox(autostartProcess, true);
processToSelectCheckBox.setAlignmentX(Component.LEFT_ALIGNMENT);
processToSelectCheckBox.setActionCommand("process_clicked: "
+ autostartProcess);
processToSelectCheckBox.addActionListener(myEventHandler);
processPanel.add(processToSelectCheckBox);
}
processToSelectCheckBox.setSelected(true);
processPanel.invalidate();
processPanel.revalidate();
} }
} }
} catch (Exception ex) {
return;
} }
return true;
} }
private void pathsWereUpdated() { private void pathsWereUpdated() {