Bug 339525 - Enrich paste context with location information
diff --git a/examples/org.eclipse.graphiti.doc/resources/docu/gfw/copy-and-paste-feature.htm b/examples/org.eclipse.graphiti.doc/resources/docu/gfw/copy-and-paste-feature.htm
index 0432c76..6701425 100644
--- a/examples/org.eclipse.graphiti.doc/resources/docu/gfw/copy-and-paste-feature.htm
+++ b/examples/org.eclipse.graphiti.doc/resources/docu/gfw/copy-and-paste-feature.htm
@@ -171,9 +171,10 @@
 		// then create new pictogram elements using the add feature</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 		Object[] objects = getFromClipboard();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 		<span class="keyword">for </span>(Object object : objects) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
-		AddContext ac = <span class="keyword">new </span>AddContext();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
-		ac.setLocation(0, 0); <span class="comment">// for simplicity paste at (0, 
-		0)</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
+		AddContext ac = <span class="keyword">new </span>AddContext();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+		<span class="comment">// For simplicity paste all objects at the location given in the</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+		<span class="comment">// context (no stacking or similar)</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+		ac.setLocation(context.getX(), context.getY()); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 		ac.setTargetContainer(diagram);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 		addGraphicalRepresentation(ac, object);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 		}<br>&nbsp;&nbsp;&nbsp; }<br>}<br></p>
diff --git a/examples/org.eclipse.graphiti.examples.tutorial/src/org/eclipse/graphiti/examples/tutorial/features/TutorialPasteEClassFeature.java b/examples/org.eclipse.graphiti.examples.tutorial/src/org/eclipse/graphiti/examples/tutorial/features/TutorialPasteEClassFeature.java
index 3f74264..22aeb2e 100644
--- a/examples/org.eclipse.graphiti.examples.tutorial/src/org/eclipse/graphiti/examples/tutorial/features/TutorialPasteEClassFeature.java
+++ b/examples/org.eclipse.graphiti.examples.tutorial/src/org/eclipse/graphiti/examples/tutorial/features/TutorialPasteEClassFeature.java
@@ -1,7 +1,7 @@
 /*******************************************************************************
  * <copyright>
  *
- * Copyright (c) 2005, 2010 SAP AG.
+ * Copyright (c) 2005, 2011 SAP AG.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
  *
  * Contributors:
  *    SAP AG - initial API, implementation and documentation
+ *    mwenz - Bug 339525 - Enrich paste context with location information
  *
  * </copyright>
  *
@@ -61,7 +62,9 @@
 		Object[] objects = getFromClipboard();
 		for (Object object : objects) {
 			AddContext ac = new AddContext();
-			ac.setLocation(0, 0); // for simplicity paste at (0, 0)
+			// For simplicity paste all objects at the location given in the
+			// context (no stacking or similar)
+			ac.setLocation(context.getX(), context.getY());
 			ac.setTargetContainer(diagram);
 			addGraphicalRepresentation(ac, object);
 		}
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramEditorContextMenuProvider.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramEditorContextMenuProvider.java
index 5b41fdb..9b40e45 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramEditorContextMenuProvider.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/editor/DiagramEditorContextMenuProvider.java
@@ -1,7 +1,7 @@
 /*******************************************************************************
  * <copyright>
  *
- * Copyright (c) 2005, 2010 SAP AG.
+ * Copyright (c) 2005, 2011 SAP AG.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -11,6 +11,7 @@
  *    SAP AG - initial API, implementation and documentation
  *    mwenz - Bug 323155 - Check usage scenarios for DefaultPrintFeature and
  *            DefaultSaveImageFeature
+ *    mwenz - Bug 339525 - Enrich paste context with location information
  *
  * </copyright>
  *
@@ -43,6 +44,7 @@
 import org.eclipse.graphiti.ui.internal.action.CustomAction;
 import org.eclipse.graphiti.ui.internal.action.DeleteAction;
 import org.eclipse.graphiti.ui.internal.action.IAvailable;
+import org.eclipse.graphiti.ui.internal.action.PasteAction;
 import org.eclipse.graphiti.ui.internal.action.RemoveAction;
 import org.eclipse.graphiti.ui.internal.action.SaveImageAction;
 import org.eclipse.graphiti.ui.internal.action.UpdateAction;
@@ -379,6 +381,12 @@
 					UpdateAction updateAction = (UpdateAction) action;
 					updateAction.setEnabled(updateAction.isEnabled());
 
+				} else if (action instanceof PasteAction) {
+					// For the paste action we need the infomation at which
+					// location the paste operation was triggered. This
+					// infomation can only be retrieved from the mouse location
+					// at the time the context menu is created
+					((PasteAction) action).setPasteLocation(getEditor().getMouseLocation());
 				}
 			}
 		}
@@ -405,4 +413,4 @@
 	private DiagramEditorInternal getEditor() {
 		return (DiagramEditorInternal) getDiagramTypeProvider().getDiagramEditor();
 	}
-}
\ No newline at end of file
+}
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/action/PasteAction.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/action/PasteAction.java
index 119f2da..aebfebb 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/action/PasteAction.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/action/PasteAction.java
@@ -1,7 +1,7 @@
 /*******************************************************************************
  * <copyright>
  *
- * Copyright (c) 2005, 2010 SAP AG.
+ * Copyright (c) 2005, 2011 SAP AG.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,12 +9,14 @@
  *
  * Contributors:
  *    SAP AG - initial API, implementation and documentation
+ *    mwenz - Bug 339525 - Enrich paste context with location information
  *
  * </copyright>
  *
  *******************************************************************************/
 package org.eclipse.graphiti.ui.internal.action;
 
+import org.eclipse.draw2d.geometry.Point;
 import org.eclipse.graphiti.features.IFeatureProvider;
 import org.eclipse.graphiti.features.IPasteFeature;
 import org.eclipse.graphiti.features.context.IPasteContext;
@@ -38,6 +40,8 @@
 
 	public static final String ACTION_ID = ActionFactory.PASTE.getId(); // "predefined
 
+	private Point pasteLocation = new Point(-1, -1);
+
 	public PasteAction(IWorkbenchPart part, IConfigurationProvider configurationProvider) {
 		super(part, configurationProvider);
 		setId(ACTION_ID);
@@ -45,9 +49,12 @@
 		setToolTipText(TOOL_TIP);
 	}
 
+	public void setPasteLocation(Point mPasteLocation) {
+		this.pasteLocation = mPasteLocation;
+	}
+
 	public boolean isAvailable() {
-		PictogramElement[] pes = getSelectedPictogramElements();
-		IPasteContext context = new PasteContext(pes);
+		IPasteContext context = createPasteContext();
 		IPasteFeature feature = getFeatureProvider().getPasteFeature(context);
 		if (feature == null) {
 			return false;
@@ -58,11 +65,10 @@
 
 	@Override
 	protected boolean calculateEnabled() {
-		PictogramElement[] pes = getSelectedPictogramElements();
-		if (pes.length == 0) {
+		IPasteContext context = createPasteContext();
+		if (context.getPictogramElements() == null || context.getPictogramElements().length == 0) {
 			return false;
 		}
-		IPasteContext context = new PasteContext(pes);
 		IFeatureProvider featureProvider = getFeatureProvider();
 		if (featureProvider == null) {
 			return false;
@@ -77,8 +83,7 @@
 
 	@Override
 	public void run() {
-		PictogramElement[] pes = getSelectedPictogramElements();
-		IPasteContext context = new PasteContext(pes);
+		IPasteContext context = createPasteContext();
 		final IFeatureProvider featureProvider = getFeatureProvider();
 		IPasteFeature feature = featureProvider.getPasteFeature(context);
 		if (feature != null) {
@@ -86,4 +91,11 @@
 			executeOnCommandStack(command);
 		}
 	}
+
+	private IPasteContext createPasteContext() {
+		PictogramElement[] pes = getSelectedPictogramElements();
+		PasteContext context = new PasteContext(pes);
+		context.setLocation(pasteLocation.x, pasteLocation.y);
+		return context;
+	}
 }
diff --git a/plugins/org.eclipse.graphiti/src/org/eclipse/graphiti/features/context/IPasteContext.java b/plugins/org.eclipse.graphiti/src/org/eclipse/graphiti/features/context/IPasteContext.java
index 48d329f..c899cc4 100644
--- a/plugins/org.eclipse.graphiti/src/org/eclipse/graphiti/features/context/IPasteContext.java
+++ b/plugins/org.eclipse.graphiti/src/org/eclipse/graphiti/features/context/IPasteContext.java
@@ -1,7 +1,7 @@
 /*******************************************************************************
  * <copyright>
  *
- * Copyright (c) 2005, 2010 SAP AG.
+ * Copyright (c) 2005, 2011 SAP AG.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
  *
  * Contributors:
  *    SAP AG - initial API, implementation and documentation
+ *    mwenz - Bug 339525 - Enrich paste context with location information
  *
  * </copyright>
  *
@@ -24,5 +25,5 @@
  * @noimplement This interface is not intended to be implemented by clients.
  * @noextend This interface is not intended to be extended by clients.
  */
-public interface IPasteContext extends IPictogramElementsContext {
+public interface IPasteContext extends IPictogramElementsContext, ILocationContext {
 }
\ No newline at end of file
diff --git a/plugins/org.eclipse.graphiti/src/org/eclipse/graphiti/features/context/impl/PasteContext.java b/plugins/org.eclipse.graphiti/src/org/eclipse/graphiti/features/context/impl/PasteContext.java
index ca27234..a3a56ba 100644
--- a/plugins/org.eclipse.graphiti/src/org/eclipse/graphiti/features/context/impl/PasteContext.java
+++ b/plugins/org.eclipse.graphiti/src/org/eclipse/graphiti/features/context/impl/PasteContext.java
@@ -1,7 +1,7 @@
 /*******************************************************************************
  * <copyright>
  *
- * Copyright (c) 2005, 2010 SAP AG.
+ * Copyright (c) 2005, 2011 SAP AG.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
  *
  * Contributors:
  *    SAP AG - initial API, implementation and documentation
+ *    mwenz - Bug 339525 - Enrich paste context with location information
  *
  * </copyright>
  *
@@ -27,6 +28,11 @@
  */
 public class PasteContext extends PictogramElementsContext implements IPasteContext {
 
+	// Use delegation for LocationContext, since we already extend
+	// PictogramElementsContext (which is parallel to LocationContext in the
+	// inheritance hierarchy)
+	LocationContext locationContext;
+
 	/**
 	 * Creates a new {@link PasteContext}.
 	 * 
@@ -34,6 +40,80 @@
 	 *            the pictogram elements
 	 */
 	public PasteContext(PictogramElement[] pictogramElements) {
+		this(pictogramElements, -1, -1);
+	}
+	
+	/**
+	 * Creates a new {@link PasteContext}.
+	 * 
+	 * @param pictogramElements
+	 *            the pictogram elements
+	 * @param pasteLocationX
+	 *            the x location to paste to
+	 * @param pasteLocationY
+	 *            the y location to paste to
+	 * @since 0.9
+	 */
+	public PasteContext(PictogramElement[] pictogramElements, int pasteLocationX, int pasteLocationY) {
 		super(pictogramElements);
+		locationContext = new LocationContext(pasteLocationX, pasteLocationY);
+	}
+
+	/**
+	 * Gets the x location to paste to
+	 * 
+	 * @return an integer representing the x location
+	 * 
+	 * @since 0.9
+	 */
+	@Override
+	public int getX() {
+		return locationContext.getX();
+	}
+
+	/**
+	 * Sets the x location to paste to
+	 * 
+	 * @param x
+	 *            an integer representing the x location
+	 * @since 0.9
+	 */
+	public void setX(int x) {
+		locationContext.setX(x);
+	}
+
+	/**
+	 * Gets the y location to paste to
+	 * 
+	 * @return an integer representing the y location
+	 * @since 0.9
+	 */
+	@Override
+	public int getY() {
+		return locationContext.getY();
+	}
+
+	/**
+	 * Sets the y location to paste to
+	 * 
+	 * @param y
+	 *            an integer representing the y location
+	 * @since 0.9
+	 */
+	public void setY(int y) {
+		locationContext.setY(y);
+	}
+
+	/**
+	 * Sets the location to paste to
+	 * 
+	 * @param x
+	 *            an integer representing the x location
+	 * @param y
+	 *            an integer representing the y location
+	 * @since 0.9
+	 */
+	public void setLocation(int x, int y) {
+		locationContext.setLocation(x, y);
 	}
 }
diff --git a/tests/org.eclipse.graphiti.bot.tests/src/org/eclipse/graphiti/bot/tests/GFPackageTests.java b/tests/org.eclipse.graphiti.bot.tests/src/org/eclipse/graphiti/bot/tests/GFPackageTests.java
index e80886a..d0ef625 100644
--- a/tests/org.eclipse.graphiti.bot.tests/src/org/eclipse/graphiti/bot/tests/GFPackageTests.java
+++ b/tests/org.eclipse.graphiti.bot.tests/src/org/eclipse/graphiti/bot/tests/GFPackageTests.java
@@ -174,6 +174,8 @@
 			public void paste(IPasteContext context) {
 				Object[] obs = getCopiesFromClipBoard(this.diagram);
 				assertTrue((obs != null && obs.length > 0 && obs[0] instanceof PictogramElement));
+				assertEquals(100, context.getX());
+				assertEquals(200, context.getY());
 				for (int i = 0; i < obs.length; i++) {
 					if (obs[i] instanceof PictogramElement) {
 						this.fromClipboard[i] = (PictogramElement) obs[i];
@@ -217,7 +219,8 @@
 		assertNotNull(s);
 		assertFalse("".equals(s));
 		final MyPasteFeature myPasteFeature = new MyPasteFeature(myDiagramTypeProvider.getFeatureProvider(), diagram);
-		final IPasteContext pasteContext = new PasteContext(pes);
+		final PasteContext pasteContext = new PasteContext(pes);
+		pasteContext.setLocation(100, 200);
 
 		syncExec(new VoidResult() {
 			@Override