/**
 * A very simple operation used in an example of the Command pattern
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class Replace
{
    private boolean           applied;
    private String            newWord, oldWord, savedState;
    private TextDocument      operand;


    /**
     * Explicit Value Constructor
     *
     * @param document   The operand of this operation
     *
     * @param oldWord   The old word
     * @param newWord   The new word
     */
    public Replace(TextDocument  document, String oldWord, String newWord)
    {
	operand = document;
	applied = false;
	this.oldWord = oldWord;
	this.newWord = newWord;
    }



    /**
     * Apply this Replace operation
     */
    public void apply()
    {
	savedState = operand.getText();
	operand.replace(oldWord, newWord);
	applied = true;
    }


    /**
     * Undo this Replace operation
     */
    public void undo()
    {
	if (applied) 
        {
	    operand.setText(savedState);
	    applied = false;
	}
    }
}
