Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ac5ad8fbd0fd9eca7baf1c5643852bd714d82cc6 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*******************************************************************************
 * Copyright (c) 2010, 2019 THALES GLOBAL SERVICES.
 * 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.business.internal.metamodel.helper;

import java.util.Collections;
import java.util.Iterator;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.UniqueEList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.business.api.logger.RuntimeLoggerManager;
import org.eclipse.sirius.common.tools.DslCommonPlugin;
import org.eclipse.sirius.common.tools.api.interpreter.EvaluationException;
import org.eclipse.sirius.common.tools.api.interpreter.IInterpreter;
import org.eclipse.sirius.common.tools.api.interpreter.IInterpreterSiriusVariables;
import org.eclipse.sirius.common.tools.api.util.EObjectCouple;
import org.eclipse.sirius.common.tools.api.util.RefreshIdsHolder;
import org.eclipse.sirius.common.tools.api.util.StringUtil;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.DNode;
import org.eclipse.sirius.diagram.DNodeListElement;
import org.eclipse.sirius.diagram.DiagramFactory;
import org.eclipse.sirius.diagram.Messages;
import org.eclipse.sirius.diagram.NodeStyle;
import org.eclipse.sirius.diagram.ResizeKind;
import org.eclipse.sirius.diagram.business.internal.metamodel.description.extensions.INodeMappingExt;
import org.eclipse.sirius.diagram.business.internal.metamodel.description.operations.AbstractNodeMappingSpecOperations;
import org.eclipse.sirius.diagram.business.internal.metamodel.description.operations.SiriusElementMappingSpecOperations;
import org.eclipse.sirius.diagram.description.NodeMapping;
import org.eclipse.sirius.diagram.description.style.NodeStyleDescription;
import org.eclipse.sirius.ext.base.Option;
import org.eclipse.sirius.ext.base.Options;
import org.eclipse.sirius.tools.api.profiler.SiriusTasksKey;
import org.eclipse.sirius.viewpoint.DSemanticDecorator;
import org.eclipse.sirius.viewpoint.SiriusPlugin;
import org.eclipse.sirius.viewpoint.Style;
import org.eclipse.sirius.viewpoint.description.style.StyleDescription;
import org.eclipse.sirius.viewpoint.description.style.StylePackage;

/**
 * Utility class to factor customizations for ContainerMapping and related.
 * 
 * @author pcdavid
 */
public final class NodeMappingHelper {
    private IInterpreter interpreter;

    private StyleHelper styleHelper;

    /**
     * Create the helper.
     * 
     * @param interpreter
     *            interpreter used to evaluate expressions.
     */
    public NodeMappingHelper(IInterpreter interpreter) {
        this.interpreter = interpreter;
        this.styleHelper = new StyleHelper(interpreter);
    }

    /**
     * Implementation of {@link NodeMapping#getNodesCandidates(EObject, EObject)}.
     * 
     * @param self
     *            the node mapping.
     * @param semanticOrigin
     *            the root element.
     * @param container
     *            the container element.
     * @return all semantic elements that are candidates for the mapping.
     */
    public static EList<EObject> getNodesCandidates(NodeMapping self, EObject semanticOrigin, EObject container) {
        SiriusPlugin.getDefault().warning(Messages.NodeMappingHelper_methodInvocationErrorMsg, null);
        return NodeMappingHelper.getNodesCandidates((INodeMappingExt) self, semanticOrigin, container, null);
    }

    /**
     * ! Implementation of {@link NodeMapping#getNodesCandidates(EObject, EObject, EObject)}.
     * 
     * @param self
     *            the node mapping.
     * @param semanticOrigin
     *            the root element.
     * @param container
     *            the container element.
     * @param containerView
     *            the view of the container.
     * @return all semantic elements that are candidates for the mapping.
     */
    public static EList<EObject> getNodesCandidates(INodeMappingExt self, EObject semanticOrigin, final EObject container, final EObject containerView) {
        DslCommonPlugin.PROFILER.startWork(SiriusTasksKey.GET_NODE_CANDIDATES_KEY);
        EObject safeContainer;
        if (container == null) {
            safeContainer = semanticOrigin;
        } else {
            safeContainer = container;
        }

        DDiagram diagram = null;
        if (containerView instanceof DDiagramElement) {
            diagram = ((DDiagramElement) containerView).getParentDiagram();
        } else if (containerView instanceof DDiagram) {
            diagram = (DDiagram) containerView;
        }
        final EObjectCouple couple = new EObjectCouple(semanticOrigin, safeContainer, RefreshIdsHolder.getOrCreateHolder(diagram));
        EList<EObject> result = self.getCandidatesCache().get(couple);
        if (result == null) {
            result = new UniqueEList<EObject>();
            Iterator<EObject> it;
            it = DiagramElementMappingHelper.getSemanticIterator(self, semanticOrigin, diagram);
            if (self.getDomainClass() != null) {
                while (it.hasNext()) {
                    final EObject eObj = it.next();
                    if (NodeMappingHelper.isInstanceOf(eObj, self.getDomainClass()) && SiriusElementMappingSpecOperations.checkPrecondition(self, eObj, safeContainer, containerView)) {
                        result.add(eObj);
                    }
                }
            } else {
                SiriusPlugin.getDefault().error(Messages.NodeMappingHelper_nodeCreationErrorMsg, new RuntimeException());
            }
            self.getCandidatesCache().put(couple, result);
        }
        DslCommonPlugin.PROFILER.stopWork(SiriusTasksKey.GET_NODE_CANDIDATES_KEY);
        return result;
    }

    /**
     * Implementation of {@link NodeMapping#createNode(EObject, EObject, DDiagram)}.
     * 
     * @param self
     *            the node mapping.
     * @param modelElement
     *            the semantic element.
     * @param container
     *            the semantic container of the element.
     * @param diagram
     *            the diagram.
     * @return the node created.
     */
    public DNode createNode(INodeMappingExt self, EObject modelElement, EObject container, DDiagram diagram) {
        final DNode newNode = DiagramFactory.eINSTANCE.createDNode();

        // getting the right style description : default or conditional
        final NodeStyleDescription style = (NodeStyleDescription) new MappingWithInterpreterHelper(interpreter).getBestStyleDescription(self, modelElement, newNode, container, diagram);

        newNode.setTarget(modelElement);
        newNode.setActualMapping(self);

        DiagramElementMappingHelper.refreshSemanticElements(self, newNode, interpreter);

        interpreter.setVariable(IInterpreterSiriusVariables.DIAGRAM, diagram);
        interpreter.setVariable(IInterpreterSiriusVariables.VIEW, newNode);
        if (style != null && style.getLabelExpression() != null) {
            try {
                final String name = interpreter.evaluateString(modelElement, style.getLabelExpression());
                newNode.setName(name);
            } catch (final EvaluationException e) {
                RuntimeLoggerManager.INSTANCE.error(style, StylePackage.eINSTANCE.getBasicLabelStyleDescription_LabelExpression(), e);
            }
        }
        if (style != null) {
            newNode.setResizeKind(style.getResizeKind());

            styleHelper.setComputedSize(newNode, style);
        }
        final NodeStyle bestStyle = (NodeStyle) new MappingWithInterpreterHelper(interpreter).getBestStyle(self, modelElement, newNode, container, diagram);
        if (bestStyle != null) {
            newNode.setOwnedStyle(bestStyle);
        }

        MappingWithInterpreterHelper.addDoneNode(self, newNode);

        interpreter.unSetVariable(IInterpreterSiriusVariables.VIEW);
        interpreter.unSetVariable(IInterpreterSiriusVariables.DIAGRAM);

        /*
         * Iterate over the border mapping of the node to initialize the border nodes.
         */
        AbstractNodeMappingSpecOperations.createBorderingNodes(self, modelElement, newNode, Collections.EMPTY_LIST, diagram);
        if (newNode.getOwnedStyle() != null) {
            Option<NodeStyle> noPreviousStyle = Options.newNone();
            new StyleHelper(interpreter).refreshStyle(newNode.getOwnedStyle(), noPreviousStyle);
        }
        return newNode;
    }

    /**
     * Implementation of {@link NodeMapping#updateNode(DNode)}.
     * 
     * @param self
     *            the node mapping.
     * @param node
     *            the node to update.
     */
    public void updateNode(NodeMapping self, DNode node) {
        final EObject modelElement = node.getTarget();

        final DSemanticDecorator container = (DSemanticDecorator) node.eContainer();
        NodeStyleDescription style = null;

        // getting the right style description : default or conditional
        DDiagram parentDiagram = node.getParentDiagram();
        if (container != null) {
            style = (NodeStyleDescription) new MappingWithInterpreterHelper(interpreter).getBestStyleDescription(self, modelElement, node, container.getTarget(), parentDiagram);
        }

        if (style != null && style.getLabelExpression() != null) {
            try {
                interpreter.setVariable(IInterpreterSiriusVariables.DIAGRAM, parentDiagram);
                interpreter.setVariable(IInterpreterSiriusVariables.VIEW, node);
                final String name = interpreter.evaluateString(modelElement, style.getLabelExpression());
                node.setName(name);
            } catch (final EvaluationException e) {
                RuntimeLoggerManager.INSTANCE.error(style, StylePackage.eINSTANCE.getBasicLabelStyleDescription_LabelExpression(), e);
            } finally {
                interpreter.unSetVariable(IInterpreterSiriusVariables.DIAGRAM);
                interpreter.unSetVariable(IInterpreterSiriusVariables.VIEW);
            }
        }

        if (style != null && style.getTooltipExpression() != null) {
            try {
                interpreter.setVariable(IInterpreterSiriusVariables.DIAGRAM, parentDiagram);
                interpreter.setVariable(IInterpreterSiriusVariables.VIEW, node);
                final String tooltip = interpreter.evaluateString(modelElement, style.getTooltipExpression());
                node.setTooltipText(tooltip);
            } catch (final EvaluationException e) {
                RuntimeLoggerManager.INSTANCE.error(style, StylePackage.eINSTANCE.getTooltipStyleDescription_TooltipExpression(), e);
            } finally {
                interpreter.unSetVariable(IInterpreterSiriusVariables.DIAGRAM);
                interpreter.unSetVariable(IInterpreterSiriusVariables.VIEW);
            }
        }

        /*
         * Getting the node size
         */
        if (node.getResizeKind() == ResizeKind.NONE_LITERAL) {
            styleHelper.setComputedSize(node, style);
        }

        // semantic elements
        DiagramElementMappingHelper.refreshSemanticElements(self, node, interpreter);

        EObject containerVariable = null;
        if (node.eContainer() instanceof DSemanticDecorator) {
            containerVariable = ((DSemanticDecorator) node.eContainer()).getTarget();
        }

        final StyleDescription bestStyleDescription = new MappingWithInterpreterHelper(interpreter).getBestStyleDescription(self, modelElement, node, containerVariable, parentDiagram);
        Style bestStyle = node.getStyle();
        if ((bestStyle == null || bestStyle.getDescription() != bestStyleDescription) && bestStyleDescription != null) {
            bestStyle = styleHelper.createStyle(bestStyleDescription);
        }

        styleHelper.setAndRefreshStyle(node, node.getStyle(), bestStyle);

        MappingWithInterpreterHelper.addDoneNode(self, node);
    }

    /**
     * Implementation of {@link NodeMapping#createListElement(EObject)}.
     * 
     * @param self
     *            the node mapping.
     * @param modelElement
     *            the semantic model element.
     * @param diagram
     *            the diagram.
     * @return the element created.
     */
    public DNodeListElement createListElement(NodeMapping self, EObject modelElement, DDiagram diagram) {

        final DNodeListElement newNode = DiagramFactory.eINSTANCE.createDNodeListElement();

        // getting the right style description : default or conditional
        final NodeStyleDescription style = (NodeStyleDescription) new MappingWithInterpreterHelper(interpreter).getBestStyleDescription(self, modelElement, newNode, null, diagram);

        newNode.setTarget(modelElement);
        newNode.setActualMapping(self);

        // semantic elements
        DiagramElementMappingHelper.refreshSemanticElements(self, newNode, interpreter);

        if (style != null && !StringUtil.isEmpty(style.getLabelExpression())) {
            newNode.setName(DiagramElementMappingHelper.computeLabel(newNode, style, diagram, interpreter));
        }

        /*
         * Getting the node size
         */
        MappingWithInterpreterHelper.addDoneNode(self, newNode);

        final NodeStyle bestStyle = (NodeStyle) new MappingWithInterpreterHelper(interpreter).getBestStyle(self, modelElement, newNode, null, diagram);
        if (bestStyle != null) {
            newNode.setOwnedStyle(bestStyle);
        }

        return newNode;
    }

    /**
     * Implementation of {@link NodeMapping#updateListElement(DNodeListElement)} .
     * 
     * @param self
     *            the node mapping.
     * @param listElement
     *            the view node list element to update.
     */
    public void updateListElement(NodeMapping self, DNodeListElement listElement) {

        final EObject modelElement = listElement.getTarget();
        final DSemanticDecorator container = (DSemanticDecorator) listElement.eContainer();
        NodeStyleDescription style = null;

        // getting the right style description : default or conditional
        DDiagram parentDiagram = listElement.getParentDiagram();
        if (container != null) {
            style = (NodeStyleDescription) new MappingWithInterpreterHelper(interpreter).getBestStyleDescription(self, modelElement, listElement, container.getTarget(),
                    parentDiagram);
        }

        if (style != null && !StringUtil.isEmpty(style.getLabelExpression())) {
            listElement.setName(DiagramElementMappingHelper.computeLabel(listElement, style, parentDiagram, interpreter));
        }

        EObject containerVariable = null;
        if (listElement.eContainer() instanceof DSemanticDecorator) {
            containerVariable = ((DSemanticDecorator) listElement.eContainer()).getTarget();
        }

        // semantic elements
        DiagramElementMappingHelper.refreshSemanticElements(self, listElement, interpreter);

        final NodeStyle bestStyle = (NodeStyle) new MappingWithInterpreterHelper(interpreter).getBestStyle(self, modelElement, listElement, containerVariable, parentDiagram);

        if ((bestStyle == null || bestStyle.getDescription() != style) && style != null) {
            listElement.setOwnedStyle(bestStyle);
        }
        styleHelper.setAndRefreshStyle(listElement, listElement.getStyle(), bestStyle);
        MappingWithInterpreterHelper.addDoneNode(self, listElement);
    }

    /**
     * Implementation of {@link NodeMapping#findDNodeFromEObject(EObject)}.
     * 
     * @param self
     *            the node mapping.
     * @param object
     *            the semantic element.
     * @return the node found.
     */
    public static EList<DDiagramElement> findDNodeFromEObject(INodeMappingExt self, EObject object) {
        EList result = self.getViewNodesDone().get(object);
        if (result == null) {
            result = new BasicEList<DDiagramElement>();
        }
        return result;
    }

    /**
     * Implementation of {@link NodeMapping#addDoneNode(DSemanticDecorator)}.
     * 
     * @param self
     *            the node mapping.
     * @param node
     *            the node to add.
     */
    public static void addDoneNode(INodeMappingExt self, DSemanticDecorator node) {
        EList<DSemanticDecorator> list = self.getViewNodesDone().get(node.getTarget());
        if (list == null) {
            list = new BasicEList<DSemanticDecorator>();
            self.getViewNodesDone().put(node.getTarget(), list);
        }
        list.add(node);
    }

    /**
     * Implementation of {@link NodeMapping#clearDNodesDone()}.
     * 
     * @param self
     *            the node mapping.
     */
    public static void clearDNodesDone(INodeMappingExt self) {
        self.getViewNodesDone().clear();
        self.getCandidatesCache().clear();
    }

    /**
     * Return <code>true</code> if <code>eObj</code> is an instance of <code>typename</code>.
     * 
     * @param eObj
     *            an object.
     * @param typename
     *            the name of the type to check.
     * @return <code>true</code> if <code>eObj</code> is an instance of <code>typename</code>.
     */
    private static boolean isInstanceOf(final EObject eObj, final String typename) {
        DslCommonPlugin.PROFILER.startWork(SiriusTasksKey.INSTANCE_OF_KEY);
        final boolean result = SiriusPlugin.getDefault().getModelAccessorRegistry().getModelAccessor(eObj).eInstanceOf(eObj, typename);
        DslCommonPlugin.PROFILER.stopWork(SiriusTasksKey.INSTANCE_OF_KEY);
        return result;
    }

}

Back to the top