summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbmaggi2014-04-30 10:07:42 (EDT)
committerBenoit Maggi2014-05-05 09:12:34 (EDT)
commit19ab9807c86df10c227ceac5c1e2c003b6d732da (patch)
treee62fa6ca8d156d8dc9c0855d36a7bf648b0459d1
parent104b0f1d1bc9402290b04c79bb14d90c5b795508 (diff)
downloadorg.eclipse.papyrus-19ab9807c86df10c227ceac5c1e2c003b6d732da.zip
org.eclipse.papyrus-19ab9807c86df10c227ceac5c1e2c003b6d732da.tar.gz
org.eclipse.papyrus-19ab9807c86df10c227ceac5c1e2c003b6d732da.tar.bz2
Bug 290261 - [Usability] Papyrus shall provide a copy/paste/cut featurerefs/changes/66/25966/1
- add cut for diagram and model explorer - cut => copy + delete Change-Id: I51680e4d4bdd775222a9d8c7fdf32ea35e3ce017 Signed-off-by: bmaggi <benoit.maggi@cea.fr>
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.menu/src/org/eclipse/papyrus/infra/gmfdiag/menu/handlers/CopyInDiagramHandler.java2
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.menu/src/org/eclipse/papyrus/infra/gmfdiag/menu/handlers/CutInDiagramHandler.java155
-rw-r--r--plugins/views/modelexplorer/org.eclipse.papyrus.views.modelexplorer/src/org/eclipse/papyrus/views/modelexplorer/handler/CutHandler.java194
3 files changed, 296 insertions, 55 deletions
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.menu/src/org/eclipse/papyrus/infra/gmfdiag/menu/handlers/CopyInDiagramHandler.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.menu/src/org/eclipse/papyrus/infra/gmfdiag/menu/handlers/CopyInDiagramHandler.java
index 3fdfc14..a99ea34 100644
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.menu/src/org/eclipse/papyrus/infra/gmfdiag/menu/handlers/CopyInDiagramHandler.java
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.menu/src/org/eclipse/papyrus/infra/gmfdiag/menu/handlers/CopyInDiagramHandler.java
@@ -128,7 +128,7 @@ public class CopyInDiagramHandler extends AbstractGraphicalCommandHandler {
//
@SuppressWarnings("restriction")
- private static class MyCopyImageCommand extends org.eclipse.gmf.runtime.diagram.ui.render.internal.commands.CopyImageCommand implements INonDirtying {
+ static class MyCopyImageCommand extends org.eclipse.gmf.runtime.diagram.ui.render.internal.commands.CopyImageCommand implements INonDirtying {
MyCopyImageCommand(String label, View viewContext, @SuppressWarnings("rawtypes") List source, DiagramEditPart diagramEP) {
super(label, viewContext, source, diagramEP);
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.menu/src/org/eclipse/papyrus/infra/gmfdiag/menu/handlers/CutInDiagramHandler.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.menu/src/org/eclipse/papyrus/infra/gmfdiag/menu/handlers/CutInDiagramHandler.java
index 09086cd..14eab1e 100644
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.menu/src/org/eclipse/papyrus/infra/gmfdiag/menu/handlers/CutInDiagramHandler.java
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.menu/src/org/eclipse/papyrus/infra/gmfdiag/menu/handlers/CutInDiagramHandler.java
@@ -13,23 +13,166 @@
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.menu.handlers;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.core.expressions.IEvaluationContext;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.commands.Command;
+import org.eclipse.gef.commands.CompoundCommand;
+import org.eclipse.gef.commands.UnexecutableCommand;
+import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy;
+import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
+import org.eclipse.gmf.runtime.diagram.ui.requests.EditCommandRequestWrapper;
+import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand;
+import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.papyrus.commands.util.NonDirtyingUtils;
+import org.eclipse.papyrus.commands.wrappers.EMFtoGEFCommandWrapper;
+import org.eclipse.papyrus.commands.wrappers.GMFtoGEFCommandWrapper;
+import org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard;
+import org.eclipse.papyrus.infra.gmfdiag.common.commands.DefaultDiagramCopyCommand;
+import org.eclipse.papyrus.infra.gmfdiag.common.strategy.IStrategy;
+import org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy;
+import org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.PasteStrategyManager;
+import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramEditPartsUtil;
+import org.eclipse.papyrus.infra.gmfdiag.menu.handlers.CopyInDiagramHandler.MyCopyImageCommand;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Text;
/**
- * Handler for the Copy Action in Diagram
+ * Handler for the Cut Action in Diagram : it's a copy followed by a delete
*/
public class CutInDiagramHandler extends AbstractGraphicalCommandHandler {
+ private static final String ACTIVE_SHELL = "activeShell"; //$NON-NLS-1$
+ private static final String ACTIVE_FOCUS_CONTROL = "activeFocusControl"; //$NON-NLS-1$
@Override
- protected boolean computeEnabled() {
- return false;
+ protected Command getCommand() {
+ CompoundCommand cutInDiagramCommand = new CompoundCommand("Cut in Diagram Command"); //$NON-NLS-1$
+ Command buildCopy = buildCopyCommand();
+ cutInDiagramCommand.add(buildCopy);
+ Command buildDelete = buildDeleteCommand();
+ cutInDiagramCommand.add(buildDelete);
+ return cutInDiagramCommand;
}
+ /**
+ * Construct a copy command with the cut selection
+ * @return the copy command
+ */
+ protected Command buildCopyCommand() {
+ PapyrusClipboard<Object> papyrusClipboard = PapyrusClipboard.getNewInstance();
+ List<IGraphicalEditPart> selectedElements = getSelectedElements();
+ TransactionalEditingDomain editingDomain = getEditingDomain();
+ Command copyCommand;
+ DefaultDiagramCopyCommand defaultDiagramCopyCommand = new DefaultDiagramCopyCommand(editingDomain, papyrusClipboard, selectedElements);
+ copyCommand = EMFtoGEFCommandWrapper.wrap(defaultDiagramCopyCommand);
+ IDiagramWorkbenchPart activeDiagramWorkbenchPart = DiagramEditPartsUtil.getActiveDiagramWorkbenchPart();
+ Diagram diagram = activeDiagramWorkbenchPart.getDiagram();
+ DiagramEditPart diagramEditPart = activeDiagramWorkbenchPart.getDiagramEditPart();
+ List<Object> selectedElementModels = new ArrayList<Object>();
+ for(IGraphicalEditPart iGraphicalEditPart : selectedElements) {
+ selectedElementModels.add(iGraphicalEditPart.getModel());
+ }
+ MyCopyImageCommand copyImageCommand = new MyCopyImageCommand("Create image to allow paste on system", diagram, selectedElementModels, diagramEditPart); //$NON-NLS-1$
+ if(copyImageCommand.canExecute()) {
+ Command gmFtoGEFCommandWrapper = GMFtoGEFCommandWrapper.wrap(copyImageCommand);
+ copyCommand = NonDirtyingUtils.chain(copyCommand, gmFtoGEFCommandWrapper);
+ } else {
+ copyImageCommand.dispose();
+ }
+
+ List<IStrategy> allStrategies = PasteStrategyManager.getInstance().getAllStrategies();
+ for(IStrategy iStrategy : allStrategies) {
+ IPasteStrategy iIPasteStrategy = (IPasteStrategy)iStrategy;
+ iIPasteStrategy.prepare(papyrusClipboard);
+ }
+
+ return copyCommand;
+ }
+
+ /**
+ * Construct a delete command with the cut selection
+ * @return the delete command
+ */
+ protected Command buildDeleteCommand() {
+
+ TransactionalEditingDomain editingDomain = getEditingDomain();
+
+ if(editingDomain == null) {
+ return UnexecutableCommand.INSTANCE;
+ }
+
+ // Retrieve currently selected IGraphicalEditPart(s)
+ List<IGraphicalEditPart> editParts = getSelectedElements();
+ if(editParts.isEmpty()) {
+ return UnexecutableCommand.INSTANCE;
+ }
+
+ // Iterate over selection and retrieve the deletion command from each
+ // edit part
+ // Add each returned command to the composite command
+ CompositeTransactionalCommand command = new CompositeTransactionalCommand(editingDomain, "Delete From Model"); //$NON-NLS-1$
+
+ Iterator<IGraphicalEditPart> it = editParts.iterator();
+ while(it.hasNext()) {
+ IGraphicalEditPart editPart = it.next();
+
+ if(!(editPart instanceof DiagramEditPart)) {
+ // Look for the GMF deletion command
+ Command curCommand = editPart.getCommand(new EditCommandRequestWrapper(new DestroyElementRequest(false)));
+ if(curCommand != null) {
+ command.compose(new CommandProxy(curCommand));
+ }
+ }
+ }
+
+ if(command.isEmpty()) {
+ return UnexecutableCommand.INSTANCE;
+ }
+
+ return new ICommandProxy(command);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.papyrus.infra.gmfdiag.menu.handlers.
+ * AbstractGraphicalCommandHandler#setEnabled(java.lang.Object)
+ */
@Override
- protected Command getCommand() {
- // TODO Auto-generated method stub
- return null;
+ public void setEnabled(Object evaluationContext) {
+ if(evaluationContext instanceof IEvaluationContext) {
+ IEvaluationContext iEvaluationContext = (IEvaluationContext)evaluationContext;
+ Object activeFocusControl = iEvaluationContext.getVariable(ACTIVE_FOCUS_CONTROL);
+ Object activeShell = iEvaluationContext.getVariable(ACTIVE_SHELL);
+ Control focusControl = null;
+ if(activeShell instanceof Shell) {
+ Shell shell = (Shell)activeShell;
+ Display display = shell.getDisplay();
+ if(display != null) {
+ focusControl = display.getFocusControl();
+ }
+ }
+ if(activeFocusControl instanceof StyledText || focusControl instanceof Text) { // true if the focus is
+ // on an internal xtext
+ // editor or a text edit
+ setBaseEnabled(false);
+ } else {
+ PapyrusClipboard<Object> instance = PapyrusClipboard.getInstance();
+ super.setEnabled(evaluationContext);
+ PapyrusClipboard.setInstance(instance);
+ }
+ }
}
}
diff --git a/plugins/views/modelexplorer/org.eclipse.papyrus.views.modelexplorer/src/org/eclipse/papyrus/views/modelexplorer/handler/CutHandler.java b/plugins/views/modelexplorer/org.eclipse.papyrus.views.modelexplorer/src/org/eclipse/papyrus/views/modelexplorer/handler/CutHandler.java
index 83961d6..09613a5 100644
--- a/plugins/views/modelexplorer/org.eclipse.papyrus.views.modelexplorer/src/org/eclipse/papyrus/views/modelexplorer/handler/CutHandler.java
+++ b/plugins/views/modelexplorer/org.eclipse.papyrus.views.modelexplorer/src/org/eclipse/papyrus/views/modelexplorer/handler/CutHandler.java
@@ -1,48 +1,146 @@
-/*****************************************************************************
- * Copyright (c) 2011 CEA LIST.
- *
- *
- * 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:
- * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.views.modelexplorer.handler;
-
-import java.util.List;
-
-import org.eclipse.emf.common.command.Command;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.edit.command.CutToClipboardCommand;
-
-/**
- * Handler for the Cur Action
- *
- *
- *
- */
-public class CutHandler extends AbstractCommandHandler {
-
- /**
- *
- * @see org.eclipse.papyrus.views.modelexplorer.handler.AbstractCommandHandler#getCommand()
- *
- * @return
- */
- @Override
- protected Command getCommand() {
- List<EObject> selection = getSelectedElements();
- return CutToClipboardCommand.create(getEditingDomain(), selection);
- }
-
- @Override
- protected boolean computeEnabled() {
- //done explicitly inorder to desactivate it.
- return false;
- }
-
-}
+/*****************************************************************************
+ * Copyright (c) 2011 CEA LIST.
+ *
+ *
+ * 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:
+ * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.views.modelexplorer.handler;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.emf.common.command.Command;
+import org.eclipse.emf.common.command.CompoundCommand;
+import org.eclipse.emf.common.command.UnexecutableCommand;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
+import org.eclipse.gmf.runtime.common.core.command.ICommand;
+import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
+import org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard;
+import org.eclipse.papyrus.infra.gmfdiag.common.commands.DefaultCopyCommand;
+import org.eclipse.papyrus.infra.gmfdiag.common.strategy.IStrategy;
+import org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.IPasteStrategy;
+import org.eclipse.papyrus.infra.gmfdiag.common.strategy.paste.PasteStrategyManager;
+import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
+import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
+
+/**
+ * Handler for the Cut Action in Model Explorer : it's a copy followed by a delete
+ *
+ */
+public class CutHandler extends AbstractCommandHandler {
+
+ /**
+ *
+ * @see org.eclipse.papyrus.views.modelexplorer.handler.AbstractCommandHandler#getCommand()
+ *
+ * @return
+ */
+ @Override
+ protected Command getCommand() {
+ CompoundCommand cutInModelExplorerCommand = new CompoundCommand("Cut in Model Explorer Command"); //$NON-NLS-1$
+ DefaultCopyCommand copyCommand = buildCopyCommand();
+ cutInModelExplorerCommand.append(copyCommand);
+ Command create = buildDeleteCommand();
+ cutInModelExplorerCommand.append(create);
+ return cutInModelExplorerCommand;
+ }
+
+
+ /**
+ * Construct a copy command with the cut selection
+ * @return the copy command
+ */
+ protected DefaultCopyCommand buildCopyCommand() {
+ List<EObject> selection = getSelectedElements();
+ TransactionalEditingDomain editingDomain = getEditingDomain();
+ PapyrusClipboard<Object> papyrusClipboard = PapyrusClipboard.getNewInstance();
+ DefaultCopyCommand defaultCopyCommand = new DefaultCopyCommand(editingDomain, papyrusClipboard, selection);
+ List<IStrategy> allStrategies = PasteStrategyManager.getInstance().getAllStrategies();
+ for(IStrategy iStrategy : allStrategies) {
+ IPasteStrategy iPasteStrategy = (IPasteStrategy)iStrategy;
+ iPasteStrategy.prepare(papyrusClipboard);
+ }
+ return defaultCopyCommand;
+ }
+
+
+ /**
+ * Construct a delete command with the cut selection
+ * @return the delete command
+ */
+ protected Command buildDeleteCommand() {
+
+ ICommand gmfCommand = null;
+
+ // Parameters store the Request parameters of the previous request
+ // if multiple elements are selected for deletion.
+ Map parameters = new HashMap();
+
+ for(EObject selectedEObject : getSelectedElements()) {
+
+ if(selectedEObject == null) {
+ continue;
+ }
+
+ IElementEditService provider = ElementEditServiceUtils.getCommandProvider(selectedEObject);
+ if(provider == null) {
+ continue;
+ }
+
+ // Retrieve delete command from the Element Edit service
+ DestroyElementRequest request = new DestroyElementRequest(selectedEObject, false);
+ // Add parameters (contains the list of previously dependents to be deleted
+ // by previous commands.
+ request.getParameters().putAll(parameters);
+
+ ICommand deleteCommand = provider.getEditCommand(request);
+
+ // Add current EObject destroy command to the global command
+ gmfCommand = CompositeCommand.compose(gmfCommand, deleteCommand);
+
+ // Store the new parameters for next delete command.
+ parameters.clear();
+ parameters.putAll(request.getParameters());
+ }
+
+ if(gmfCommand == null) {
+ return UnexecutableCommand.INSTANCE;
+ }
+
+ Command emfCommand = new org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper(gmfCommand.reduce());
+ return emfCommand;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.papyrus.views.modelexplorer.handler.AbstractCommandHandler#computeEnabled()
+ */
+ @Override
+ protected boolean computeEnabled() { // copy is enable as long as there is an EObject to put in the Clipboard
+ return !getSelectedElements().isEmpty();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.papyrus.views.modelexplorer.handler.AbstractCommandHandler#setEnabled(java.lang.Object)
+ */
+ @Override
+ public void setEnabled(Object evaluationContext) {
+ PapyrusClipboard<Object> instance = PapyrusClipboard.getInstance();
+ super.setEnabled(evaluationContext); // setEnabled should'nt clear/modify the clipboard
+ PapyrusClipboard.setInstance(instance);
+ }
+
+}