Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4a22e299f62c66b9af37f4dea16b82099c153394 (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
/*****************************************************************************
 * 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:
 *   CEA LIST - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.sequence.edit.policies;



import java.util.ArrayList;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editpolicies.GraphicalEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.gmf.runtime.draw2d.ui.figures.PolylineConnectionEx;
import org.eclipse.papyrus.uml.diagram.sequence.keyboardlistener.IKeyPressState;
import org.eclipse.papyrus.uml.diagram.sequence.keyboardlistener.KeyboardListener;
import org.eclipse.papyrus.uml.diagram.sequence.util.SequenceUtil;
import org.eclipse.swt.SWT;
import org.eclipse.ui.PlatformUI;

/**
 * the goal of this editpat is to propose a set of method and action by SHIFT key button to move messages and execution spec under the current editpart
 * 
 * @since 4.0
 *
 */
public abstract class UpdateWeakReferenceEditPolicy extends GraphicalEditPolicy implements IKeyPressState {

	protected KeyboardListener SHIFTDown = new KeyboardListener(this, SWT.SHIFT, true);
	protected KeyboardListener SHIFTUp = new KeyboardListener(this, SWT.SHIFT, false);
	protected boolean mustMove = true;

	/**
	 * Constructor.
	 *
	 */
	public UpdateWeakReferenceEditPolicy() {
		super();
	}

	@Override
	public void activate() {
		super.activate();
		// activate listeners
		PlatformUI.getWorkbench().getDisplay().addFilter(SWT.KeyDown, SHIFTDown);
		PlatformUI.getWorkbench().getDisplay().addFilter(SWT.KeyUp, SHIFTUp);
	}

	@Override
	public void deactivate() {
		PlatformUI.getWorkbench().getDisplay().removeFilter(SWT.KeyDown, SHIFTDown);
		PlatformUI.getWorkbench().getDisplay().removeFilter(SWT.KeyUp, SHIFTUp);
		super.deactivate();
	}


	/**
	 * Add a command of reconnection of the given connection editpart at the location.
	 * 
	 * @param hostEditpart
	 *            the current editpart that is the origin of this impact
	 * @param connectionEditPart
	 *            the given editpart to move
	 * @param location
	 *            the next location of the anchor
	 * @param senderList
	 *            the list of editpart that are origin of this request
	 * @param reconnectType
	 *            the type of the reconnection see {@link RequestConstants}
	 * @return return a request of reconnection
	 */
	protected static ReconnectRequest createReconnectRequest(EditPart hostEditpart, ConnectionEditPart connectionEditPart, Rectangle location, ArrayList<EditPart> senderList, String reconnectType) {
		ReconnectRequest reconnectRequest = new ReconnectRequest();
		reconnectRequest.setConnectionEditPart(connectionEditPart);
		SenderRequestUtils.addRequestSenders(reconnectRequest, senderList);
		SenderRequestUtils.addRequestSender(reconnectRequest, hostEditpart);
		reconnectRequest.setLocation(location.getLocation().getCopy());
		reconnectRequest.setType(reconnectType);
		if (RequestConstants.REQ_RECONNECT_TARGET.equals(reconnectType)) {
			reconnectRequest.setTargetEditPart(connectionEditPart.getTarget());
		} else {
			reconnectRequest.setTargetEditPart(connectionEditPart.getSource());
		}
		return reconnectRequest;
	}

	@Override
	public void setKeyPressState(Boolean isPressed) {
		mustMove = !isPressed;

	}

	/**
	 * Move the editPart to move at this location withe the move delta
	 * 
	 * @param moveDelta
	 *            the move delta (very important)
	 * @param compoundCommand
	 *            the command that will contain the result of the request
	 * @param editPartToMove
	 *            the editpart to move
	 */
	protected static void moveRoundedEditPart(EditPart hostEditPart, Point moveDelta, CompoundCommand compoundCommand, EditPart editPartToMove, ArrayList<EditPart> senderList) {
		ChangeBoundsRequest changeBoundsRequest = new ChangeBoundsRequest(RequestConstants.REQ_MOVE);
		SenderRequestUtils.addRequestSenders(changeBoundsRequest, senderList);
		SenderRequestUtils.addRequestSender(changeBoundsRequest, hostEditPart);
		GraphicalEditPart gEditPart = (GraphicalEditPart) editPartToMove;
		changeBoundsRequest.setLocation(new Point(gEditPart.getFigure().getBounds().getTopLeft().x, gEditPart.getFigure().getBounds().getTopLeft().x + moveDelta.y()));
		changeBoundsRequest.setEditParts(editPartToMove);
		changeBoundsRequest.setMoveDelta(moveDelta);
		changeBoundsRequest.setSizeDelta(new Dimension(0, 0));
		compoundCommand.add(editPartToMove.getCommand(changeBoundsRequest));
	}

	/**
	 * move the target anchor of the connection editpartPart
	 * 
	 * @param hostEditPart
	 *            the editpart that impacts the connection editpart
	 * @param moveDelta
	 *            the delta to move the anchors
	 * @param compoundCommand
	 *            the compound command that can have the result of the request
	 * @param connectionEditPart
	 *            the editpart to move
	 * @param senderList
	 *            the list of editpart that are origin of this move
	 */
	protected static void moveTargetConnectionEditPart(EditPart hostEditPart, Point moveDelta, CompoundCommand compoundCommand, ConnectionEditPart connectionEditPart, ArrayList<EditPart> senderList) {
		PolylineConnectionEx polyline = (PolylineConnectionEx) connectionEditPart.getFigure();
		Rectangle newAnchorPositionOnScreen;
		Point positiononScreen = polyline.getTargetAnchor().getReferencePoint();
		newAnchorPositionOnScreen = new Rectangle(positiononScreen.x, positiononScreen.y + moveDelta.y, 0, 0);
		ReconnectRequest reconnectTargetRequest = createReconnectRequest(hostEditPart, connectionEditPart, newAnchorPositionOnScreen, senderList, RequestConstants.REQ_RECONNECT_TARGET);
		reconnectTargetRequest.getExtendedData().put(SequenceUtil.DO_NOT_CHECK_HORIZONTALITY, true);
		compoundCommand.add(connectionEditPart.getTarget().getCommand(reconnectTargetRequest));
	}

	protected static void moveSourceConnectionEditPart(EditPart hostEditPart, Point moveDelta, CompoundCommand compoundCommand, ConnectionEditPart connectionEditPart, ArrayList<EditPart> senderList) {
		PolylineConnectionEx polyline = (PolylineConnectionEx) connectionEditPart.getFigure();
		Point anchorPositionOnScreen = polyline.getSourceAnchor().getReferencePoint();
		Rectangle newAnchorPositionOnScreen = new Rectangle(anchorPositionOnScreen.x, anchorPositionOnScreen.y + moveDelta.y, 0, 0);
		ReconnectRequest reconnectSourceRequest = createReconnectRequest(hostEditPart, connectionEditPart, newAnchorPositionOnScreen, senderList, RequestConstants.REQ_RECONNECT_SOURCE);
		reconnectSourceRequest.getExtendedData().put(SequenceUtil.DO_NOT_CHECK_HORIZONTALITY, true);
		compoundCommand.add(connectionEditPart.getSource().getCommand(reconnectSourceRequest));
	}

}

Back to the top