Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7bf9781b1e97182741508f8c7c69512b580d844e (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
/******************************************************************************
 * Copyright (c) 2007, 2016 IBM Corporation 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:
 *    IBM Corporation - initial API and implementation 
 ****************************************************************************/
package org.eclipse.sirius.diagram.ui.tools.internal.ruler;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.SnapToGeometry;
import org.eclipse.gef.SnapToGrid;
import org.eclipse.gef.SnapToHelper;
import org.eclipse.gef.rulers.RulerProvider;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.internal.editparts.ISurfaceEditPart;
import org.eclipse.gmf.runtime.diagram.ui.internal.ruler.CompoundSnapToHelperEx;
import org.eclipse.gmf.runtime.diagram.ui.internal.ruler.SnapToGridEx;
import org.eclipse.gmf.runtime.diagram.ui.internal.ruler.SnapToGuidesEx;
import org.eclipse.gmf.runtime.diagram.ui.internal.ruler.SnapToHelperUtil;
import org.eclipse.sirius.diagram.ui.edit.api.part.AbstractDiagramNodeEditPart;

/**
 * Utility class for the snapping behavior. This class adds the capability to
 * snap to all visible figures.
 * 
 * @author <a href="mailto:laurent.redor@obeo.fr">Laurent Redor</a>
 */
public class SiriusSnapToHelperUtil extends SnapToHelperUtil {
    // CHECKSTYLE:OFF Method duplicate from SnapToHelperUtil and not formatted
    // to facilitate comparison.
    /**
     * returns the the appropriate snap helper(s), this method will always reach
     * for the first reachable DiagramEditPart using the passed edit part, then
     * use this Diagram edit part to get the snap helper
     * 
     * @param editPart
     *            , edit part to get the snap helper for
     * @return
     */
    static public Object getSnapHelper(GraphicalEditPart editPart) {
        // get the diagram Edit Part
        GraphicalEditPart diagramEditPart = editPart;
        while (diagramEditPart != null && !(diagramEditPart instanceof DiagramEditPart)) {
            diagramEditPart = (GraphicalEditPart) diagramEditPart.getParent();
        }

        if (diagramEditPart == null) {
            return null;
        }

        // for snap to geometry, attempt to locate a compartment as a parent
        GraphicalEditPart parent = editPart;
        // If the editPart is an AbstractDiagramNodeEditPart, this means that
        // element to snap is a border node of a DNode, this is an exception
        // where we do not attempt to locate a compartment as a parent.
        if (!(parent instanceof AbstractDiagramNodeEditPart)) {
            while (parent != null && !(parent instanceof ISurfaceEditPart)) {
                parent = (GraphicalEditPart) parent.getParent();
            }
        }

        if (parent == null) {
            parent = diagramEditPart;
        }

        List<SnapToHelper> snapStrategies = new ArrayList<SnapToHelper>();
        EditPartViewer viewer = diagramEditPart.getViewer();

        Boolean val = (Boolean) viewer.getProperty(RulerProvider.PROPERTY_RULER_VISIBILITY);

        if (val != null && val.booleanValue()) {
            snapStrategies.add(new SnapToGuidesEx(diagramEditPart));
        }

        val = (Boolean) viewer.getProperty(SnapToGeometry.PROPERTY_SNAP_ENABLED);
        if (val != null && val.booleanValue()) {
            snapStrategies.add(new SiriusSnapToGeometry(parent));
        }

        val = (Boolean) viewer.getProperty(SnapToGrid.PROPERTY_GRID_ENABLED);

        if (val != null && val.booleanValue()) {
            snapStrategies.add(new SnapToGridEx(diagramEditPart));
        }

        if (snapStrategies.size() == 0) {
            return null;
        }

        if (snapStrategies.size() == 1) {
            return snapStrategies.get(0);
        }

        SnapToHelper ss[] = new SnapToHelper[snapStrategies.size()];
        for (int i = 0; i < snapStrategies.size(); i++) {
            ss[i] = snapStrategies.get(i);
        }
        return new CompoundSnapToHelperEx(ss);
    }
    // CHECKSTYLE:ON
}

Back to the top