Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b7f19f32ea0b24b0bc4fb3bccc6251b410ccc19 (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
/*******************************************************************************
 * 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.business.internal.migration.resource.session.diagram.data;

import org.eclipse.emf.ecore.EObject;

import org.eclipse.sirius.common.tools.api.util.Option;
import org.eclipse.sirius.DDiagram;
import org.eclipse.sirius.DDiagramElement;
import org.eclipse.sirius.DEdge;
import org.eclipse.sirius.DSemanticDecorator;
import org.eclipse.sirius.EdgeTarget;
import org.eclipse.sirius.business.api.query.DDiagramElementQuery;
import org.eclipse.sirius.description.RepresentationElementMapping;

/**
 * Factory of all lost elements.
 * 
 * @author dlecan
 */
public final class LostElementFactory {

    private LostElementFactory() {
        // Nothing.
    }

    /**
     * Create lost node data from diagram element.
     * 
     * @param diagramElement
     *            Diagram element.
     * @return Lost node data.
     */
    public static LostNodeData createLostNodeData(final DDiagramElement diagramElement) {
        Option<? extends RepresentationElementMapping> mapping = new DDiagramElementQuery(diagramElement).getMapping();

        LostNodeData data = new LostNodeData();
        data.setTarget(diagramElement.getTarget());
        data.setMapping(mapping.get());
        data.setParentData(LostElementFactory.createLostNodeParentData(diagramElement));
        return data;
    }

    /**
     * Create lost edge data from diagram element.
     * 
     * @param diagramElement
     *            Diagram element.
     * @return Lost edge data.
     */
    public static LostEdgeData createLostEdgeData(final DDiagramElement diagramElement) {
        Option<? extends RepresentationElementMapping> mapping = new DDiagramElementQuery(diagramElement).getMapping();

        final LostEdgeData data = new LostEdgeData();
        data.setTarget(diagramElement.getTarget());
        data.setMapping(mapping.get());

        final EdgeTarget sourceNode = ((DEdge) diagramElement).getSourceNode();
        final EdgeTarget targetNode = ((DEdge) diagramElement).getTargetNode();
        if (sourceNode instanceof DDiagramElement && LostElementFactory.isCompleteDiagramElement((DDiagramElement) sourceNode)) {
            final LostNodeData sourceData = LostElementFactory.createLostNodeData((DDiagramElement) sourceNode);
            data.setSourceData(sourceData);

            if (targetNode instanceof DDiagramElement && LostElementFactory.isCompleteDiagramElement((DDiagramElement) targetNode)) {
                final LostNodeData targetData = LostElementFactory.createLostNodeData((DDiagramElement) targetNode);
                data.setTargetData(targetData);
            }
        }

        return data;
    }

    /**
     * Check if diagram element is complete.
     * 
     * @param diagElement
     *            Diagram element to check. .
     * @return <code>true</code> if diagram element and mapping and target are
     *         not <code>null</code>.
     */
    public static boolean isCompleteDiagramElement(final DDiagramElement diagElement) {
        return diagElement != null && diagElement.getDiagramElementMapping() != null && diagElement.getTarget() != null && !diagElement.getTarget().eIsProxy();
    }

    private static ILostElementDataContainer createLostNodeParentData(final DDiagramElement diagElement) {
        final EObject eContainer = diagElement.eContainer();

        ILostElementDataContainer parent = null;

        if (eContainer != null) {
            if (eContainer instanceof DDiagram) {
                final LostDiagramData parentData = new LostDiagramData();
                if (eContainer instanceof DSemanticDecorator) {
                    parentData.setTarget(((DSemanticDecorator) eContainer).getTarget());
                }
                parent = parentData;

            } else if (eContainer instanceof DDiagramElement) {
                final DDiagramElement containerDiagramElement = (DDiagramElement) eContainer;

                if (LostElementFactory.isCompleteDiagramElement(containerDiagramElement)) {
                    parent = LostElementFactory.createLostNodeData(containerDiagramElement);
                }
            }
        }
        return parent;
    }
}

Back to the top