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

package rec.house;


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

public abstract class Tracker extends Recognizer implements Registerable
{
  Surface surface;
  HashSet tracked;

  public Tracker()
  {
    tracked = new HashSet();
    surface = null;
  }
  
  public boolean recognize( Surface s, Widget w )
  {
    setSurface( s );

    track( w );

    return false;
  }

  public void setSurface( Surface s )
  {
    this.surface = s;
  }

  public void track( Widget w )
  {
    if ( !surface.isRegistered( this, w ) )
      {
	surface.register( this, w );
	tracked.add( w );
      }
  }

  public void forget( Widget w )
  {
    surface.unregister( this, w );
    tracked.remove( w );
  }

  public boolean knows( Widget w )
  {
    boolean b = surface.isRegistered( this, w );

    Assert.check( b == ( tracked.contains( w ) ),
		  "break in registry and track set." );

    return b;
  }


  public Iterator getTracked( )
  {
    return tracked.iterator();
  }


  public void removed( Widget widget )
  {
    tracked.remove( widget );
  }

  public void replaced( Widget ow, Collection nws )  
  {
    tracked.remove( ow );
    Iterator e = nws.iterator();
    while ( e.hasNext() )
      tracked.add( e.next() );
  }

  public void replaced( Widget ow, Widget nw )
  {
    tracked.remove( ow );
    tracked.add( nw );
  }

  public void altered( Widget widget, Transition t )
  {

  }

  public Iterator getOutputTypes() 
  {
    return getInputTypes();
  }

} // end Tracker
