Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0edd40a338a31d1f10f8f85386ffc5ae9e3d9551 (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
/*******************************************************************************
 * 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.ui.tool.internal.edit.policy;

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

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editparts.LayerManager;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;

import com.google.common.collect.Lists;

import org.eclipse.sirius.common.tools.api.util.Option;
import org.eclipse.sirius.DDiagram;
import org.eclipse.sirius.description.tool.AbstractToolDescription;
import org.eclipse.sirius.description.tool.ContainerCreationDescription;
import org.eclipse.sirius.diagram.graphical.edit.policies.ContainerCreationEditPolicy;
import org.eclipse.sirius.diagram.graphical.edit.policies.CreationUtil;
import org.eclipse.sirius.diagram.sequence.SequenceDDiagram;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.ISequenceElement;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.ISequenceElementAccessor;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.ISequenceEvent;
import org.eclipse.sirius.diagram.sequence.business.internal.elements.SequenceDiagram;
import org.eclipse.sirius.diagram.sequence.business.internal.layout.LayoutConstants;
import org.eclipse.sirius.diagram.sequence.business.internal.operation.VerticalSpaceExpansion;
import org.eclipse.sirius.diagram.sequence.description.tool.CombinedFragmentCreationTool;
import org.eclipse.sirius.diagram.sequence.description.tool.InteractionUseCreationTool;
import org.eclipse.sirius.diagram.sequence.ordering.EventEnd;
import org.eclipse.sirius.diagram.sequence.ui.tool.internal.edit.command.SequenceDelegatingCommandFactory;
import org.eclipse.sirius.diagram.sequence.ui.tool.internal.edit.part.SequenceDiagramEditPart;
import org.eclipse.sirius.diagram.sequence.ui.tool.internal.edit.validator.FrameCreationValidator;
import org.eclipse.sirius.diagram.sequence.ui.tool.internal.util.CreateRequestQuery;
import org.eclipse.sirius.diagram.sequence.util.Range;
import org.eclipse.sirius.diagram.tools.api.command.DoNothingCommand;
import org.eclipse.sirius.diagram.tools.api.editor.DDiagramEditor;
import org.eclipse.sirius.diagram.ui.tools.internal.edit.command.CommandFactory;
import org.eclipse.sirius.tools.api.command.IDiagramCommandFactory;
import org.eclipse.sirius.tools.api.command.IDiagramCommandFactoryProvider;

/**
 * An extension of the standard Sirius ContainerCreationEditPolicy which
 * knows how to handle the specific tools use to create frames (i.e. Interaction
 * Uses and Combined Fragments).
 * 
 * @author pcdavid
 */
public class FrameAwareContainerCreationPolicy extends ContainerCreationEditPolicy {

    /** Label use for the Interaction Use creation. */
    public static final String INTERACTION_USE_CREATION_CMD_LABEL = "Interaction Use creation";

    /** Label use for the Combined Fragment creation. */
    public static final String COMBINED_FRAGMENT_CREATION_CMD_LABEL = "Combined Fragment creation";

    /**
     * Additional figures for feedback.
     */
    protected Collection<Figure> guides = Lists.newArrayList();

    /**
     * {@inheritDoc}
     */
    @Override
    protected Command getCreateContainerOnDiagramCommand(CreateRequest request, ContainerCreationDescription ccdTool, DDiagram diagram) {
        Command result;

        boolean frameCreationTool = ccdTool instanceof InteractionUseCreationTool || ccdTool instanceof CombinedFragmentCreationTool;
        if (frameCreationTool && getHost() instanceof SequenceDiagramEditPart && diagram instanceof SequenceDDiagram) {
            SequenceDiagramEditPart sdep = (SequenceDiagramEditPart) getHost();
            TransactionalEditingDomain domain = sdep.getEditingDomain();
            Diagram gmfDiagram = sdep.getDiagramView();
            SequenceDiagram sequenceDiagram = ISequenceElementAccessor.getSequenceDiagram(gmfDiagram).get();
            FrameCreationValidator creationValidator = FrameCreationValidator.getOrCreateValidator(sequenceDiagram, ccdTool, new CreateRequestQuery(request, sdep));

            if (creationValidator.isValid()) {
                EventEnd startingEndPredecessor = creationValidator.getStartingEndPredecessor();
                EventEnd finishingEndPredecessor = creationValidator.getFinishingEndPredecessor();
                List<EObject> coverage = creationValidator.getCoverage();
                Range expansionZone = creationValidator.getExpansionZone();

                CreationUtil creationUtil = new CreationUtil(request, getDiagramCommandFactory(startingEndPredecessor, finishingEndPredecessor, coverage, getCreationRange(request)),
                        getRealLocation(request), getRealSize(ccdTool, request), getHost());
                result = creationUtil.getContainerCreationDescription(diagram, ccdTool);

                // Add a vertical expansion command if we do inclusion
                if (expansionZone != null && !expansionZone.isEmpty() && result != null && result.canExecute()) {
                    // Shift the element to not include int the range of the
                    // AbstractFrame to create
                    VerticalSpaceExpansion verticalSpaceExpansion = new VerticalSpaceExpansion(sequenceDiagram, expansionZone, 0, Collections.<ISequenceEvent> emptyList());
                    ICommand expandSubEventsCmd = CommandFactory.createICommand(domain, verticalSpaceExpansion);

                    result = new ICommandProxy(expandSubEventsCmd).chain(result);
                }
                if (result != null) {
                    if (ccdTool instanceof InteractionUseCreationTool) {
                        result.setLabel(INTERACTION_USE_CREATION_CMD_LABEL);
                    } else if (ccdTool instanceof CombinedFragmentCreationTool) {
                        result.setLabel(COMBINED_FRAGMENT_CREATION_CMD_LABEL);
                    }
                }
            } else {
                result = creationValidator.getCoverage().isEmpty() ? DoNothingCommand.INSTANCE : UnexecutableCommand.INSTANCE;
            }
        } else {
            result = super.getCreateContainerOnDiagramCommand(request, ccdTool, diagram);
        }
        return result;
    }

    /**
     * Overridden to show the feedback of the expansion zone for
     * InteractionUse/CombinedFragment creation when there is inclusion of
     * existing sequence events in its creation range and vertical space
     * expansion is needed for some sequence events.
     * 
     * 
     * {@inheritDoc}
     */
    @Override
    public void showTargetFeedback(Request request) {
        eraseTargetFeedback(request);
        if (request instanceof CreateRequest && this.getHost() instanceof SequenceDiagramEditPart) {
            SequenceDiagramEditPart sdep = (SequenceDiagramEditPart) getHost();
            CreateRequest createRequest = (CreateRequest) request;
            Option<ISequenceElement> seqDiag = ISequenceElementAccessor.getISequenceElement((View) this.getHost().getModel());
            AbstractToolDescription tool = getTool(createRequest);
            if (seqDiag.some() && seqDiag.get() instanceof SequenceDiagram && tool instanceof InteractionUseCreationTool || tool instanceof CombinedFragmentCreationTool) {
                FrameCreationValidator validator = FrameCreationValidator.getOrCreateValidator((SequenceDiagram) seqDiag.get(), (ContainerCreationDescription) tool, new CreateRequestQuery(
                        createRequest, sdep));
                if (validator != null) {
                    SequenceInteractionFeedBackBuilder feedBackBuilder = new SequenceInteractionFeedBackBuilder(validator, getFeedbackLayer(), (IGraphicalEditPart) getHost());
                    for (Figure fig : feedBackBuilder.buildFeedBack()) {
                        addFeedback(fig);
                        guides.add(fig);
                    }
                }
            }
        }
    }

    /**
     * Overridden to erase feedback for AbstractFrame creation request.
     * 
     * {@inheritDoc}
     */
    @Override
    public void eraseTargetFeedback(Request request) {
        removeFeedBackOnGuides();
    }

    private void removeFeedBackOnGuides() {
        if (guides != null && !guides.isEmpty()) {
            for (Figure hGuide : guides) {
                removeFeedback(hGuide);
            }
            guides.clear();
        }
    }

    /**
     * Add a IFigure to the feedbackLayer.
     * 
     * @param figure
     *            the feedback figure to add
     */
    protected void addFeedback(IFigure figure) {
        getFeedbackLayer().add(figure);
    }

    /**
     * Returns the layer used for displaying feedback.
     * 
     * @return the feedback layer
     */
    protected IFigure getFeedbackLayer() {
        return getLayer(LayerConstants.FEEDBACK_LAYER);
    }

    /**
     * Convenience method to return the host's Figure.
     * 
     * @return The host GraphicalEditPart's Figure
     */
    protected IFigure getHostFigure() {
        return ((GraphicalEditPart) getHost()).getFigure();
    }

    /**
     * Obtains the specified layer.
     * 
     * @param layer
     *            the key identifying the layer
     * @return the requested layer
     */
    protected IFigure getLayer(Object layer) {
        return LayerManager.Helper.find(getHost()).getLayer(layer);
    }

    /**
     * Removes the specified <code>Figure</code> from the
     * {@link LayerConstants#FEEDBACK_LAYER}.
     * 
     * @param figure
     *            the feedback to remove
     */
    protected void removeFeedback(IFigure figure) {
        getFeedbackLayer().remove(figure);
    }

    private Range getCreationRange(CreateRequest request) {
        Point realLocation = getRealLocation(request);
        Range result = Range.emptyRange();
        if (request.getSize() != null) {
            result = new Range(realLocation.y, realLocation.y + request.getSize().height);
        }
        return result;
    }

    private Dimension getRealSize(ContainerCreationDescription ccdTool, CreateRequest request) {
        Dimension realSize = request.getSize();
        if (realSize == null) {
            realSize = new Dimension(0, 0);
            if (ccdTool instanceof InteractionUseCreationTool) {
                realSize.height = LayoutConstants.DEFAULT_INTERACTION_USE_HEIGHT;
            } else if (ccdTool instanceof CombinedFragmentCreationTool) {
                realSize.height = LayoutConstants.DEFAULT_COMBINED_FRAGMENT_HEIGHT;
            }
        }
        return realSize;
    }

    private IDiagramCommandFactory getDiagramCommandFactory(EventEnd startingEndPredecessor, EventEnd finishingEndPredecessor, List<EObject> coverage, Range creationRange) {
        SequenceDiagram seqDiag = null;
        EditPart host = getHost();
        if (host instanceof SequenceDiagramEditPart) {
            seqDiag = ((SequenceDiagramEditPart) host).getSequenceDiagram();
        }

        final DDiagramEditor diagramEditor = (DDiagramEditor) host.getViewer().getProperty(DDiagramEditor.EDITOR_ID);
        if (diagramEditor == null || seqDiag == null) {
            return null;
        }
        TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(seqDiag.getNotationDiagram());
        final Object adapter = diagramEditor.getAdapter(IDiagramCommandFactoryProvider.class);
        final IDiagramCommandFactoryProvider cmdFactoryProvider = (IDiagramCommandFactoryProvider) adapter;
        final IDiagramCommandFactory diagramCommandFactory = cmdFactoryProvider.getCommandFactory(domain);
        return new SequenceDelegatingCommandFactory(diagramCommandFactory, domain, seqDiag, startingEndPredecessor, finishingEndPredecessor, coverage);
    }
}

Back to the top