Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 977fda403a8c76a70c77c0aed2a077e7af3d8915 (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
/*****************************************************************************
 * Copyright (c) 2013 CEA
 *
 *    
 * 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:
 *   Soyatec - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.edit.policies;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.AbstractExecutionSpecificationEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.AbstractMessageEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.util.OccurrenceSpecificationHelper;
import org.eclipse.uml2.uml.ExecutionSpecification;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.MessageEnd;
import org.eclipse.uml2.uml.OccurrenceSpecification;
import org.eclipse.uml2.uml.UMLFactory;


/**
 * This class fixed the bug when reconnecting message from the end of an execution to the execution, at this moment, the model of the source and
 * target are not changed, but the UML2 model must be changed from message end to execution occurrence specification.
 * 
 * @author Jin Liu (jin.liu@soyatec.com)
 */
public class ExecutionGraphicalNodeEditPolicy extends ElementCreationWithMessageEditPolicy {

	@Override
	protected Command getReconnectSourceCommand(ReconnectRequest request) {
		Command command = super.getReconnectSourceCommand(request);
		if(command != null && command.canExecute() && !relationshipSourceHasChanged(request)) {
			command = appendUpdateExecutionEndCommand(request, command, false);
		}
		return command;
	}

	@Override
	protected Command getReconnectTargetCommand(ReconnectRequest request) {
		Command command = super.getReconnectTargetCommand(request);
		if(command != null && command.canExecute() && !relationshipTargetHasChanged(request)) {
			command = appendUpdateExecutionEndCommand(request, command, true);
		}
		return command;
	}

	/**
	 * @param request
	 * @param command
	 * @return
	 */
	private Command appendUpdateExecutionEndCommand(ReconnectRequest request, Command command, boolean messageTarget) {
		EditPart target = request.getTarget();
		ExecutionSpecification execution = null;
		if(target instanceof AbstractExecutionSpecificationEditPart) {
			EObject elt = ((AbstractExecutionSpecificationEditPart)target).resolveSemanticElement();
			if(elt instanceof ExecutionSpecification) {
				execution = (ExecutionSpecification)elt;
			}
		}
		MessageEnd messageEnd = null;
		ConnectionEditPart conn = request.getConnectionEditPart();
		if(conn instanceof AbstractMessageEditPart) {
			EObject elt = ((AbstractMessageEditPart)conn).resolveSemanticElement();
			if(elt instanceof Message) {
				messageEnd = messageTarget ? ((Message)elt).getReceiveEvent() : ((Message)elt).getSendEvent();
			}
		}
		if(execution != null && messageEnd != null) {
			OccurrenceSpecification start = execution.getStart();
			OccurrenceSpecification finish = execution.getFinish();
			if(start != null && start == messageEnd) {
				command = command.chain(new ICommandProxy(new ResetExecutionEndCommand(((IGraphicalEditPart)target).getEditingDomain(), execution, true)));
			} else if(finish != null && finish == messageEnd) {
				command = command.chain(new ICommandProxy(new ResetExecutionEndCommand(((IGraphicalEditPart)target).getEditingDomain(), execution, false)));
			}
		}
		return command;
	}

	private boolean relationshipSourceHasChanged(ReconnectRequest request) {
		return !request.getConnectionEditPart().getSource().equals(request.getTarget());
	}

	private boolean relationshipTargetHasChanged(ReconnectRequest request) {
		return !request.getConnectionEditPart().getTarget().equals(request.getTarget());
	}

	private static class ResetExecutionEndCommand extends AbstractTransactionalCommand {

		private ExecutionSpecification execution;

		private boolean resetStart;

		/**
		 * Constructor.
		 * 
		 * @param domain
		 * @param label
		 * @param affectedFiles
		 */
		public ResetExecutionEndCommand(TransactionalEditingDomain domain, ExecutionSpecification execution, boolean resetStart) {
			super(domain, "", null);
			this.execution = execution;
			this.resetStart = resetStart;
		}

		/**
		 * @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(execution != null) {
				if(resetStart) {
					OccurrenceSpecificationHelper.resetExecutionStart(execution, UMLFactory.eINSTANCE.createExecutionOccurrenceSpecification());
				} else {
					OccurrenceSpecificationHelper.resetExecutionFinish(execution, UMLFactory.eINSTANCE.createExecutionOccurrenceSpecification());
				}
			}
			return CommandResult.newOKCommandResult();
		}

	}
}

Back to the top