Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3afa1eff1b7f4cd6127dd80d729d2330e62cc243 (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
/*******************************************************************************
 * Copyright (c) 2010, 2016 THALES GLOBAL SERVICES and others.
 * 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.ui.tools.internal.actions.repair;

import java.text.MessageFormat;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.sirius.common.tools.api.util.EclipseUtil;
import org.eclipse.sirius.common.tools.api.util.StringUtil;
import org.eclipse.sirius.diagram.AbstractDNode;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.DEdge;
import org.eclipse.sirius.diagram.description.IEdgeMapping;
import org.eclipse.sirius.diagram.ui.provider.Messages;
import org.eclipse.sirius.diagram.ui.tools.api.migration.IDiagramIdentifierProvider;
import org.eclipse.sirius.viewpoint.Customizable;
import org.eclipse.sirius.viewpoint.description.RepresentationElementMapping;

/**
 * Identifier for
 * {@link org.eclipse.sirius.diagram.tools.internal.actions.migration.state.IDiagramElementState}
 * .
 * <p>
 * Just a wrapper around a {@link String}.
 * </p>
 * 
 * @author dlecan
 */
public final class Identifier {

    /**
     * Empty identifier.
     */
    public static final Identifier EMPTY_ID = new Identifier(StringUtil.EMPTY_STRING);

    /** The extension point ID. */
    private static final String PROVIDERS_ID = "org.eclipse.sirius.diagram.ui.diagramIdentifierProvider"; //$NON-NLS-1$

    /** The class attribute. */
    private static final String PROVIDERS_ATTRIBUTE = "providerClass"; //$NON-NLS-1$

    /** The string target. */
    private static final String TARGET = "target"; //$NON-NLS-1$

    /** All providers. */
    private static List<IDiagramIdentifierProvider> allProviders;

    private String identifier;

    private Identifier(String identifier) {
        this.identifier = identifier;
    }

    private Identifier(StringBuilder identifier) {
        this(identifier.toString());
    }

    /**
     * Creates the identifier of the element.
     * 
     * @param element
     *            the diagram element.
     * @return Identifier.
     */
    public static Identifier createIdentifier(final DDiagramElement element) {
        Identifier result = EMPTY_ID;
        String id = getId(element);
        if (id != null) {
            result = new Identifier(id);
        }
        return result;
    }

    private static String getId(DDiagramElement element) {
        String id = null;
        if (element instanceof DEdge) {
            id = Identifier.getEdgeId((DEdge) element);
        } else if (element instanceof AbstractDNode) {
            id = Identifier.getNodeId((AbstractDNode) element);
        }
        return id;
    }

    /**
     * Get a id for a edge.
     * 
     * @param edge
     *            the edge.
     * @return id
     */
    private static String getEdgeId(final DEdge edge) {
        StringBuilder buffer = new StringBuilder();

        if (edge != null) {
            if (!Identifier.identifierFromExtensions(edge, buffer)) {
                Identifier.createDiagramIdentifier(edge.getParentDiagram(), buffer);

                if (edge.getSourceNode() instanceof AbstractDNode) {
                    Identifier.createNodeIdentifier((AbstractDNode) edge.getSourceNode(), buffer);
                } else if (edge.getSourceNode() instanceof DEdge) {
                    Identifier.createEdgeIdentifier((DEdge) edge.getSourceNode(), buffer);
                }

                if (edge.getTargetNode() instanceof AbstractDNode) {
                    Identifier.createNodeIdentifier((AbstractDNode) edge.getTargetNode(), buffer);
                } else if (edge.getTargetNode() instanceof DEdge) {
                    Identifier.createEdgeIdentifier((DEdge) edge.getTargetNode(), buffer);
                }
                Identifier.checkNotNull(edge.getTarget(), TARGET);
                buffer.append(EcoreUtil.getURI(edge.getTarget()));

                IEdgeMapping actualMapping = edge.getActualMapping();
                Identifier.checkNotNull(actualMapping, "actualMapping"); //$NON-NLS-1$
                buffer.append(EcoreUtil.getURI(actualMapping));
            }
        }
        return buffer.toString();
    }

    /**
     * Get a id for a node.
     * 
     * @param node
     *            the node.
     * @return id
     */
    private static String getNodeId(final AbstractDNode node) {
        StringBuilder sb = new StringBuilder();

        if (node != null) {
            Identifier.createDiagramIdentifier(node.getParentDiagram(), sb);
            Identifier.createNodeIdentifier(node, sb);
        }
        return sb.toString();
    }

    /**
     * Computes the identifier of the node.
     * 
     * @param node
     *            the node.
     * @param buffer
     *            the buffer.
     */
    private static void createNodeIdentifier(final AbstractDNode node, final StringBuilder buffer) {
        if (node != null) {
            if (!Identifier.identifierFromExtensions(node, buffer)) {
                if (node.eContainer() instanceof AbstractDNode) {
                    Identifier.createNodeIdentifier((AbstractDNode) node.eContainer(), buffer);
                }

                Identifier.checkNotNull(node.getTarget(), TARGET);
                buffer.append(EcoreUtil.getURI(node.getTarget()));

                RepresentationElementMapping mapping = node.getMapping();
                Identifier.checkNotNull(mapping, "mapping"); //$NON-NLS-1$
                buffer.append(EcoreUtil.getURI(mapping));
            }
        }
    }

    /**
     * Computes the identifier of the edge.
     * 
     * @param edge
     *            the edge.
     * @param buffer
     *            the buffer.
     */
    private static void createEdgeIdentifier(final DEdge edge, final StringBuilder buffer) {
        if (edge != null) {
            if (!Identifier.identifierFromExtensions(edge, buffer)) {
                Identifier.checkNotNull(edge.getTarget(), TARGET);
                buffer.append(EcoreUtil.getURI(edge.getTarget()));

                RepresentationElementMapping mapping = edge.getMapping();
                Identifier.checkNotNull(mapping, "mapping"); //$NON-NLS-1$
                buffer.append(EcoreUtil.getURI(mapping));
            }
        }
    }

    private static void createDiagramIdentifier(final DDiagram diagram, final StringBuilder buffer) {
        buffer.append(EcoreUtil.getURI(diagram));
    }

    /**
     * Get a {@link Identifier} for the specified {@link Customizable}.
     * 
     * @param customizable
     *            the specified {@link Customizable}
     * @return a {@link Identifier}
     */
    public static Identifier createCustomizableIdentifier(Customizable customizable) {
        StringBuilder sb = new StringBuilder();
        DDiagramElement dDiagramElement = getDDiagramElement(customizable);
        String id = getId(dDiagramElement);
        if (id != null) {
            sb.append(id);
        }
        if (!(customizable.eContainer() instanceof DDiagramElement)) {
            EObject eContainer = customizable.eContainer();
            sb.append("." + eContainer.eContainingFeature().getName() + "."); //$NON-NLS-1$ //$NON-NLS-2$
        }
        sb.append(customizable.eContainingFeature().getName());
        if (customizable.eContainingFeature().isMany()) {
            Object list = customizable.eContainer().eGet(customizable.eContainingFeature());
            if (list instanceof List<?>) {
                @SuppressWarnings("unchecked")
                List<?> values = (List<Object>) list;
                sb.append("[" + values.indexOf(customizable) + "]"); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }
        return new Identifier(sb);
    }

    private static DDiagramElement getDDiagramElement(Customizable customizable) {
        DDiagramElement dDiagramElement = null;
        EObject container = customizable.eContainer();
        while (!(container instanceof DDiagramElement) && container != null) {
            container = container.eContainer();
        }
        if (container instanceof DDiagramElement) {
            dDiagramElement = (DDiagramElement) container;
        }
        return dDiagramElement;
    }

    private static void checkNotNull(Object obj, String propertyName) {
        if (obj == null) {
            throw new IllegalArgumentException(MessageFormat.format(Messages.Identifier_invalidNullObject, propertyName));
        }
    }

    private static boolean identifierFromExtensions(final DDiagramElement element, final StringBuilder buffer) {
        for (final IDiagramIdentifierProvider identifierProvider : Identifier.getAllProviders()) {
            if (identifierProvider.provides(element)) {
                final String string = identifierProvider.computeIdentifier(element);
                buffer.append(string);
                return true;
            }
        }
        return false;
    }

    /**
     * Returns all providers.
     * 
     * @return all providers.
     */
    private static List<IDiagramIdentifierProvider> getAllProviders() {
        if (allProviders == null) {
            allProviders = EclipseUtil.getExtensionPlugins(IDiagramIdentifierProvider.class, PROVIDERS_ID, PROVIDERS_ATTRIBUTE);
        }
        return allProviders;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    // Eclipse generated code
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
        return result;
    }

    /**
     * {@inheritDoc}
     */
    // CHECKSTYLE:OFF
    @Override
    // Eclipse generated code
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Identifier)) {
            return false;
        }
        Identifier other = (Identifier) obj;
        if (identifier == null) {
            if (other.identifier != null) {
                return false;
            }
        } else if (!identifier.equals(other.identifier)) {
            return false;
        }
        return true;
    }

    // CHECKSTYLE:ON

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "id: " + identifier; //$NON-NLS-1$
    }

}

Back to the top