Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0fd585ed84ef918df4e85a21004d37082e45ec3d (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
/*******************************************************************************
 * Copyright (c) 2010, 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.sequence.business.internal.elements;

import java.util.Collections;
import java.util.List;

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 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.DDiagramElement;
import org.eclipse.sirius.diagram.sequence.business.internal.query.SequenceNodeQuery;
import org.eclipse.sirius.diagram.sequence.business.internal.util.RangeSetter;
import org.eclipse.sirius.diagram.sequence.description.DescriptionPackage;
import org.eclipse.sirius.diagram.sequence.util.NotationPredicate;
import org.eclipse.sirius.diagram.sequence.util.Range;

/**
 * Represents a combined fragment container.
 * 
 * @author pcdavid
 */
public class CombinedFragment extends AbstractFrame {
    /**
     * The visual ID. Same as a normal container
     * 
     * @see DNodeContainerEditPart.VISUAL_ID.
     */
    public static final int VISUAL_ID = 2002;

    /**
     * The visual ID of the compartment contained by the combined fragment. It
     * is this compartment that contains the operands.
     * 
     * @see DNodeContainerViewNodeContainerCompartmentEditPart.VISUAL_ID.
     */
    public static final int COMPARTMENT_VISUAL_ID = 7001;

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

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

    /**
     * Constructor.
     * 
     * @param node
     *            the GMF Node representing the combined fragment.
     */
    CombinedFragment(Node node) {
        super(node);
        Preconditions.checkArgument(CombinedFragment.notationPredicate().apply(node), "The node does not represent a combined fragment.");
    }

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

    /**
     * Returns a predicate to check whether a GMF View represents an combined
     * fragment compartment.
     * 
     * @return a predicate to check whether a GMF View represents an combined
     *         fragment compartment.
     */
    public static Predicate<View> compartmentNotationPredicate() {
        return new NotationPredicate(NotationPackage.eINSTANCE.getNode(), COMPARTMENT_VISUAL_ID, CombinedFragment.viewpointElementPredicate());
    }

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

    /**
     * {@inheritDoc}
     */
    public Range getVerticalRange() {
        // Rectangle logicalBounds = getProperLogicalBounds();
        // return new Range(logicalBounds.y, logicalBounds.bottom());
        return new SequenceNodeQuery(getNotationNode()).getVerticalRange();
    }

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

    /**
     * {@inheritDoc}
     */
    public List<ISequenceEvent> getSubEvents() {
        return Lists.newArrayList(Iterables.filter(getOperands(), ISequenceEvent.class));
    }

    /**
     * Get the operands of the current combined fragment.
     * 
     * @return the operands of the current combined fragment.
     */
    public List<Operand> getOperands() {
        List<Operand> result = Lists.newArrayList();
        Predicate<View> compartementView = new Predicate<View>() {

            public boolean apply(View input) {
                return input.getType().equals(Integer.toString(COMPARTMENT_VISUAL_ID));
            }
        };
        // The combined fragment contains a compartment that contains the
        // operands
        for (View view : Iterables.filter(Iterables.filter(this.view.eContents(), View.class), compartementView)) {
            // Filtering compartments
            for (View viewChild : Iterables.filter(view.eContents(), View.class)) {
                // Filtering operands
                Option<Operand> operand = ISequenceElementAccessor.getOperand(viewChild);
                if (operand.some()) {
                    result.add(operand.get());
                }
            }
        }
        Collections.sort(result, Range.lowerBoundOrdering().onResultOf(ISequenceEvent.VERTICAL_RANGE));
        return result;
    }

    /**
     * Finds the operand identified by the index.
     * 
     * @param index
     *            the position of the wanted operand
     * @return an Option of Operand if there is an operand at the given index,
     *         an Options.newNone() otherwise
     */
    public Option<Operand> getOperand(int index) {
        try {
            return Options.newSome(getOperands().get(index));
        } catch (IndexOutOfBoundsException e) {
            return Options.newNone();
        }
    }

    /**
     * calculate the index of the operand among the list of operands in this
     * {@link CombinedFragment}.
     * 
     * @param operand
     *            the operand to find out its index
     * @return the index of the operand
     */
    public int getIndexOfOperand(Operand operand) {
        return getOperands().lastIndexOf(operand);
    }

    /**
     * Finds the first operand of this {@link CombinedFragment}.
     * 
     * @return the first operand of this {@link CombinedFragment}
     */
    public Operand getFirstOperand() {
        // A combined fragment always have at least one operand
        return getOperand(0).get();
    }

    /**
     * Finds the last operand of this {@link CombinedFragment}.
     * 
     * @return the last operand of this {@link CombinedFragment}
     */
    public Operand getLastOperand() {
        // A combined fragment always have at least one operand
        int lastIndex = getOperands().size() - 1;
        return getOperand(lastIndex).get();
    }
}

Back to the top