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

// $Id: ShallowPoolBuilder.java,v 1.5 2003/03/06 00:40:48 moltmans Exp $
// Time-stamp: <2000-03-11 18:54:45 moltmans>
package rec.core;



import java.io.*;
import java.util.*;
import rec.util.*;
import rec.sys.*;
import rec.geo.*;

public class ShallowPoolBuilder extends PoolBuilder 
{
  public ShallowPoolBuilder()
  {
    super();
  }


  /**
   * Called by Surface on each relevant Recognizer.
   *
   * @param w   The last thing added to the Surface, the root Widget
   *     which caused recognition to begin again.
   *
   * 
   */
  protected Collection findRelevantWidgets( Surface s, WidgetPool wp, 
					    Widget sw )
  {
    Collection relevant = new HashSet();
    
    relevant.addAll( s.getSurfacePool().getWidgets() );
    
    Iterator it = s.getSurfacePool().iterator();
    while( it.hasNext() ) {
      Widget w = (Widget)it.next();
      
      Collection parents = w.getParents();
      Collection children = w.getChildren();
      
      while ( parents.size() == 1 ) {
	Widget next_p = (Widget)parents.iterator().next();
	if ( wp.contains( next_p ) ) {
	  relevant.add( next_p );
	}
	parents = next_p.getParents();
      }
      
      while ( children.size() == 1) {
	if ( children.iterator().next() instanceof WidgetSet ) {
	  children =
	    ((WidgetSet)children.iterator().next()).getChildren();
	}
	else {
	  Widget next_c = (Widget)children.iterator().next();
	  if ( wp.contains( next_c ) ) {
	    relevant.add( next_c );
	  }
	  children = next_c.getChildren();
	}
      }
    }
    return relevant;
  }

}
