// (c) MIT 2003.  All rights reserved.

package rec.fsm;



import java.awt.*;
import java.util.*;
import rec.core.*;
import rec.sys.*;
import rec.util.*;





public class NodeAligner extends Aligner
{

  public NodeAligner()
  {
  }
  
  /**
   * This looks at a new node and tries to snap any available edge
   * objects to it.
   */
  public boolean recognize( Surface s, Widget t )
  {
    //    trans = new MultiTransition( s );

    //Console.writeln( "Aligning node " + t );
    Console.indent();

    // return transition only if edges were snapped into place.
    boolean ignore = alignNode( s, (Node) t );

    Console.outdent();
    return false;
  }
  
  public Iterator getInputTypes()
  {
    Vector v = new Vector();
    
    v.addElement( "rec.fsm.Node" );
    
    return v.iterator();
  }
  

  // what if you don't have an output type?
  public Iterator getOutputTypes( )
  {
    return getInputTypes();
  }


  /**
   * This actually just snaps any near edges onto the node.  The
   * node is not much changed, except for getting pointers to the
   * edges which get snapped to it.
   */
  public boolean alignNode( Surface b, Node node )
  {
    boolean changed = false;
    int counter = 0;

    Iterator e = b.getWidgetsIterator();

    // hack
    Vector tmp = new Vector();
    while( e.hasNext() )
    {
      Object o = e.next();
      if ( o instanceof rec.fsm.Edge )
	tmp.addElement( o );
    }
    e = tmp.iterator();

    while( e.hasNext() )
      {
	Object o = e.next();
	if ( o instanceof rec.fsm.Edge )
	  {
	    //Console.writeln( "examining edge #" + ++counter + ": " + o );
	    Edge edge = (Edge)o;

	    changed = snapEdgeToNode( b, edge, node ) || changed;
	  }
      }

    return changed;
  }


}
