/*
 * Copyright 2005-2010 Ignis Software Tools Ltd. All rights reserved.
 */
package jsystem.treeui.actionItems;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.logging.Logger;

import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.SwingWorker;

import jsystem.framework.FrameworkOptions;
import jsystem.framework.report.ListenerstManager;
import jsystem.framework.scenario.Parameter;
import jsystem.framework.scenario.Scenario;
import jsystem.framework.scenario.ScenarioChangeType;
import jsystem.framework.scenario.ScenarioListener;
import jsystem.framework.scenario.ScenariosManager;
import jsystem.guiMapping.JsystemMapping;
import jsystem.runner.ErrorLevel;
import jsystem.treeui.WaitDialog;
import jsystem.treeui.dialog.DialogWithCheckBox;
import jsystem.treeui.error.ErrorPanel;
import jsystem.treeui.images.ImageCenter;
import jsystem.treeui.utilities.UnmodifiableFileHandler;

public class SaveScenarioAction extends IgnisAction implements ScenarioListener {

	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(SaveScenarioAction.class.getName());

	private static SaveScenarioAction action;

	public SaveScenarioAction() {
		super();
		putValue(Action.NAME, JsystemMapping.getInstance().getSaveScenarioButton());
		putValue(Action.SHORT_DESCRIPTION, JsystemMapping.getInstance().getSaveScenarioButton());
		putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		putValue(Action.ACTION_COMMAND_KEY, "save-scenario");
		putValue(Action.SMALL_ICON, ImageCenter.getInstance().getImage(ImageCenter.ICON_SAVE_SCENARIO));
		putValue(Action.LARGE_ICON_KEY, ImageCenter.getInstance().getImage(ImageCenter.ICON_SAVE_SCENARIO));
		setEnabled(false);
	}

	public static SaveScenarioAction getInstance() {
		if (action == null) {
			action = new SaveScenarioAction();
			ListenerstManager.getInstance().addListener(action);
		}
		return action;
	}

	public int saveCurrentScenarioWithConfirmation() throws Exception {
		int ans = JOptionPane.YES_OPTION;
		if (ScenariosManager.isDirty()) {
			if (ScenariosManager.getInstance().getCurrentScenario().canWrite()) {
				ans = DialogWithCheckBox.showConfirmDialog("You have unsaved changes",
						"Do you want to save the changes?", "In the future, don't show this dialog and Auto-save",
						FrameworkOptions.AUTO_SAVE_NO_CONFIRMATION);
				if (ans != JOptionPane.YES_OPTION) {
					return ans;
				}
			}
		} else {
			return ans;
		}
		actionPerformed(null);
		return ans;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (!UnmodifiableFileHandler.getInstance().makeWritable(
				ScenariosManager.getInstance().getCurrentScenario().getScenarioFiles())) {
			// Failed to set file permissions to writable or user canceled
			// operation.
			return;
		}
		WaitDialog.launchWaitDialog("Saving Scenario", null);
		try {
			saveCurrentScenario();
		} catch (Exception ex) {
			ErrorPanel.showErrorDialog("Save Scenario", ex, ErrorLevel.Error);
		}
		log.fine("Save Scenario swing worker - closing waitDialog");
		WaitDialog.endWaitDialog();
	}

	public void saveCurrentScenario() throws Exception {

		ScenariosManager.setDirtyStateEventsSilent(true);
		Scenario s = ScenariosManager.getInstance().getCurrentScenario();
		s.save();
		ScenariosManager.setDirtyStateEventsSilent(false);
		ScenariosManager.resetDirty();
	}

	class SaveScenario extends SwingWorker<String, Object> {
		public String doInBackground() {
			try {
				saveCurrentScenario();
			} catch (Exception e) {
				ErrorPanel.showErrorDialog("Save Scenario", e, ErrorLevel.Error);
			}
			return "";
		}

		protected void done() {
			log.fine("Save Scenario swing worker - closing waitDialog");
			WaitDialog.endWaitDialog();
		}
	}

	@Override
	public void scenarioChanged(Scenario current, ScenarioChangeType type) {
		// TODO Auto-generated method stub

	}

	@Override
	public void scenarioDirectoryChanged(File directory) {
		// TODO Auto-generated method stub

	}

	@Override
	public void scenarioDirtyStateChanged(Scenario s, boolean isDirty) {
		setEnabled(isDirty);
	}

	@Override
	public void testParametersChanged(String testIIUUD, Parameter[] oldValues, Parameter[] newValues) {
		// TODO Auto-generated method stub

	}
}

@@ -119,583 +119,129 @@
 rt j
-ava.awt.event.ActionEvent;%0Aimport java.awt.event.KeyEvent;%0Aimport java.io.File;%0Aimport java.util.logging.Logger;%0A%0Aimport javax.swing.Action;%0Aimport javax.swing.JOptionPane;%0Aimport javax.swing.KeyStroke;%0Aimport javax.swing.SwingWorker;%0A%0Aimport jsystem.framework.FrameworkOptions;%0Aimport jsystem.framework.report.ListenerstManager;%0Aimport jsystem.framework.scenario.Parameter;%0Aimport jsystem.framework.scenario.Scenario;%0Aimport jsystem.framework.scenario.ScenarioChangeType;%0Aimport jsystem.framework.scenario.ScenarioListener;%0Aimport jsystem.framework.scenario.ScenariosManager
+system.framework.FrameworkOptions;%0Aimport jsystem.framework.report.ListenerstManager;%0Aimport jsystem.framework.scenario.*
 ;%0Aim
@@ -537,16 +537,160 @@
 ndler;%0A%0A
+import javax.swing.*;%0Aimport java.awt.event.ActionEvent;%0Aimport java.awt.event.KeyEvent;%0Aimport java.io.File;%0Aimport java.util.logging.Logger;%0A%0A
 public c
@@ -2362,26 +2362,262 @@
 nEvent e) %7B%0A
-%09%09
+        //Fire property change event with the name of the Action class%0A        //In order to get this event you need to register to PropertyChangeListener.%0A        firePropertyChange(action.getClass().getSimpleName(),null,null);%0A%0A        
 if (!Unmodif

Add SaveScenarioAction event using "firePropertyChange" in the actionPerformed method.