Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bdf49e96251c3e1de0d5d2dfc90fc0e147273196 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*******************************************************************************
 * Copyright (c) 2012 THALES GLOBAL SERVICES.
 * 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:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.tools.internal.util;

import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.draw2d.ui.figures.BaseSlidableAnchor;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.IdentityAnchor;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationFactory;
import org.eclipse.gmf.runtime.notation.RelativeBendpoints;
import org.eclipse.gmf.runtime.notation.datatype.RelativeBendpoint;

import com.google.common.collect.Lists;

import org.eclipse.sirius.common.tools.api.util.Option;
import org.eclipse.sirius.common.tools.api.util.Options;
import org.eclipse.sirius.diagram.business.api.query.EdgeQuery;
import org.eclipse.sirius.diagram.internal.refresh.GMFHelper;

/**
 * Utilities for GMF notation model modifications.
 * 
 * @author <a href="mailto:laurent.redor@obeo.fr">Laurent Redor</a>
 */
public final class GMFNotationUtilities {

    /**
     * Error message for case where we detect an edge connected to other edge.
     */
    private static final String MSG_EDGE_ON_EDGE_NOT_MANAGED = "Edge on edge not managed";

    /**
     * Default constructor.
     */
    private GMFNotationUtilities() {

    }

    /**
     * Set the source anchor of the <code>edge</code> according to the new
     * points list. The second point of this list is used to determine the new X
     * location of this Anchor.
     * 
     * @param edge
     *            The edge to modify
     * @param newPoints
     *            The new points list
     * @return an optional Point representing the source reference point
     *         corresponding to the new source anchor. This Option can be null
     *         if the anchor is not changed.
     */
    public static Option<Point> setSourceAnchor(Edge edge, PointList newPoints) {
        Point referencePointOfChangedAnchor = null;
        if (edge.getSource() instanceof Node) {
            // Get the source bounds
            Rectangle sourceFigure = GMFHelper.getBounds((Node) edge.getSource(), true);
            // Get the new x location of anchor according to second points of
            // the
            // newPoints list (this point is the end of the first segment)
            int newXAnchorLocation = newPoints.getPoint(1).x;
            // Compute new X anchor percentage
            double newXAnchorPercentage = ((double) (newXAnchorLocation - sourceFigure.x)) / ((double) sourceFigure.width);

            if (edge.getSourceAnchor() instanceof IdentityAnchor) {
                // Get current anchor position
                IdentityAnchor anchor = (IdentityAnchor) edge.getSourceAnchor();
                PrecisionPoint relativeReferencePoint = BaseSlidableAnchor.parseTerminalString(((IdentityAnchor) anchor).getId());
                if (relativeReferencePoint.preciseX != newXAnchorPercentage) {
                    // Change x id anchor
                    setIdentityAnchorId(anchor, newXAnchorPercentage, relativeReferencePoint.preciseY);
                    referencePointOfChangedAnchor = new Point(sourceFigure.getLocation().x + sourceFigure.width * newXAnchorPercentage, sourceFigure.getLocation().y + sourceFigure.height
                            * relativeReferencePoint.preciseY);
                }
            } else if (edge.getSourceAnchor() == null) {
                // Use the center of the source if there is no previous
                // sourceAnchor
                IdentityAnchor anchor = NotationFactory.eINSTANCE.createIdentityAnchor();
                setIdentityAnchorId(anchor, newXAnchorPercentage, 0.5d);
                edge.setSourceAnchor(anchor);
                referencePointOfChangedAnchor = new Point(sourceFigure.getLocation().x + sourceFigure.width * newXAnchorPercentage, sourceFigure.getLocation().y + sourceFigure.height * 0.5d);
            }
        } else if (edge.getSource() instanceof Edge) {
            throw new UnsupportedOperationException(MSG_EDGE_ON_EDGE_NOT_MANAGED);
        }
        if (referencePointOfChangedAnchor == null) {
            return Options.newNone();
        } else {
            return Options.newSome(referencePointOfChangedAnchor);
        }
    }

    /**
     * Set the target anchor of the <code>edge</code> according to the new
     * points list. The third point of this list is used to determine the new X
     * location of this Anchor.
     * 
     * @param edge
     *            The edge to modify
     * @param newPoints
     *            The new points list
     * @return an optional Point representing the target reference point
     *         corresponding to the new target anchor. This Option can be null
     *         if the anchor is not changed.
     */
    public static Option<Point> setTargetAnchor(Edge edge, PointList newPoints) {
        Point referencePointOfChangedAnchor = null;
        if (edge.getTarget() instanceof Node) {
            // Get the target bounds
            Rectangle targetFigure = GMFHelper.getBounds((Node) edge.getTarget(), true);
            // Get the new x location of anchor according to third points of the
            // newPoints list (this point is the beginning of the last segment)
            int newXAnchorLocation = newPoints.getPoint(2).x;
            // Compute new X anchor percentage
            double newXAnchorPercentage = ((double) (newXAnchorLocation - targetFigure.x)) / ((double) targetFigure.width);

            if (edge.getTargetAnchor() instanceof IdentityAnchor) {
                // Get current anchor position
                IdentityAnchor anchor = (IdentityAnchor) edge.getTargetAnchor();
                PrecisionPoint relativeReferencePoint = BaseSlidableAnchor.parseTerminalString(((IdentityAnchor) anchor).getId());
                if (relativeReferencePoint.preciseX != newXAnchorPercentage) {
                    // Change x id anchor
                    setIdentityAnchorId(anchor, newXAnchorPercentage, relativeReferencePoint.preciseY);
                    referencePointOfChangedAnchor = new Point(targetFigure.getLocation().x + targetFigure.width * newXAnchorPercentage, targetFigure.getLocation().y + targetFigure.height
                            * relativeReferencePoint.preciseY);
                }
            } else if (edge.getTargetAnchor() == null) {
                // Use the center of the source if there is no previous
                // sourceAnchor
                IdentityAnchor anchor = NotationFactory.eINSTANCE.createIdentityAnchor();
                setIdentityAnchorId(anchor, newXAnchorPercentage, 0.5d);
                edge.setTargetAnchor(anchor);
                referencePointOfChangedAnchor = new Point(targetFigure.getLocation().x + targetFigure.width * newXAnchorPercentage, targetFigure.getLocation().y + targetFigure.height * 0.5d);
            }
        } else if (edge.getTarget() instanceof Edge) {
            throw new UnsupportedOperationException(MSG_EDGE_ON_EDGE_NOT_MANAGED);
        }
        if (referencePointOfChangedAnchor == null) {
            return Options.newNone();
        } else {
            return Options.newSome(referencePointOfChangedAnchor);
        }
    }

    /**
     * Set the id of an IdentiyAnchor from x and y percentages.
     * 
     * @param anchor
     *            The anchor on which to set id
     * @param xPercentage
     *            the x percentage to use in id
     * @param yPercentage
     *            the y percentage to use in id
     */
    public static void setIdentityAnchorId(IdentityAnchor anchor, double xPercentage, double yPercentage) {
        anchor.setId(getTerminalString(xPercentage, yPercentage));
    }

    /**
     * Get the terminal string used in {@link IdentityAnchor} from its
     * xPercentage and yPercentage.
     * 
     * @param xPercentage
     *            the x percentage to use in id
     * @param yPercentage
     *            the y percentage to use in id
     * @return the string terminal used in {@link IdentityAnchor}
     */
    public static String getTerminalString(double xPercentage, double yPercentage) {
        return "(" + String.valueOf(xPercentage) + "," + String.valueOf(yPercentage) + ")";
    }

    /**
     * Change the bendpoints of the GMF edge according to draw2d elements
     * (pointList, sourceRefPoint and targetRefPoint).
     * 
     * @param edge
     *            The edge to modify
     * @param newPoints
     *            The new points list
     * @param sourceRefPoint
     *            The source reference point (to compute the GMF
     *            RelativeBendpoints)
     * @param targetRefPoint
     *            The target reference point (to compute the GMF
     *            RelativeBendpoints)
     */
    public static void setGMFBendpoints(Edge edge, PointList newPoints, Point sourceRefPoint, Point targetRefPoint) {
        List<Object> newBendpoints = Lists.newArrayList();
        int numOfPoints = newPoints.size();
        for (short i = 0; i < numOfPoints; i++) {
            Dimension s = newPoints.getPoint(i).getDifference(sourceRefPoint);
            Dimension t = newPoints.getPoint(i).getDifference(targetRefPoint);
            newBendpoints.add(new RelativeBendpoint(s.width, s.height, t.width, t.height));
        }
        RelativeBendpoints points = (RelativeBendpoints) edge.getBendpoints();
        points.setPoints(newBendpoints);
    }

    /**
     * Compute the new source anchor according to an horizontal move
     * representing by the deltaX.
     * 
     * @param edge
     *            The edge to modify
     * @param deltaX
     *            The horizontal delta move
     * 
     * @return an optional Point representing the source reference point
     *         corresponding to the new source. This Option can be null if the
     *         anchor is not changed.
     */
    public static Option<Point> setSourceAnchor(Edge edge, int deltaX) {
        // Compute new x anchor of source point after horizontal source
        // segment movement
        if (edge.getSource() instanceof Node) {
            Rectangle sourceFigure = GMFHelper.getBounds((Node) edge.getSource(), true);
            if (edge.getSourceAnchor() instanceof IdentityAnchor) {
                // Get current anchor position
                IdentityAnchor anchor = (IdentityAnchor) edge.getSourceAnchor();
                PrecisionPoint relativeReferencePoint = BaseSlidableAnchor.parseTerminalString(((IdentityAnchor) anchor).getId());
                // Get the current x location of end segment
                double xCurrentPosition = sourceFigure.x + sourceFigure.width * relativeReferencePoint.preciseX;
                // Compute position of x location of end segment after move
                double xNewPosition = xCurrentPosition - deltaX;
                // Compute new anchor percentage
                double newXAnchorPercentage = ((double) (xNewPosition - sourceFigure.x)) / ((double) sourceFigure.width);
                // Change x id anchor
                setIdentityAnchorId(anchor, newXAnchorPercentage, relativeReferencePoint.preciseY);
                return Options.newSome(new Point(sourceFigure.getLocation().x + sourceFigure.width * newXAnchorPercentage, sourceFigure.getLocation().y + sourceFigure.height
                        * relativeReferencePoint.preciseY));
            }
        } else {
            throw new UnsupportedOperationException(MSG_EDGE_ON_EDGE_NOT_MANAGED);
        }
        return Options.newNone();
    }

    /**
     * Compute the new target anchor according to an horizontal move
     * representing by the deltaX.
     * 
     * @param edge
     *            The edge to modify
     * @param deltaX
     *            The horizontal delta move
     * 
     * @return an optional Point representing the target reference point
     *         corresponding to the new target anchor. This Option can be null
     *         if the anchor is not changed.
     */
    public static Option<Point> setTargetAnchor(Edge edge, int deltaX) {
        // Compute new x anchor of target point after horizontal target
        // segment movement
        if (edge.getTarget() instanceof Node) {
            Rectangle targetFigure = GMFHelper.getBounds((Node) edge.getTarget(), true);
            if (edge.getTargetAnchor() instanceof IdentityAnchor) {
                // Get current anchor position
                IdentityAnchor anchor = (IdentityAnchor) edge.getTargetAnchor();
                PrecisionPoint relativeReferencePoint = BaseSlidableAnchor.parseTerminalString(((IdentityAnchor) anchor).getId());
                // Get the current x location of end segment
                double xCurrentPosition = targetFigure.x + targetFigure.width * relativeReferencePoint.preciseX;
                // Compute position of x location of end segment after move
                double xNewPosition = xCurrentPosition - deltaX;
                // Compute new anchor percentage
                double newXAnchorPercentage = ((double) (xNewPosition - targetFigure.x)) / ((double) targetFigure.width);
                // Change x id anchor
                setIdentityAnchorId(anchor, newXAnchorPercentage, relativeReferencePoint.preciseY);
                return Options.newSome(new Point(targetFigure.getLocation().x + targetFigure.width * newXAnchorPercentage, targetFigure.getLocation().y + targetFigure.height
                        * relativeReferencePoint.preciseY));
            }
        } else {
            throw new UnsupportedOperationException(MSG_EDGE_ON_EDGE_NOT_MANAGED);
        }
        return Options.newNone();
    }

    /**
     * Recompute all the bendpoints of <code>edgeToModify</code> according to a
     * reference edge. This two edges must be branch on the same tree
     * (TreeRouter).
     * 
     * @param referenceEdge
     *            The reference edge
     * @param edgeToModify
     *            Edge to modify
     */
    public static void setBendpoints(Edge referenceEdge, Edge edgeToModify) {
        String sourceTerminalString = getTerminalString(0.5d, 0.5d);
        if (edgeToModify.getSourceAnchor() instanceof IdentityAnchor) {
            sourceTerminalString = ((IdentityAnchor) edgeToModify.getSourceAnchor()).getId();
        }
        String targetTerminalString = getTerminalString(0.5d, 0.5d);
        if (edgeToModify.getTargetAnchor() instanceof IdentityAnchor) {
            targetTerminalString = ((IdentityAnchor) edgeToModify.getTargetAnchor()).getId();
        }
        PrecisionPoint sourceAnchorReference = BaseSlidableAnchor.parseTerminalString(sourceTerminalString);
        PrecisionPoint targetAnchorReference = BaseSlidableAnchor.parseTerminalString(targetTerminalString);

        Point sourceLocation = null;
        Point targetLocation = null;
        Rectangle sourceBounds = null;
        Rectangle targetBounds = null;
        if (edgeToModify.getSource() instanceof Node) {
            sourceBounds = GMFHelper.getBounds((Node) edgeToModify.getSource(), true);
            sourceLocation = new Point(sourceBounds.x + sourceBounds.width * sourceAnchorReference.preciseX, sourceBounds.y + sourceBounds.height * sourceAnchorReference.preciseY);
        } else if (edgeToModify.getSource() instanceof Edge) {
            // TODO Manage edge on egde ...
            throw new UnsupportedOperationException(MSG_EDGE_ON_EDGE_NOT_MANAGED);
        }
        if (edgeToModify.getTarget() instanceof Node) {
            targetBounds = GMFHelper.getBounds((Node) edgeToModify.getTarget(), true);
            targetLocation = new Point(targetBounds.x + targetBounds.width * targetAnchorReference.preciseX, targetBounds.y + targetBounds.height * targetAnchorReference.preciseY);
        } else if (edgeToModify.getTarget() instanceof Edge) {
            // TODO Manage edge on egde ...
            throw new UnsupportedOperationException(MSG_EDGE_ON_EDGE_NOT_MANAGED);
        }

        if (referenceEdge.getBendpoints() instanceof RelativeBendpoints) {
            Object objectSecondRelativeBendpointOfMovedEdge = ((RelativeBendpoints) referenceEdge.getBendpoints()).getPoints().get(1);
            if (objectSecondRelativeBendpointOfMovedEdge instanceof RelativeBendpoint) {
                RelativeBendpoint secondRelativeBendpointOfMovedEdge = (RelativeBendpoint) objectSecondRelativeBendpointOfMovedEdge;
                List<Object> brotherNewBendpoints = Lists.newArrayList();
                brotherNewBendpoints.add(new RelativeBendpoint(0, sourceBounds.y - sourceLocation.y, sourceLocation.x - targetLocation.x, sourceBounds.y - targetLocation.y));
                brotherNewBendpoints.add(new RelativeBendpoint(0, targetLocation.y + secondRelativeBendpointOfMovedEdge.getTargetY() - sourceLocation.y, sourceLocation.x - targetLocation.x,
                        secondRelativeBendpointOfMovedEdge.getTargetY()));
                brotherNewBendpoints.add(new RelativeBendpoint(targetLocation.x - sourceLocation.x, targetLocation.y + secondRelativeBendpointOfMovedEdge.getTargetY() - sourceLocation.y, 0,
                        ((RelativeBendpoint) secondRelativeBendpointOfMovedEdge).getTargetY()));
                brotherNewBendpoints.add(new RelativeBendpoint(targetLocation.x - sourceLocation.x, targetBounds.y + targetBounds.width - sourceLocation.y, 0, targetBounds.y + targetBounds.width
                        - targetLocation.y));
                ((RelativeBendpoints) edgeToModify.getBendpoints()).setPoints(brotherNewBendpoints);
            }
        }
    }

    /**
     * Set the target anchor of the <code>edgeToModify</code> with the same
     * target anchor of the <code>referencEdge</code> (only if reference anchor
     * is an identity anchor and, the anchor of edge to modify is null or is an
     * identify anchor).
     * 
     * @param referenceEdge
     *            reference edge
     * @param edgeToModify
     *            edge to modify
     */
    public static void setTargetAnchor(Edge referenceEdge, Edge edgeToModify) {
        if (referenceEdge.getTargetAnchor() instanceof IdentityAnchor && (edgeToModify.getTargetAnchor() instanceof IdentityAnchor || edgeToModify.getTargetAnchor() == null)) {
            IdentityAnchor refAnchor = (IdentityAnchor) referenceEdge.getTargetAnchor();
            if (edgeToModify.getTargetAnchor() == null) {
                edgeToModify.setTargetAnchor(NotationFactory.eINSTANCE.createIdentityAnchor());
            }
            IdentityAnchor anchorToModify = (IdentityAnchor) edgeToModify.getTargetAnchor();
            anchorToModify.setId(refAnchor.getId());
        }
    }

    /**
     * Set the source anchor of the <code>edgeToModify</code> with the same
     * target anchor of the <code>referenceEdge</code> (only if reference anchor
     * is an identity anchor and, the anchor of edge to modify is null or is an
     * identify anchor).
     * 
     * @param referenceEdge
     *            reference edge
     * @param edgeToModify
     *            edge to modify
     */
    public static void setSourceAnchor(Edge referenceEdge, Edge edgeToModify) {
        if (referenceEdge.getSourceAnchor() instanceof IdentityAnchor && (edgeToModify.getSourceAnchor() instanceof IdentityAnchor || edgeToModify.getSourceAnchor() == null)) {
            IdentityAnchor refAnchor = (IdentityAnchor) referenceEdge.getSourceAnchor();
            if (edgeToModify.getSourceAnchor() == null) {
                edgeToModify.setSourceAnchor(NotationFactory.eINSTANCE.createIdentityAnchor());
            }
            IdentityAnchor anchorToModify = (IdentityAnchor) edgeToModify.getSourceAnchor();
            anchorToModify.setId(refAnchor.getId());
        }
    }

    /**
     * Change the source or target anchor and the bendpoints of the brothers of
     * <code>edge</code> according to this edge.
     * 
     * @param edge
     *            The edge reference
     */
    public static void setBrothersAnchorAndBendpointsAccordingToEdge(Edge edge) {
        EdgeQuery edgeQuery = new EdgeQuery(edge);
        boolean sourceSide = false;
        List<Edge> brothers;
        if (edgeQuery.isEdgeOnTreeOnSourceSide()) {
            brothers = edgeQuery.getBrothersOnTreeOnSourceSide();
            sourceSide = true;
        } else if (edgeQuery.isEdgeOnTreeOnTargetSide()) {
            brothers = edgeQuery.getBrothersOnTreeOnTargetSide();
        } else {
            brothers = Lists.newArrayList();
        }
        for (Edge brother : brothers) {
            if (sourceSide) {
                GMFNotationUtilities.setSourceAnchor(edge, brother);
            } else {
                GMFNotationUtilities.setTargetAnchor(edge, brother);
            }
            GMFNotationUtilities.setBendpoints(edge, brother);
        }
    }
}

Back to the top