Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2a87b40a77bc7e98fe5884e301d031285d549bc0 (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
/*******************************************************************************
 * Copyright (c) 2010, 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.util;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Location;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.RelativeBendpoints;
import org.eclipse.gmf.runtime.notation.Size;
import org.eclipse.gmf.runtime.notation.datatype.RelativeBendpoint;

import com.google.common.collect.Iterables;

import org.eclipse.sirius.common.tools.api.util.Option;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.AbstractNodeEvent;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.CombinedFragment;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.EndOfLife;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.ISequenceNode;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.InstanceRole;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.InteractionUse;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.Lifeline;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.LostMessageEnd;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.Message;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.Operand;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.State;
import org.eclipse.sirius.diagram.sequence.business.internal.query.SequenceMessageViewQuery;
import org.eclipse.sirius.diagram.sequence.business.internal.query.SequenceNodeQuery;
import org.eclipse.sirius.diagram.sequence.util.Range;

/**
 * .
 * 
 * @author mporhel
 * 
 */
public final class RangeSetter {

    /**
     * Avoid instanticiation.
     */
    private RangeSetter() {
        // Do nothing.
    }

    /**
     * Common implementation of
     * {@link ISequenceEventEditPart#setVerticalRange(Range)} for lifelines and
     * executions. Assumes the {@link ISequenceEventEditPart} is a Node.
     * 
     * @param self
     *            the (root) execution edit part.
     * @param range
     *            the vertical range of the given sequence event.
     */
    public static void setVerticalRange(AbstractNodeEvent self, Range range) {
        Range oldRange = self.getVerticalRange();
        int deltaY = range.getLowerBound() - oldRange.getLowerBound();
        int size = range.width();

        RangeSetter.setVerticalRange(self.getNotationNode(), deltaY, size);
    }

    /**
     * Common implementation of
     * {@link ISequenceEventEditPart#setVerticalRange(Range)} for states.
     * Assumes the {@link ISequenceEventEditPart} is a Node.
     * 
     * @param self
     *            the state edit part.
     * @param range
     *            the vertical range of the given sequence event.
     */
    public static void setVerticalRange(State self, Range range) {
        Range oldRange = self.getVerticalRange();
        int deltaY = range.getLowerBound() - oldRange.getLowerBound();
        int size = range.width();

        RangeSetter.setVerticalRange(self.getNotationNode(), deltaY, size);
    }

    /**
     * Common implementation of
     * {@link ISequenceEventEditPart#setVerticalRange(Range)} for interaction
     * uses. Assumes the {@link ISequenceEventEditPart} is a Node.
     * 
     * @param self
     *            the InteractionUse.
     * @param range
     *            the vertical range of the given sequence event.
     */
    public static void setVerticalRange(InteractionUse self, Range range) {
        Range oldRange = self.getVerticalRange();
        int deltaY = range.getLowerBound() - oldRange.getLowerBound();
        int size = range.width();

        RangeSetter.setVerticalRange(self.getNotationNode(), deltaY, size);
    }

    /**
     * Common implementation of
     * {@link ISequenceEventEditPart#setVerticalRange(Range)} for combined
     * fragments. Assumes the {@link ISequenceEventEditPart} is a Node.
     * 
     * @param self
     *            the CombinedFragment.
     * @param range
     *            the vertical range of the given sequence event.
     */
    public static void setVerticalRange(CombinedFragment self, Range range) {
        Range oldRange = self.getVerticalRange();
        int deltaY = range.getLowerBound() - oldRange.getLowerBound();
        int size = range.width();

        RangeSetter.setVerticalRange(self.getNotationNode(), deltaY, size);
    }

    /**
     * Common implementation of
     * {@link ISequenceEventEditPart#setVerticalRange(Range)} for combined
     * fragments. Assumes the {@link ISequenceEventEditPart} is a Node.
     * 
     * @param self
     *            the CombinedFragment.
     * @param range
     *            the vertical range of the given sequence event.
     */
    public static void setVerticalRange(Operand self, Range range) {
        Range oldRange = self.getVerticalRange();
        int deltaY = range.getLowerBound() - oldRange.getLowerBound();
        int size = range.width();

        RangeSetter.setVerticalRange(self.getNotationNode(), deltaY, size);
    }

    /**
     * Common implementation of
     * {@link ISequenceEventEditPart#setVerticalRange(Range)} for lifelines and
     * executions. Assumes the {@link ISequenceEventEditPart} is a Node.
     * 
     * @param self
     *            the (root) execution edit part.
     * @param range
     *            the vertical range of the given sequence event.
     */
    public static void setVerticalRange(Lifeline self, Range range) {
        InstanceRole instanceRole = self.getInstanceRole();
        Rectangle irepBounds = instanceRole.getBounds();
        Range irRange = new Range(range.getLowerBound() - irepBounds.height, range.getUpperBound());
        RangeSetter.setVerticalRange(instanceRole, irRange);

        Range oldRange = self.getVerticalRange();
        int deltaY = range.getLowerBound() - oldRange.getLowerBound();
        int size = range.width();
        RangeSetter.setVerticalRange(self.getNotationNode(), deltaY, size);

        Option<EndOfLife> eol = self.getEndOfLife();
        if (eol.some()) {
            EndOfLife endOfLife = eol.get();
            Rectangle eolBounds = endOfLife.getBounds();
            Range eolRange = new Range(range.getUpperBound(), range.getUpperBound() + eolBounds.height);
            RangeSetter.setVerticalRange(endOfLife, eolRange);
        }
    }

    /**
     * .
     * 
     * @param self
     *            .
     * @param range
     *            .
     */
    private static void setVerticalRange(InstanceRole self, Range range) {
        Node node = self.getNotationNode();
        Range oldRange = new SequenceNodeQuery(node).getVerticalRange();

        int deltaY = range.getLowerBound() - oldRange.getLowerBound();
        int size = oldRange.width();

        RangeSetter.setVerticalRange(node, deltaY, size);
    }

    /**
     * .
     * 
     * @param self
     *            .
     * @param range
     *            .
     */
    private static void setVerticalRange(EndOfLife self, Range range) {
        Node node = self.getNotationNode();
        Range oldRange = new SequenceNodeQuery(node).getVerticalRange();

        int deltaY = range.getLowerBound() - oldRange.getLowerBound();
        int size = oldRange.width();

        RangeSetter.setVerticalRange(node, deltaY, size);
    }

    /**
     * .
     * 
     * @param node
     *            .
     * @param deltaY
     *            .
     * @param size
     *            .
     */
    private static void setVerticalRange(Node node, int deltaY, int size) {
        LayoutConstraint layoutConstraint = node.getLayoutConstraint();
        int realDeltaY = deltaY;
        // if (realDeltaY > 0) {
        // realDeltaY -= IBorderItemOffsets.DEFAULT_OFFSET.height;
        // }else if(realDeltaY<0){
        // realDeltaY += IBorderItemOffsets.DEFAULT_OFFSET.height;
        // }
        if (layoutConstraint instanceof Location && realDeltaY != 0) {
            Location location = (Location) layoutConstraint;
            location.setY(location.getY() + realDeltaY);
        }
        if (layoutConstraint instanceof Size) {
            Size s = (Size) layoutConstraint;
            if (s.getHeight() != size) {
                s.setHeight(size);
            }
        }
    }

    /**
     * Common implementation of
     * {@link ISequenceEventEditPart#setVerticalRange(Range)} for lifelines and
     * executions. Assumes the {@link ISequenceEventEditPart} is a Node.
     * 
     * @param self
     *            the (root) execution edit part.
     * @param range
     *            the vertical range of the given sequence event.
     */
    public static void setVerticalRange(Message self, Range range) {
        RangeSetter.handlePotentialLostEnd(self.getSourceElement(), range);
        RangeSetter.handlePotentialLostEnd(self.getTargetElement(), range);

        Edge edge = self.getNotationEdge();
        SequenceMessageViewQuery query = new SequenceMessageViewQuery(edge);

        int firstPointVerticalPosition = query.getFirstPointVerticalPosition(true);
        int lastPointVerticalPosition = query.getLastPointVerticalPosition(true);

        int firstPointVerticalPositionFromTarget = query.getFirstPointVerticalPosition(false);
        int lastPointVerticalPositionFromTarget = query.getLastPointVerticalPosition(false);

        int deltaFirstPointY = range.getLowerBound() - firstPointVerticalPosition;
        int deltaFirstPointYFromTarget = range.getLowerBound() - firstPointVerticalPositionFromTarget;

        int deltaLastPointY = range.getUpperBound() - lastPointVerticalPosition;
        int deltaLastPointYFromTarget = range.getUpperBound() - lastPointVerticalPositionFromTarget;

        RangeSetter.updateBendpoints(edge, firstPointVerticalPosition, lastPointVerticalPosition, deltaFirstPointY, deltaFirstPointYFromTarget, deltaLastPointY, deltaLastPointYFromTarget);
    }

    private static void handlePotentialLostEnd(ISequenceNode end, Range range) {
        if (end instanceof LostMessageEnd) {
            Node node = end.getNotationNode();
            LayoutConstraint layoutConstraint = node.getLayoutConstraint();
            if (layoutConstraint instanceof Bounds) {
                Bounds bounds = (Bounds) layoutConstraint;
                int middleValue = bounds.getY() + bounds.getHeight() / 2;
                RangeSetter.setVerticalRange(node, range.middleValue() - middleValue, bounds.getHeight());
            }
        }
    }

    private static void updateBendpoints(Edge edge, int firstPointVerticalPosition, int lastPointVerticalPosition, int deltaFirstPointY, int deltaFirstPointYFromTarget, int deltaLastPointY,
            int deltaLastPointYFromTarget) {
        List<RelativeBendpoint> newBendpoints = new ArrayList<RelativeBendpoint>();
        RelativeBendpoints bendpoints = (RelativeBendpoints) edge.getBendpoints();

        Iterable<RelativeBendpoint> relPoints = Iterables.filter(bendpoints.getPoints(), RelativeBendpoint.class);

        for (int i = 0; i < Iterables.size(relPoints) / 2; i++) {
            RelativeBendpoint p = Iterables.get(relPoints, i);
            RangeSetter.addBendpoint(newBendpoints, p, deltaFirstPointY, deltaFirstPointYFromTarget);
        }

        for (int i = Iterables.size(relPoints) / 2; i < Iterables.size(relPoints); i++) {
            RelativeBendpoint p = Iterables.get(relPoints, i);
            RangeSetter.addBendpoint(newBendpoints, p, deltaLastPointY, deltaLastPointYFromTarget);
        }

        if (!BendpointsHelper.areSameBendpoints(bendpoints.getPoints(), newBendpoints)) {
            bendpoints.setPoints(newBendpoints);
        }
    }

    private static void addBendpoint(List<RelativeBendpoint> newBendpoints, RelativeBendpoint p, double deltaSourceY, double deltaTargetY) {
        newBendpoints.add(new RelativeBendpoint(p.getSourceX(), (int) (p.getSourceY() + deltaSourceY), p.getTargetX(), (int) (p.getTargetY() + deltaTargetY)));
    }
}

Back to the top