Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 76d2020f69a381edf56ec3ffa3245d0b9e17b25b (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
/*******************************************************************************
 * Copyright (c) 2011 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.sequence.business.internal.refresh;

import java.util.ArrayList;
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.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
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.NotationPackage;
import org.eclipse.gmf.runtime.notation.RelativeBendpoints;
import org.eclipse.gmf.runtime.notation.Routing;
import org.eclipse.gmf.runtime.notation.Style;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.gmf.runtime.notation.datatype.RelativeBendpoint;

import org.eclipse.sirius.AbstractDNode;
import org.eclipse.sirius.DEdge;
import org.eclipse.sirius.diagram.business.api.view.SiriusLayoutDataManager;
import org.eclipse.sirius.diagram.business.internal.view.EdgeLayoutData;
import org.eclipse.sirius.diagram.business.internal.view.LayoutData;
import org.eclipse.sirius.diagram.internal.refresh.GMFHelper;
import org.eclipse.sirius.diagram.internal.refresh.edge.SlidableAnchor;
import org.eclipse.sirius.diagram.tools.api.graphical.edit.styles.IBorderItemOffsets;

/**
 * A Command to update sequence message bendpoints after the
 * CanonicalSynchronizer.
 * 
 * @author <a href="mailto:esteban.dugueperoux@obeo.fr">Esteban Dugueperoux</a>
 */
public class FixBendpointsOnCreationCommand extends RecordingCommand {

    private Edge newEdge;

    private View source;

    private View target;

    // Anchors
    private String sourceTerminal;

    private String targetTerminal;

    // Bendpoints
    private PointList pointList;

    // Routing
    private Routing routing;

    /**
     * Default constructor.
     * 
     * @param domain
     *            the {@link TransactionalEditingDomain} on which execute this
     *            Command
     * 
     * @param createdEdge
     *            the {@link Edge} for which update the bendpoints
     */
    public FixBendpointsOnCreationCommand(TransactionalEditingDomain domain, Edge createdEdge) {
        super(domain);
        this.newEdge = createdEdge;
    }

    /**
     * Overridden to update the bendpoints of the specified {@link Edge}.
     * 
     * {@inheritDoc}
     */
    @Override
    protected void doExecute() {

        this.source = newEdge.getSource();
        this.target = newEdge.getTarget();

        if (source != null && target != null) {

            EObject element = newEdge.getElement();
            if (element instanceof DEdge) {
                DEdge dEdge = (DEdge) element;
                updateEdge(dEdge);
            }
        }
    }

    private void updateEdge(DEdge dEdge) {

        // Bendpoints
        pointList = new PointList();
        Point sourceRefPoint = new Point(0, 0);
        Point targetRefPoint = new Point(0, 0);

        EdgeLayoutData egdeLayoutData = null;
        SiriusLayoutDataManager.INSTANCE.setIgnoreConsumeState(true);
        try {
            egdeLayoutData = SiriusLayoutDataManager.INSTANCE.getData(dEdge, false);
        } finally {
            SiriusLayoutDataManager.INSTANCE.setIgnoreConsumeState(false);
        }
        if (egdeLayoutData != null) {

            sourceTerminal = egdeLayoutData.getSourceTerminal();
            targetTerminal = egdeLayoutData.getTargetTerminal();

            // pointList, sourceRefPoint, targetRefPoint of
            // the edgeLayoutData can be null if the edge is
            // hide when the layout data is stored
            if (egdeLayoutData.getPointList() != null) {
                pointList = egdeLayoutData.getPointList();
            }
            if (egdeLayoutData.getSourceRefPoint() != null) {
                sourceRefPoint = egdeLayoutData.getSourceRefPoint();
            }
            if (egdeLayoutData.getTargetRefPoint() != null) {
                targetRefPoint = egdeLayoutData.getTargetRefPoint();
            }

            routing = egdeLayoutData.getRouting();

        } else {
            computeEdgeDataWithoutEdgeLayoutData();
        }

        // Set connection anchors :
        updateConnectionAnchors();

        // Set Bendpoints :
        updateBendpoints(sourceRefPoint, targetRefPoint);
    }

    private void computeEdgeDataWithoutEdgeLayoutData() {
        Point firstClick = new Point(0, 0);
        Rectangle sourceBounds = GMFHelper.getAbsoluteBounds((Node) source);
        if (source.getElement() instanceof AbstractDNode) {
            AbstractDNode sourceDNode = (AbstractDNode) source.getElement();
            if (sourceDNode.eContainer() instanceof AbstractDNode) {
                AbstractDNode parentDNode = (AbstractDNode) sourceDNode.eContainer();
                if (parentDNode.getOwnedBorderedNodes().contains(sourceDNode)) {
                    sourceBounds.y += IBorderItemOffsets.DEFAULT_OFFSET.height;
                    sourceBounds.height += 5;
                }
            }
            LayoutData sourceLayoutData = null;
            SiriusLayoutDataManager.INSTANCE.setIgnoreConsumeState(true);
            try {
                sourceLayoutData = SiriusLayoutDataManager.INSTANCE.getData(sourceDNode);
            } finally {
                SiriusLayoutDataManager.INSTANCE.setIgnoreConsumeState(false);
            }
            if (sourceLayoutData == null) {
                firstClick = sourceBounds.getCenter();
            } else {
                Point sourceLocation = sourceLayoutData.getLocation();
                firstClick = sourceLocation;
            }
        }

        PrecisionPoint sourceRelativeLocation = BaseSlidableAnchor.getAnchorRelativeLocation(firstClick, sourceBounds);
        sourceTerminal = "(" + sourceRelativeLocation.preciseX + "," + sourceRelativeLocation.preciseY + ")";

        Point secondClick = new Point(0, 0);
        Rectangle targetBounds = GMFHelper.getAbsoluteBounds((Node) target);

        if (target.getElement() instanceof AbstractDNode) {
            AbstractDNode targetDNode = (AbstractDNode) target.getElement();
            if (targetDNode.eContainer() instanceof AbstractDNode) {
                AbstractDNode parentDNode = (AbstractDNode) targetDNode.eContainer();
                if (parentDNode.getOwnedBorderedNodes().contains(targetDNode)) {
                    targetBounds.y += IBorderItemOffsets.DEFAULT_OFFSET.height;
                    targetBounds.height += 5;
                }
            }
            LayoutData targetLayoutData = null;
            SiriusLayoutDataManager.INSTANCE.setIgnoreConsumeState(true);
            try {
                targetLayoutData = SiriusLayoutDataManager.INSTANCE.getData(targetDNode);
            } finally {
                SiriusLayoutDataManager.INSTANCE.setIgnoreConsumeState(false);
            }

            if (targetLayoutData == null) {
                secondClick = targetBounds.getCenter();
            } else {
                Point targetLocation = targetLayoutData.getLocation();
                secondClick = targetLocation;
            }
        }

        PrecisionPoint targetRelativeLocation = BaseSlidableAnchor.getAnchorRelativeLocation(secondClick, targetBounds);
        targetTerminal = "(" + targetRelativeLocation.preciseX + "," + targetRelativeLocation.preciseY + ")";

        // Computes pointList
        PrecisionPoint sourceRelativeReference = SequenceSlidableAnchor.parseTerminalString(sourceTerminal);
        SlidableAnchor sourceAnchor = new SequenceSlidableAnchor((Node) source, sourceRelativeReference);
        pointList.addPoint(sourceAnchor.getLocation(sourceAnchor.getReferencePoint()));

        PrecisionPoint targetRelativeReference = SequenceSlidableAnchor.parseTerminalString(targetTerminal);
        SlidableAnchor targetAnchor = new SequenceSlidableAnchor((Node) target, targetRelativeReference);
        pointList.addPoint(targetAnchor.getLocation(targetAnchor.getReferencePoint()));
    }

    private void updateConnectionAnchors() {
        if (sourceTerminal != null) {
            if (sourceTerminal.length() == 0)
                newEdge.setSourceAnchor(null);
            else {
                IdentityAnchor a = (IdentityAnchor) newEdge.getSourceAnchor();
                if (a == null)
                    a = NotationFactory.eINSTANCE.createIdentityAnchor();
                a.setId(sourceTerminal);
                newEdge.setSourceAnchor(a);
            }
        }
        if (targetTerminal != null) {
            if (targetTerminal.length() == 0)
                newEdge.setTargetAnchor(null);
            else {
                IdentityAnchor a = (IdentityAnchor) newEdge.getTargetAnchor();
                if (a == null)
                    a = NotationFactory.eINSTANCE.createIdentityAnchor();
                a.setId(targetTerminal);
                newEdge.setTargetAnchor(a);
            }

        }
    }

    private void updateBendpoints(Point sourceRefPoint, Point targetRefPoint) {
        List<RelativeBendpoint> newBendpoints = new ArrayList<RelativeBendpoint>();
        int numOfPoints = pointList.size();
        for (short i = 0; i < numOfPoints; i++) {
            Dimension s = pointList.getPoint(i).getDifference(sourceRefPoint);
            Dimension t = pointList.getPoint(i).getDifference(targetRefPoint);
            newBendpoints.add(new RelativeBendpoint(s.width, s.height, t.width, t.height));
        }

        RelativeBendpoints points = (RelativeBendpoints) newEdge.getBendpoints();
        points.setPoints(newBendpoints);

        // Routing
        if (routing != null) {
            Style routingStyle = newEdge.getStyle(NotationPackage.Literals.ROUTING_STYLE);
            routingStyle.eSet(NotationPackage.Literals.ROUTING_STYLE__ROUTING, routing);
        }
    }
}

Back to the top