Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7670f8b238e14b076148b83761a010e982bb962c (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
/*******************************************************************************
 * Copyright (c) 2015, 2017 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.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.gef.ConnectionEditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.Request;
import org.eclipse.gef.SharedCursors;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.BendpointRequest;
import org.eclipse.sirius.diagram.ui.graphical.edit.policies.MoveEdgeGroupManager;
import org.eclipse.sirius.ext.gmf.runtime.diagram.ui.tools.MoveInDiagramDragTracker;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Cursor;

/**
 * A specific Sirius tracker to handle the move edge group feature.
 * 
 * @author Florian Barbin
 *
 */
@SuppressWarnings("restriction")
public class SiriusSelectConnectionEditPartTracker extends SelectConnectionEditPartTracker implements MoveInDiagramDragTracker {

    private boolean moveGroupActivated;

    /**
     * The getSourceRequest method is private so we need to keep the reference in the createSourceRequest method.
     */
    private BendpointRequest bendpointRequest;

    private Point previousMouseLocation;

    /**
     * Method SelectConnectionEditPartTracker.
     * 
     * @param owner
     *            ConnectionNodeEditPart that creates and owns the tracker object
     */
    public SiriusSelectConnectionEditPartTracker(ConnectionEditPart owner) {
        super(owner);
        setDisabledCursor(SharedCursors.NO);
    }

    @Override
    protected Request createSourceRequest() {
        Request rq = super.createSourceRequest();
        if (rq instanceof BendpointRequest) {
            bendpointRequest = (BendpointRequest) rq;
        }
        return rq;
    }

    @Override
    protected void updateSourceRequest() {
        super.updateSourceRequest();
        if (bendpointRequest != null) {
            if (moveGroupActivated) {
                bendpointRequest.getExtendedData().put(MoveEdgeGroupManager.EDGE_MOVE_DELTA, computeDelta());
                bendpointRequest.getExtendedData().put(MoveEdgeGroupManager.EDGE_GROUP_MOVE_KEY, Boolean.TRUE);
                bendpointRequest.getExtendedData().put(MoveEdgeGroupManager.EDGE_GROUP_MOVE_HAS_BEEN_ACTIVATED_KEY, Boolean.TRUE);
            } else {
                bendpointRequest.getExtendedData().remove(MoveEdgeGroupManager.EDGE_MOVE_DELTA);
                bendpointRequest.getExtendedData().put(MoveEdgeGroupManager.EDGE_GROUP_MOVE_KEY, Boolean.FALSE);
            }
        }
    }

    @Override
    public void deactivate() {
        super.deactivate();
        // We also set our field to null.
        bendpointRequest = null;
    }

    private Dimension computeDelta() {
        Point newLocation = bendpointRequest.getLocation();
        Dimension diff;
        if (newLocation != null) {
            diff = newLocation.getDifference(getStartLocation());
        } else {
            diff = getDragMoveDelta();
        }
        return diff;
    }

    @Override
    protected boolean handleKeyDown(KeyEvent e) {
        if (SWT.F3 == e.keyCode) {
            moveGroupActivated = true;
            return true;
        }
        return super.handleKeyDown(e);
    }

    @Override
    protected boolean handleKeyUp(KeyEvent e) {
        if (SWT.F3 == e.keyCode) {
            moveGroupActivated = false;
            return true;
        }
        return super.handleKeyUp(e);
    }

    @Override
    protected Cursor calculateCursor() {
        Cursor cursorToReturn;
        if (moveGroupActivated) {
            Command command = getCurrentCommand();
            if (command == null || !command.canExecute()) {
                cursorToReturn = getDisabledCursor();
            } else {
                cursorToReturn = SharedCursors.ARROW;
            }
        } else {
            cursorToReturn = super.calculateCursor();
        }
        return cursorToReturn;
    }

    @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
    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();
    }

}

Back to the top