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

// -*-Mode: Java-*- 
// $Id: WCommand.java,v 1.3 2003/03/06 00:40:47 moltmans Exp $
// WCommand.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Fri Feb 25 19:12:10 2000> 
// Time-stamp: <2000-02-26 18:12:49 moltmans> 
package rec.core.widgets;

import java.util.*;

import rec.sys.*;
import rec.core.*;
import rec.util.*;

/**
 * This is a convenient wrapper for making simple commands.  It can be
 * undone but not redone.  And it cleans itself up by adding a
 * dieTransition to the suitcase.
 *
 * Just implement:
 *   performAction that will be called every time the widget is elected.
 *   performUndo   will get called to undo the performAction
 **/
public class WCommand extends Widget
{
  private Command m_parent;
  private Object m_arg;
  private boolean m_isUndone = false;

  public WCommand( Command parent )
  {
    this( parent, null );
  }

  public WCommand( Command parent, Object arg )
  {
    m_parent = parent;
    m_arg = arg;
  }

  public Object getArg() 
  {
    return m_arg;
  }

  public void makeDefaultSuitcase( Surface s )
  {
    DoTransition dt = new DoTransition(s) {
      public void doDo( )
      {
	if ( ! m_isUndone ) {
	  m_parent.performAction( m_Surface, m_arg );
	}
      }

      public void doUndo( )
      {
	if ( ! m_isUndone ) {
	  m_parent.performUndo( m_Surface, m_arg );
	  m_isUndone = true;
	}
      }

      public String getAction( )
      {
	return "Run a command";
      }
    };
    
    addToSuitcase( dt );
    addToSuitcase( new DieTransition( s, this) );
  }

}

