Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e79b80e9bb4c0c39e16973c8bf0a36641a0f427e (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
/*******************************************************************************
 * Copyright (c) 2010, 2021 THALES GLOBAL SERVICES and others.
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.sequence.business.internal.elements;

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

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.sequence.business.api.util.Range;
import org.eclipse.sirius.diagram.sequence.business.internal.layout.LayoutConstants;
import org.eclipse.sirius.diagram.sequence.business.internal.query.ISequenceEventQuery;
import org.eclipse.sirius.diagram.sequence.business.internal.query.SequenceNodeQuery;
import org.eclipse.sirius.diagram.sequence.business.internal.util.ParentOperandFinder;
import org.eclipse.sirius.diagram.sequence.business.internal.util.RangeSetter;
import org.eclipse.sirius.diagram.sequence.business.internal.util.SubEventsHelper;
import org.eclipse.sirius.diagram.sequence.description.DescriptionPackage;
import org.eclipse.sirius.diagram.sequence.tool.internal.Messages;
import org.eclipse.sirius.ext.base.Option;
import org.eclipse.sirius.ext.base.Options;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
 * Common interface for all the elements of a sequence diagram.
 * 
 * @author mporhel
 */
public class Lifeline extends AbstractSequenceNode implements ISequenceEvent {
    /**
     * The visual id.
     * 
     * @see org.eclipse.sirius.diagram.internal.edit.parts.DNode2EditPart.VISUAL_ID
     */
    public static final int VISUAL_ID = 3001;

    /**
     * Predicate to check whether a Sirius DDiagramElement represents a lifeline.
     */
    private enum SiriusElementPredicate implements Predicate<DDiagramElement> {
        INSTANCE;

        @Override
        public boolean apply(DDiagramElement input) {
            return AbstractSequenceElement.isSequenceDiagramElement(input, DescriptionPackage.eINSTANCE.getExecutionMapping())
                    && InstanceRole.viewpointElementPredicate().apply((DDiagramElement) input.eContainer());
        }
    }

    /**
     * .
     * 
     * @param node
     *            .
     */
    Lifeline(Node node) {
        super(node);
        Preconditions.checkArgument(Lifeline.notationPredicate().apply(node), Messages.Lifeline_nonLifelineNode);
    }

    /**
     * Returns a predicate to check whether a GMF View represents a lifeline.
     * 
     * @return a predicate to check whether a GMF View represents a lifeline.
     */
    public static Predicate<View> notationPredicate() {
        return new NotationPredicate(NotationPackage.eINSTANCE.getNode(), VISUAL_ID, Lifeline.viewpointElementPredicate());
    }

    /**
     * Returns a predicate to check whether a Sirius DDiagramElement represents a lifeline.
     * 
     * @return a predicate to check whether a Sirius DDiagramElement represents a lifeline.
     */
    public static Predicate<DDiagramElement> viewpointElementPredicate() {
        return SiriusElementPredicate.INSTANCE;
    }

    public InstanceRole getInstanceRole() {
        return ISequenceElementAccessor.getInstanceRole((View) getNotationNode().eContainer()).get();
    }

    @Override
    public Range getVerticalRange() {
        return new SequenceNodeQuery(getNotationNode()).getVerticalRange();
    }

    @Override
    public boolean isLogicallyInstantaneous() {
        return false;
    }

    @Override
    public void setVerticalRange(Range range) throws IllegalStateException {
        RangeSetter.setVerticalRange(this, range);
    }

    /**
     * Returns the0 EOL marker for this lifeline, if any. Note that the mere presence of an EOL does not mean this
     * lifeline is explicitly destroyed, as EOLs can be used just to serve as visual hints of the end of lifelines and
     * convenient resize handles.
     * 
     * @return the EOL marker for this lifeline, if any.
     */
    public Option<EndOfLife> getEndOfLife() {
        for (View child : Iterables.filter(getNotationView().getChildren(), View.class)) {
            if (EndOfLife.notationPredicate().apply(child)) {
                return ISequenceElementAccessor.getEndOfLife(child);
            }
        }
        return Options.newNone();
    }

    @Override
    public Option<Lifeline> getLifeline() {
        return Options.newSome(this);
    }

    @Override
    public Rectangle getProperLogicalBounds() {
        if (getNotationNode().getLayoutConstraint() instanceof Bounds) {
            Bounds bounds = (Bounds) getNotationNode().getLayoutConstraint();
            Rectangle irBounds = getInstanceRole().getProperLogicalBounds();
            Point bottom = irBounds.getBottom();
            int width = bounds.getWidth();
            return new Rectangle(bottom.x - width / 2, bottom.y, bounds.getWidth(), bounds.getHeight());
        } else {
            throw new RuntimeException();
        }
    }

    /**
     * Lifelines are the root of the events hierarchy and thus have not parent.
     * <p>
     * {@inheritDoc}
     */
    @Override
    public ISequenceEvent getParentEvent() {
        return null;
    }

    @Override
    public List<ISequenceEvent> getSubEvents() {
        return new SubEventsHelper(this).getSubEvents();
    }

    @Override
    public Collection<ISequenceEvent> getEventsToMoveWith() {
        return getSubEvents();
    }

    @Override
    public boolean canChildOccupy(ISequenceEvent child, Range range) {
        return new SubEventsHelper(this).canChildOccupy(child, range);
    }

    @Override
    public boolean canChildOccupy(ISequenceEvent child, Range range, List<ISequenceEvent> eventsToIgnore, Collection<Lifeline> lifelines) {
        return new SubEventsHelper(this).canChildOccupy(child, range, eventsToIgnore, lifelines);
    }

    @Override
    public Range getOccupiedRange() {
        return new ISequenceEventQuery(this).getOccupiedRange();
    }

    @Override
    public Range getValidSubEventsRange() {
        Range result = getVerticalRange();
        if (result.width() > 2 * LayoutConstants.EXECUTION_CHILDREN_MARGIN) {
            result = result.shrinked(LayoutConstants.EXECUTION_CHILDREN_MARGIN);
        }
        return result;
    }

    /**
     * Locate the destruction message which destroys the lifeline, if any.
     * 
     * @return the destruction message which destroys the lifeline, if any.
     */
    public Option<Message> getDestructionMessage() {
        Option<EndOfLife> optEOL = getEndOfLife();
        if (optEOL.some()) {
            return optEOL.get().getDestructionMessage();
        } else {
            return Options.newNone();
        }
    }

    /**
     * Tests whether the lifeline is explicitly destroyed by a destruction message, or if it goes until the end of the
     * sequence.
     * 
     * @return <code>true</code> if the lifeline is explicitly destroyed by a destruction message.
     */
    public boolean isExplicitlyDestroyed() {
        Option<EndOfLife> optEOL = getEndOfLife();
        if (optEOL.some()) {
            return optEOL.get().isExplicitelyDestroyed();
        } else {
            return false;
        }
    }

    /**
     * Tests whether the lifeline is explicitly created by a creation message, or if it starts from the beginning of the
     * sequence.
     * 
     * @return <code>true</code> if the lifeline is explicitly created by a creation message.
     */
    public boolean isExplicitlyCreated() {
        InstanceRole opt = getInstanceRole();
        if (opt != null) {
            return opt.isExplicitlyCreated();
        } else {
            return false;
        }
    }

    /**
     * Locate the destruction message which creates the lifeline, if any.
     * 
     * @return the destruction message which creates the lifeline, if any.
     */
    public Option<Message> getCreationMessage() {
        InstanceRole opt = getInstanceRole();
        if (opt != null) {
            return opt.getCreationMessage();
        }
        return Options.newNone();
    }

    @Override
    public Option<Operand> getParentOperand() {
        return getParentOperand(getVerticalRange());
    }

    /**
     * Finds the deepest Operand container including the position if existing.
     * 
     * @param verticalPosition
     *            the position where to look for the deepest operand
     * @return the deepest Operand convering this lifeline at this range
     * @see ISequenceEvent#getParentOperand()
     */
    public Option<Operand> getParentOperand(final int verticalPosition) {
        return new ParentOperandFinder(this).getParentOperand(new Range(verticalPosition, verticalPosition));
    }

    /**
     * Finds the deepest Operand container convering the range if existing.
     * 
     * @param range
     *            the range where to look for the deepest operand
     * @return the deepest Operand convering this lifeline at this range
     * @see ISequenceEvent#getParentOperand()
     */
    public Option<Operand> getParentOperand(final Range range) {
        return new ParentOperandFinder(this).getParentOperand(range);
    }

    @Override
    public ISequenceEvent getHierarchicalParentEvent() {
        return null;
    }

    /**
     * Investigate recursively sub events to find all covering interaction uses.
     * 
     * @return all interaction uses covering this lifeline recursively
     */
    public Collection<InteractionUse> getAllCoveringInteractionUses() {
        Predicate<InteractionUse> interactionUseCoveringLifeline = new Predicate<InteractionUse>() {

            @Override
            public boolean apply(InteractionUse input) {
                return input.computeCoveredLifelines().contains(Lifeline.this);
            }
        };
        return Lists.newArrayList(Iterables.filter(getDiagram().getAllInteractionUses(), interactionUseCoveringLifeline));
    }
}

Back to the top