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

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.Edge;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;

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

import org.eclipse.sirius.common.tools.api.util.Option;
import org.eclipse.sirius.common.tools.api.util.Options;
import org.eclipse.sirius.DDiagramElement;
import org.eclipse.sirius.diagram.sequence.description.DescriptionPackage;
import org.eclipse.sirius.diagram.sequence.util.NotationPredicate;

/**
 * Represents the EndOfLife marker which can appear at the bottom of a lifeline.
 * This element can be present even in the case where the lifeline is not
 * explicitly destroyed by a destruction message. In that case, it is used as a
 * convenience to allow the user to resize the lifeline vertically by dragging
 * the EndOfLife marker.
 * 
 * @author mporhel, pcdavid
 */
public class EndOfLife extends AbstractSequenceNode {
    /**
     * The visual ID. Same as a normal bordered node.
     * 
     * @see DNode2EditPart.VISUAL_ID
     */
    public static final int VISUAL_ID = 3001;

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

        public boolean apply(DDiagramElement input) {
            return AbstractSequenceElement.isSequenceDiagramElement(input, DescriptionPackage.eINSTANCE.getEndOfLifeMapping());
        }
    }

    /**
     * Constructor.
     * 
     * @param node
     *            the GMF Node which represents this EOL.
     */
    EndOfLife(Node node) {
        super(node);
        Preconditions.checkArgument(EndOfLife.notationPredicate().apply(node), "The node does not represent an end-of-life.");
    }

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

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

    /**
     * {@inheritDoc}
     */
    public Option<Lifeline> getLifeline() {
        return getParentLifeline();
    }

    /**
     * Returns the destruction message which targets this EOL, if any.
     * 
     * @return the destruction message which targets this EOL, if any.
     */
    public Option<Message> getDestructionMessage() {
        Node node = getNotationNode();
        for (Edge edge : Iterables.filter(node.getTargetEdges(), Edge.class)) {
            Option<Message> message = ISequenceElementAccessor.getMessage(edge);
            if (message.some() && message.get().getKind() == Message.Kind.DESTRUCTION) {
                return message;
            }
        }
        return Options.newNone();
    }

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

    /**
     * Tests whether this EOL marker (and thus the associated lifeline) is
     * explicitly destroyed by a destruction message.
     * 
     * @return <code>true</code> if this EOL marker is the target of a
     *         destruction message.
     */
    public boolean isExplicitelyDestroyed() {
        return getDestructionMessage().some();
    }

}

Back to the top