Pārlūkot izejas kodu

Implemented Semantic copy operation

Andreas Mülder 14 gadi atpakaļ
vecāks
revīzija
4c2ab19546

+ 26 - 3
plugins/org.yakindu.sct.ui.editor/plugin.xml

@@ -500,8 +500,8 @@
       </decoratorProvider>
    </extension>
    
-   <!-- Global Print Action handler -->
-    <extension point="org.eclipse.gmf.runtime.common.ui.services.action.globalActionHandlerProviders" id="global-actions">
+   <!-- Global Action handler -->
+    <extension point="org.eclipse.gmf.runtime.common.ui.services.action.globalActionHandlerProviders" id="global-print-actions">
       <GlobalActionHandlerProvider
          class="org.eclipse.gmf.runtime.diagram.ui.printing.render.providers.DiagramWithPrintGlobalActionHandlerProvider"
          id="Print handler">
@@ -512,6 +512,24 @@
             </ElementType>
          </ViewId>
       </GlobalActionHandlerProvider>
+      <GlobalActionHandlerProvider class="org.yakindu.sct.ui.editor.providers.SemanticCopyGlobalActionHandlerProvider" id="global-actions">
+         <Priority name="Lowest">
+         </Priority>
+         <ViewId id="org.yakindu.sct.ui.editor.editor.StatechartDiagramEditor">
+            <ElementType class="org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart">
+               <GlobalActionId actionId="delete">
+               </GlobalActionId>
+                  <GlobalActionId actionId="save">
+               </GlobalActionId>
+                  <GlobalActionId actionId="copy">
+               </GlobalActionId>
+                  <GlobalActionId actionId="paste">
+               </GlobalActionId>
+                  <GlobalActionId actionId="cut">
+               </GlobalActionId>
+            </ElementType>
+         </ViewId>
+      </GlobalActionHandlerProvider>
       </extension>
    
    <!-- context menu contribution -->
@@ -519,7 +537,6 @@
 	<command description="Change Flow Kind" id="org.yakindu.sct.ui.editor.Entry" name="Change Entry Kind">
 	</command>
 </extension>
-   
    <extension point="org.eclipse.ui.popupMenus">
       <objectContribution
             adaptable="false"
@@ -564,5 +581,11 @@
          </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>

+ 41 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/clipboard/StatechartClipboardSupport.java

@@ -0,0 +1,41 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU and others.
+ * 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ * Contributors:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.ui.editor.clipboard;
+
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.gmf.runtime.emf.clipboard.core.AbstractClipboardSupport;
+import org.eclipse.gmf.runtime.emf.clipboard.core.PasteAction;
+import org.yakindu.sct.model.sgraph.SGraphPackage;
+
+/**
+ * 
+ * @author andreas muelder
+ * 
+ */
+public class StatechartClipboardSupport extends AbstractClipboardSupport {
+
+	protected EAttribute getNameAttribute(EClass eClass) {
+		return SGraphPackage.Literals.NAMED_ELEMENT__NAME;
+	}
+
+	@Override
+	public PasteAction getPasteCollisionAction(EClass eClass) {
+		return PasteAction.CLONE;
+	}
+
+	public boolean isCopyAlways(EObject context, EReference eReference,
+			Object value) {
+		return true;
+	}
+}

+ 34 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/clipboard/StatechartClipboardSupportFactory.java

@@ -0,0 +1,34 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU and others.
+ * 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ * Contributors:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.ui.editor.clipboard;
+
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.gmf.runtime.emf.clipboard.core.IClipboardSupport;
+import org.eclipse.gmf.runtime.emf.clipboard.core.IClipboardSupportFactory;
+
+/**
+ * 
+ * @author andreas muelder
+ * 
+ */
+public class StatechartClipboardSupportFactory implements
+		IClipboardSupportFactory {
+
+	private final IClipboardSupport support = new StatechartClipboardSupport();
+
+	public StatechartClipboardSupportFactory() {
+		super();
+	}
+
+	public IClipboardSupport newClipboardSupport(EPackage ePackage) {
+		return support;
+	}
+}

+ 6 - 17
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/commands/CreateTransitionCommand.java

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2010-21011 committers of YAKINDU and others.
+ * Copyright (c) 2011 committers of YAKINDU and others.
  * 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
@@ -25,7 +25,7 @@ import org.yakindu.sct.model.sgraph.Vertex;
  * 
  * @author muelder
  * @author terfloth
- *
+ * 
  */
 public class CreateTransitionCommand extends EditElementCommand {
 
@@ -48,26 +48,15 @@ public class CreateTransitionCommand extends EditElementCommand {
 			return false;
 		if (target != null && !(target instanceof Vertex))
 			return false;
-		return checkConstraints();
-	}
-
-	private boolean checkConstraints() {
-//		if (source instanceof FinalState)
-//			return false;
-//		if (target instanceof Entry)
-//			return false;
-//		if (source instanceof HistoryState)
-//			return false;
-//		if (source instanceof InitialState && ((InitialState) source).getOutgoingTransitions().size() > 0)
-//			return false;
-
 		return true;
 	}
 
 	@Override
-	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
+	protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
+			IAdaptable info) throws ExecutionException {
 		if (!canExecute()) {
-			throw new ExecutionException("Invalid arguments in create link command");
+			throw new ExecutionException(
+					"Invalid arguments in create link command");
 		}
 		if (getSource() != null && getTarget() != null) {
 			Transition transition = SGraphFactory.eINSTANCE.createTransition();

+ 66 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/commands/SemanticCopyCommand.java

@@ -0,0 +1,66 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU and others.
+ * 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ * Contributors:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.ui.editor.commands;
+
+import java.util.List;
+
+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.transaction.TransactionalEditingDomain;
+import org.eclipse.gmf.runtime.common.core.command.CommandResult;
+import org.eclipse.gmf.runtime.common.ui.action.actions.global.ClipboardManager;
+import org.eclipse.gmf.runtime.common.ui.util.CustomData;
+import org.eclipse.gmf.runtime.common.ui.util.CustomDataTransfer;
+import org.eclipse.gmf.runtime.common.ui.util.ICustomData;
+import org.eclipse.gmf.runtime.emf.clipboard.core.ClipboardUtil;
+import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
+
+/**
+ * 
+ * @author andreas muelder
+ * 
+ */
+public class SemanticCopyCommand extends AbstractTransactionalCommand {
+
+	private static final String COPY = "Copy";
+
+	// TODO
+	public static final String DRAWING_SURFACE = "Drawing Surface";
+
+	private final List<EObject> selectedObjects;
+
+	public SemanticCopyCommand(TransactionalEditingDomain domain,
+			List<EObject> selectedObjects) {
+		super(domain, COPY, null);
+		this.selectedObjects = selectedObjects;
+	}
+
+	@Override
+	protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
+			IAdaptable info) throws ExecutionException {
+		String clipString = ClipboardUtil.copyElementsToString(
+				getSelectedObjects(), null, new NullProgressMonitor());
+
+		CustomData data = new CustomData(DRAWING_SURFACE, clipString.getBytes());
+		ClipboardManager.getInstance().addToCache(new ICustomData[] { data },
+				CustomDataTransfer.getInstance());
+
+		return CommandResult.newOKCommandResult();
+	}
+
+	private List<EObject> getSelectedObjects() {
+		return selectedObjects;
+	}
+
+}

+ 57 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/commands/SemanticPasteCommand.java

@@ -0,0 +1,57 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU and others.
+ * 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ * Contributors:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.ui.editor.commands;
+
+import java.util.List;
+
+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.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.emf.clipboard.core.ClipboardUtil;
+import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
+
+/**
+ * 
+ * @author andreas muelder
+ * 
+ */
+public class SemanticPasteCommand extends AbstractTransactionalCommand {
+
+	private static final String PASTE = "Paste";
+	private final List<EObject> targets;
+
+	public SemanticPasteCommand(List<EObject> target) {
+		super(TransactionUtil.getEditingDomain(target.get(0)), PASTE, null);
+		this.targets = target;
+	}
+
+	@Override
+	protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
+			IAdaptable info) throws ExecutionException {
+		ICustomData[] clipboardData = ClipboardManager.getInstance()
+				.getClipboardData(SemanticCopyCommand.DRAWING_SURFACE,
+						ClipboardContentsHelper.getInstance());
+		String data = new String(clipboardData[0].getData());
+		for (EObject target : targets) {
+			ClipboardUtil.pasteElementsFromString(data, target, null,
+					new NullProgressMonitor());
+		}
+		return CommandResult.newOKCommandResult();
+	}
+
+}

+ 119 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/providers/SemanticCopyGlobalActionHandlerProvider.java

@@ -0,0 +1,119 @@
+/******************************************************************************
+ * Copyright (c) 2002, 2005 IBM Corporation and others.
+ * 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *    IBM Corporation - initial API and implementation 
+ ****************************************************************************/
+
+package org.yakindu.sct.ui.editor.providers;
+
+import java.util.Hashtable;
+
+import org.eclipse.gmf.runtime.common.ui.services.action.global.AbstractGlobalActionHandlerProvider;
+import org.eclipse.gmf.runtime.common.ui.services.action.global.IGlobalActionHandler;
+import org.eclipse.gmf.runtime.common.ui.services.action.global.IGlobalActionHandlerContext;
+import org.eclipse.ui.IPartListener;
+import org.eclipse.ui.IWorkbenchPart;
+
+/**
+ * Concrete class that implements the <code>IGlobalActionHandlerProvider</code>.
+ * 
+ * @author Vishy Ramaswamy
+ */
+@SuppressWarnings("rawtypes")
+public final class SemanticCopyGlobalActionHandlerProvider
+	extends AbstractGlobalActionHandlerProvider {
+
+	/**	 * List that contains all the IGlobalActionHandlers mapped to the
+	 * IWorkbenchParts
+	 */
+	private Hashtable handlerList = new Hashtable();
+
+	/**
+	 * Constructor for DiagramGlobalActionHandlerProvider.
+	 */
+	public SemanticCopyGlobalActionHandlerProvider() {
+		super();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see org.eclipse.gmf.runtime.common.ui.services.action.global.IGlobalActionHandlerProvider#getGlobalActionHandler(org.eclipse.gmf.runtime.common.ui.services.action.global.IGlobalActionHandlerContext)
+	 */
+	@SuppressWarnings("unchecked")
+	public IGlobalActionHandler getGlobalActionHandler(
+			final IGlobalActionHandlerContext context) {
+		/* create the handler */
+		if (!getHandlerList().containsKey(context.getActivePart())) {
+			getHandlerList().put(context.getActivePart(),
+				new SemanticCopyPasteGlobalActionHandler());
+			/*
+			 * register as a part listener so that the cache can be cleared when
+			 * the part is disposed
+			 */
+			context.getActivePart().getSite().getPage().addPartListener(
+				new IPartListener() {
+
+					private IWorkbenchPart localPart = context.getActivePart();
+
+					/**
+					 * @see org.eclipse.ui.IPartListener#partActivated(IWorkbenchPart)
+					 */
+					public void partActivated(IWorkbenchPart part) {
+						// NULL implementation
+					}
+
+					/**
+					 * @see org.eclipse.ui.IPartListener#partBroughtToTop(IWorkbenchPart)
+					 */
+					public void partBroughtToTop(IWorkbenchPart part) {
+						// NULL implementation
+					}
+
+					/**
+					 * @see org.eclipse.ui.IPartListener#partClosed(IWorkbenchPart)
+					 */
+					public void partClosed(IWorkbenchPart part) {
+						/* remove the cache associated with the part */
+						if (part != null && part == localPart
+							&& getHandlerList().containsKey(part)) {
+							getHandlerList().remove(part);
+							localPart.getSite().getPage().removePartListener(
+								this);
+							localPart = null;
+						}
+					}
+
+					/**
+					 * @see org.eclipse.ui.IPartListener#partDeactivated(IWorkbenchPart)
+					 */
+					public void partDeactivated(IWorkbenchPart part) {
+						// NULL implementation
+					}
+
+					/**
+					 * @see org.eclipse.ui.IPartListener#partOpened(IWorkbenchPart)
+					 */
+					public void partOpened(IWorkbenchPart part) {
+						// NULL implementation
+					}
+				});
+		}
+		return (SemanticCopyPasteGlobalActionHandler) getHandlerList().get(
+			context.getActivePart());
+	}
+
+	/**
+	 * Returns the handlerList.
+	 * 
+	 * @return Hashtable
+	 */
+	private Hashtable getHandlerList() {
+		return handlerList;
+	}
+}

+ 91 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/providers/SemanticCopyPasteGlobalActionHandler.java

@@ -0,0 +1,91 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU and others.
+ * 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ * Contributors:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.ui.editor.providers;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.emf.transaction.util.TransactionUtil;
+import org.eclipse.gef.EditPart;
+import org.eclipse.gmf.runtime.common.core.command.ICommand;
+import org.eclipse.gmf.runtime.common.ui.action.global.GlobalActionId;
+import org.eclipse.gmf.runtime.common.ui.services.action.global.IGlobalActionContext;
+import org.eclipse.gmf.runtime.diagram.ui.providers.DiagramGlobalActionHandler;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.yakindu.sct.ui.editor.commands.SemanticCopyCommand;
+import org.yakindu.sct.ui.editor.commands.SemanticPasteCommand;
+
+/**
+ * 
+ * @author andreas muelder
+ * 
+ */
+public class SemanticCopyPasteGlobalActionHandler extends
+		DiagramGlobalActionHandler {
+
+	public ICommand getCommand(IGlobalActionContext cntxt) {
+		String actionId = cntxt.getActionId();
+		if (actionId.equals(GlobalActionId.COPY)) {
+			return getCopyCommand(cntxt);
+		} else if (actionId.equals(GlobalActionId.CUT)) {
+			return getCutCommand(cntxt);
+		} else if (actionId.equals(GlobalActionId.PASTE)) {
+			return getPasteCommand(cntxt);
+		}
+		return super.getCommand(cntxt);
+	}
+
+	private ICommand getCutCommand(IGlobalActionContext cntxt) {
+		// TODO: Implement me
+		return null;
+	}
+
+	protected ICommand getCopyCommand(IGlobalActionContext cntxt) {
+		List<EObject> objects = getSelectedElements(cntxt.getSelection());
+		if (objects.size() > 0) {
+			TransactionalEditingDomain domain = TransactionUtil
+					.getEditingDomain(objects.get(0));
+			SemanticCopyCommand copyCmd = new SemanticCopyCommand(domain,
+					objects);
+			return copyCmd;
+		}
+		return null;
+	}
+
+	private ICommand getPasteCommand(IGlobalActionContext cntxt) {
+		List<EObject> objects = getSelectedElements(cntxt.getSelection());
+		if (objects.size() > 0) {
+			return new SemanticPasteCommand(objects);
+		}
+		return null;
+	}
+
+	protected List<EObject> getSelectedElements(ISelection selection) {
+		List<EObject> results = new ArrayList<EObject>();
+		if (selection == null || selection.isEmpty())
+			return results;
+		Iterator<?> iterator = ((IStructuredSelection) selection).iterator();
+		while (iterator.hasNext()) {
+			Object selectedElement = iterator.next();
+			if (selectedElement instanceof EditPart) {
+				EObject element = (EObject) ((EditPart) selectedElement)
+						.getAdapter(EObject.class);
+				results.add(element);
+			}
+		}
+		return results;
+	}
+
+}