Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a9f8992914c7875a8a35c1fa924f64bf57f93931 (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
/*****************************************************************************
 * Copyright (c) 2013 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.command;

import java.util.Collections;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
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.EditPartViewer;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateConnectionViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequestFactory;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.AnnotatedLinkEndEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.AnnotatedLinkStartEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.providers.UMLElementTypes;
import org.eclipse.papyrus.uml.diagram.sequence.util.SequenceUtil;

/**
 * @author Jin Liu (jin.liu@soyatec.com)
 */
public class RestoreDurationConstraintLinkCommand extends AbstractTransactionalCommand {

	private IAdaptable dcViewAdapter;

	private EditPartViewer viewer;

	private Boolean isOnTop;

	private Point targetLocation;

	private PreferencesHint diagramPreferenceHint;

	/**
	 * Constructor.
	 *
	 * @param domain
	 * @param label
	 * @param affectedFiles
	 */
	public RestoreDurationConstraintLinkCommand(TransactionalEditingDomain domain, IAdaptable dcViewAdapter, EditPartViewer viewer, Boolean isOnTop, Point targetLocation, PreferencesHint diagramPreferenceHint) {
		super(domain, "Resotore Annotated Link", null);
		this.dcViewAdapter = dcViewAdapter;
		this.viewer = viewer;
		this.isOnTop = isOnTop;
		this.targetLocation = targetLocation;
		this.diagramPreferenceHint = diagramPreferenceHint;
	}

	/**
	 * @see org.eclipse.core.commands.operations.AbstractOperation#canExecute()
	 *
	 * @return
	 */
	@Override
	public boolean canExecute() {
		if (dcViewAdapter == null || viewer == null || targetLocation == null) {
			return false;
		}
		return super.canExecute();
	}

	/**
	 * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
	 *
	 * @param monitor
	 * @param info
	 * @return
	 * @throws ExecutionException
	 */
	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		if (!canExecute()) {
			return CommandResult.newCancelledCommandResult();
		}
		View view = dcViewAdapter.getAdapter(View.class);
		if (view == null) {
			return CommandResult.newErrorCommandResult("No view found");
		}
		EditPart sourceEditPart = (EditPart) viewer.getEditPartRegistry().get(view);
		if (sourceEditPart == null) {
			return CommandResult.newCancelledCommandResult();
		}
		Rectangle bounds = SequenceUtil.getAbsoluteBounds((IGraphicalEditPart) sourceEditPart);
		Point sourceLocation = null;
		if (isOnTop != null) {
			sourceLocation = isOnTop.booleanValue() ? bounds.getTop() : bounds.getBottom();
		} else if (targetLocation.y >= bounds.getCenter().y) {
			sourceLocation = bounds.getBottom();
		} else {
			sourceLocation = bounds.getTop();
		}
		EditPart targetEditPart = null;
		CreateConnectionViewRequest request = CreateViewRequestFactory.getCreateConnectionRequest(UMLElementTypes.Comment_AnnotatedElementEdge, diagramPreferenceHint);
		request.setLocation(sourceLocation);
		request.setType(AnnotatedLinkStartEditPolicy.REQ_ANNOTATED_LINK_START);
		request.setSourceEditPart(sourceEditPart);
		request.setTargetEditPart(sourceEditPart);
		Command command = sourceEditPart.getCommand(request);
		// connect...
		request.setLocation(targetLocation);
		request.setType(AnnotatedLinkEndEditPolicy.REQ_ANNOTATED_LINK_END);
		targetEditPart = sourceEditPart.getViewer().findObjectAtExcluding(targetLocation, Collections.emptySet(), getTargetingConditional(request));
		request.setTargetEditPart(targetEditPart);
		command = targetEditPart.getCommand(request);
		if (command != null && command.canExecute()) {
			command.execute();
		}
		return CommandResult.newOKCommandResult();
	}

	protected EditPartViewer.Conditional getTargetingConditional(final Request req) {
		return new EditPartViewer.Conditional() {

			@Override
			public boolean evaluate(EditPart editpart) {
				return editpart.getTargetEditPart(req) != null;
			}
		};
	}
}

Back to the top