diff options
author | vlorenzo | 2014-02-19 11:43:46 +0000 |
---|---|---|
committer | vlorenzo | 2014-02-19 12:24:50 +0000 |
commit | c16ef5a1249f77d2e8fde41a289b18bbe34f4814 (patch) | |
tree | fece313af298681d6016f4b1367730c9020e77ac | |
parent | 2f0f1cd9f54523198f4f81ed876855e337462891 (diff) | |
download | org.eclipse.papyrus-c16ef5a1249f77d2e8fde41a289b18bbe34f4814.tar.gz org.eclipse.papyrus-c16ef5a1249f77d2e8fde41a289b18bbe34f4814.tar.xz org.eclipse.papyrus-c16ef5a1249f77d2e8fde41a289b18bbe34f4814.zip |
427129: Figures newly created via the palette should be snapped to grid
if "snap to grid" is activated
https://bugs.eclipse.org/bugs/show_bug.cgi?id=427129
3 files changed, 204 insertions, 35 deletions
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/XYLayoutWithConstrainedResizedEditPolicy.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/XYLayoutWithConstrainedResizedEditPolicy.java index 766cbf5c445..87d508506f5 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/XYLayoutWithConstrainedResizedEditPolicy.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/XYLayoutWithConstrainedResizedEditPolicy.java @@ -13,21 +13,34 @@ *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.editpolicies;
+import java.util.Iterator;
import java.util.List;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Point;
+import org.eclipse.draw2d.geometry.PrecisionPoint;
+import org.eclipse.draw2d.geometry.PrecisionRectangle;
+import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.NodeEditPart;
+import org.eclipse.gef.SnapToHelper;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.requests.ChangeBoundsRequest;
+import org.eclipse.gef.requests.CreateRequest;
+import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
+import org.eclipse.gmf.runtime.diagram.ui.commands.SetBoundsCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramRootEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.INodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.XYLayoutEditPolicy;
+import org.eclipse.gmf.runtime.diagram.ui.figures.LayoutHelper;
+import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramUIMessages;
+import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest;
+import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.gmfdiag.common.Activator;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.FixAnchorHelper;
@@ -43,6 +56,72 @@ public class XYLayoutWithConstrainedResizedEditPolicy extends XYLayoutEditPolicy private FixAnchorHelper helper = null;
/**
+ * Called in response to a <tt>REQ_CREATE</tt> request. Returns a command
+ * to set each created element bounds and auto-size properties.
+ *
+ * @param request
+ * a create request (understands instances of {@link CreateViewRequest}).
+ * @return a command to satisfy the request; <tt>null</tt> if the request is not
+ * understood.
+ */
+ protected Command getCreateCommand(CreateRequest request) {
+ CreateViewRequest req = (CreateViewRequest)request;
+
+
+ TransactionalEditingDomain editingDomain = ((IGraphicalEditPart)getHost()).getEditingDomain();
+
+ CompositeTransactionalCommand cc = new CompositeTransactionalCommand(editingDomain, DiagramUIMessages.AddCommand_Label);
+ Iterator<?> iter = req.getViewDescriptors().iterator();
+ final Rectangle BOUNDS = (Rectangle)getConstraintFor(request);
+ boolean couldBeSnaped = request.getLocation().equals(LayoutHelper.UNDEFINED.getLocation()) && req.isSnapToEnabled();
+ while(iter.hasNext()) {
+ CreateViewRequest.ViewDescriptor viewDescriptor = (CreateViewRequest.ViewDescriptor)iter.next();
+ Rectangle rect = getBoundsOffest(req, BOUNDS, viewDescriptor);
+
+ //see bug 427129: Figures newly created via the palette should be snapped to grid if "snap to grid" is activated
+ if(couldBeSnaped) {
+ //this code fix the bug in some case...
+ int add = 0;
+ DiagramRootEditPart drep = (DiagramRootEditPart)getHost().getRoot();
+ double spacing = drep.getGridSpacing();
+ final double max_value = spacing * 20;
+ final SnapToHelper helper = (SnapToHelper)getHost().getAdapter(SnapToHelper.class);
+ final LayoutHelper layoutHelper = new LayoutHelper();
+ while(add < max_value) {//we define a max value to do test
+ Rectangle LOCAL_BOUNDS = BOUNDS.getCopy();
+ LOCAL_BOUNDS.translate(add, add);
+ Rectangle tmp_rect = getBoundsOffest(req, LOCAL_BOUNDS, viewDescriptor);
+ final PrecisionRectangle resultRect = new PrecisionRectangle(tmp_rect);
+ resultRect.setWidth(-1);
+ resultRect.setHeight(-1);
+ PrecisionPoint res1 = new PrecisionPoint(tmp_rect.getLocation());
+ helper.snapPoint(request, PositionConstants.NORTH_WEST, res1.getPreciseCopy(), res1);
+ final Point pt = layoutHelper.validatePosition(getHostFigure(), resultRect.setLocation(res1));
+ if(couldBeSnaped) {
+ if(pt.equals(resultRect.getLocation())) {
+ rect.setLocation(resultRect.getLocation());
+ break;
+ } else {
+ add += spacing;
+ continue;
+ }
+ }
+ }
+ }
+
+ // Default set bounds command.
+ cc.compose(new SetBoundsCommand(editingDomain, DiagramUIMessages.SetLocationCommand_Label_Resize, viewDescriptor, rect));
+ break;
+
+ }
+
+ if(cc.reduce() == null) {
+ return null;
+ }
+ return chainGuideAttachmentCommands(request, new ICommandProxy(cc.reduce()));
+ }
+
+ /**
* Returns the <code>Command</code> to resize a group of children.
*
* @param request
diff --git a/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.common/src-common-gmf/org/eclipse/papyrus/gmf/diagram/common/edit/policy/DefaultXYLayoutEditPolicy.java b/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.common/src-common-gmf/org/eclipse/papyrus/gmf/diagram/common/edit/policy/DefaultXYLayoutEditPolicy.java index b347f29b0b6..29bf93747f3 100644 --- a/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.common/src-common-gmf/org/eclipse/papyrus/gmf/diagram/common/edit/policy/DefaultXYLayoutEditPolicy.java +++ b/plugins/sysml/diagram/org.eclipse.papyrus.sysml.diagram.common/src-common-gmf/org/eclipse/papyrus/gmf/diagram/common/edit/policy/DefaultXYLayoutEditPolicy.java @@ -15,73 +15,101 @@ package org.eclipse.papyrus.gmf.diagram.common.edit.policy; import java.util.Iterator; +import org.eclipse.draw2d.PositionConstants; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.PrecisionPoint; +import org.eclipse.draw2d.geometry.PrecisionRectangle; import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gef.SnapToHelper; import org.eclipse.gef.commands.Command; import org.eclipse.gef.requests.CreateRequest; import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; import org.eclipse.gmf.runtime.diagram.ui.commands.SetBoundsCommand; +import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramRootEditPart; import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; +import org.eclipse.gmf.runtime.diagram.ui.figures.LayoutHelper; import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramUIMessages; import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest; import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand; import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.XYLayoutWithConstrainedResizedEditPolicy; /** - * Avoid forcing (-1, -1) dimension in set bounds command (let the view factories deal with + * Avoid forcing (-1, -1) dimension in set bounds command (let the view factories deal with * size initialization if needed). */ public class DefaultXYLayoutEditPolicy extends XYLayoutWithConstrainedResizedEditPolicy { + protected double spacing = 80; + /** * Called in response to a <tt>REQ_CREATE</tt> request. Returns a command * to set each created element bounds and auto-size properties. * - * @param request a create request (understands instances of {@link CreateViewRequest}). + * @param request + * a create request (understands instances of {@link CreateViewRequest}). * @return a command to satisfy the request; <tt>null</tt> if the request is not - * understood. + * understood. */ protected Command getCreateCommand(CreateRequest request) { - CreateViewRequest req = (CreateViewRequest) request; - - - TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()) - .getEditingDomain(); - - CompositeTransactionalCommand cc = new CompositeTransactionalCommand( - editingDomain, DiagramUIMessages.AddCommand_Label); - Iterator<?> iter = req.getViewDescriptors().iterator(); + CreateViewRequest req = (CreateViewRequest)request; - final Rectangle BOUNDS = (Rectangle) getConstraintFor(request); + TransactionalEditingDomain editingDomain = ((IGraphicalEditPart)getHost()).getEditingDomain(); - - while (iter.hasNext()) { - CreateViewRequest.ViewDescriptor viewDescriptor = (CreateViewRequest.ViewDescriptor)iter.next(); - Rectangle rect = getBoundsOffest(req, BOUNDS,viewDescriptor); + CompositeTransactionalCommand cc = new CompositeTransactionalCommand(editingDomain, DiagramUIMessages.AddCommand_Label); + Iterator<?> iter = req.getViewDescriptors().iterator(); + final Rectangle BOUNDS = (Rectangle)getConstraintFor(request); + boolean couldBeSnaped = request.getLocation().equals(LayoutHelper.UNDEFINED.getLocation()) && req.isSnapToEnabled(); + while(iter.hasNext()) { + CreateViewRequest.ViewDescriptor viewDescriptor = (CreateViewRequest.ViewDescriptor)iter.next(); + Rectangle rect = getBoundsOffest(req, BOUNDS, viewDescriptor); - if (rect.getSize().isEmpty()) { + //see bug 427129: Figures newly created via the palette should be snapped to grid if "snap to grid" is activated + if(couldBeSnaped) { + //this code fix the bug in some case... + int add = 0; + DiagramRootEditPart drep = (DiagramRootEditPart)getHost().getRoot(); + double spacing = drep.getGridSpacing(); + final double max_value = spacing*20; + final SnapToHelper helper = (SnapToHelper)getHost().getAdapter(SnapToHelper.class); + final LayoutHelper layoutHelper = new LayoutHelper(); + while(add < max_value) {//we define a max value to do test + Rectangle LOCAL_BOUNDS = BOUNDS.getCopy(); + LOCAL_BOUNDS.translate(add, add); + Rectangle tmp_rect = getBoundsOffest(req, LOCAL_BOUNDS, viewDescriptor); + final PrecisionRectangle resultRect = new PrecisionRectangle(tmp_rect); + resultRect.setWidth(-1); + resultRect.setHeight(-1); + PrecisionPoint res1 = new PrecisionPoint(tmp_rect.getLocation()); + helper.snapPoint(request, PositionConstants.NORTH_WEST, res1.getPreciseCopy(), res1); + final Point pt = layoutHelper.validatePosition(getHostFigure(), resultRect.setLocation(res1)); + if(couldBeSnaped) { + if(pt.equals(resultRect.getLocation())) { + rect.setLocation(resultRect.getLocation()); + break; + } else { + add +=spacing; + continue; + } + } + } + } + if(rect.getSize().isEmpty()) { // Only set location and let the ViewFactory deal with dimension. - cc.compose(new SetBoundsCommand(editingDomain, - DiagramUIMessages.SetLocationCommand_Label_Resize, - viewDescriptor, - rect.getLocation().getCopy())); - + cc.compose(new SetBoundsCommand(editingDomain, DiagramUIMessages.SetLocationCommand_Label_Resize, viewDescriptor, rect.getLocation().getCopy())); + } else { // Default set bounds command. - cc.compose(new SetBoundsCommand(editingDomain, - DiagramUIMessages.SetLocationCommand_Label_Resize, - viewDescriptor, - rect)); - + cc.compose(new SetBoundsCommand(editingDomain, DiagramUIMessages.SetLocationCommand_Label_Resize, viewDescriptor, rect)); } - + break; + } - - if( cc.reduce() == null ) - return null; - return chainGuideAttachmentCommands( request, - new ICommandProxy(cc.reduce())); + if(cc.reduce() == null) { + return null; + } + return chainGuideAttachmentCommands(request, new ICommandProxy(cc.reduce())); } } diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/service/AspectUnspecifiedTypeCreationTool.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/service/AspectUnspecifiedTypeCreationTool.java index 52c7f2f948d..f2fe10a7da9 100644 --- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/service/AspectUnspecifiedTypeCreationTool.java +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.common/src/org/eclipse/papyrus/uml/diagram/common/service/AspectUnspecifiedTypeCreationTool.java @@ -24,14 +24,21 @@ import java.util.List; import java.util.Map; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.Platform; +import org.eclipse.draw2d.PositionConstants; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.PrecisionRectangle; +import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.emf.common.notify.Notification; import org.eclipse.emf.transaction.TransactionalEditingDomain; import org.eclipse.emf.transaction.util.TransactionUtil; import org.eclipse.gef.EditPart; import org.eclipse.gef.EditPartViewer; import org.eclipse.gef.Request; +import org.eclipse.gef.SnapToHelper; import org.eclipse.gef.commands.Command; import org.eclipse.gef.commands.CompoundCommand; +import org.eclipse.gef.requests.CreateRequest; import org.eclipse.gmf.runtime.common.core.command.ICommand; import org.eclipse.gmf.runtime.diagram.core.edithelpers.CreateElementRequestAdapter; import org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker; @@ -56,6 +63,7 @@ import org.eclipse.papyrus.uml.diagram.common.part.PaletteUtil; import org.eclipse.papyrus.uml.diagram.common.service.palette.IAspectAction; import org.eclipse.papyrus.uml.diagram.common.service.palette.IPostAction; import org.eclipse.papyrus.uml.diagram.common.service.palette.IPreAction; +import org.eclipse.swt.SWT; import org.w3c.dom.NodeList; /** @@ -278,7 +286,10 @@ public class AspectUnspecifiedTypeCreationTool extends UnspecifiedTypeCreationTo request = new CreateViewAndElementRequest(viewDescriptor); request.setExtendedData(getExtendedData()); } - + if(request instanceof CreateRequest) { + //see bug 427129: Figures newly created via the palette should be snapped to grid if "snap to grid" is activated + ((CreateRequest)request).setSnapToEnabled(!getCurrentInput().isModKeyDown(MODIFIER_NO_SNAPPING)); + } request.setType(getType()); requests.put(elementType, request); } @@ -299,4 +310,55 @@ public class AspectUnspecifiedTypeCreationTool extends UnspecifiedTypeCreationTo } + @Override + protected void createShapeAt(Point point) { + setTargetEditPart(getCurrentViewer().getRootEditPart().getContents()); + getCreateRequest().setLocation(point); + setCurrentCommand(getCommand()); + performCreation(0); + } + + /** + * Key modifier for ignoring snap while dragging. It's CTRL on Mac, and ALT + * on all other platforms. + */ + static final int MODIFIER_NO_SNAPPING; + + static { + if(Platform.OS_MACOSX.equals(Platform.getOS())) { + MODIFIER_NO_SNAPPING = SWT.CTRL; + } else { + MODIFIER_NO_SNAPPING = SWT.ALT; + } + } + + /** + * + * @see org.eclipse.gef.tools.CreationTool#updateTargetRequest() + * + */ + protected void updateTargetRequest() { + super.updateTargetRequest(); + CreateRequest createRequest = getCreateRequest(); + //see bug 427129: Figures newly created via the palette should be snapped to grid if "snap to grid" is activated + if(!isInState(STATE_DRAG_IN_PROGRESS) && !getCurrentInput().isModKeyDown(MODIFIER_NO_SNAPPING)) { + //allow to do a snap to grid for creation with one click + if(getTargetEditPart() != null) { + SnapToHelper helper = (SnapToHelper)getTargetEditPart().getAdapter(SnapToHelper.class); + Point loq = getLocation(); + Rectangle bounds = new Rectangle(loq, loq); + createRequest.setSnapToEnabled(!getCurrentInput().isModKeyDown(MODIFIER_NO_SNAPPING)); + createRequest.setLocation(bounds.getLocation()); + if(helper != null && createRequest.isSnapToEnabled()) { + + PrecisionRectangle baseRect = new PrecisionRectangle(bounds); + PrecisionRectangle result = baseRect.getPreciseCopy(); + helper.snapRectangle(createRequest, PositionConstants.NORTH_WEST, baseRect, result); + createRequest.setLocation(result.getLocation()); + } + enforceConstraintsForSizeOnDropCreate(createRequest); + } + } + } + } |