Skip to main content
summaryrefslogtreecommitdiffstats
blob: ab5c65ab2bbd5dc70e48a0b9efeb90e2d357a686 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package org.eclipse.papyrus.infra.gmfdiag.common.linklf;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
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.gef.EditPartViewer;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.SnapToGrid;
import org.eclipse.gmf.runtime.draw2d.ui.figures.FigureUtilities;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.SlidableAnchor;

public class SlidableSnapToGridAnchor extends SlidableAnchor {

	private EditPartViewer myGridProvider;

	public SlidableSnapToGridAnchor(NodeFigure f, PrecisionPoint p) {
		super(f, p);
	}

	public void setEditPartViewer(EditPartViewer viewer) {
		myGridProvider = viewer;
	}

	@Override
	public Point getOrthogonalLocation(Point orthoReference) {
		//		System.err.println("SlidableSnapToGridAnchor.getOrthogonalLocation(): " + orthoReference);
		//		System.err.println("\t" + this);
		//		Point ownRef = getReferencePoint();
		//		if (ownRef != null && ownRef.y > 430 && ownRef.y < 440) {
		//			System.err.println("here");
		//		}

		Point result = super.getOrthogonalLocation(orthoReference);
		return result;
	}

	private boolean myReentryLock = false;

	@Override
	public Point getLocation(Point reference) {
		//		System.err.println("SlidableSnapToGridAnchor.getLocation(): " + reference);
		//		System.err.println("\t" + this);

		Rectangle gridSpecAbs = getAbsoluteGridSpec();
		if (gridSpecAbs != null && !myReentryLock) {
			myReentryLock = true;
			try {
				double gridX = gridSpecAbs.preciseWidth();
				double gridY = gridSpecAbs.preciseWidth();
				Point gridOrigin = gridSpecAbs.getLocation();

				Point notOnGrid = super.getLocation(reference).getCopy();
				PrecisionRectangle bounds = new PrecisionRectangle(FigureUtilities.getAnchorableFigureBounds(getOwner()));
				getOwner().translateToAbsolute(bounds);

				List<Point> onVerticalGridLocs = new LinkedList<Point>();
				PrecisionPoint fakeRefAbove = new PrecisionPoint(notOnGrid);
				PrecisionPoint fakeRefBelow = new PrecisionPoint(notOnGrid);
				fakeRefAbove.setPreciseY(bounds.preciseY() - bounds.preciseHeight() / 2);
				fakeRefBelow.setPreciseY(bounds.preciseY() + bounds.preciseHeight() / 2 + bounds.preciseHeight());

				double reminderX = Math.IEEEremainder(notOnGrid.preciseX() - gridOrigin.preciseX(), gridX);
				if (reminderX < 0) {
					reminderX += gridX;
				}
				for (double nextX = notOnGrid.preciseX() - reminderX; nextX >= bounds.preciseX(); nextX -= gridX) {
					fakeRefAbove.setPreciseX(nextX);
					fakeRefBelow.setPreciseX(nextX);
					Point onGridForAboveRef = getOrthogonalLocation2(fakeRefAbove);
					Point onGridForBelowRef = getOrthogonalLocation2(fakeRefBelow);
					onVerticalGridLocs.add(onGridForAboveRef);
					if (!onGridForBelowRef.equals(onGridForAboveRef)) {
						onVerticalGridLocs.add(onGridForBelowRef);
					}
				}
				for (double nextX = gridX + notOnGrid.preciseX() - reminderX; nextX <= bounds.preciseX() + bounds.preciseWidth(); nextX += gridX) {
					fakeRefAbove.setPreciseX(nextX);
					fakeRefBelow.setPreciseX(nextX);
					Point onGridForAboveRef = getOrthogonalLocation2(fakeRefAbove);
					Point onGridForBelowRef = getOrthogonalLocation2(fakeRefBelow);
					onVerticalGridLocs.add(onGridForAboveRef);
					if (!onGridForBelowRef.equals(onGridForAboveRef)) {
						onVerticalGridLocs.add(onGridForBelowRef);
					}
				}

				List<Point> onHorizontalGridLocs = new LinkedList<Point>();
				PrecisionPoint fakeRefLeft = new PrecisionPoint(notOnGrid);
				PrecisionPoint fakeRefRight = new PrecisionPoint(notOnGrid);
				fakeRefLeft.setPreciseX(bounds.preciseX() - bounds.preciseWidth() / 2);
				fakeRefRight.setPreciseX(bounds.preciseX() + bounds.preciseWidth() + bounds.preciseWidth() / 2);
				double reminderY = Math.IEEEremainder(notOnGrid.preciseY() - gridOrigin.preciseY(), gridY);
				if (reminderY < 0) {
					reminderY += gridY;
				}
				for (double nextY = notOnGrid.preciseY() - reminderY; nextY >= bounds.preciseY(); nextY -= gridY) {
					fakeRefLeft.setPreciseY(nextY);
					fakeRefRight.setPreciseY(nextY);
					Point onGridForLeftRef = getOrthogonalLocation2(fakeRefLeft);
					Point onGridForRightRef = getOrthogonalLocation2(fakeRefRight);
					onHorizontalGridLocs.add(onGridForLeftRef);
					if (!onGridForLeftRef.equals(onGridForRightRef)) {
						onHorizontalGridLocs.add(onGridForRightRef);
					}
				}
				for (double nextY = gridY + notOnGrid.preciseY() - reminderY; nextY <= bounds.preciseY() + bounds.preciseHeight(); nextY += gridY) {
					fakeRefLeft.setPreciseY(nextY);
					fakeRefRight.setPreciseY(nextY);
					Point onGridForLeftRef = getOrthogonalLocation2(fakeRefLeft);
					Point onGridForRightRef = getOrthogonalLocation2(fakeRefRight);
					onHorizontalGridLocs.add(onGridForLeftRef);
					if (!onGridForLeftRef.equals(onGridForRightRef)) {
						onHorizontalGridLocs.add(onGridForRightRef);
					}
				}

				//System.err.println("Found: on horizontal grid: " + onHorizontalGridLocs);
				//System.err.println("Found: on vertical grid: " + onVerticalGridLocs);

				if (!onHorizontalGridLocs.isEmpty() || !onVerticalGridLocs.isEmpty()) {
					return pickClosestPointToSet(getReferencePoint(), onHorizontalGridLocs, onVerticalGridLocs);
				}
			} finally {
				myReentryLock = false;
			}
		}

		Point result = super.getLocation(reference);
		return result;
	}

	@Override
	public String toString() {
		return "SSTGA:" + "terminal: " + getTerminal() + ", terminalLoc: " + getReferencePoint() + ", box: " + getBox();
	}

	/**
	 * @deprecated copy-pasted from super class, [GMFRT] make protected
	 */
	@Deprecated
	private Point getOrthogonalLocation2(Point orthoReference) {
		PrecisionPoint ownReference = new PrecisionPoint(getReferencePoint());
		PrecisionRectangle bounds = new PrecisionRectangle(FigureUtilities.getAnchorableFigureBounds(getOwner()));
		getOwner().translateToAbsolute(bounds);
		bounds.expand(0.000001, 0.000001);
		PrecisionPoint preciseOrthoReference = new PrecisionPoint(orthoReference);
		int orientation = PositionConstants.NONE;
		if (bounds.contains(preciseOrthoReference)) {
			int side = getClosestSide2(ownReference, bounds);
			switch (side) {
			case PositionConstants.LEFT:
			case PositionConstants.RIGHT:
				ownReference.preciseY = preciseOrthoReference.preciseY();
				orientation = PositionConstants.HORIZONTAL;
				break;
			case PositionConstants.TOP:
			case PositionConstants.BOTTOM:
				ownReference.preciseX = preciseOrthoReference.preciseX();
				orientation = PositionConstants.VERTICAL;
				break;
			}
		} else if (preciseOrthoReference.preciseX >= bounds.preciseX && preciseOrthoReference.preciseX <= bounds.preciseX + bounds.preciseWidth) {
			ownReference.preciseX = preciseOrthoReference.preciseX;
			orientation = PositionConstants.VERTICAL;
		} else if (preciseOrthoReference.preciseY >= bounds.preciseY && preciseOrthoReference.preciseY <= bounds.preciseY + bounds.preciseHeight) {
			ownReference.preciseY = preciseOrthoReference.preciseY;
			orientation = PositionConstants.HORIZONTAL;
		}
		ownReference.updateInts();

		Point location = getLocation(ownReference, preciseOrthoReference);
		if (location == null) {
			location = getLocation(orthoReference);
			orientation = PositionConstants.NONE;
		}

		if (orientation != PositionConstants.NONE) {
			PrecisionPoint loc = new PrecisionPoint(location);
			if (orientation == PositionConstants.VERTICAL) {
				loc.preciseX = preciseOrthoReference.preciseX;
			} else {
				loc.preciseY = preciseOrthoReference.preciseY;
			}
			loc.updateInts();
			location = loc;
		}

		return location;
	}

	/**
	 * Returns the position of the closest edge of the rectangle closest to the point
	 * @param p the point
	 * @param r the rectangle
	 * @return position of the closest edge
	 * @deprecated copy-pasted from super class, [GMFRT] make protected
	 */
	@Deprecated
	private static int getClosestSide2(Point p, Rectangle r) {
		double diff = Math.abs(r.preciseX() + r.preciseWidth() - p.preciseX());
		int side = PositionConstants.RIGHT;
		double currentDiff = Math.abs(r.preciseX() - p.preciseX());
		if (currentDiff < diff) {
			diff = currentDiff;
			side = PositionConstants.LEFT;
		}
		currentDiff = Math.abs(r.preciseY() + r.preciseHeight() - p.preciseY());
		if (currentDiff < diff) {
			diff = currentDiff;
			side = PositionConstants.BOTTOM;
		}
		currentDiff = Math.abs(r.preciseY() - p.preciseY());
		if (currentDiff < diff) {
			diff = currentDiff;
			side = PositionConstants.TOP;
		}
		return side;
	}

	/**
	 * If grid provider had been set up and has grid enabled then returns active grid specification in absolute coordinates. 
	 * Otherwise returns null.
	 * @return <code>null</code> if no active grid or grid provider had not been set up. 
	 */
	protected Rectangle getAbsoluteGridSpec() {
		return myGridProvider == null ? null : getAbsoluteGridSpec(myGridProvider);
	}

	/**
	 * Computes actual grid specification (origin + single cell width and height). Translates result into the absolute coordinates.
	 * @param viewer
	 * @return <code>null</code> if grid is not enabled or absolute grid specification
	 */
	protected static PrecisionRectangle getAbsoluteGridSpec(EditPartViewer viewer) {
		Boolean enabled = (Boolean) viewer.getProperty(SnapToGrid.PROPERTY_GRID_ENABLED);
		if (enabled == null || !enabled) {
			return null;
		}
		double gridX = 0;
		double gridY = 0;
		Dimension spacing = (Dimension) viewer.getProperty(SnapToGrid.PROPERTY_GRID_SPACING);
		if (spacing != null) {
			gridX = spacing.preciseWidth();
			gridY = spacing.preciseHeight();
		}
		if (gridX <= 0) {
			gridX = SnapToGrid.DEFAULT_GRID_SIZE;
		}
		if (gridY <= 0) {
			gridY = SnapToGrid.DEFAULT_GRID_SIZE;
		}
		Point origin = (Point) viewer.getProperty(SnapToGrid.PROPERTY_GRID_ORIGIN);
		PrecisionRectangle result = new PrecisionRectangle(//
				origin == null ? 0 : origin.preciseX(), origin == null ? 0 : origin.preciseY(), gridX, gridY);

		GraphicalEditPart diagramEP = (GraphicalEditPart) viewer.getContents();
		diagramEP.getContentPane().translateToAbsolute(result);

		return result;
	}

	private static Point pickClosestPointToSet(Point source, Collection<? extends Point> set1, Collection<? extends Point> set2) {
		double bestDistSquared = Double.MAX_VALUE;
		Point result = null;
		for (Point next : set1) {
			double nextDistSquared = source.getDistance(next);
			if (nextDistSquared < bestDistSquared) {
				result = next;
				bestDistSquared = nextDistSquared;
			}
		}
		for (Point next : set2) {
			double nextDistSquared = source.getDistance(next);
			if (nextDistSquared < bestDistSquared) {
				result = next;
				bestDistSquared = nextDistSquared;
			}
		}
		return result;
	}

}

Back to the top