Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 469bd2a079b610e58bbeb20a08df750d970e4444 (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
/*****************************************************************************
 * Copyright (c) 2010 CEA
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Soyatec - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.edit.policies;

import java.lang.reflect.Field;
import java.util.Iterator;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.DropRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.command.AnnotatedLinkEditCommand;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.AnnotatedLinkEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.CombinedFragmentEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.InteractionEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.InteractionOperandEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.figures.EllipseDecoration;
import org.eclipse.swt.SWT;
import org.eclipse.uml2.uml.NamedElement;

/**
 * An editpolicy for handling connections end from Comment, Constraint and Observations.
 *
 * The semantic element of the Host should be a instance of {@link NamedElement}.
 *
 * @see AnnotatedLinkStartEditPolicy
 *
 * @author Jin Liu (jin.liu@soyatec.com)
 */
public class AnnotatedLinkEndEditPolicy extends GraphicalNodeEditPolicy {

	public static final String ANNOTATED_LINK_END_ROLE = "Annotated Link End Edit Policy";

	public static final String REQ_ANNOTATED_LINK_END = "annotated link end";

	public static final String REQ_ANNOTATED_LINK_REORIENT_END = "annotated link reorient end";

	@Override
	public EditPart getTargetEditPart(Request request) {
		if (REQ_ANNOTATED_LINK_END.equals(request.getType()) || REQ_ANNOTATED_LINK_REORIENT_END.equals(request.getType())) {
			Point location = ((DropRequest) request).getLocation();
			EditPart host = getHost();
			if (isEnterAnchorArea(host, location)) {
				return host;
			}
		}
		return null;
	}

	private boolean isEnterAnchorArea(EditPart editPart, Point location) {
		Point p = location.getCopy();
		if (editPart instanceof InteractionEditPart || editPart instanceof CombinedFragmentEditPart || editPart instanceof InteractionOperandEditPart) {
			IFigure figure = ((AbstractGraphicalEditPart) editPart).getFigure();
			figure.translateToRelative(p);
			// if mouse location is far from border, do not handle connection event
			Rectangle innerRetangle = figure.getBounds().getCopy().shrink(10, 10);
			if (innerRetangle.contains(p)) {
				return false;
			}
		}
		return true;
	}

	@Override
	public Command getCommand(Request request) {
		if (REQ_ANNOTATED_LINK_END.equals(request.getType())) {
			return getConnectionCompleteCommand((CreateConnectionRequest) request);
		} else if (REQ_ANNOTATED_LINK_REORIENT_END.equals(request.getType())) {
			return getReconnectTargetCommand((ReconnectRequest) request);
		}
		return null;
	}

	@Override
	protected Command getReconnectTargetCommand(ReconnectRequest request) {
		ICommandProxy c = (ICommandProxy) super.getReconnectTargetCommand(request);
		if (c == null) {
			return null;
		}
		if (request.getConnectionEditPart() instanceof AnnotatedLinkEditPart) {
			CompositeCommand cc = (CompositeCommand) c.getICommand();
			AnnotatedLinkEditCommand ac = new AnnotatedLinkEditCommand(getEditingDomain());
			ac.setAnnotatedLink((AnnotatedLinkEditPart) request.getConnectionEditPart());
			ac.setTarget(getHost());
			cc.add(ac);
			return c;
		}
		return UnexecutableCommand.INSTANCE;
	}

	private TransactionalEditingDomain getEditingDomain() {
		return ((IGraphicalEditPart) getHost()).getEditingDomain();
	}

	@Override
	protected Command getConnectionCompleteCommand(CreateConnectionRequest request) {
		ICommandProxy proxy = (ICommandProxy) super.getConnectionCompleteCommand(request);
		if (proxy == null) {
			return null;
		}
		CompositeCommand cc = (CompositeCommand) proxy.getICommand();
		@SuppressWarnings("rawtypes")
		Iterator it = cc.iterator();
		AnnotatedLinkEditCommand command = null;
		while (it.hasNext()) {
			Object next = it.next();
			if (next instanceof AnnotatedLinkEditCommand) {
				command = (AnnotatedLinkEditCommand) next;
				break;
			}
		}
		if (command == null) {
			return UnexecutableCommand.INSTANCE;
		}
		command.setTarget(getHost());
		return proxy;
	}

	@Override
	public void eraseTargetFeedback(Request request) {
		if (REQ_ANNOTATED_LINK_END.equals(request.getType())) {
			CreateConnectionRequest connReq = (CreateConnectionRequest) request;
			EditPart sourceEditPart = connReq.getSourceEditPart();
			EditPolicy editPolicy = sourceEditPart.getEditPolicy(AnnotatedLinkStartEditPolicy.ANNOTATED_LINK_START_ROLE);
			PolylineConnection connectionFeedback = (PolylineConnection) getConnectionFeedback(editPolicy);
			if (connectionFeedback != null) {
				connectionFeedback.setTargetDecoration(null);
			}
		}
	}

	@Override
	public void showSourceFeedback(Request request) {
		// do nothing, this is not the primary GraphicalNodeEditPolicy.
	}

	@Override
	public void eraseSourceFeedback(Request request) {
		// do nothing, this is not the primary GraphicalNodeEditPolicy.
	}

	@Override
	public void showTargetFeedback(Request request) {
		if (REQ_ANNOTATED_LINK_END.equals(request.getType())) {
			// Show circle feedback at target anchor
			CreateConnectionRequest connReq = (CreateConnectionRequest) request;
			EditPart sourceEditPart = connReq.getSourceEditPart();
			EditPolicy editPolicy = sourceEditPart.getEditPolicy(AnnotatedLinkStartEditPolicy.ANNOTATED_LINK_START_ROLE);
			editPolicy.showSourceFeedback(request);
			PolylineConnection connectionFeedback = (PolylineConnection) getConnectionFeedback(editPolicy);
			if (connectionFeedback != null) {
				Command command = getHost().getCommand(request);
				if (command != null && command.canExecute()) {
					EllipseDecoration dec = new EllipseDecoration();
					dec.setPreferredSize(10, 10);
					dec.setSize(10, 10);
					dec.setLineWidth(2);
					dec.setAntialias(SWT.ON);
					connectionFeedback.setTargetDecoration(dec);
				} else {
					connectionFeedback.setTargetDecoration(null);
				}
			}
		}
	}

	private Connection getConnectionFeedback(EditPolicy policy) {
		if (policy != null) {
			try {
				Field f = org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy.class.getDeclaredField("connectionFeedback");
				f.setAccessible(true);
				return (Connection) f.get(policy);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}

Back to the top