// File: ActionCloseBeh.java
// Classes: ActionCloseBeh
// Author: Kedar Patankar

package edu.neu.ccs.demeter.tools.apstudio.graphedit;

import javax.swing.tree.DefaultMutableTreeNode;

public class ActionCloseBeh extends Action
{
// executeit resets this and doIt sets it if any error/cancel occurs during its course of execution	
	private boolean _userCancelled; 
	private DefaultMutableTreeNode _behaviorNode;

	public ActionCloseBeh(Editor editor,DefaultMutableTreeNode node) {super(editor);_behaviorNode= node;}
	
	public String name() { return "Close Behavior File"; }

	public boolean executeIt()
	{
		_userCancelled=false;
		doIt();
		return !_userCancelled;
	}

	public void doIt() 
	{
		BehaviorNode _behavior = (BehaviorNode)_behaviorNode.getUserObject();

		if(!_behavior.needsSaving())
			shutDown();
		else
		{	
			String title="Confirm File Close";
			String message1="Save changes to Behavior file?";
			String message2=_behavior.getName();
			String buttonString="YNC";
			AlarmDialog ad = new AlarmDialog
				(_editor,title,message1,message2,buttonString,"Y");
			ad.setVisible(true);

			String choice = ad.getChoice();

			if(choice.equals("YES"))
			{
//	public ActionSaveBeh(Editor editor,DefaultMutableTreeNode node) {super(editor);_behaviorNode= node;}
//				BehaviorNode behavior = (BehaviorNode) node.getUserObject();
//				if(!behavior.needsSaving()&&(!behavior.isFirstTime())) return;
				ActionSaveBeh act=new ActionSaveBeh(_editor,_behaviorNode);
				if(act.executeIt())
					shutDown();
				else
					_userCancelled = true;
			}
			else
			if(choice.equals("CANCEL"))
				_userCancelled = true;
			else // choice is "NO"
				shutDown();
		}
		
	}

	public void undoIt() { }

	private void shutDown()
	{
		_editor.detach(_behaviorNode);
	}

} /* end class ActionCloseBeh */

