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

package rec.fsm;

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

public class FSMSelectionLoopRecognizer extends SelectionLoopRecognizer
{
  public FSMSelectionLoopRecognizer()
  {
  }

  protected WGroup findGroup( Surface s, Squiggle st )
  {
    GPolySegment poly = st.getPolygon();
    WGroup grp = null;

    Iterator e = s.getVisibleWidgets().iterator();
      while( e.hasNext() ) {

	VisibleWidget vw = (VisibleWidget)e.next();
	if ( vw.equals( st ) )
	  continue;

	if ( vw instanceof Edge )
	  {
	    if ( poly.contains( ((Edge)vw).getHead() )
		 && poly.contains( ((Edge)vw).getTail() ) )
	      {
		if ( grp == null ) grp = new WCircleGroup( st );
		grp.add( vw );
	      }
	  }
	else if( poly.contains( vw.getLocation().getCenter() ) ) {

	  if ( grp == null )
	    grp = new WCircleGroup( st );

	  grp.add( vw );
	}
      }
      
      if ( (grp == null) || ( grp.nsize() == 1 ) )
	return null;

      return grp;
  }

}

