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

// $Id: DeleteGroupRecognizer.java,v 1.7 2003/03/06 00:40:48 moltmans Exp $
// Time-stamp: <2000-04-04 20:19:48 moltmans>
package rec.core.recognizers;



////////////////////////////////////////////////////////////////////////////////
//
// FILE:	DeleteGroupRecognizer.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	October, 1998
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:	Drawing recognition
//
// HISTORY:     October, 1998
//              MDM
//              adapted from ModifyModeRecognizer
//
///////////////////////////////////////////////////////////////////////////////

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

public class DeleteGroupRecognizer extends TargetRecognizer
{

  public boolean recognize( Surface s, Widget w )
  {
    // make this the general delete method
    WSelection sel = (WSelection)getTarget();
    WGroup group = (WGroup)sel.getWidget();
    Rectangle bounds = group.getBounds();
    WLine l = (WLine)w;

    // check if the delete stroke crosses either axis of the selection
    // this turns out to be a good heuristic for dealing with a
    // variety of object shapes (including lines).
    GSegment vert =
      new GSegment( new GPoint( bounds.x + bounds.width / 2.0,
				bounds.y),
		    new GPoint( bounds.x + bounds.width / 2.0,
				bounds.y + bounds.height ) );
    GSegment horiz =
      new GSegment( new GPoint( bounds.x,
				bounds.y + bounds.height / 2.0),
		    new GPoint( bounds.x + bounds.width,
				bounds.y + bounds.height / 2.0 ) );

    if ( (vert.getIntersectionOnSegment( l.getLine() ) == null) &&
	 (horiz.getIntersectionOnSegment( l.getLine() ) == null) )
    {
      return false;
    }
    
//      if ( bounds.contains( l.getLocation().getCenter().getIntX(),
//    			  l.getLocation().getCenter().getIntY() ) )
//	 &&
//  	 !bounds.contains( l.getHead().getIntX(), l.getHead().getIntY() ) &&
//  	 !bounds.contains( l.getTail().getIntX(), l.getTail().getIntY() ) )


    WDeletion dl = new WDeletion( (WGroup)sel.getWidget() );
    s.replaceWidget( w, dl );
	
    return true;
  }

  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.core.widgets.WDeletion" );

    return( v.iterator() );
  }
}

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