Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cfccdbf25cf6e8c641aaa65a0add849d691a999b (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
/*****************************************************************************
 * 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.util;

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

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramRootEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.papyrus.infra.gmfdiag.common.selection.SelectSeveralLinksEditPartTracker;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.AbstractMessageEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.part.UMLDiagramEditorPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.PlatformUI;

/**
 * @author Patrick Tessier
 * @since 3.0
 *
 */
public class SelectMessagesEditPartTracker extends SelectSeveralLinksEditPartTracker {
	protected Listener KeyDownListener = new Listener() {

		@Override
		public void handleEvent(Event event) {
			// in case the SHIFT key is released, the creation mode goes back to normal
			if (event.keyCode == SWT.SHIFT) {
				allowReoder = true;
			}
		}

	};
	protected Listener KeyUPListener = new Listener() {

		@Override
		public void handleEvent(Event event) {
			// in case the SHIFT key is released, the creation mode goes back to normal
			if (event.keyCode == SWT.SHIFT) {
				allowReoder = false;
			}
		}

	};
	protected int MinDistancetop = Integer.MAX_VALUE;
	protected int MinDistancebottom = Integer.MAX_VALUE;
	protected Dimension delta = null;

	/**
	 * @see org.eclipse.gef.tools.AbstractTool#activate()
	 *
	 */
	@Override
	public void activate() {
		super.activate();
	}

	/**
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.selection.SelectSeveralLinksEditPartTracker#handleButtonDown(int)
	 *
	 * @param button
	 * @return
	 */
	@Override
	protected boolean handleButtonDown(int button) {
		Dimension delta = null;
		MinDistancetop = Integer.MAX_VALUE;
		MinDistancebottom = Integer.MAX_VALUE;
		// 1. look for all Nodes connected by connections
		// and find the MinDistancetop (maximum mouvment without reorder to the top) and the MinDistancebottom (maximum distance without reorder to the bottom)


		ArrayList<GraphicalEditPart> nodeEditPart = new ArrayList<>();

		List selectedEditparts = getOperationSet();

		for (int i = 0; i < selectedEditparts.size(); i++) {
			Object currentEditPart = selectedEditparts.get(i);
			if (currentEditPart instanceof org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) {
				org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart currentConnectionEdiPart = (org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) currentEditPart;
				nodeEditPart.add((GraphicalEditPart) currentConnectionEdiPart.getSource());

			}

		}
		// 2. take all messages between this nodes
		ArrayList<AbstractMessageEditPart> messageEditPartList = new ArrayList<>();
		for (GraphicalEditPart anodeEditPart : nodeEditPart) {
			for (Object connection : anodeEditPart.getSourceConnections()) {

				if (connection instanceof AbstractMessageEditPart) {
					if (!(selectedEditparts.contains(connection))) {
						messageEditPartList.add((AbstractMessageEditPart) connection);
					}
				}
			}
			for (Object connection : anodeEditPart.getTargetConnections()) {
				if (!(selectedEditparts.contains(connection))) {
					if (connection instanceof AbstractMessageEditPart) {
						messageEditPartList.add((AbstractMessageEditPart) connection);
					}
				}
			}
		}

		// 3 calculate minimum distance
		for (AbstractMessageEditPart abstractMessageEditPart : messageEditPartList) {
			Point currentConnectionPosition = abstractMessageEditPart.getConnectionFigure().getPoints().getFirstPoint().getCopy();
			for (Object selectedEditPart : selectedEditparts) {
				if (selectedEditPart instanceof AbstractMessageEditPart) {
					AbstractMessageEditPart currentSelectedMessage = (AbstractMessageEditPart) selectedEditPart;
					Point currentSelectedConnectionPosition = currentSelectedMessage.getConnectionFigure().getPoints().getFirstPoint().getCopy();

					// selected Message is below the currentConnection
					if (currentConnectionPosition.y < currentSelectedConnectionPosition.y) {
						if (MinDistancetop > (currentSelectedConnectionPosition.y - currentConnectionPosition.y)) {
							MinDistancetop = (currentSelectedConnectionPosition.y - currentConnectionPosition.y);
						}
					}
				}

			}
		}

		return super.handleButtonDown(button);
	}

	/**
	 * Constructor.
	 *
	 * @param owner
	 */
	public SelectMessagesEditPartTracker(ConnectionEditPart owner) {
		super(owner);
		PlatformUI.getWorkbench().getDisplay().addFilter(SWT.KeyDown, KeyDownListener);
		PlatformUI.getWorkbench().getDisplay().addFilter(SWT.KeyUp, KeyUPListener);
	}

	private boolean allowReoder = false;



	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.gef.Tool#deactivate()
	 */
	@Override
	public void deactivate() {
		PlatformUI.getWorkbench().getDisplay().removeFilter(SWT.KeyUp, KeyDownListener);
		PlatformUI.getWorkbench().getDisplay().removeFilter(SWT.KeyUp, KeyUPListener);
		super.deactivate();
	}

	/**
	 * @see org.eclipse.gef.tools.SimpleDragTracker#updateSourceRequest()
	 */
	@Override
	protected void updateSourceRequest() {
		if (!allowReoder) {
			Dimension computedDelta = getLocation().getDifference(getStartLocation());
			delta = null;
			if (computedDelta.height < 0) {
				UMLDiagramEditorPlugin.log.trace(LogOptions.SEQUENCE_DEBUG_UTIL, "Move " + computedDelta.height + " MinDistancetop" + MinDistancetop);//$NON-NLS-1$
				if (MinDistancetop + computedDelta.height < 0) {
					computedDelta.height = -MinDistancetop;
					delta = computedDelta.getCopy();
				}
			} else {
				UMLDiagramEditorPlugin.log.trace(LogOptions.SEQUENCE_DEBUG_UTIL, "Move " + computedDelta.height + " MinDistancebottom" + MinDistancebottom);//$NON-NLS-1$
				if (MinDistancebottom - computedDelta.height - 10 < 0) {
					computedDelta.height = MinDistancebottom - 10;
					delta = computedDelta.getCopy();
				}

			}

		}
		super.updateSourceRequest();
	}

	@Override
	protected Dimension getDragMoveDelta() {
		if (delta == null) {
			return getLocation().getDifference(getStartLocation());
		} else {
			return delta;
		}
	}

	/**
	 * Walks up the editpart hierarchy to find and return the
	 * <code>TopGraphicEditPart</code> instance.
	 */
	public DiagramEditPart getDiagramEditPart(EditPart editPart) {
		while (editPart instanceof IGraphicalEditPart) {
			if (editPart instanceof DiagramEditPart) {
				return (DiagramEditPart) editPart;
			}

			editPart = editPart.getParent();
		}
		if (editPart instanceof DiagramRootEditPart) {
			return (DiagramEditPart) ((DiagramRootEditPart) editPart).getChildren().get(0);
		}
		return null;
	}
}

Back to the top