/*
 * Created on 10.12.2003 17:03:44
 *
 * Multimediale Algorithmen und Datenstrukturen Assessments
 */
package mauda.undoredo;

import mauda.Exercise;
//import mauda.MAUDA;
import mauda.operation.*;
import mauda.plugin.*;

import jedas.*;
/**
 * UndoRedo which animates redo-actions
 * 
 * @author Markus Krebs
 */
public class AnimatedUndoRedo extends UndoRedo {

	/**
	 * Creates a UndoRedo-Handler.
	 * @param exercise Exercise
	 */
	public AnimatedUndoRedo(Exercise exercise) {
		super(exercise);
	}
	// Snapshot des CompPanel's machen
	protected void snapshot(Object o) {
		// Testen ob gleiche Operation gemacht wird
		// siehe auch OperationRecorder
		SimpleOperation op = (SimpleOperation)o;
		if(op!=null) {
			SimpleOperation undoRedoOp = (SimpleOperation)exercise.getOperationRecorder().getNextOperation();
			//System.out.println("UndoRedo: op = "+op+" / undoRedoOp = "+undoRedoOp);
			if(undoRedoOp!=null&&undoRedoOp.equals(op)) {
				position++;
				return;
			} 
		}
		int counter = compPanel.getItemCount();
		//System.out.println("SNAPSHOT: #Items: "+counter);
		Item item = compPanel.getFirstItem();
		//System.out.println("SNAPSHOT-Item: "+item);
		
		// Das Richtige müsste immer das erste Item sein
		/*
		Item temp = compPanel.getFirstItem();;
		do {
			System.out.println("  - "+temp);
			//if(item instanceof FibHeapExt) break;	// Bekanntes Objekt gefunden
			temp = compPanel.getNextItem();
			counter--;			
		} while(counter>0);
		*/
		/*
		// Testen ob ein bekanntes Objekt gefunden wurde
		if(!(item instanceof FibHeapExt)) {
			javax.swing.JOptionPane.showMessageDialog(MAUDA.getApplicationMainFrame(), "UndoRedo.snapshot: No FibHeap found");
			return;
		}*/
		// Undo-Information:
		position++;
		undoRedoInfo.storeChange(position, vector, ((Copyable)exercise.getDSObject()).deepCopy());

		//System.out.println("UndoRedo: cutting");
		//while (vector.size() > position) vector.removeElementAt(position);
		//vector.add(((Copyable)exercise.getDSObject()).deepCopy());
	}
	/* (non-Javadoc)
	 * @see mauda.undoredo.UndoRedo#forward()
	 */
	protected void forward() {
		if(!canForward()) return;
		SimpleOperation op = (SimpleOperation)exercise.getOperationRecorder().getNextOperation();		
		exercise.commit((SimpleOperation)op.clone());
		return;
	}
}
