Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c9a2785295e6bd18ad3a62506024bb10ce49fe55 (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
/*******************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 * 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
 *******************************************************************************/
package org.eclipse.papyrus.uml.diagram.timing.custom.edit.commands;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.timing.custom.parts.CustomPaletteFactory;
import org.eclipse.papyrus.uml.diagram.timing.custom.utils.EcoreUtils;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.TimeConstraint;
import org.eclipse.uml2.uml.TimeExpression;
import org.eclipse.uml2.uml.TimeInterval;
import org.eclipse.uml2.uml.UMLFactory;

public class CustomTimeConstraintCreateCommand extends EditElementCommand {

	public CustomTimeConstraintCreateCommand(final CreateElementRequest req) {
		super(req.getLabel(), null, req);
	}

	@Override
	public boolean canExecute() {
		return true;
	}

	@Override
	protected CommandResult doExecuteWithResult(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {
		final TimeConstraint timeConstraint = UMLFactory.eINSTANCE.createTimeConstraint();
		final CreateElementRequest request = (CreateElementRequest) getRequest();
		final EditPart originalTarget = (EditPart) request.getParameter(CustomPaletteFactory.ORIGINAL_TARGET);
		final NamedElement targetElement = (NamedElement) ((View) originalTarget.getModel()).getElement();

		// TODO: firstEvent depending on the click location relative to the originalTarget EditPart figure

		final Package containingPackage = EcoreUtils.getContaining(request.getContainer(), Package.class);
		if (containingPackage == null) {
			return CommandResult.newErrorCommandResult("The container must be in a Package"); //$NON-NLS-1$
		}
		final Interaction containingInteraction = EcoreUtils.getContaining(request.getContainer(), Interaction.class);
		if (containingInteraction == null) {
			return CommandResult.newErrorCommandResult("The container must be in an Interaction"); //$NON-NLS-1$
		}

		containingInteraction.getOwnedRules().add(timeConstraint);
		timeConstraint.getConstrainedElements().clear();
		timeConstraint.getConstrainedElements().add(targetElement);

		final TimeInterval timeInterval = UMLFactory.eINSTANCE.createTimeInterval();
		timeConstraint.setSpecification(timeInterval);

		final TimeExpression minTime = UMLFactory.eINSTANCE.createTimeExpression();
		final TimeExpression maxTime = UMLFactory.eINSTANCE.createTimeExpression();
		containingPackage.getPackagedElements().add(minTime);
		containingPackage.getPackagedElements().add(maxTime);
		timeInterval.setMin(minTime);
		timeInterval.setMax(maxTime);

		final LiteralString minExpr = UMLFactory.eINSTANCE.createLiteralString();
		final LiteralString maxExpr = UMLFactory.eINSTANCE.createLiteralString();
		minTime.setExpr(minExpr);
		maxTime.setExpr(maxExpr);
		minExpr.setValue("t1"); //$NON-NLS-1$
		maxExpr.setValue("t2"); //$NON-NLS-1$

		((CreateElementRequest) getRequest()).setNewElement(timeConstraint);
		return CommandResult.newOKCommandResult(timeConstraint);

	}
}

Back to the top