From d1374f5b07a3f2e2cff017a2e1e27ac3a5b5e30c Mon Sep 17 00:00:00 2001 From: vlorenzo Date: Wed, 19 Feb 2014 15:57:00 +0100 Subject: 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 FIX NPE on Snap Helper--- .../XYLayoutWithConstrainedResizedEditPolicy.java | 38 ++++++++++--------- .../edit/policy/DefaultXYLayoutEditPolicy.java | 44 +++++++++++----------- 2 files changed, 42 insertions(+), 40 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 87d508506f5..aa77b8fa7f1 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 @@ -86,24 +86,26 @@ public class XYLayoutWithConstrainedResizedEditPolicy extends XYLayoutEditPolicy 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(helper != null) { + 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; + } } } } 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 29bf93747f3..2ad1e4c7118 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 @@ -40,8 +40,6 @@ import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.XYLayoutWithConstra */ public class DefaultXYLayoutEditPolicy extends XYLayoutWithConstrainedResizedEditPolicy { - protected double spacing = 80; - /** * Called in response to a REQ_CREATE request. Returns a command * to set each created element bounds and auto-size properties. @@ -64,33 +62,35 @@ public class DefaultXYLayoutEditPolicy extends XYLayoutWithConstrainedResizedEdi 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 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(helper != null) { + 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; + } } } } -- cgit v1.2.3