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

// $Id: SimPolyRecognizer.java,v 1.6 2003/03/06 00:40:41 moltmans Exp $
// Time-stamp: <2000-03-11 18:59:37 moltmans>
package rec.simulate.recognizers;


import java.util.*;
import java.awt.*;
import rec.util.*;
import rec.sys.*;
import rec.core.widgets.*;
import rec.core.recognizers.*;
import rec.simulate.widgets.*;
import rec.simulate.*;

// This is code written by Luke Weisman, with small modifications
// by Christine Alvarado

public class SimPolyRecognizer extends Recognizer
{
  public final static double BREAK = 20.0;

  public SimPolyRecognizer()
  {
  }

  /////////////////////////////////////////////////////////////////////////////

  public boolean recognize( Surface s, Widget w )
  {
    WLine l = (WLine)w;

    Console.writeln( "SimPoly recognizer called" );
    // build up list of all possible lines.
    Iterator e = s.getWidgetsIterator();
    HashSet line_pool = new HashSet();
    while( e.hasNext() )
      {
	Object o = e.next();
	if ( o instanceof WLine )
	{
	  line_pool.add( o );
	}
      }


    PolygonInfo pi = null;
    if( ((pi = PolygonParser.parse( line_pool, l )) != null) ) {
      
      Console.writeln( "Sim Polygon online" );
      
      WSimPolygon q = ((SimulateSurface)s).makeSimPolygon( pi );
      
      // remove the lines
      for( int i = 0; i < pi.getNumberOfSides(); i++ ) {
	s.removeWidget( pi.getLineAt( i ) );
      }
      s.addWidget( q );

      return( true );
    }

    return( false );
  }

  public Iterator getInputTypes()
  {
    Vector v = new Vector();

    v.addElement( "rec.core.widgets.WLine" );

    return( v.iterator() );
  }

  public Iterator getOutputTypes()
  {
    Vector v = new Vector();

    v.addElement( "rec.simulate.widgets.WSimPolygon" );

    return( v.iterator() );
  }
}

////////////////////////////////////////////////////////////////////////////////
