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

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



import rec.util.*;
import java.util.*;

public class SeverTransition extends DoTransition
{
  private Collection m_Widgets;
  private Vector m_Transitions;

  /**
   * @param c the set of widgets we want to isolate
   */
  public SeverTransition( Surface s, Collection c )
  {
    super( s );
    m_Widgets = c;
    m_Transitions = new Vector();
  }


  /**
   * perform the transition 
   */
  public void doDo() 
  {
    Iterator it = m_Widgets.iterator();
    while ( it.hasNext() ) {
      Widget w = (Widget)it.next();

      Vector v = new Vector();
      
      Iterator p = w.getParentsIterator();
      while ( p.hasNext() ) {
	Widget pw = (Widget)p.next();
	if ( !m_Widgets.contains( pw ) ) {
	  v.addElement( new DisinheritTransition( m_Surface, w, pw ) );
	}
      }

      Iterator c = w.getChildrenIterator();
      while ( c.hasNext() ) {
        FamilyMember cw = (FamilyMember)c.next();
	if ( cw instanceof Widget ) {
	  if ( !m_Widgets.contains( cw ) ) {
	    v.addElement( new DisinheritTransition( m_Surface, cw, w ) );
	  }
	}
	else {
	  v.addElement( new DisinheritTransition( m_Surface, cw, w ) );
	}
      }

      if ( w.inLitter() ) {
	v.addElement( new RemoveLitterTransition( m_Surface, w ) );
      }

      Iterator trans_it = v.iterator();
      while ( trans_it.hasNext() ) {
	((DoTransition)trans_it.next()).doDo();
      }

      m_Transitions.addAll( v );

    } 
      
  }

  /**
   * undoes the transition 
   */
  public void doUndo() 
  {
    ListIterator li = m_Transitions.listIterator( m_Transitions.size() );
    while ( li.hasPrevious() ) {
      ((DoTransition)li.previous()).doUndo();
    } 
  }

  /**
   * get the effects of the action without performing the do 
   */
  // for now this is just a string, soon it will be an action object
  // that can be more easily reasoned about.
  public String getAction()
  {
    return( "severs widgets in set from rest of widgets" );
  }

}

