/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.gui.action;

import java.awt.event.ActionEvent;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

import javax.swing.JFileChooser;

import org.apache.jmeter.exceptions.IllegalUserActionException;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.tree.JMeterTreeNode;
import org.apache.jmeter.gui.util.FileDialoger;
import org.apache.jmeter.save.OldSaveService;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;

/**
 * @author Michael Stover
 * @author <a href="mailto:klancast@swbell.net">Keith Lancaster</a>
 * @version $Revision$ updated on $Date$
 */
public class Save implements Command {
	private static final Logger log = LoggingManager.getLoggerForClass();

	private static Set commands = new HashSet();
	static {
		commands.add(ActionNames.SAVE_AS);
		commands.add(ActionNames.SAVE_ALL_AS);
		commands.add(ActionNames.SAVE);
	}

	/**
	 * Constructor for the Save object.
	 */
	public Save() {
	}

	/**
	 * Gets the ActionNames attribute of the Save object.
	 * 
	 * @return the ActionNames value
	 */
	public Set getActionNames() {
		return commands;
	}

	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)) {
			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(GuiPackage.getInstance().getTreeListener()
					.getCurrentNode().getName()
					+ ".jmx");
			if (chooser == null) {
				return;
			}
			updateFile = chooser.getSelectedFile().getAbsolutePath();
			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);
			if (SaveService.isSaveTestPlanFormat20()) {
				OldSaveService.saveSubTree(subTree, ostream);
			} else {
				SaveService.saveTree(subTree, ostream);
			}
		} catch (Throwable ex) {
			GuiPackage.getInstance().setTestPlanFile(null);
			log.error("", ex);
			throw new IllegalUserActionException("Couldn't save test plan to file: " + updateFile);
		} finally {
            JOrphanUtils.closeQuietly(ostream);
		}
	}

	// package protected to all for separate test code
	void convertSubTree(HashTree tree) {
		Iterator iter = new LinkedList(tree.list()).iterator();
		while (iter.hasNext()) {
			JMeterTreeNode item = (JMeterTreeNode) iter.next();
			convertSubTree(tree.getTree(item));
			TestElement testElement = item.getTestElement();
			tree.replace(item, testElement);
		}
	}
}

@@ -1571,139 +1571,98 @@
 %0A * 
-@author Michael Stover%0A * @author %3Ca href=%22mailto:klancast@swbell.net%22%3EKeith Lancaster%3C/a%3E%0A * @version $Revision$ updated on $Date$
+Save the current test plan; implements:%0A * Save%0A * Save TestPlan As%0A * Save (Selection) As
 %0A */
@@ -1861,24 +1861,47 @@
 es.SAVE_AS);
+ // Save (Selection) As
 %0A%09%09commands.
@@ -1929,16 +1929,36 @@
 ALL_AS);
+ // Save TestPlan As
 %0A%09%09comma
@@ -1983,16 +1983,24 @@
 s.SAVE);
+ // Save
 %0A%09%7D%0A%0A%09/*
@@ -2957,16 +2957,31 @@
 %22.jmx%22);
+ // $NON-NLS-1$
 %0A%09%09%09if (

Update comments; add NLS marker