//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/god4j
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : god4j@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.god4j.actions;
import java.awt.event.ActionEvent;
import java.util.List;

import org.huihoo.god4j.GOD4JFactory;
import org.huihoo.god4j.JGraphFrame;
import org.huihoo.god4j.jgraph.XPDLJGraph;
import org.huihoo.god4j.jgraph.XPDLJGraphModel;
import org.huihoo.god4j.jgraph.event.XPDLJGraphUndoManager;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class Redo extends ActionBase
{
	public Redo()
	{
		super();
		setEnabled(false);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		JGraphFrame  graphFrame=GOD4JFactory.getGOD4J().getControlPanel().getSelectedJGraph();
		if(graphFrame ==null)
		{
			return;
		}
		
		XPDLJGraph jGraph=graphFrame.getJGraph();
		XPDLJGraphUndoManager undoMgr=jGraph.getUndoManager();
		
		List oldList = ((XPDLJGraphModel) jGraph.getModel()).getChildren();
		
		if(undoMgr.canRedo(jGraph.getGraphLayoutCache()))
		{
			undoMgr.redo(jGraph.getGraphLayoutCache());
		}
		
		List newList = ((XPDLJGraphModel)jGraph.getModel()).getChildren();
		GOD4JFactory.getGOD4J().refreshCollections(oldList, newList);
		GOD4JFactory.getGOD4J().getActionMap().getAction("paste").setEnabled(false);
		GOD4JFactory.getGOD4J().setDirty(true);
		update();
		GOD4JFactory.getGOD4J().getActionMap().getAction("undo").update();
		GOD4JFactory.getGOD4J().getControlPanel().getSelectedJGraph().getJGraph().repaint();
	}
	
	public void update()
	{
		setEnabled(false);
		
		JGraphFrame  graphFrame=GOD4JFactory.getGOD4J().getControlPanel().getSelectedJGraph();
		if(graphFrame ==null)
		{
			return;
		}
		
		XPDLJGraph jGraph=graphFrame.getJGraph();
		XPDLJGraphUndoManager undoMgr=jGraph.getUndoManager();
		
		if (undoMgr.canRedo(jGraph.getGraphLayoutCache()))
		{
			setEnabled(true);
		}
	}
}