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

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.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.uml.diagram.sequence.edit.commands.DurationConstraintCreateCommand;
import org.eclipse.papyrus.uml.diagram.sequence.providers.ElementInitializers;
import org.eclipse.papyrus.uml.diagram.sequence.util.SequenceRequestConstant;
import org.eclipse.papyrus.uml.diagram.sequence.util.SequenceUtil;
import org.eclipse.uml2.uml.CombinedFragment;
import org.eclipse.uml2.uml.DurationConstraint;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.OccurrenceSpecification;
import org.eclipse.uml2.uml.UMLFactory;

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

	/**
	 * Constructor.
	 *
	 * @param req
	 * @param eObject
	 */
	public CustomDurationConstraintCreateCommand(CreateElementRequest req, EObject eObject, Diagram diagram) {
		super(req, eObject, diagram);
	}

	/**
	 * Constructor.
	 *
	 * @param req
	 */
	public CustomDurationConstraintCreateCommand(CreateElementRequest req, Diagram diagram) {
		super(req, diagram);
	}

	/**
	 * @Override enable only if there is occurrence specifications
	 */
	@Override
	public boolean canExecute() {
		/*
		 * // check first occurrence specification
		 * if(!getRequest().getParameters().containsKey(SequenceRequestConstant.NEAREST_OCCURRENCE_SPECIFICATION)) {
		 * return true; // duration creation is in progress; source is not defined yet
		 * }
		 * Object paramOccurrence1 = getRequest().getParameter(SequenceRequestConstant.NEAREST_OCCURRENCE_SPECIFICATION);
		 * List<OccurrenceSpecification> occ1List = SequenceUtil.getAsOccSpecList(paramOccurrence1);
		 * if(occ1List.isEmpty()) {
		 * return false;
		 * }
		 * // check second occurrence specification
		 * if(!getRequest().getParameters().containsKey(SequenceRequestConstant.NEAREST_OCCURRENCE_SPECIFICATION_2)) {
		 * return true; // duration creation is in progress; target is not defined yet
		 * }
		 * Object paramOccurrence2 = getRequest().getParameter(SequenceRequestConstant.NEAREST_OCCURRENCE_SPECIFICATION_2);
		 * List<OccurrenceSpecification> occ2List = SequenceUtil.getAsOccSpecList(paramOccurrence2);
		 * if(occ2List.isEmpty()) {
		 * return false;
		 * }
		 * // disable duration constraint on a same event
		 * if(!Collections.disjoint(occ1List, occ2List)) {
		 * return false;
		 * }
		 * // enable duration constraint only on a same lifeline or on message
		 * OccurrenceSpecification[] pair = SequenceUtil.getPairOfCorrespondingOccSpec(occ1List, occ2List);
		 * if(pair != null && pair.length > 1) {
		 * boolean enabled = DurationConstraintHelper.coversSameLifeline(pair[0], pair[1]);
		 * // handle creation on message
		 * enabled |= DurationConstraintHelper.endsOfSameMessage(pair[0], pair[1]);
		 * return enabled;
		 * }
		 * return false;
		 */
		return true;
	}

	/**
	 * @Override get the Lifeline parent as owner, assign the occurrence specifications
	 */
	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		DurationConstraint newElement = UMLFactory.eINSTANCE.createDurationConstraint();
		// get the Lifeline parent as owner
		Namespace owner = getNamespace(getElementToEdit());
		owner.getOwnedRules().add(newElement);
		ElementInitializers.getInstance().init_DurationConstraint_Shape(newElement);
		// assign the occurrence specification
		Object paramOccurrence1 = getRequest().getParameter(SequenceRequestConstant.NEAREST_OCCURRENCE_SPECIFICATION);
		List<OccurrenceSpecification> occ1List = SequenceUtil.getAsOccSpecList(paramOccurrence1);
		Object paramOccurrence2 = getRequest().getParameter(SequenceRequestConstant.NEAREST_OCCURRENCE_SPECIFICATION_2);
		List<OccurrenceSpecification> occ2List = SequenceUtil.getAsOccSpecList(paramOccurrence2);
		OccurrenceSpecification[] pair = SequenceUtil.getPairOfCorrespondingOccSpec(occ1List, occ2List);
		if (pair != null && pair.length > 1) {
			newElement.getConstrainedElements().add(pair[0]);
			newElement.getConstrainedElements().add(pair[1]);
		}
		doConfigure(newElement, monitor, info);
		((CreateElementRequest) getRequest()).setNewElement(newElement);
		return CommandResult.newOKCommandResult(newElement);
	}

	protected Namespace getNamespace(EObject element) {
		if (element instanceof Namespace) {
			return (Namespace) element;
		}
		if (element instanceof CombinedFragment) {
			return ((CombinedFragment) element).getNamespace();
		}
		return getNamespace(element.eContainer());
	}
}

Back to the top