Skip to main content
summaryrefslogtreecommitdiffstats
blob: f1398a885fe28ca1615f4567439bccd5bca67f06 (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
/*****************************************************************************
 * 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:
 *  Remi Schnekenburger (CEA LIST) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.profile.externalresource.helper;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.util.UMLUtil.StereotypeApplicationHelper;


/**
 * Specific Stereotype Application Helper for Papyrus tool. If it detects the model is not a model handled by Papyrus, it will delegate to the standard Stereotype application helper.
 */
public class PapyrusStereotypeApplicationHelper extends StereotypeApplicationHelper {
	
	/** Key for the location strategy value  */
	public static final String LOCATION_STRATEGY_KEY = "locationStrategy";
	
	/** source for the eannotation that is used to store specific URI for stereotype application resource */
	public static final String PAPYRUS_EXTERNAL_RESOURCE_EANNOTATION_SOURCE = "http://www.eclipse.org/papyrus/uml/profile/externalresource"; //$NON-NLS-1$
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public EObject applyStereotype(Element element, EClass definition) {
		return super.applyStereotype(element, definition);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean addToContainmentList(Element element, EObject stereotypeApplication) {
		return super.addToContainmentList(element, stereotypeApplication);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected EList<EObject> getContainmentList(Element element, EClass definition) {
		// 1. find the current strategy used for the model element
		// 2. use the strategy to find the resource and the containment list
		// 3. if no strategy found, return usual strategy ==> stereotype application are directly contained by the resource containing the stereotyped element.
		IStereotypeApplicationLocationStrategy locationStrategy = getCurrentLocationStrategy(element);
		if(locationStrategy ==null) {
			return StandardApplicationLocationStrategy.getInstance().getContainmentList(element, definition);
		} 
		return locationStrategy.getContainmentList(element, definition);
	}

	/**
	 * Returns the location strategy to use for the given couple element/definition
	 * @param element the stereotyped element
	 * @param definition the definition of the stereotype to be applied
	 * @return the location strategy or <code>null</code> if none was found
	 */
	public static IStereotypeApplicationLocationStrategy getCurrentLocationStrategy(EObject element) {
		EObject container = EcoreUtil.getRootContainer(element, true);
		if(container instanceof Element) {
			EAnnotation annotation = ((Element)container).getEAnnotation(PAPYRUS_EXTERNAL_RESOURCE_EANNOTATION_SOURCE);
			if(annotation == null) {
				return null;
			}
			
			String location = annotation.getDetails().get(LOCATION_STRATEGY_KEY);
			if(location == null) {
				return null;
			}
			
			IStereotypeApplicationLocationStrategy strategy = StrategyRegistry.getInstance().getStrategy(location);
			if(strategy != null) {
				return strategy;
			}
		}
		return null;
	}
	
}

Back to the top