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

// $Id: DisinheritTransition.java,v 1.3 2003/03/06 00:40:40 moltmans Exp $
// Time-stamp: <2000-03-11 18:59:36 moltmans>
package rec.sys;


import java.util.*;

public class DisinheritTransition extends DoTransition
{
  private Collection m_Parents;
  private Collection m_Children;

  /**
   * Take the children, then the parents.
   */
  DisinheritTransition( Surface s, FamilyMember f, Widget p )
  {
    super( s );
    m_Parents = Collections.singleton( p );
    m_Children = Collections.singleton( f );
  }

  DisinheritTransition( Surface s, Widget child, Collection p )
  {
    super( s );
    m_Parents = p;
    m_Children = Collections.singleton( child );
  }
  
  DisinheritTransition( Surface s, Collection children, Collection p )
  {
    super( s );
    m_Parents = p;
    m_Children = children;
  }
  
  public void doDo()
  {
    Iterator it = m_Parents.iterator();
    while( it.hasNext() )
      {
	Widget parent = (Widget)it.next();

	Iterator it2 = m_Children.iterator();
	while ( it2.hasNext() )
	  {
	    FamilyMember child = (FamilyMember)it2.next();
	    
	    parent.removeChild( child );
	    if ( child instanceof Widget ) {
	      ((Widget)child).removeParent( parent );
	    }
	    else {
	      Iterator it3 = child.getChildrenIterator();
	      while ( it3.hasNext() ) {
		((Widget)it3.next()).removeParent( parent );
	      }
	    }
	  }
      }
  }

  public void doUndo()
  {
    Iterator it = m_Parents.iterator();
    while( it.hasNext() )
      {
	Widget parent = (Widget)it.next();

	Iterator it2 = m_Children.iterator();
	while ( it2.hasNext() )
	  {
	    FamilyMember child = (FamilyMember)it2.next();
	    parent.addChild( child );
    
	    if ( child instanceof Widget ) {
	      ((Widget)child).addParent( parent );
	    }
	    else {
	      Iterator it3 = child.getChildrenIterator();
	      while ( it3.hasNext() ) {
		((Widget)it3.next()).addParent( parent );
	      }
	    }
	  }
      }
  }

  public String getAction()
  {
    return  "breaks the connection between " + m_Children + " and "
      + m_Parents;
  }
  

}
