in taverna-external-tool-activity-ui/src/main/java/org/apache/taverna/activities/externaltool/views/SaveDescriptionAction.java [79:138]
public static boolean saveStringToFile(Component parent, String dialogTitle, String extension, ToolDescription description) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle(dialogTitle);
fileChooser.resetChoosableFileFilters();
fileChooser.setAcceptAllFileFilterUsed(true);
fileChooser.setFileFilter(new ExtensionFileFilter(new String[] { extension }));
Preferences prefs = Preferences.userNodeForPackage(FileTools.class);
String curDir = prefs
.get("currentDir", System.getProperty("user.home"));
fileChooser.setCurrentDirectory(new File(curDir));
boolean tryAgain = true;
while (tryAgain) {
tryAgain = false;
int returnVal = fileChooser.showSaveDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
prefs.put("currentDir", fileChooser.getCurrentDirectory()
.toString());
File file = fileChooser.getSelectedFile();
if (!file.getName().contains(".")) {
String newName = file.getName() + extension;
file = new File(file.getParentFile(), newName);
}
// TODO: Open in separate thread to avoid hanging UI
try {
List<ToolDescription> currentDescriptions;
if (file.exists()) {
currentDescriptions = ToolDescriptionParser.readDescriptionsFromStream(new FileInputStream(file));
} else {
currentDescriptions = new ArrayList<ToolDescription>();
}
Element overallElement = new Element("tooldescs");
for (ToolDescription ud : currentDescriptions) {
if (!ud.getTooldescid().equals(description.getTooldescid())) {
overallElement.addContent(ud.writeToXMLElement());
}
}
overallElement.addContent(description.writeToXMLElement());
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(outputter.outputString(overallElement));
out.close();
logger.info("Saved content by overwriting " + file);
return true;
} catch (IOException ex) {
logger.warn("Could not save content to " + file, ex);
JOptionPane.showMessageDialog(parent,
"Could not save to " + file + ": \n\n"
+ ex.getMessage(), "Warning",
JOptionPane.WARNING_MESSAGE);
return false;
}
}
}
return false;
}