Просмотр исходного кода

Added basic copy and paste functionality for region compartments.

markus.muehlbrandt@itemis.de 14 лет назад
Родитель
Сommit
d8fe5d1195

+ 2 - 1
plugins/org.yakindu.sct.ui.editor/META-INF/MANIFEST.MF

@@ -37,7 +37,8 @@ Require-Bundle: org.eclipse.core.runtime,
  org.yakindu.sct.model.sgraph.edit,
  de.itemis.xtext.utils.gmf;bundle-version="[1.0.0,2.0.0)",
  de.itemis.xtext.utils.jface;bundle-version="[1.0.0,2.0.0)",
- de.itemis.gmf.runtime.commons;bundle-version="1.0.0"
+ de.itemis.gmf.runtime.commons;bundle-version="1.0.0",
+ org.eclipse.gmf.runtime.notation.providers;bundle-version="1.3.0"
 Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
 Export-Package: org.yakindu.sct.ui.editor,

+ 1 - 7
plugins/org.yakindu.sct.ui.editor/plugin.xml

@@ -518,7 +518,7 @@
             </ElementType>
          </ViewId>
       </GlobalActionHandlerProvider>
-      <GlobalActionHandlerProvider class="org.yakindu.sct.ui.editor.providers.SemanticCopyGlobalActionHandlerProvider" id="global-actions">
+      <GlobalActionHandlerProvider class="org.eclipse.gmf.runtime.diagram.ui.providers.DiagramGlobalActionHandlerProvider" id="global-actions">
          <Priority name="Lowest">
          </Priority>
          <ViewId id="org.yakindu.sct.ui.editor.editor.StatechartDiagramEditor">
@@ -587,11 +587,5 @@
          </action>    
       </objectContribution>           
 </extension>
- <extension point="org.eclipse.gmf.runtime.emf.clipboard.core.clipboardSupport">
-      <factory
-            class="org.yakindu.sct.ui.editor.clipboard.StatechartClipboardSupportFactory"
-            nsURI="http://www.yakindu.org/sct/2.0.0"
-            priority="highest"/>
-   </extension>
 
 </plugin>

+ 45 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/clipboard/SCTClipboardUtil.java

@@ -0,0 +1,45 @@
+package org.yakindu.sct.ui.editor.clipboard;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+public final class SCTClipboardUtil {
+	
+	private static final int KILOBYTE = 1024;
+	private static final int BUFFER_SIZE = 128 * KILOBYTE;
+	
+	private SCTClipboardUtil(){
+		//This class shouldn't be instantiated.
+	}
+	
+	public static byte[] getByteArrayFromObject(Object object) {
+		
+		ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
+		try {
+			ObjectOutputStream o = new ObjectOutputStream( out );
+			o.writeObject(object);
+			return out.toByteArray();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return new byte[0];
+	}
+	
+	public static Object getObjectFromByteArray(byte[] array) {
+		
+		ByteArrayInputStream out = new ByteArrayInputStream(array);
+		
+		try {
+			ObjectInputStream o = new ObjectInputStream(out);
+			return o.readObject();
+		} catch (IOException e) {
+			e.printStackTrace();
+		} catch (ClassNotFoundException e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+}

+ 31 - 11
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/commands/SemanticCopyCommand.java

@@ -11,7 +11,10 @@
 package org.yakindu.sct.ui.editor.commands;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.runtime.IAdaptable;
@@ -28,6 +31,7 @@ import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
 import org.eclipse.gmf.runtime.emf.clipboard.core.ClipboardUtil;
 import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
 import org.eclipse.gmf.runtime.notation.View;
+import org.yakindu.sct.ui.editor.clipboard.SCTClipboardUtil;
 
 /**
  * 
@@ -52,18 +56,12 @@ public class SemanticCopyCommand extends AbstractTransactionalCommand {
 	@Override
 	protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
 			IAdaptable info) throws ExecutionException {
-		String semanticString = ClipboardUtil.copyElementsToString(
-				getSemanticElements(), null, new NullProgressMonitor());
-		String notationString = ClipboardUtil.copyElementsToString(
-				getNotationElements(), null, new NullProgressMonitor());
-
-		CustomData semanticData = new CustomData(DRAWING_SURFACE,
-				semanticString.getBytes());
-		CustomData notationData = new CustomData(DRAWING_SURFACE,
-				notationString.getBytes());
-
+		
+		byte[] a = SCTClipboardUtil.getByteArrayFromObject(buildMap());
+		CustomData data = new CustomData (DRAWING_SURFACE,a);
+		
 		ClipboardManager.getInstance().addToCache(
-				new ICustomData[] { notationData, semanticData },
+				new ICustomData[] {data},
 				CustomDataTransfer.getInstance());
 		
 		ClipboardManager.getInstance().flushCacheToClipboard();
@@ -88,4 +86,26 @@ public class SemanticCopyCommand extends AbstractTransactionalCommand {
 		}
 		return result;
 	}
+	
+	private Map<byte[], String> buildMap() {
+		Map<byte[], String> map = new HashMap<byte[], String>();
+		for (IGraphicalEditPart editPart : selectedObjects) {
+			
+			// Collections.SingletonList(...) returns an immutable list and
+			// ClipboardUtil.copyElementsToString want to remove elements from
+			// the list...
+			List<EObject> list = new ArrayList<EObject>(
+					Collections.singletonList(editPart.resolveSemanticElement()));
+			
+			String semanticElementString = ClipboardUtil.copyElementsToString(
+					list, null, new NullProgressMonitor());
+			
+			View view = editPart.getNotationView();
+			
+			map.put(semanticElementString.getBytes(),
+					view.eResource().getURIFragment(view));
+		}
+
+		return map;
+	}
 }

+ 69 - 17
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/commands/SemanticPasteCommand.java

@@ -12,20 +12,28 @@ package org.yakindu.sct.ui.editor.commands;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.emf.transaction.util.TransactionUtil;
 import org.eclipse.gmf.runtime.common.core.command.CommandResult;
 import org.eclipse.gmf.runtime.common.ui.action.actions.global.ClipboardContentsHelper;
 import org.eclipse.gmf.runtime.common.ui.action.actions.global.ClipboardManager;
 import org.eclipse.gmf.runtime.common.ui.util.ICustomData;
+import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
 import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
 import org.eclipse.gmf.runtime.emf.clipboard.core.ClipboardUtil;
 import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
+import org.eclipse.gmf.runtime.notation.Node;
 import org.eclipse.gmf.runtime.notation.View;
+import org.yakindu.sct.ui.editor.clipboard.SCTClipboardUtil;
+import org.yakindu.sct.ui.editor.providers.SemanticHints;
 
 /**
  * 
@@ -44,31 +52,75 @@ public class SemanticPasteCommand extends AbstractTransactionalCommand {
 		this.targets = target;
 	}
 
+	@SuppressWarnings("unchecked")
 	@Override
 	protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
 			IAdaptable info) throws ExecutionException {
+		
 		ICustomData[] clipboardData = ClipboardManager.getInstance()
 				.getClipboardData(SemanticCopyCommand.DRAWING_SURFACE,
 						ClipboardContentsHelper.getInstance());
-		String notationData = new String(clipboardData[0].getData());
-		System.out.println(notationData);
-		String semanticData = new String(clipboardData[1].getData());
-		System.out.println(semanticData);
-
-		for (IGraphicalEditPart target : targets) {
-			Collection semanticElements = ClipboardUtil.pasteElementsFromString(semanticData,
-					target.resolveSemanticElement(), null,
-					new NullProgressMonitor());
-			
-			Collection<View> notationElements = ClipboardUtil.pasteElementsFromString(notationData,
-					target.getNotationView(), null, new NullProgressMonitor());
-			
-			
-			System.out.println(semanticElements);
-			System.out.println(notationElements);
-			
+		
+		Object object = SCTClipboardUtil.getObjectFromByteArray(clipboardData[0].getData());
+		
+		if (object instanceof Map<?, ?>) {
+			copyElementsAndNotationViewAttributes((Map<byte[], String>) object);
+		}
+		
+		else {
+			String dataString = new String(clipboardData[0].getData());
+			for (IGraphicalEditPart target : targets) {
+				
+				Collection semanticElements = ClipboardUtil.pasteElementsFromString(dataString,
+						target.resolveSemanticElement(), null,
+						new NullProgressMonitor());
+			}
 		}
 		return CommandResult.newOKCommandResult();
 	}
+	
+	@SuppressWarnings("unchecked")
+	private void copyElementsAndNotationViewAttributes(Map<byte[], String> map) {
+		for (IGraphicalEditPart target : targets) {
+			Resource resource = target.resolveSemanticElement().eResource();
+			for (byte[] semanticKeySet : map.keySet()) {
+
+				EObject eObject = resource.getEObject(map.get(semanticKeySet));
+
+				if (eObject instanceof Node) {
+					Node view = (Node) eObject;
 
+					Collection<EObject> semanticElements = (Collection<EObject>) ClipboardUtil
+							.pasteElementsFromString(
+									new String(semanticKeySet),
+									target.resolveSemanticElement(), null,
+									new NullProgressMonitor());
+
+					EObject semanticElement = semanticElements.iterator()
+							.next();
+
+					View compartmentView = getStateCompartmentView(target.getNotationView());
+					if (compartmentView!=null) {
+						Node newNode = ViewService.createNode(
+								compartmentView, semanticElement,
+								view.getType(), target.getDiagramPreferencesHint());
+						
+						newNode.setLayoutConstraint(EcoreUtil.copy(view
+								.getLayoutConstraint()));
+					}
+				}
+			}
+		}
+	}
+	
+	private View getStateCompartmentView(View stateView) {
+		for (Object object : stateView.getChildren()) {
+			if (object instanceof View
+					&& ((View) object).getType() == SemanticHints.REGION_COMPARTMENT) {
+				return (View) object;
+			}
+		}
+		return null;
+	}
+	
 }

+ 11 - 1
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/editparts/RegionCompartmentEditPart.java

@@ -26,7 +26,9 @@ import org.yakindu.sct.ui.editor.policies.RegionCompartmentXYLayoutEditPolicy;
  * @author muelder
  */
 public class RegionCompartmentEditPart extends ShapeCompartmentEditPart {
-
+	
+	private boolean isSupportingViewActions = true;
+	
 	public RegionCompartmentEditPart(View view) {
 		super(view);
 	}
@@ -43,6 +45,14 @@ public class RegionCompartmentEditPart extends ShapeCompartmentEditPart {
 		installEditPolicy(EditPolicy.LAYOUT_ROLE,
 				new RegionCompartmentXYLayoutEditPolicy());
 	}
+	
+	public boolean isSupportingViewActions(){
+		return this.isSupportingViewActions;
+	}
+	
+	public void setIsSupportingViewActions(boolean supportsViewActions){
+		this.isSupportingViewActions = supportsViewActions;
+	}
 
 	@Override
 	protected IFigure createFigure() {

+ 16 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/editparts/RegionEditPart.java

@@ -16,9 +16,11 @@ import org.eclipse.draw2d.geometry.Dimension;
 import org.eclipse.emf.ecore.EStructuralFeature;
 import org.eclipse.gef.EditPart;
 import org.eclipse.gef.EditPolicy;
+import org.eclipse.gef.Request;
 import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart;
 import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
 import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ResizableEditPolicyEx;
+import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants;
 import org.eclipse.gmf.runtime.draw2d.ui.figures.FigureUtilities;
 import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
 import org.eclipse.gmf.runtime.notation.NotationPackage;
@@ -54,6 +56,20 @@ public class RegionEditPart extends ShapeNodeEditPart {
 				new ResizableEditPolicyEx());
 	}
 
+	@Override
+	public EditPart getTargetEditPart(Request request) {
+
+		if (request.getType().toString() == RequestConstants.REQ_PASTE) {
+			for (Object object : getChildren()) {
+				if (object instanceof RegionCompartmentEditPart) {
+					return (EditPart) object;
+				}
+			}
+		}
+
+		return super.getTargetEditPart(request);
+	}
+
 	@Override
 	public IFigure getContentPane() {
 		return getPrimaryShape().getCompartmentPane();