//----------------------------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.jgraph.event;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

import org.huihoo.god4j.jgraph.JGraphActivity;
import org.huihoo.god4j.jgraph.XPDLJGraph;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class XPDLJGraphKeyListener extends KeyAdapter
{
	protected XPDLJGraph graph;
	public XPDLJGraphKeyListener(XPDLJGraph graph)
	{
		this.graph = graph;
	}
	public void keyPressed(KeyEvent evt)
	{
		switch (evt.getKeyCode())
		{
			case KeyEvent.VK_DELETE :
			graph.graphDelete();
				break;
				
			case KeyEvent.VK_UP :
				moveUp();
				break;
			case KeyEvent.VK_DOWN :
				moveDown();
				break;
			case KeyEvent.VK_LEFT :
				moveLeft();
				break;
			case KeyEvent.VK_RIGHT :
				moveRight();
				break;
		}
	}
	protected void moveUp()
	{	
		Object cell = graph.getSelectionCell();
		if (cell == null)
		{
			return;
		}
		if (cell instanceof JGraphActivity)
		{
			DefaultGraphCell tmp = (DefaultGraphCell) cell;
			Rectangle rect = GraphConstants.getBounds(tmp.getAttributes()).getBounds();
			Rectangle newRect =new Rectangle(rect.x, rect.y-1, rect.width, rect.height);
			Map attributeMap = new HashMap();
			Map map = new HashMap();
			GraphConstants.setBounds(map, newRect);
			attributeMap.put(tmp, map);
			graph.getModel().edit(attributeMap, null, null, null);
		}
	}
	protected void moveDown()
	{
		Object cell = graph.getSelectionCell();
		if (cell == null)
		{
			return;
		}
		if (cell instanceof JGraphActivity)
		{
			DefaultGraphCell tmp = (DefaultGraphCell) cell;
			Rectangle rect = GraphConstants.getBounds(tmp.getAttributes()).getBounds();
			Rectangle newRect =new Rectangle(rect.x, rect.y+1, rect.width, rect.height);
			Map attributeMap = new HashMap();
			Map map = new HashMap();
			GraphConstants.setBounds(map, newRect);
			attributeMap.put(tmp, map);
			graph.getModel().edit(attributeMap, null, null, null);
		}
	}
	protected void moveRight()
	{
		Object cell = graph.getSelectionCell();
				
		if (cell == null)
		{
			return;
		}
		if (cell instanceof JGraphActivity)
		{			
			DefaultGraphCell tmp = (DefaultGraphCell) cell;
			Rectangle rect = GraphConstants.getBounds(tmp.getAttributes()).getBounds();
			Rectangle newRect =new Rectangle(rect.x + 1, rect.y, rect.width, rect.height);
			Map attributeMap = new HashMap();
			Map map = new HashMap();
			GraphConstants.setBounds(map, newRect);
			attributeMap.put(tmp, map);
			graph.getModel().edit(attributeMap, null, null, null);
		}
	}
	protected void moveLeft()
	{
		Object cell = graph.getSelectionCell();
		if (cell == null)
		{
			return;
		}
		if (cell instanceof JGraphActivity)
		{
			DefaultGraphCell tmp = (DefaultGraphCell) cell;
			Rectangle rect = GraphConstants.getBounds(tmp.getAttributes()).getBounds();
			Rectangle newRect =new Rectangle(rect.x - 1, rect.y, rect.width, rect.height);
			Map attributeMap = new HashMap();
			Map map = new HashMap();
			GraphConstants.setBounds(map, newRect);
			attributeMap.put(tmp, map);
			graph.getModel().edit(attributeMap, null, null, null);
		}
	}
}
