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

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

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

import javax.swing.tree.DefaultMutableTreeNode;

/* Purpose : Representation of behavior files */

public class BehaviorNode implements Behdata
{
	private String _name;
	private EditorMenus _menu;
	private Vector _graphNames;
	private boolean _firstTime;

	private boolean _needsSaving; 

	public BehaviorNode(String name,EditorMenus menu)
	{
		_name =name;
		_menu= menu;
		_graphNames = new Vector();
		_needsSaving = false;
		_firstTime=true;
	}

	public boolean needsSaving()
	{
		return _needsSaving;
	}

	public void updateName(String oldname, String newname)
	{
		_name = newname;
	}
	public void isFirstTime(boolean status)
	{
		_firstTime = status;
	}
	public boolean isFirstTime()
	{
		return _firstTime;
	}
	public boolean needsSaving(boolean needsaving)
	{
		if(_needsSaving == needsaving)
			return false; // indicates that status didn't change
		_needsSaving = needsaving;
		return true;  // indicates status changed
	}
	
	public String toString(){if(_needsSaving) return _name+" *"; return _name;}
	public void setName(String name){_name = name;}
	public String getName(){return _name;}

	public void setMenu(){_menu.setMenuforBehavior();}

	public String getDescription(){return "Behavior file doesn't contain any strategies";}

	public String nextGraphName()
	{
		int count =0;
		String trialName=new String();

		for(boolean found = true;found;)
		{
			count++;
			trialName = "SGraph_"+count;
			found = _graphNames.contains(trialName);
		}
		_graphNames.addElement(trialName);
		return trialName;
	}
	public Vector getGraphNames(){return _graphNames;}
	public void changeGraphName(String oldname,String newname)
	{
		_graphNames.removeElement(oldname);
		_graphNames.addElement(newname);
	}
	public void removeSGraphName(String name)
	{
		_graphNames.removeElement(name);
	}

	public String getString(DefaultMutableTreeNode node)
	{
		if( ! (node.getUserObject() instanceof BehaviorNode) ) // should never happen
			return null;
		// If the graph is empty return blank string
		if(node.getChildCount()<1)
			return new String("\n");

		// If one of the strategy graphs can't be saved return null
		Enumeration enum = node.children();
		String stratGraphString=new String();
		while(enum.hasMoreElements())
		{
			DefaultMutableTreeNode graphNode = (DefaultMutableTreeNode)enum.nextElement();
			SGraphNode graph = (SGraphNode)graphNode.getUserObject();

			String stratDefinition = graph.getString(graphNode);
			if(stratDefinition == null)
				return null;
			stratGraphString = stratGraphString+"strategy "+graph.toString()+" = "+stratDefinition+" .\n";
		}
		return stratGraphString;
	}

} /* end class BehaviorNode */

