package org.cytoscape.ding.impl.cyannotator.listeners;

import org.cytoscape.ding.impl.cyannotator.CyAnnotator;
import org.cytoscape.ding.impl.cyannotator.api.Annotation;
import org.cytoscape.ding.impl.cyannotator.api.ShapeAnnotation;

import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import java.util.Set;

import org.cytoscape.ding.impl.DGraphView;
import org.cytoscape.ding.impl.InnerCanvas;

public class CanvasKeyListener implements KeyListener {
	private final CyAnnotator cyAnnotator;
	private final InnerCanvas networkCanvas;
	private final DGraphView view;

	public CanvasKeyListener(CyAnnotator c, DGraphView view) {
		this.cyAnnotator = c;
		this.view = view;
		this.networkCanvas = view.getCanvas();
	}

	public void keyPressed(KeyEvent e) {
		int code = e.getKeyCode();
		Set<Annotation> selectedAnnotations = cyAnnotator.getSelectedAnnotations();

		if ((selectedAnnotations != null && selectedAnnotations.size() > 0) &&
		    ((code == KeyEvent.VK_UP) || 
		    (code == KeyEvent.VK_DOWN) || 
		    (code == KeyEvent.VK_LEFT)|| 
		    (code == KeyEvent.VK_RIGHT)))
		{
			//Some annotations have been double clicked and selected
			int move=2;
			for (Annotation annotation: selectedAnnotations) {
				Component c = annotation.getComponent();
				int x=c.getX(), y=c.getY();
				int shiftMask = e.getModifiers() & KeyEvent.SHIFT_DOWN_MASK;
				if (annotation instanceof ShapeAnnotation && 
				    (shiftMask == KeyEvent.SHIFT_DOWN_MASK)) {
					ShapeAnnotation sa = (ShapeAnnotation)annotation;
					int width = c.getWidth(), height = c.getHeight();
					if (code == KeyEvent.VK_UP) {
						y-=move; height += move;
					} else if (code == KeyEvent.VK_DOWN) {
						height += move;
					} else if (code == KeyEvent.VK_LEFT) {
						x-=move; width += move;
					} else if (code == KeyEvent.VK_RIGHT) {
						width += move;
					}

					//Adjust the locations of the selected annotations
					sa.getComponent().setLocation(x,y);
					// Adjust the size of the selected annotations
					sa.setSize((double)width, (double)height);
				} else {
					if (code == KeyEvent.VK_UP)
						y-=move;
					else if (code == KeyEvent.VK_DOWN)
						y+=move;
					else if (code == KeyEvent.VK_LEFT)
						x-=move;
					else if (code == KeyEvent.VK_RIGHT)
						x+=move;

					//Adjust the locations of the selected annotations
					annotation.getComponent().setLocation(x,y);
				}
				annotation.getCanvas().repaint();	
			}
		} else {
			networkCanvas.keyPressed(e);
		}
	}

	public void keyReleased(KeyEvent e) { }

	public void keyTyped(KeyEvent e) { }
}

@@ -1447,55 +1447,22 @@
  && 
-%0A%09%09%09%09    (shiftMask == KeyEvent.SHIFT_DOWN_MASK
+e.isShiftDown(
 )) %7B
@@ -1617,25 +1617,16 @@
 %09%09%09%09
-y-=move; 
 height 
-+
+-
 = mo
@@ -1749,24 +1749,15 @@
 %09%09%09%09
-x-=move; 
 width 
-+
+-
 = mo
@@ -1830,112 +1830,14 @@
 ve;%0A
+
 %09%09%09%09%09%7D
-%0A%0A%09%09%09%09%09//Adjust the locations of the selected annotations%0A%09%09%09%09%09sa.getComponent().setLocation(x,y);
 %0A%09%09%09

Fix CanvasKeyListener resize option.