Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5f1652611acfd14b91d06ce996aa4d809459879b (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
/*****************************************************************************
 * Copyright (c) 2010 Atos Origin.
 *
 *    
 * 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:
 *   Mathieu Velten (Atos Origin) mathieu.velten@atosorigin.com - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.service.types.helper.advice;

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

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.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.commands.ConfigureElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyDependentsRequest;
import org.eclipse.papyrus.uml.diagram.common.helper.DurationConstraintHelper;
import org.eclipse.papyrus.uml.diagram.common.helper.DurationObservationHelper;
import org.eclipse.papyrus.uml.diagram.common.helper.TimeConstraintHelper;
import org.eclipse.papyrus.uml.diagram.common.helper.TimeObservationHelper;
import org.eclipse.papyrus.uml.service.types.utils.SequenceRequestConstant;
import org.eclipse.uml2.uml.ExecutionSpecification;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.MessageEnd;
import org.eclipse.uml2.uml.OccurrenceSpecification;

/**
 * Helper advice for all {@link OccurrenceSpecification} elements.
 */
public class OccurrenceSpecificationHelperAdvice extends AbstractEditHelperAdvice {


	/**
	 * <pre>
	 * {@inheritDoc}
	 * 
	 * Complete the creation by adding the covered lifeline.
	 * </pre>
	 */
	@Override
	protected ICommand getBeforeConfigureCommand(final ConfigureRequest request) {

		return new ConfigureElementCommand(request) {

			@Override
			protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
				Object coveredParam = request.getParameters().get(SequenceRequestConstant.COVERED);

				Lifeline coveredLifeline = null;
				if (coveredParam instanceof Lifeline) {
					coveredLifeline = (Lifeline) coveredParam;
				}

				final OccurrenceSpecification occurrenceSpecification = (OccurrenceSpecification) request.getElementToConfigure();
				if( coveredLifeline!=null){
					occurrenceSpecification.setCovered(coveredLifeline);
				}

				return CommandResult.newOKCommandResult(occurrenceSpecification);
			}

		};
	}


	/**
	 * <pre>
	 * Add a command to destroy :
	 * - related time elements
	 * - linked general ordering
	 * </pre>
	 * 
	 * @see org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice#getBeforeDestroyDependentsCommand(org.eclipse.gmf.runtime.emf.type.core.requests.DestroyDependentsRequest)
	 * 
	 * @param request
	 *        the request
	 * @return the command to execute before the edit helper work is done
	 */
	@Override
	protected ICommand getBeforeDestroyDependentsCommand(DestroyDependentsRequest request) {
		List<EObject> dependentsToDestroy = new ArrayList<EObject>();

		OccurrenceSpecification os = (OccurrenceSpecification)request.getElementToDestroy();

		//look for all Execution that references this Occurrence specification
		InteractionFragment containerPackage= (InteractionFragment)os.getOwner();
		if( containerPackage!=null) {
			Iterator<EObject> contentIterator=containerPackage.eAllContents();
			while (contentIterator.hasNext()) {
				EObject currentEObject= contentIterator.next();
				if( currentEObject instanceof Message) {
					Message m=(Message)currentEObject;
					if( os.equals(m.getSendEvent())) {
						dependentsToDestroy.add(m);
						if(m.getReceiveEvent()!=null) {
							dependentsToDestroy.add(m.getReceiveEvent());
						}
					}
					if( os.equals(m.getReceiveEvent())) {
						dependentsToDestroy.add(m);
						if(m.getSendEvent()!=null) {
							dependentsToDestroy.add(m.getSendEvent());
						}
					}
				}
				if( currentEObject instanceof ExecutionSpecification) {
					ExecutionSpecification exec=(ExecutionSpecification)currentEObject;
					if( os.equals(exec.getStart())) {
						dependentsToDestroy.add(exec);
						if( exec.getFinish()!= null&&!(exec.getFinish() instanceof MessageEnd)) {
							dependentsToDestroy.add(exec.getFinish());
						}
					}
					if( os.equals(exec.getFinish())) {
						dependentsToDestroy.add(exec);
						if( exec.getStart()!= null&& !(exec.getStart() instanceof MessageEnd)) {
							dependentsToDestroy.add(exec.getStart());
						}
					}
				}
			}
		}
		// delete linked time elements
		dependentsToDestroy.addAll(TimeObservationHelper.getTimeObservations(os));
		dependentsToDestroy.addAll(TimeConstraintHelper.getTimeConstraintsOn(os));
		dependentsToDestroy.addAll(DurationObservationHelper.getDurationObservationsOn(os));
		dependentsToDestroy.addAll(DurationConstraintHelper.getDurationConstraintsOn(os));

		// delete linked general ordering
		/**
		 * Note: GeneralOrdering should be necessarily removed because the opposite
		 * references 'GeneralOrdering::before[1]' and 'GeneralOrdering::after[1]' which designate 
		 * this OccurrenceSpecification are mandatory 
		 */
		dependentsToDestroy.addAll(os.getToBefores());
		dependentsToDestroy.addAll(os.getToAfters());

		if(!dependentsToDestroy.isEmpty()) {
			return request.getDestroyDependentsCommand(dependentsToDestroy);
		} 

		return null;
	}
}

Back to the top