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

package rec.house;

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

public class UndoableWingGraph extends WingGraph
{
  TransitionRecorder tr = null;
  Surface s = null;

  public UndoableWingGraph( Surface surf )
  {
    this.tr = surf.recorder();
    s = surf;
  }
  
  public void addPoint( Object p )
  {
    super.addPoint(p);
    recordPoint( p, true );
  }

  public void removePoint( Object p )
  {
    super.removePoint(p);
    recordPoint( p, false );
  }

  void recordPoint( Object p, boolean add )
  {
    final Object pf = p;
    Transition t = new FlipTransition( s ) {
      public void doUndo( )
	{
	  addPoint( pf );
	}
      public void doRedo( )
	{
	  removePoint( pf );
	}
      public String name()
	{
	  return "uwg-point[" + pf + "]";
	}
    };

    if ( add )
      tr.record( t.getRedoTransition() );
    else
      tr.record( t );
  }

   
  public void replaceEdge( Object old_edge, Object new_edge )
  {
    super.replaceEdge( old_edge, new_edge );

    final Object oe = old_edge;
    final Object ne = new_edge;

    tr.record( new FlipTransition( s ) {
      public void doUndo( )
	{
	  replaceEdge( ne, oe );
	}
      public void doRedo( )
	{
	  replaceEdge( oe, ne );
	}
      public String name()
	{
	  return "uwg-rep[" + oe + "/" + ne + "]";
	}
    } );
  }

  

  public void removeEdge( Object w )
  {
    Object h = getHead( w );
    Object t = getTail( w );

    super.removeEdge( w );
    
    recordEdge( w, h, t, false );
  }

  /**
   * Add edge with object 'edge' connecting objects 'head'
   * to 'tail'
   */
  public void addEdge( Object edge, Object head, Object tail )
  {
    super.addEdge( edge, head, tail );

    recordEdge( edge, head, tail, true );
  }


  void recordEdge( Object edge, Object head, Object tail, boolean add )
  {
    final Object pe = edge;
    final Object ph = head;
    final Object pt = tail;

    Transition t = new FlipTransition( s ) {
      public void doUndo( )
	{
	  addEdge( pe, ph, pt );
	}
      public void doRedo( )
	{
	  removeEdge( pe );
	}
      public String name()
	{
	  return "uwg-edge[" + pe + "]";
	}
    };

    if ( add )
      tr.record( t.getRedoTransition() );
    else
      tr.record( t );
  }

  
} // end UndoableWingGraph


































