Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1fd19cce2b19309d133b97b25e5c5218d8274c28 (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
/*****************************************************************************
 * Copyright (c) 2013 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
 *
 * Contributors:
 *  Patrick Tessier (CEA LIST) patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.commands;

import java.util.ArrayList;
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.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.helper.NamedElementHelper;
import org.eclipse.papyrus.uml.diagram.common.service.ApplyStereotypeRequest;
import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Stereotype;


public class ApplyStereotypeCommand extends AbstractTransactionalCommand {

	protected ApplyStereotypeRequest req;

	public ApplyStereotypeCommand(TransactionalEditingDomain domain, ApplyStereotypeRequest req) {
		super(domain, "Apply stereotype", null);
		this.req = req;
	}

	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		// retrieves the list of stereotypes to be applied
		List<String> stereotypeQNames = req.getStereotypesToApply();
		Element element = req.getElement();
		List<EObject> result = new ArrayList<EObject>();
		for(String stereotypeQName : stereotypeQNames) {
			// retrieve the stereotype to apply
			Stereotype stereotype = element.getApplicableStereotype(stereotypeQName);
			if(stereotype == null) {
				// stereotype has no been found. should ask for
				// profile application ?
				Activator.log.warn("impossible to retrieve the stereotype " + stereotypeQName);
			} else {
				result.add(element.applyStereotype(stereotype));
			}
		}

		if (req.renameWithFirstStereotype() && element instanceof NamedElement && !stereotypeQNames.isEmpty()) {
			String stereotypeName = NamedElementUtil.getNameFromQualifiedName(stereotypeQNames.get(0));
			// find a new name for the element
			String name = NamedElementHelper.EINSTANCE.getNewUMLElementName(element.getOwner(), stereotypeName);
			((NamedElement)element).setName(name);
		}


		if (result.size() > 1) {
			return CommandResult.newOKCommandResult(result);
		} else if (result.size() == 1) {
			return CommandResult.newOKCommandResult(result.get(0));
		}
		return CommandResult.newOKCommandResult();
	}
}

Back to the top