Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Lorenzo2014-03-11 13:45:25 +0000
committerVincent Lorenzo2014-03-11 13:45:25 +0000
commitb740c91e47696af9ce99dff6ae6eb5e4c969135a (patch)
treef63ba5f033716921804a7fabd237f613ba854532
parentd233e3e53a03a08ad1cf965dda1a117cdc5883f2 (diff)
downloadorg.eclipse.papyrus-b740c91e47696af9ce99dff6ae6eb5e4c969135a.tar.gz
org.eclipse.papyrus-b740c91e47696af9ce99dff6ae6eb5e4c969135a.tar.xz
org.eclipse.papyrus-b740c91e47696af9ce99dff6ae6eb5e4c969135a.zip
430099: [Diagram] Snap to Grid for elements dropped from the
ModelExplorer is ignored https://bugs.eclipse.org/bugs/show_bug.cgi?id=430099
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DeferredSnapToGridCommand.java247
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/snap/NodeSnapHelper.java6
-rw-r--r--plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.common/src-common-gmf/org/eclipse/papyrus/gmf/diagram/common/edit/policy/CommonDiagramDragDropEditPolicy.java12
-rw-r--r--plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.internalblock/src/org/eclipse/papyrus/sysml/diagram/internalblock/utils/BlockDropHelper.java17
-rw-r--r--plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/listeners/DropTargetListener.java6
5 files changed, 285 insertions, 3 deletions
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DeferredSnapToGridCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DeferredSnapToGridCommand.java
new file mode 100644
index 00000000000..ad47a39aad6
--- /dev/null
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DeferredSnapToGridCommand.java
@@ -0,0 +1,247 @@
+/*****************************************************************************
+ * 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:
+ *
+ * CEALIST - Initial API and implementation (Adapted code from DeferredLayoutCommand)
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.gmfdiag.common.commands;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.draw2d.IFigure;
+import org.eclipse.draw2d.geometry.PrecisionRectangle;
+import org.eclipse.emf.transaction.RunnableWithResult;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
+import org.eclipse.gef.SnapToHelper;
+import org.eclipse.gef.commands.CompoundCommand;
+import org.eclipse.gef.requests.ChangeBoundsRequest;
+import org.eclipse.gmf.runtime.common.core.command.CommandResult;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.BorderedBorderItemEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.util.EditPartUtil;
+import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.papyrus.infra.gmfdiag.common.snap.BorderNodeSnapHelper;
+import org.eclipse.papyrus.infra.gmfdiag.common.snap.NodeSnapHelper;
+
+/**
+ * This command is used to snap editparts on the grid, when only the view
+ * adapters are available at the time of creating the command. It is necessary
+ * to have the editparts when creating a snap command so this command defers
+ * the creation of the layout command until execution time at which point it can
+ * get the editparts from the editpart registry using the view adapters.
+ *
+ * @author vlorenzo
+ */
+//TODO : creation from the palette should use me
+//TODO : move action could use me???
+public class DeferredSnapToGridCommand extends AbstractTransactionalCommand {
+
+ /** the IAdaptables from which an View can be retrieved */
+ protected List<?> viewAdapters;
+
+ /** the diagram editpart used to get the editpart registry */
+ protected IGraphicalEditPart containerEP;
+
+ /**
+ * Constructor for <code>DefferedSnapToGridCommand</code>.
+ *
+ * @param editingDomain
+ * the editing domain through which model changes are made
+ * @param viewAdapters
+ * the IAdaptables from which an IView can be retrieved
+ * @param containerEP
+ * the container editpart used to get the editpart registry
+ */
+ public DeferredSnapToGridCommand(TransactionalEditingDomain editingDomain, List<?> viewAdapters, IGraphicalEditPart containerEP) {
+ super(editingDomain, "Deferred Snap to grid command", null); //$NON-NLS-1$
+ this.viewAdapters = viewAdapters;
+ this.containerEP = containerEP;
+ }
+
+ /**
+ *
+ * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#getAffectedFiles()
+ *
+ * @return
+ */
+ @SuppressWarnings("rawtypes")
+ public List getAffectedFiles() {
+ if(containerEP != null) {
+ View view = (View)containerEP.getModel();
+ if(view != null) {
+ IFile f = WorkspaceSynchronizer.getFile(view.eResource());
+ return f != null ? Collections.singletonList(f) : Collections.EMPTY_LIST;
+ }
+ }
+ return super.getAffectedFiles();
+ }
+
+ /**
+ * Executes a layout command with all the editparts for the view adapters.
+ *
+ */
+ protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
+
+ final RunnableWithResult<List<IGraphicalEditPart>> refreshRunnable = new RunnableWithResult<List<IGraphicalEditPart>>() {
+
+ private IStatus status;
+
+ private List<IGraphicalEditPart> result;
+
+ public List<IGraphicalEditPart> getResult() {
+ return result;
+ }
+
+ public void setStatus(IStatus status) {
+ this.status = status;
+ }
+
+ public IStatus getStatus() {
+ return status;
+ }
+
+ public void run() {
+ containerEP.refresh();
+
+ // The layout command requires that the figure world is updated.
+ getContainerFigure().invalidate();
+ getContainerFigure().validate();
+
+ List<IGraphicalEditPart> editParts = new ArrayList<IGraphicalEditPart>(viewAdapters.size());
+ Map<?, ?> epRegistry = containerEP.getRoot().getViewer().getEditPartRegistry();
+ for(Iterator<?> iter = viewAdapters.iterator(); iter.hasNext();) {
+ IAdaptable ad = (IAdaptable)iter.next();
+ View view = (View)ad.getAdapter(View.class);
+ Object ep = epRegistry.get(view);
+ if(ep instanceof IGraphicalEditPart) {
+ editParts.add((IGraphicalEditPart)ep);
+ }
+ }
+
+ if(editParts.isEmpty()) {
+ result = editParts;
+ return;
+ }
+
+ //probably useless
+ // Set<IGraphicalEditPart> layoutSet = new HashSet<IGraphicalEditPart>(editParts.size());
+ // layoutSet.addAll(editParts);
+ //
+ // // refresh source and target connections of any shapes in the container not being considered for layout
+ // Iterator<?> iter = containerEP.getChildren().iterator();
+ // while(iter.hasNext()) {
+ // Object obj = iter.next();
+ // if(!layoutSet.contains(obj) && obj instanceof IGraphicalEditPart) {
+ // IGraphicalEditPart ep = (IGraphicalEditPart)obj;
+ // ep.refresh();
+ // }
+ // }
+
+ result = editParts;
+ }
+ };
+
+ EditPartUtil.synchronizeRunnableToMainThread(containerEP, refreshRunnable);
+ List<IGraphicalEditPart> editParts = refreshRunnable.getResult();
+ if(editParts == null || editParts.isEmpty()) {
+ return CommandResult.newOKCommandResult();
+ }
+
+ // add an arrange command, to layout the related shapes
+ CompoundCommand cc = new CompoundCommand("Snap Command"); //$NON-NLS-1$
+ for(final IGraphicalEditPart current : editParts) {
+ final SnapToHelper snapHelper = (SnapToHelper)((IAdaptable)current).getAdapter(SnapToHelper.class);
+ final NodeSnapHelper nodeSnapHelper;
+ final PrecisionRectangle boundsFigure = new PrecisionRectangle(((GraphicalEditPart)current).getFigure().getBounds());
+ current.getFigure().translateToAbsolute(boundsFigure);
+ final PrecisionRectangle result = new PrecisionRectangle(boundsFigure);
+ if(current instanceof BorderedBorderItemEditPart) {
+ nodeSnapHelper = new BorderNodeSnapHelper(snapHelper, boundsFigure);
+ } else {
+ nodeSnapHelper = new NodeSnapHelper(snapHelper, boundsFigure);
+ }
+ ChangeBoundsRequest request = new ChangeBoundsRequest("move"); //$NON-NLS-1$
+ request.setEditParts(Collections.singletonList(current));
+ request.setSnapToEnabled(true);
+ request.setLocation(boundsFigure.getLocation());
+ nodeSnapHelper.snapPoint(request);
+ request.setLocation(result.getLocation());
+ cc.add(((GraphicalEditPart)current).getCommand(request));
+ }
+
+
+ if(!cc.isEmpty() && cc.canExecute()) {
+ cc.execute();
+ }
+ return CommandResult.newOKCommandResult();
+ }
+
+ /**
+ *
+ * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#cleanup()
+ *
+ */
+ protected void cleanup() {
+ containerEP = null;//for garbage collection
+ viewAdapters = null;
+ super.cleanup();
+ }
+
+ /**
+ * gets the container edit part's figure
+ *
+ * @return the container figure
+ */
+ protected IFigure getContainerFigure() {
+ return containerEP.getFigure();
+ }
+
+ /**
+ * gets the container edit part
+ *
+ * @return the container edit part
+ */
+ protected IGraphicalEditPart getContainerEP() {
+ return containerEP;
+ }
+
+ /**
+ * gets a list of <code>IAdaptable</code> that can adapt to <code>
+ * View</code>
+ *
+ * @return view adapters
+ */
+ protected List<?> getViewAdapters() {
+ return viewAdapters;
+ }
+
+ /**
+ *
+ * @see org.eclipse.core.commands.operations.AbstractOperation#canExecute()
+ *
+ * @return
+ */
+ @Override
+ public boolean canExecute() {
+ return super.canExecute() && containerEP != null;
+ }
+}
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/snap/NodeSnapHelper.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/snap/NodeSnapHelper.java
index 329b6229324..25bfe373d68 100644
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/snap/NodeSnapHelper.java
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/snap/NodeSnapHelper.java
@@ -330,9 +330,9 @@ public class NodeSnapHelper {
final Point ref = baseRectTopLeft.getTopLeft();
distVSPoints.put(distance(ref, topLeftCornerDelta), topLeftCornerDelta);
- distVSPoints.put(distance(ref, topRightCornerDelta), topRightCornerDelta);
- distVSPoints.put(distance(ref, bottomLeftCornerDelta), bottomLeftCornerDelta);
- distVSPoints.put(distance(ref, bottomRightCornerDelta), bottomRightCornerDelta);
+ // distVSPoints.put(distance(ref, topRightCornerDelta), topRightCornerDelta);
+ // distVSPoints.put(distance(ref, bottomLeftCornerDelta), bottomLeftCornerDelta);
+ // distVSPoints.put(distance(ref, bottomRightCornerDelta), bottomRightCornerDelta);
}
return distVSPoints;
}
diff --git a/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.common/src-common-gmf/org/eclipse/papyrus/gmf/diagram/common/edit/policy/CommonDiagramDragDropEditPolicy.java b/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.common/src-common-gmf/org/eclipse/papyrus/gmf/diagram/common/edit/policy/CommonDiagramDragDropEditPolicy.java
index 2b0ddf98a55..1733997dcd0 100644
--- a/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.common/src-common-gmf/org/eclipse/papyrus/gmf/diagram/common/edit/policy/CommonDiagramDragDropEditPolicy.java
+++ b/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.common/src-common-gmf/org/eclipse/papyrus/gmf/diagram/common/edit/policy/CommonDiagramDragDropEditPolicy.java
@@ -56,6 +56,8 @@ import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.commands.wrappers.CommandProxyWithResult;
import org.eclipse.papyrus.gmf.diagram.common.provider.IGraphicalTypeRegistry;
+import org.eclipse.papyrus.infra.gmfdiag.common.commands.DeferredSnapToGridCommand;
+import org.eclipse.papyrus.infra.gmfdiag.common.preferences.PreferencesConstantsHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.ViewDescriptorUtil;
import org.eclipse.papyrus.uml.diagram.common.commands.CommonDeferredCreateConnectionViewCommand;
import org.eclipse.papyrus.uml.diagram.common.commands.SemanticAdapter;
@@ -161,6 +163,16 @@ public abstract class CommonDiagramDragDropEditPolicy extends DiagramDragDropEdi
Command dropCommand = new ICommandProxy(gmfDropCommand);
completeDropCommand.add(dropCommand.chain(refreshCommand));
completeDropCommand.add(arrangeCommand);
+
+ //add snap command if required
+ //430099: [Diagram] Snap to Grid for elements dropped from the ModelExplorer is ignored
+ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=430099
+ final Object value = dropRequest.getExtendedData().get(PreferencesConstantsHelper.SNAP_TO_GRID_CONSTANT);
+ if(value instanceof Boolean && Boolean.TRUE.equals(value)) {
+ DeferredSnapToGridCommand snapCommand = new DeferredSnapToGridCommand(getEditingDomain(), newValues, (IGraphicalEditPart)getHost());
+ completeDropCommand.add(new ICommandProxy(snapCommand));
+ }
+
}
return completeDropCommand;
diff --git a/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.internalblock/src/org/eclipse/papyrus/sysml/diagram/internalblock/utils/BlockDropHelper.java b/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.internalblock/src/org/eclipse/papyrus/sysml/diagram/internalblock/utils/BlockDropHelper.java
index 25b35621f7b..e662f87e867 100644
--- a/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.internalblock/src/org/eclipse/papyrus/sysml/diagram/internalblock/utils/BlockDropHelper.java
+++ b/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.internalblock/src/org/eclipse/papyrus/sysml/diagram/internalblock/utils/BlockDropHelper.java
@@ -13,6 +13,8 @@
*****************************************************************************/
package org.eclipse.papyrus.sysml.diagram.internalblock.utils;
+import java.util.Collections;
+
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
@@ -32,6 +34,8 @@ import org.eclipse.gmf.runtime.emf.type.core.ISpecializationType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.papyrus.infra.gmfdiag.common.commands.DeferredSnapToGridCommand;
+import org.eclipse.papyrus.infra.gmfdiag.common.preferences.PreferencesConstantsHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.ViewDescriptorUtil;
import org.eclipse.papyrus.infra.services.edit.commands.ConfigureFeatureCommandFactory;
import org.eclipse.papyrus.infra.services.edit.commands.IConfigureCommandFactory;
@@ -114,6 +118,12 @@ public class BlockDropHelper extends ElementHelper {
cc.add(new ICommandProxy(createElementCommand));
cc.add(viewCreateCommand);
+ final Object value = request.getExtendedData().get(PreferencesConstantsHelper.SNAP_TO_GRID_CONSTANT);
+ if(value instanceof Boolean && Boolean.TRUE.equals(value)) {
+ DeferredSnapToGridCommand snapCommand = new DeferredSnapToGridCommand(getEditingDomain(), Collections.singletonList(descriptor), host);
+ cc.add(new ICommandProxy(snapCommand));
+ }
+
return cc;
}
@@ -159,10 +169,17 @@ public class BlockDropHelper extends ElementHelper {
createViewRequest.setLocation(request.getLocation().getCopy());
Command viewCreateCommand = host.getCommand(createViewRequest);
+
// 3. Create the compound command
cc.add(new ICommandProxy(createElementCommand));
cc.add(viewCreateCommand);
+ final Object value = request.getExtendedData().get(PreferencesConstantsHelper.SNAP_TO_GRID_CONSTANT);
+ if(value instanceof Boolean && Boolean.TRUE.equals(value)) {
+ DeferredSnapToGridCommand snapCommand = new DeferredSnapToGridCommand(getEditingDomain(), Collections.singletonList(descriptor), host);
+ cc.add(new ICommandProxy(snapCommand));
+ }
+
return cc;
}
diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/listeners/DropTargetListener.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/listeners/DropTargetListener.java
index 50c795a36fb..915428e9cfc 100644
--- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/listeners/DropTargetListener.java
+++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/listeners/DropTargetListener.java
@@ -23,7 +23,9 @@ import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.Request;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramDropTargetListener;
+import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramGraphicalViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.infra.gmfdiag.common.preferences.PreferencesConstantsHelper;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;
@@ -46,6 +48,10 @@ public abstract class DropTargetListener extends DiagramDropTargetListener {
Request r = super.createTargetRequest();
if(r != null && r.getExtendedData() != null) {
r.getExtendedData().put(EVENT_DETAIL, getCurrentEvent().detail);
+ //430099: [Diagram] Snap to Grid for elements dropped from the ModelExplorer is ignored
+ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=430099
+ boolean isSnapping = ((DiagramGraphicalViewer)getViewer()).getWorkspaceViewerPreferenceStore().getBoolean(PreferencesConstantsHelper.SNAP_TO_GRID_CONSTANT);
+ r.getExtendedData().put(PreferencesConstantsHelper.SNAP_TO_GRID_CONSTANT, isSnapping);
}
return r;
}

Back to the top