-
Notifications
You must be signed in to change notification settings - Fork 23
/
ScrollingConnectionEndpointTracker.java
206 lines (183 loc) · 6.89 KB
/
ScrollingConnectionEndpointTracker.java
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
/*******************************************************************************
* Copyright (c) 2019, 2022 Johannes Kepler University Linz,
* Primetals Technologies Austria
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alois Zoitl - initial API and implementation and/or initial documentation
* - added the option to duplicate the connection when pressing
* the ctrl key during dragging
* - keep connection draging within canvas bounds
* - used new InlineConnectionCreationTool for duplicating connections
*******************************************************************************/
package org.eclipse.fordiac.ide.gef.tools;
import java.util.Map;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.fordiac.ide.gef.figures.HideableConnection;
import org.eclipse.fordiac.ide.gef.router.MoveableRouter;
import org.eclipse.fordiac.ide.model.ui.editors.AdvancedScrollingGraphicalViewer;
import org.eclipse.fordiac.ide.ui.preferences.ConnectionPreferenceValues;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.gef.tools.ConnectionEndpointTracker;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
public class ScrollingConnectionEndpointTracker extends ConnectionEndpointTracker {
// Safety border around the canvas to ensure that during dragging connections
// the canvas is not growing
private static final Insets CONNECTION_CANVAS_BORDER = new Insets(ConnectionPreferenceValues.HANDLE_SIZE,
MoveableRouter.MIN_CONNECTION_FB_DISTANCE_SCREEN + HideableConnection.BEND_POINT_BEVEL_SIZE
+ ConnectionPreferenceValues.HANDLE_SIZE,
ConnectionPreferenceValues.HANDLE_SIZE, ConnectionPreferenceValues.HANDLE_SIZE);
private InlineConnectionCreationTool conCreationTool = null;
public ScrollingConnectionEndpointTracker(final ConnectionEditPart cep) {
super(cep);
}
@Override
public void mouseDrag(final MouseEvent me, final EditPartViewer viewer) {
if (isActive() && (viewer instanceof final AdvancedScrollingGraphicalViewer advViewer)) {
final Point oldViewPort = advViewer.getViewLocation();
advViewer.checkScrollPositionDuringDragBounded(me,
new Point(MoveableRouter.MIN_CONNECTION_FB_DISTANCE_SCREEN
+ HideableConnection.BEND_POINT_BEVEL_SIZE + ConnectionPreferenceValues.HANDLE_SIZE,
ConnectionPreferenceValues.HANDLE_SIZE));
final Dimension delta = oldViewPort
.getDifference(((AdvancedScrollingGraphicalViewer) viewer).getViewLocation());
// Compensate the moved scrolling in the start position for correct dropping of
// moved parts
setStartLocation(getStartLocation().getTranslated(delta));
CanvasHelper.bindToContentPane(me, (AdvancedScrollingGraphicalViewer) viewer, getCanvasBorder());
}
if (null != conCreationTool) {
getCurrentInput().setInput(me);
conCreationTool.mouseDrag(me, viewer);
} else if ((me.stateMask & SWT.MOD1) != 0) {
startConnCreation();
} else {
super.mouseDrag(me, viewer);
}
}
@SuppressWarnings("static-method") // allow sub-classes to override the border calculation
protected Insets getCanvasBorder() {
return CONNECTION_CANVAS_BORDER;
}
@Override
public void keyDown(final KeyEvent keyEvent, final EditPartViewer viewer) {
if ((null == conCreationTool) && (keyEvent.keyCode == SWT.MOD1)) {
// Ctrl or Command key was pressed and conn creation was not active
startConnCreation();
} else {
super.keyDown(keyEvent, viewer);
}
}
private void startConnCreation() {
final EditPart target = (getCommandName().equals(RequestConstants.REQ_RECONNECT_SOURCE))
? getConnectionEditPart().getTarget()
: getConnectionEditPart().getSource();
conCreationTool = InlineConnectionCreationTool.createInlineConnCreationTool(target, getDomain(),
getCurrentViewer(), getLocation());
updateTarget(getStartLocation());
}
private void updateTarget(final Point p) {
final ReconnectRequest request = (ReconnectRequest) getTargetRequest();
request.setLocation(p);
final EditPart target = (getCommandName().equals(RequestConstants.REQ_RECONNECT_SOURCE))
? getConnectionEditPart().getSource()
: getConnectionEditPart().getTarget();
request.setTargetEditPart(target);
getConnectionEditPart().showSourceFeedback(request);
getConnectionEditPart().showTargetFeedback(request);
}
@Override
public void keyUp(final KeyEvent keyEvent, final EditPartViewer viewer) {
if ((null != conCreationTool) && (keyEvent.keyCode == SWT.MOD1)) {
// Ctrl or Command key was released
deactivateConCreationTool();
updateTarget(getLocation());
}
super.keyUp(keyEvent, viewer);
}
@Override
public void mouseMove(final MouseEvent me, final EditPartViewer viewer) {
if (null != conCreationTool) {
conCreationTool.mouseMove(me, viewer);
} else {
super.mouseMove(me, viewer);
}
}
@Override
public void mouseUp(final MouseEvent me, final EditPartViewer viewer) {
if (null != conCreationTool) {
conCreationTool.mouseUp(me, viewer);
deactivateConCreationTool();
handleFinished();
} else {
super.mouseUp(me, viewer);
}
}
@Override
public void setEditDomain(final EditDomain domain) {
if (null != conCreationTool) {
conCreationTool.setEditDomain(domain);
}
super.setEditDomain(domain);
}
@Override
public void setViewer(final EditPartViewer viewer) {
if (null != conCreationTool) {
conCreationTool.setViewer(viewer);
}
super.setViewer(viewer);
}
@Override
public void viewerEntered(final MouseEvent mouseEvent, final EditPartViewer viewer) {
if (null != conCreationTool) {
conCreationTool.viewerEntered(mouseEvent, viewer);
}
super.viewerEntered(mouseEvent, viewer);
}
@Override
public void viewerExited(final MouseEvent mouseEvent, final EditPartViewer viewer) {
if (null != conCreationTool) {
conCreationTool.viewerExited(mouseEvent, viewer);
}
super.viewerExited(mouseEvent, viewer);
}
@Override
public void setProperties(final Map properties) {
if (null != conCreationTool) {
conCreationTool.setProperties(properties);
}
super.setProperties(properties);
}
@Override
public void commitDrag() {
if (null != conCreationTool) {
conCreationTool.commitDrag();
}
super.commitDrag();
}
@Override
public void deactivate() {
if (conCreationTool != null) {
deactivateConCreationTool();
}
super.deactivate();
}
private void deactivateConCreationTool() {
conCreationTool.deactivate();
conCreationTool = null;
}
}