}
public void doAction(ActionEvent e) throws IllegalUserActionException {
HashTree subTree = null;
if (!commands.contains(e.getActionCommand())) {
throw new IllegalUserActionException("Invalid user command:" + e.getActionCommand());
}
if (e.getActionCommand().equals(ActionNames.SAVE_AS)) {
JMeterTreeNode[] nodes = GuiPackage.getInstance().getTreeListener().getSelectedNodes();
if (nodes.length > 1){
JMeterUtils.reportErrorToUser(
JMeterUtils.getResString("save_as_error"), // $NON-NLS-1$
JMeterUtils.getResString("save_as")); // $NON-NLS-1$
return;
}
subTree = GuiPackage.getInstance().getCurrentSubTree();
} else {
subTree = GuiPackage.getInstance().getTreeModel().getTestPlan();
}
String updateFile = GuiPackage.getInstance().getTestPlanFile();
if (!ActionNames.SAVE.equals(e.getActionCommand()) || updateFile == null) {
JFileChooser chooser = FileDialoger.promptToSaveFile(updateFile == null ? GuiPackage.getInstance().getTreeListener()
.getCurrentNode().getName()
+ JMX_FILE_EXTENSION : updateFile);
if (chooser == null) {
return;
}
updateFile = chooser.getSelectedFile().getAbsolutePath();
// Make sure the file ends with proper extension
if(FilenameUtils.getExtension(updateFile).equals("")) {
updateFile = updateFile + JMX_FILE_EXTENSION;
}
// Check if the user is trying to save to an existing file
File f = new File(updateFile);
if(f.exists()) {
int response = JOptionPane.showConfirmDialog(GuiPackage.getInstance().getMainFrame(),
JMeterUtils.getResString("save_overwrite_existing_file"), // $NON-NLS-1$
JMeterUtils.getResString("save?"), // $NON-NLS-1$
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.CLOSED_OPTION || response == JOptionPane.NO_OPTION) {
return ; // Do not save, user does not want to overwrite
}
}
if (!e.getActionCommand().equals(ActionNames.SAVE_AS)) {
GuiPackage.getInstance().setTestPlanFile(updateFile);
}
}
// TODO: doesn't putting this here mark the tree as
// saved even though a failure may occur later?
ActionRouter.getInstance().doActionNow(new ActionEvent(subTree, e.getID(), ActionNames.SUB_TREE_SAVED));
try {
convertSubTree(subTree);
} catch (Exception err) {
}
FileOutputStream ostream = null;
try {
ostream = new FileOutputStream(updateFile);
SaveService.saveTree(subTree, ostream);
} catch (Throwable ex) {
GuiPackage.getInstance().setTestPlanFile(null);
log.error("", ex);
if (ex instanceof Error){
throw (Error) ex;
}
if (ex instanceof RuntimeException){
throw (RuntimeException) ex;
}
throw new IllegalUserActionException("Couldn't save test plan to file: " + updateFile);
} finally {
JOrphanUtils.closeQuietly(ostream);
}
GuiPackage.getInstance().updateCurrentGui();
}