Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f4f11f0e80299b04a17f42053cd9763ddf4f2c76 (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
/*******************************************************************************
 * Copyright (c) 2017 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.ui.business.internal.migration;

import java.util.Collection;
import java.util.HashSet;

import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.gmf.runtime.diagram.core.util.ViewType;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Shape;
import org.eclipse.gmf.runtime.notation.TextAlignment;
import org.eclipse.gmf.runtime.notation.TextStyle;
import org.eclipse.sirius.business.api.migration.AbstractRepresentationsFileMigrationParticipant;
import org.eclipse.sirius.business.api.query.DViewQuery;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.ui.business.api.query.DDiagramGraphicalQuery;
import org.eclipse.sirius.diagram.ui.business.api.query.ViewQuery;
import org.eclipse.sirius.diagram.ui.internal.view.factories.SiriusNoteViewFactory;
import org.eclipse.sirius.ext.base.Option;
import org.eclipse.sirius.viewpoint.DAnalysis;
import org.eclipse.sirius.viewpoint.DView;
import org.osgi.framework.Version;

import com.google.common.collect.Iterables;

/**
 * Default label alignment of Note's shapes have been updated in GMF runtime 1.8.0 (see
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=432387). This migration participant restores the old default alignment
 * value.
 * 
 * @see org.eclipse.sirius.diagram.ui.internal.edit.parts.SiriusDescriptionCompartmentEditPart
 * @see org.eclipse.sirius.diagram.ui.internal.providers.SiriusNoteViewProvider
 * @see org.eclipse.sirius.diagram.ui.internal.view.factories.SiriusNoteViewFactory
 * 
 * @author <a href="mailto:axel.richard@obeo.fr">Axel Richard</a>
 * 
 */
public class NoteShapeDefaultLabelAlignmentMigrationParticipant extends AbstractRepresentationsFileMigrationParticipant {

    /**
     * The Sirius version for which this migration is added.
     */
    private static final Version MIGRATION_VERSION = new Version("12.0.0.201704070000"); //$NON-NLS-1$

    @Override
    public Version getMigrationVersion() {
        return MIGRATION_VERSION;
    }

    @Override
    protected void postLoad(DAnalysis dAnalysis, Version loadedVersion) {
        if (loadedVersion.compareTo(MIGRATION_VERSION) < 0) {
            for (Shape noteShape : getNoteShapes(dAnalysis)) {
                EAnnotation specificStyles = noteShape.getEAnnotation(ViewQuery.SPECIFIC_STYLES);
                if (specificStyles == null) {
                    specificStyles = SiriusNoteViewFactory.createDefaultVerticalAlignmentEAnnotation();
                    noteShape.getEAnnotations().add(specificStyles);
                    for (TextStyle textStyle : Iterables.filter(noteShape.getStyles(), TextStyle.class)) {
                        if (TextAlignment.LEFT_LITERAL == textStyle.getTextAlignment()) {
                            textStyle.setTextAlignment(TextAlignment.CENTER_LITERAL);
                        }
                    }
                }

            }
        }
    }

    /**
     * Get the {@link DDiagram}s contained in the given resource to migrate.
     * 
     * @param dAnalysis
     *            The analysis of the resource to migrate.
     * @return the {@link DDiagram}s contained in the given resource to migrate.
     */
    private Collection<DDiagram> getDiagrams(DAnalysis dAnalysis) {
        Collection<DDiagram> diagrams = new HashSet<>();
        for (DView view : dAnalysis.getOwnedViews()) {
            Iterables.addAll(diagrams, Iterables.filter(new DViewQuery(view).getLoadedRepresentations(), DDiagram.class));
        }
        return diagrams;
    }

    /**
     * Get the {@link Shape}s with Note types contained in the given resource to migrate.
     * 
     * @param dAnalysis
     *            The analysis of the resource to migrate.
     * @return the {@link Shape}s with Note types contained in the given resource to migrate.
     */
    private Collection<Shape> getNoteShapes(DAnalysis dAnalysis) {
        Collection<Shape> shapes = new HashSet<>();
        for (DDiagram dDiagram : getDiagrams(dAnalysis)) {
            shapes.addAll(getNoteShapes(dDiagram));
        }
        return shapes;
    }

    /**
     * Get the {@link Shape}s with Note types contained in the given {@link DDiagram}.
     * 
     * @param dDiagram
     *            the given {@link DDiagram}.
     * @return the {@link Shape}s with Note types contained in the given {@link DDiagram}.
     */
    private Collection<Shape> getNoteShapes(DDiagram dDiagram) {
        Collection<Shape> shapes = new HashSet<>();
        DDiagramGraphicalQuery query = new DDiagramGraphicalQuery(dDiagram);
        Option<Diagram> gmfDiagram = query.getAssociatedGMFDiagram();
        if (gmfDiagram.some()) {
            for (Shape shape : Iterables.filter(gmfDiagram.get().getChildren(), Shape.class)) {
                if (ViewType.NOTE.equals(shape.getType())) {
                    shapes.add(shape);
                }
            }
        }
        return shapes;
    }
}

Back to the top