Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 54a58027a2d192048f7cfee9af6b92ffc354f922 (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
/*******************************************************************************
 * Copyright (c) 2009, 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.tools.internal.ui;

import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.SharedCursors;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.sirius.ext.gmf.runtime.diagram.ui.tools.MoveInDiagramDragTracker;
import org.eclipse.swt.events.MouseEvent;

/**
 * Specific implementation of
 * {@link org.eclipse.gmf.runtime.diagram.ui.tools.DragEditPartsTrackerEx}.
 * <br/>
 * It adds margins around parent dragged edit part in order to lower drag 'n
 * drop sensibility.
 * 
 * @author dlecan
 */
public class SiriusDragEditPartsTrackerEx extends SnapToAllDragEditPartsTracker implements MoveInDiagramDragTracker {

    /**
     * The x margin used when moving bordered node to not have drag'n drop
     * considered.
     */
    private static final int X_MARGIN = 30;

    /**
     * The y margin used when moving bordered node to not have drag'n drop
     * considered.
     */
    private static final int Y_MARGIN = X_MARGIN;

    private Point previousMouseLocation;

    /**
     * Constructor.
     * 
     * @param sourceEditPart
     *            Edit part.
     */
    public SiriusDragEditPartsTrackerEx(final EditPart sourceEditPart) {
        super(sourceEditPart);
    }

    /**
     * Overridden to find the {@link EditPart} under the adapted mouse location,
     * to allow when moving bordered node to not have drag'n drop considered.
     * 
     * {@inheritDoc}
     */
    @Override
    protected boolean updateTargetUnderMouse() {
        if (!isTargetLocked()) {

            EditPart editPart = getCurrentViewer().findObjectAtExcluding(getAdaptedMouseLocation(), getExclusionSet(), getTargetingConditional());

            if (editPart != null) {
                editPart = editPart.getTargetEditPart(getTargetRequest());
            }

            final boolean changed = getTargetEditPart() != editPart;
            setTargetEditPart(editPart);

            return changed;
        } else {
            return false;
        }
    }

    /**
     * Get the parent of the current edit part.
     * 
     * @return The parent of the current edit part
     */
    protected EditPart getSourceParentEditPart() {
        final EditPart parent = getSourceEditPart().getParent();
        return parent;
    }

    /**
     * Indicates if mouse location has to be adapted.
     * 
     * @param parentBounds
     *            Parent bounds
     * @param mouseLocation
     *            Current mouse location
     * @return <code>true</code> if mouse location has to be adapted.
     *         <code>false</code> otherwise
     */
    protected boolean hasToAdaptMouseLocation(final Rectangle parentBounds, final Point mouseLocation) {
        final Rectangle boundsAndMargins = parentBounds.getExpanded(X_MARGIN, Y_MARGIN);
        return boundsAndMargins.contains(mouseLocation) && !parentBounds.contains(mouseLocation);
    }

    /**
     * Adapt mouse location. Algorithm does an homothetic transformation,
     * centered on center of the provided rectangle to relocalize mouse
     * position.
     * 
     * @return Relocated mouse position.
     */
    protected Point getAdaptedMouseLocation() {
        final Rectangle parentBounds = getAbsoluteSourceParentBounds();

        final Point mouseRealLocation = super.getLocation();
        Point mouseAdaptedLocation;
        if (hasToAdaptMouseLocation(parentBounds, mouseRealLocation)) {
            final Point center = parentBounds.getCenter();
            final Dimension rectangleDimension = parentBounds.getSize();

            final double xLocation = getAdaptedMouseXLocation(mouseRealLocation.x, center.x, rectangleDimension.width);
            final double yLocation = getAdaptedMouseYLocation(mouseRealLocation.y, center.y, rectangleDimension.height);
            mouseAdaptedLocation = new PrecisionPoint(xLocation, yLocation);
        } else {
            mouseAdaptedLocation = mouseRealLocation.getCopy();
        }

        return mouseAdaptedLocation;
    }

    /**
     * Get absolute parent bounds.
     * 
     * @return absolute parent bounds
     */
    private Rectangle getAbsoluteSourceParentBounds() {
        final EditPart parent = getSourceParentEditPart();

        final Rectangle absoluteBounds = ((GraphicalEditPart) parent).getFigure().getBounds().getCopy();
        ((GraphicalEditPart) parent).getFigure().translateToAbsolute(absoluteBounds);
        return absoluteBounds;
    }

    private double getAdaptedMouseXLocation(final int xMouseLocation, final int xCenter, final double width) {
        return getAdaptedMouseCoordinateLocation(xMouseLocation, xCenter, width, X_MARGIN);
    }

    private double getAdaptedMouseYLocation(final int yMouseLocation, final int yCenter, final double height) {
        return getAdaptedMouseCoordinateLocation(yMouseLocation, yCenter, height, Y_MARGIN);
    }

    private double getAdaptedMouseCoordinateLocation(final int mouseLocation, final int center, final double length, final int delta) {
        final double halfLength = length / 2;
        final double result = center + (halfLength / (halfLength + delta)) * (mouseLocation - center);

        return result;
    }

    /**
     * Overridden to update the created {@link ChangeBoundsRequest} to use the
     * adapted mouse location, to allow when moving bordered node to not have
     * drag'n drop considered.
     * 
     * {@inheritDoc}
     */
    @Override
    protected void updateTargetRequest() {
        super.updateTargetRequest();
        if (getTargetRequest() instanceof ChangeBoundsRequest) {
            ChangeBoundsRequest targetRequest = (ChangeBoundsRequest) getTargetRequest();
            targetRequest.setLocation(getAdaptedMouseLocation());
        }
    }

    /**
     * Always disable the clone with Ctrl key in Sirius because it only clone
     * the graphical element and not the semantic element.
     * 
     * @param cloneActive
     *            true if cloning should be active (never considered here)
     * @see org.eclipse.gef.tools.DragEditPartsTracker#setCloneActive(boolean)
     */
    @Override
    protected void setCloneActive(boolean cloneActive) {
        super.setCloneActive(false);
    }

    @Override
    protected boolean handleButtonDown(int button) {
        if (button == 2) {
            setCursor(SharedCursors.HAND);
            return stateTransition(STATE_INITIAL, STATE_SCROLL_DIAGRAM);
        } else {
            return super.handleButtonDown(button);
        }
    }

    @Override
    protected boolean handleButtonUp(int button) {
        // The above code is copied from the super classes (SnapToAllDragEditPartsTracker, DragEditPartsTracker and
        // SelectEditPartTracker) to disable the reveal.
        boolean result = false;
        if (stateTransition(STATE_DRAG_IN_PROGRESS, STATE_TERMINAL)) {
            eraseSourceFeedback();
            eraseTargetFeedback();
            performDrag();
            result = true;
        } else if (isInState(STATE_DRAG)) {
            performSelection();
            if (getFlag(org.eclipse.gef.tools.SelectEditPartTracker.MAX_FLAG))
                // SelectEditPartTracker.MAX_FLAG is a protected constant equals to
                // SelectEditPartTracker.FLAG_ENABLE_DIRECT_EDIT (that must be originally used here).
                performDirectEdit();
            // The SelectEditPartTracker behavior is overridden here to never reveal the selected element.
            // if (button == 1 && getSourceEditPart().getSelected() != EditPart.SELECTED_NONE)
            // getCurrentViewer().reveal(getSourceEditPart());
            setState(STATE_TERMINAL);
            result = true;
        }
        // Clean up the mode to original state.
        snapToAllShape = SnapToAllDragEditPartsTracker.DEFAULT_SNAP_TO_SHAPE_MODE;
        return result;
    }

    @Override
    public void mouseDrag(MouseEvent me, EditPartViewer viewer) {
        previousMouseLocation = getCurrentInput().getMouseLocation().getCopy();
        super.mouseDrag(me, viewer);
    }

    @Override
    protected boolean handleDragStarted() {
        if (isInState(STATE_SCROLL_DIAGRAM)) {
            return stateTransition(STATE_SCROLL_DIAGRAM, STATE_SCROLL_DIAGRAM_IN_PROGRESS);
        }
        return super.handleDragStarted();
    }

    @Override
    protected boolean handleDragInProgress() {
        if (isInState(STATE_SCROLL_DIAGRAM_IN_PROGRESS)) {
            if (getCurrentViewer().getControl() instanceof FigureCanvas) {
                FigureCanvas figureCanvas = (FigureCanvas) getCurrentViewer().getControl();
                Point currentMouseLocation = getCurrentInput().getMouseLocation();
                Dimension difference = previousMouseLocation.getDifference(currentMouseLocation);
                Point location = figureCanvas.getViewport().getViewLocation();
                figureCanvas.scrollTo(location.x + difference.width, location.y + difference.height);
            }
            return true;
        }
        return super.handleDragInProgress();
    }

    @Override
    protected void reveal(EditPart editpart) {
        // In Sirius, the drag'n'drop can change (delete and create a new
        // one) the previous container of the drag'n'droped element. In this
        // case, the reveal causes a NPE because the hierarchy of edit part is
        // broken.
        if (editpart.getRoot() != null) {
            super.reveal(editpart);
        }
    }
}

Back to the top