Skip to main content
summaryrefslogtreecommitdiffstats
blob: 946701bce32358075f58d370bf81e2f6a92a6254 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*****************************************************************************
 * 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
 *
 * Contributors:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.nattable.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.infra.nattable.utils.Constants;
import org.eclipse.papyrus.uml.nattable.paste.StereotypeApplicationStructure;
import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;

/**
 * 
 * This class provides useful methods to manage stereotypes in the table
 * 
 */
public class UMLTableUtils {

	public static final String PROPERTY_OF_STEREOTYPE_PREFIX = "property_of_stereotype:/"; //$NON-NLS-1$

	private UMLTableUtils() {
		//to prevent instanciation
	}

	/**
	 * 
	 * @param eobject
	 *        an element of the model (currently, if it is not an UML::Element, we can't find the property)
	 * @param id
	 *        the id used to identify the property of the stereotype
	 * 
	 * @return
	 *         the UML::Property or <code>null</code> if we can't resolve it (the required profile is not applied)
	 */
	public static Property getRealStereotypeProperty(final EObject eobject, final String id) {
		assert id.startsWith(PROPERTY_OF_STEREOTYPE_PREFIX);
		if(eobject instanceof Element) {
			final Element element = (Element)eobject;
			final String propertyQN = id.replace(UMLTableUtils.PROPERTY_OF_STEREOTYPE_PREFIX, ""); //$NON-NLS-1$
			final String propertyName = NamedElementUtil.getNameFromQualifiedName(propertyQN);
			final String stereotypeQN = NamedElementUtil.getParentQualifiedName(propertyQN);
			final String stereotypeName = NamedElementUtil.getNameFromQualifiedName(stereotypeQN);
			final String profileQN = NamedElementUtil.getParentQualifiedName(stereotypeQN);
			final Profile profile = element.getNearestPackage().getAppliedProfile(profileQN, true);
			if(profile != null) {
				final Stereotype ste = profile.getOwnedStereotype(stereotypeName);
				return (Property)ste.getMember(propertyName);
			}

		}
		return null;
	}

	/**
	 * 
	 * @param eobject
	 *        an element of the model (currently, if it is not an UML::Element, we can't find the property)
	 * @param id
	 *        the id used to identify the property of the stereotype
	 * @param sharedMap
	 *        a map owning interesting information, like {@link StereotypeApplicationStructure} which can be used to find stereotype, stereotype
	 *        application and so on
	 * @return
	 *         the UML::Property or <code>null</code> if we can't resolve it (the required profile is not applied)
	 */
	public static Property getRealStereotypeProperty(final EObject eobject, final String id, final Map<?, ?> sharedMap) {
		assert id.startsWith(PROPERTY_OF_STEREOTYPE_PREFIX);
		if(eobject instanceof Element) {
			final Element element = (Element)eobject;
			final String propertyQN = id.replace(UMLTableUtils.PROPERTY_OF_STEREOTYPE_PREFIX, ""); //$NON-NLS-1$
			final String propertyName = NamedElementUtil.getNameFromQualifiedName(propertyQN);
			final String stereotypeQN = NamedElementUtil.getParentQualifiedName(propertyQN);
			final String stereotypeName = NamedElementUtil.getNameFromQualifiedName(stereotypeQN);
			final String profileQN = NamedElementUtil.getParentQualifiedName(stereotypeQN);
			final Package nearestPackage;
			if(sharedMap != null) {
				final Element context = (Element)sharedMap.get(Constants.PASTED_ELEMENT_CONTAINER_KEY);
				nearestPackage = context.getNearestPackage();
			} else {
				nearestPackage = element.getNearestPackage();
			}
			if(nearestPackage != null) {
				final Profile profile = nearestPackage.getAppliedProfile(profileQN, true);
				if(profile != null) {
					final Stereotype ste = profile.getOwnedStereotype(stereotypeName);
					return (Property)ste.getMember(propertyName);
				}
			}

		}
		return null;
	}

	/**
	 * 
	 * @param element
	 *        the UML::Element on which we are looking for applied Stereotype with the property identified by its id
	 * @param id
	 *        the id used to identify the property of the stereotype
	 * 
	 * @return
	 *         the list of UML::Stereotype which have the property identified by this id and which are applied on the Element or <code>null</code> if
	 *         we can't resolve it (the required profile is not applied)
	 */
	public static final List<Stereotype> getAppliedStereotypesWithThisProperty(final Element element, final String id) {
		assert id.startsWith(PROPERTY_OF_STEREOTYPE_PREFIX);
		final List<Stereotype> stereotypes = new ArrayList<Stereotype>();
		if(element.eResource() != null) {
			final Object prop = getRealStereotypeProperty(element, id);
			if(prop instanceof Property) {
				for(final Stereotype current : element.getAppliedStereotypes()) {
					if(current.getAllAttributes().contains(prop)) {
						stereotypes.add(current);
					}
				}
			}
		}
		return stereotypes;
	}

	/**
	 * 
	 * @param element
	 *        the UML::Element on which we are looking for applied Stereotype with the property identified by its id
	 * @param id
	 *        the id used to identify the property of the stereotype
	 * 
	 * @return
	 *         the list of UML::Stereotype which have the property identified by this id and which are applied on the Element or <code>null</code> if
	 *         we can't resolve it (the required profile is not applied)
	 */
	public static final List<Stereotype> getApplicableStereotypesWithThisProperty(final Element element, final String id) {
		assert id.startsWith(PROPERTY_OF_STEREOTYPE_PREFIX);
		final List<Stereotype> stereotypes = new ArrayList<Stereotype>();
		if(element.eResource() != null) {
			final Object prop = getRealStereotypeProperty(element, id);
			if(prop instanceof Property) {
				for(final Stereotype current : element.getApplicableStereotypes()) {
					if(current.getAllAttributes().contains(prop)) {
						stereotypes.add(current);
					}
				}
			}
		}
		return stereotypes;
	}

	/**
	 * 
	 * @param element
	 *        the UML::Element on which we are looking for applied Stereotype with the property identified by its id
	 * @param id
	 *        the id used to identify the property of the stereotype
	 * @param sharedMap
	 *        a map owning interesting information, like {@link StereotypeApplicationStructure} which can be used to find stereotype, stereotype
	 *        application and so on
	 * @return
	 *         the list of UML::Stereotype which have the property identified by this id and which are applied on the Element or <code>null</code> if
	 *         we can't resolve it (the required profile is not applied)
	 */
	public static final List<Stereotype> getAppliedStereotypesWithThisProperty(final Element element, final String id, Map<?, ?> sharedMap) {
		assert id.startsWith(PROPERTY_OF_STEREOTYPE_PREFIX);
		final List<Stereotype> stereotypes = new ArrayList<Stereotype>();
		if(sharedMap != null) {
			final List<StereotypeApplicationStructure> struct = findStereotypeApplicationDataStructure(element, id, sharedMap);
			for(final StereotypeApplicationStructure current : struct) {
				stereotypes.add(current.getStereotype());
			}
		}
		if(element.eResource() != null) {
			final Object prop = getRealStereotypeProperty(element, id, sharedMap);
			if(prop instanceof Property) {
				for(final Stereotype current : element.getAppliedStereotypes()) {
					if(current.getAllAttributes().contains(prop)) {
						stereotypes.add(current);
					}
				}
			}
		}
		return stereotypes;
	}
	
	/**
	 * 
	 * @param editedElement
	 *        the edited element
	 * @param id
	 *        the id of the edited stereotype feature
	 * @param sharedMap
	 *        a map owning interesting information, like {@link StereotypeApplicationStructure} which can be used to find stereotype, stereotype
	 *        application and so on
	 * @return
	 *         the list of the found data structure
	 */
	public static final List<StereotypeApplicationStructure> findStereotypeApplicationDataStructure(final Element editedElement, final String id, final Map<?, ?> sharedMap) {
		//TODO : enhance the data structure to look for an editedElement + a propQn as Key!
		@SuppressWarnings("unchecked")
		List<StereotypeApplicationStructure> list = (List<StereotypeApplicationStructure>)sharedMap.get(editedElement);
		if(list != null && !list.isEmpty()) {
			final String propertyQN = id.replace(UMLTableUtils.PROPERTY_OF_STEREOTYPE_PREFIX, ""); //$NON-NLS-1$
			for(final StereotypeApplicationStructure current : list) {
				if(current.getProperty().getQualifiedName().equals(propertyQN)) {
					return Collections.singletonList(current);//TODO : doesn't manage when several stereotype with the same property are applied
				}
			}
		}
		return null;
	}
}

Back to the top