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

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

import java.util.Vector;
import java.util.Enumeration;

import javax.swing.tree.DefaultMutableTreeNode;

public class ActionCloseallBeh extends Action
{
	DefaultMutableTreeNode _root;
	public ActionCloseallBeh(Editor e,DefaultMutableTreeNode root)
	{
		super(e);_root=root;
	}

	public String name() { return "Close All Behaviors"; }

	public void doIt() 
	{
		// If there are no behaviors opened in the strategy panel return. Should never happen
		if(_root.getChildCount()<1)
			return ;

		// separate into modified and non-modified nodes.
		Enumeration enum = _root.children();
		Vector vClose = new Vector();
		Vector vSaveNClose = new Vector();
		while(enum.hasMoreElements())
		{
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)enum.nextElement();
			if(((BehaviorNode)node.getUserObject()).needsSaving())
				vSaveNClose.addElement(node);
			else
				vClose.addElement(node);
		}
		// Non-modified behaviors are closed iteratively.
		int j=vClose.size();
		for(int i=0;i<j;i++)
		{
			ActionCloseBeh act= new ActionCloseBeh(_editor,(DefaultMutableTreeNode)vClose.elementAt(i));
			act.doIt();
		}

// Files those need saving are passed to ActionCloseMultiple to handle
		ActionCloseMultipleBehaviors act = new ActionCloseMultipleBehaviors(_editor,vSaveNClose); 
		act.doIt(); // This can terminate prematurely because user clicked cancel sometime during saving.
	}

	public void undoIt() { }

} /* end class ActionCloseallBeh */

