Skip to main content
summaryrefslogtreecommitdiffstats
blob: c076b1c052432bfffd80e9466ed4262ef48605a8 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*****************************************************************************
 * Copyright (c) 2017 CEA LIST 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:
 *   Patrick Tessier (CEA LIST) - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.gmfdiag.common.selection;

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

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.SnapToHelper;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.requests.BendpointRequest;
import org.eclipse.gef.requests.LocationRequest;
import org.eclipse.gef.tools.SelectEditPartTracker;
import org.eclipse.gmf.runtime.draw2d.ui.geometry.PointListUtilities;
import org.eclipse.gmf.runtime.gef.ui.internal.l10n.Cursors;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.PlatformUI;

/**
 * @since 3.0
 * this class is used to move several links at the same time.
 */
public class SelectSeveralLinksEditPartTracker extends SelectEditPartTracker {



	/**
	 * Key modifier for ignoring snap while dragging.  It's CTRL on Mac, and ALT on all
	 * other platforms.
	 */
	private final int MODIFIER_NO_SNAPPING;
	private Request sourceRequest;
	private int index = -1;
	private String type;
	private boolean bSourceFeedback = false;

	private PrecisionRectangle sourceRectangle;	
	private Point originalLocation = null;
	protected HashMap<EditPart, Point> locationsForEditParts= new HashMap<EditPart, Point>();

	int[] relativePosition=null;


	/**
	 * Method SelectConnectionEditPartTracker.
	 * @param owner ConnectionNodeEditPart that creates and owns the tracker object
	 */
	public SelectSeveralLinksEditPartTracker(ConnectionEditPart owner) {
		super(owner);

		if (SWT.getPlatform().equals("carbon"))//$NON-NLS-1$
			MODIFIER_NO_SNAPPING = SWT.CTRL;
		else
			MODIFIER_NO_SNAPPING = SWT.ALT;
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.gef.tools.AbstractTool#handleButtonDown(int)
	 */
	protected boolean handleButtonDown(int button) {
		if (!super.handleButtonDown(button))
			return false;

		Point p = getLocation();
		getConnection().translateToRelative(p);

		PointList points = getConnection().getPoints();
		Dimension size = new Dimension(7, 7);
		getConnection().translateToRelative(size);
		for (int i=1; i<points.size()-1; i++) {
			Point ptCenter = points.getPoint(i);
			Rectangle rect = new Rectangle( ptCenter.x - size.width / 2, ptCenter.y - size.height / 2, size.width, size.height);

			if (rect.contains(p)) {
				setType(RequestConstants.REQ_MOVE_BENDPOINT);
				setIndex(i);
			}
		}

		if (getIndex() == -1) {
			setIndex(PointListUtilities.findNearestLineSegIndexOfPoint(getConnection().getPoints(), new Point(p.x, p.y)));

			setIndex(getIndex() - 1);
			setType(RequestConstants.REQ_CREATE_BENDPOINT);
		}

		//compute the relative position be fore the beginning of the move
		List editparts = getOperationSet();
		relativePosition=new int[editparts.size()];
		if(editparts.size()>1){
			for (int i=0; i<editparts.size()-1;i++){
				Object currentEditPart = editparts.get(i);
				Object nextEditPart = editparts.get(i+1);
				if(currentEditPart instanceof org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart && nextEditPart instanceof org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart){
					Connection currentConnection=((org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart)currentEditPart).getConnectionFigure();
					Connection nextConnection=((org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart)nextEditPart).getConnectionFigure();
					Point currentConnectionPosition = currentConnection.getPoints().getFirstPoint().getCopy();
					Point nextConnectionPosition = nextConnection.getPoints().getFirstPoint().getCopy();
					relativePosition[i]=nextConnectionPosition.y-currentConnectionPosition.y;
				}
			}
		}

		return true;
	}

	/**
	 * Determines if the the connection should be dragged or not.
	 * 
	 * @return <code>boolean</code> <code>true</code> if dragging can occur, 
	 * <code>false</code> otherwise.
	 */
	protected boolean shouldAllowDrag() {
		return (getIndex() != -1);
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.gef.tools.AbstractTool#handleButtonUp(int)
	 */
	protected boolean handleButtonUp(int button) {

		boolean bExecuteDrag = isInState(STATE_DRAG_IN_PROGRESS) && shouldAllowDrag();

		boolean bRet = super.handleButtonUp(button);

		if (bExecuteDrag) {
			eraseSourceFeedback();
			setCurrentCommand(getCommand());
			executeCurrentCommand();
		}

		return bRet;
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.gef.tools.AbstractTool#handleDragInProgress()
	 */
	protected boolean handleDragInProgress() {
		if (isInState(STATE_DRAG_IN_PROGRESS) && shouldAllowDrag()) {
			updateSourceRequest();
			showSourceFeedback();
			setCurrentCommand(getCommand());
		}
		return true;
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.gef.tools.AbstractTool#handleDragStarted()
	 */
	protected boolean handleDragStarted() {
		originalLocation = null;
		sourceRectangle = null;		
		return stateTransition(STATE_DRAG, STATE_DRAG_IN_PROGRESS);
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.gef.tools.AbstractTool#calculateCursor()
	 */
	protected Cursor calculateCursor() {
		if (getType() == RequestConstants.REQ_MOVE_BENDPOINT) {
			return Cursors.CURSOR_SEG_MOVE;
		}

		return getConnection().getCursor();
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.gef.Tool#deactivate()
	 */
	public void deactivate() {
		if (!isInState(STATE_TERMINAL))
			eraseSourceFeedback();
		sourceRequest = null;
		super.deactivate();
	}

	/**
	 * @return boolean true if feedback is being displayed, false otherwise.
	 */
	private boolean isShowingFeedback() {
		return bSourceFeedback;
	}

	/**
	 * Method setShowingFeedback.
	 * @param bSet boolean to set the feedback flag on or off.
	 */
	private void setShowingFeedback(boolean bSet) {
		bSourceFeedback = bSet;
	}

	/**
	 * @see org.eclipse.gef.tools.AbstractTool#createOperationSet()
	 */
	protected List createOperationSet() {
		List list = new ArrayList();
		list.add(getConnectionEditPart());
		return list;
	}

	/**
	 * Method showSourceFeedback.
	 * Show the source drag feedback for the drag occurring
	 * within the viewer.
	 */
	private void showSourceFeedback() {
		List editParts = getOperationSet();
		for (int i = 0; i < editParts.size(); i++) {
			EditPart editPart = (EditPart) editParts.get(i);
			LocationRequest locationRequest= (LocationRequest)getSourceRequest();
			if( locationsForEditParts.get(editPart)!=null){
				Point location= locationsForEditParts.get(editPart);
				locationRequest.setLocation(location);
				editPart.showSourceFeedback(locationRequest);
			}
			//editPart.showSourceFeedback(getSourceRequest());
		}
		setShowingFeedback(true);
	}

	/**
	 * Method eraseSourceFeedback.
	 * Show the source drag feedback for the drag occurring
	 * within the viewer.
	 */
	private void eraseSourceFeedback() {	
		if (!isShowingFeedback())
			return;
		setShowingFeedback(false);
		List editParts = getOperationSet();

		for (int i = 0; i < editParts.size(); i++) {
			EditPart editPart = (EditPart) editParts.get(i);
			LocationRequest locationRequest= (LocationRequest)getSourceRequest();
			if( locationsForEditParts.get(editPart)!=null){
				Point location= locationsForEditParts.get(editPart);
				locationRequest.setLocation(location);
				editPart.eraseSourceFeedback(locationRequest);
			}

		}
	}

	/**
	 * Method getSourceRequest.
	 * @return Request
	 */
	private Request getSourceRequest() {
		if (sourceRequest == null)
			sourceRequest = createSourceRequest();
		return sourceRequest;
	}

	/**
	 * Determines the type of request that will be created for the drag
	 * operation.
	 * @return Object
	 */
	protected Object getType() {
		return type;
	}

	/**
	 * Sets the type of request that will be created for the drag operation.
	 * 
	 * @param type the <code>String</code> that represents the type of request.
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * Creates the source request that is activated when the drag operation
	 * occurs.
	 * 
	 * @return a <code>Request</code> that is the newly created source request
	 */
	protected Request createSourceRequest() {
		BendpointRequest request = new BendpointRequest();
		request.setType(getType());
		request.setIndex(getIndex());
		request.setSource((ConnectionEditPart)getSourceEditPart());
		return request;
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.gef.tools.AbstractTool#getCommand()
	 */
	protected Command getCommand() {
		List editparts = getOperationSet();
		EditPart part;

		CompoundCommand command = new CompoundCommand();
		command.setDebugLabel("change bounds");//$NON-NLS-1$
		for (int i = 0; i < editparts.size(); i++) {
			part = (EditPart) editparts.get(i);
			LocationRequest locationRequest= (LocationRequest)getSourceRequest();
			if( locationsForEditParts.get(part)!=null){
				Point location= locationsForEditParts.get(part);
				locationRequest.setLocation(location);
				command.add(part.getCommand(locationRequest));
			}
		}
		return command.unwrap();
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.gef.tools.AbstractTool#getCommandName()
	 */
	protected String getCommandName() {
		return getType().toString();
	}

	/**
	 * @return the <code>Connection</code> that is referenced by the connection edit part.
	 */
	private Connection getConnection() {
		return (Connection) getConnectionEditPart().getFigure();
	}

	/**
	 * Method getConnectionEditPart.
	 * @return ConnectionEditPart
	 */
	private ConnectionEditPart getConnectionEditPart() {
		return (ConnectionEditPart)getSourceEditPart();
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.gef.tools.AbstractTool#getDebugName()
	 */
	protected String getDebugName() {
		return "Bendpoint Handle Tracker " + getCommandName(); //$NON-NLS-1$
	}

	/**
	 * Gets the current line segment index that the user clicked on to 
	 * activate the drag tracker.
	 * 
	 * @return int
	 */
	protected int getIndex() {
		return index;
	}

	/**
	 * Method setIndex.
	 * Sets the current line segment index based on the location the user 
	 * clicked on the connection.
	 * @param i int representing the line segment index in the connection.
	 */
	public void setIndex(int i) {
		index = i;
	}

	/**
	 * @see org.eclipse.gef.tools.SimpleDragTracker#updateSourceRequest()
	 */
	protected void updateSourceRequest() {
		List editparts = getOperationSet();
		Dimension delta = getDragMoveDelta();
		Point mouseLocation = getStartLocation().getCopy();
		mouseLocation.translate(delta.width, delta.height);
		for (int index=0; index<editparts.size();index++){
			Object currentEditPart = editparts.get(index);
			if(currentEditPart instanceof org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart ){
				if( index>0){
					mouseLocation.translate(new Dimension (0,relativePosition[index-1]));
				}
				locationsForEditParts.put((EditPart)currentEditPart, mouseLocation.getCopy());

			}
		}
	}
	/**
	 * @see org.eclipse.gef.tools.AbstractTool#getOperationSet()
	 *
	 * @return
	 */
	@Override
	protected List getOperationSet() {
		if(getCurrentViewer().getSelectedEditParts().contains(getSourceEditPart())){
			return getCurrentViewer().getSelectedEditParts();}
		ArrayList result= new ArrayList<>();
		result.add(getSourceEditPart());
		return result;
	}
}

Back to the top