Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6302cbacf9dd7e5d17cd6288bda8fd59b8c7f78d (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*****************************************************************************
 * 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Juan Cadavid (CEA LIST) juan.cadavid@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.uml.nattable.provider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.infra.nattable.utils.AxisUtils;
import org.eclipse.papyrus.infra.widgets.providers.IRestrictedContentProvider;
import org.eclipse.papyrus.uml.nattable.manager.axis.UMLStereotypePropertyAxisManager;
import org.eclipse.papyrus.uml.tools.providers.UMLStereotypePropertyContentProvider;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageImport;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.ProfileApplication;
import org.eclipse.uml2.uml.Stereotype;

/**
 * Restricted Content Provider for the properties of Stereotypes
 *
 * @author Juan Cadavid
 *
 */
// TODO we maybe should use the AbstractRestrictedContentProvider
// of extends a generic uml content provider?
public class UMLStereotypeRestrictedPropertyContentProvider extends UMLStereotypePropertyContentProvider implements IRestrictedContentProvider {

	/**
	 * we show the value according to the table content
	 * if <code>true</code> -> the properties of stereotypes which are not applied on elements visible on the table aren't returned
	 */
	private boolean isRestricted;

	/**
	 * the stereotype axis manager
	 */
	private UMLStereotypePropertyAxisManager umlStereotypePropertyManager;

	/**
	 * the list of the allowed contents (Profile and Stereotype) when we are in restricted mode
	 */
	private Collection<Element> restrictedElements;

	/**
	 *
	 * Constructor.
	 *
	 * @param umlStereotypePropertyManager
	 *            the UML stereotype axis manager
	 * @param isRestricted
	 *            restrict mode
	 */
	public UMLStereotypeRestrictedPropertyContentProvider(final UMLStereotypePropertyAxisManager umlStereotypePropertyManager, final boolean isRestricted) {
		super();
		setIgnoreBaseProperty(true);
		// setIgnoreInheritedElements(true);
		this.isRestricted = isRestricted;
		this.umlStereotypePropertyManager = umlStereotypePropertyManager;
		init();
	}

	/**
	 * inits the fields of the table
	 */
	protected void init() {
		setProfiles(new ArrayList<Profile>(getAllAvailableProfiles()));
		this.restrictedElements = calculusOfAllowedElements();
	}

	/**
	 *
	 * @see org.eclipse.papyrus.uml.tools.providers.UMLStereotypePropertyContentProvider#getElements()
	 *
	 * @return
	 */
	@Override
	public Object[] getElements() {
		if (this.isRestricted) {
			final boolean isColumnManager = umlStereotypePropertyManager.isUsedAsColumnManager();
			final List<?> elements;
			if (isColumnManager) {
				elements = this.umlStereotypePropertyManager.getTableManager().getRowElementsList();
			} else {
				elements = this.umlStereotypePropertyManager.getTableManager().getColumnElementsList();
			}
			if (elements.isEmpty()) {
				return new Object[0];
			} else {
				List<Object> values = Arrays.asList(super.getElements());
				values = new ArrayList<Object>(values);
				values.retainAll(restrictedElements);
				return values.toArray();
			}
		} else {
			return super.getElements();
		}
	}

	/**
	 *
	 * @return
	 *         the list of the allowed profiles and stereotypes
	 */
	protected Collection<Element> calculusOfAllowedElements() {
		final Collection<Element> restrictedElements = new HashSet<Element>();
		// we are restricted so we show only the elements available for the current contents of the table
		final Set<Stereotype> restrictedStereotypes = new HashSet<Stereotype>();
		final boolean isColumnManager = umlStereotypePropertyManager.isUsedAsColumnManager();
		final List<?> elementsList;
		if (isColumnManager) {
			elementsList = this.umlStereotypePropertyManager.getTableManager().getRowElementsList();
		} else {
			elementsList = this.umlStereotypePropertyManager.getTableManager().getColumnElementsList();
		}
		for (Object object : elementsList) {
			Object representedElement = AxisUtils.getRepresentedElement(object);
			if (representedElement instanceof Element) {
				restrictedStereotypes.addAll(((Element) representedElement).getAppliedStereotypes());
			}
		}

		final Collection<Namespace> stereotypesNamespace = new HashSet<Namespace>();
		stereotypesNamespace.addAll(restrictedStereotypes);
		for (Stereotype stereotype : restrictedStereotypes) {
			stereotypesNamespace.addAll(stereotype.allNamespaces());
		}

		restrictedElements.addAll(restrictedStereotypes);
		restrictedElements.addAll(stereotypesNamespace);
		return restrictedElements;
	}

	protected Profile getTopRootProfile(final Profile profile, final Collection<Profile> availableProfiles) {
		EObject container = profile.eContainer();
		Profile topProfile = profile;
		while (container != null) {
			if (container instanceof Profile) {
				if (availableProfiles.contains(container)) {
					topProfile = (Profile) container;
				}
			}
			container = container.eContainer();
		}

		return topProfile;
	}

	/**
	 *
	 * @return
	 *         the list of all the profiles applied somewhere in the model
	 */
	protected Set<Profile> getAllAvailableProfiles() {
		final Set<Profile> coll = new HashSet<Profile>();
		final EObject context = umlStereotypePropertyManager.getTableManager().getTableContext();
		if (context instanceof Element) {
			final EObject container = EcoreUtil.getRootContainer(context);
			if (container instanceof Package) {
				// get the list of all profile applied in the model
				final Collection<Profile> allAppliedProfiles = getAppliedProfilesInWholePackage((Package) container);


				for (Profile profile : allAppliedProfiles) {
					coll.add(getTopRootProfile(profile, allAppliedProfiles));
				}


				// // coll.addAll(allAppliedProfiles);
				// //for each of these profiles, we look for the top profile which is applied in the model
				// //for example, if only SysML::Blocks is applied, we don't want to display SysML as root profile, but only SysML::Blocks
				// for(final Profile current : allAppliedProfiles) {
				// Profile topPackage = null;
				// for(final Package pack : current.allOwningPackages()) {
				// if(pack instanceof Profile && allAppliedProfiles.contains(pack)) {
				// topPackage = (Profile)pack;
				// break;
				// }
				// }
				// if(topPackage == null) {
				// topPackage = current;
				// }
				// coll.add(topPackage);
				// }
			}
		}
		return coll;
	}


	/**
	 *
	 * @param pack
	 *            a package
	 * @return
	 *         a collection with all profiles applied in the package and its subpackage
	 *         //TODO : should be moved in an upper plugin
	 */
	private static final Collection<Profile> getAppliedProfilesInWholePackage(final Package pack) {
		final Collection<Profile> appliedProfiles = new HashSet<Profile>();
		final List<ProfileApplication> result = getInstancesFilteredByType(pack, ProfileApplication.class, null);
		for (ProfileApplication profileApplication : result) {
			if (EcoreUtil.getRootContainer((profileApplication.getApplyingPackage())) == pack) {// restriction to avoid to find profile application from an imported model
				appliedProfiles.add(profileApplication.getAppliedProfile());
			}
		}
		return appliedProfiles;
	}

	/**
	 * Retrieve an list of all instances in the model that are instances of
	 * the java.lang.Class metaType or with a stereotype applied
	 *
	 * @param <T>
	 *
	 * @param metaType
	 *            selected classes
	 * @param model
	 *            to check
	 * @param appliedStereotype
	 *            may be null, metatype is ignored if not null
	 * @return a list containing the selected instances
	 */
	// TODO : replace me by ElementUtils.getInstance...
	@SuppressWarnings("unchecked")
	private static final <T extends EObject> List<T> getInstancesFilteredByType(final Package topPackage, final java.lang.Class<T> metaType, final Stereotype appliedStereotype) {
		// retrieve parent element
		// Package topPackage = Util.topPackage(element);
		// Assert.isNotNull(topPackage,
		// "Top package should not be null for element " + element);
		Iterator<EObject> iter = topPackage.eAllContents();
		List<T> filteredElements = new ArrayList<T>();

		while (iter.hasNext()) {
			EObject currentElt = iter.next();

			// If currentElt is an ElementImport, it is replaced by the imported
			// Element.
			if (currentElt instanceof ElementImport) {
				ElementImport elementImport = (ElementImport) currentElt;
				currentElt = elementImport.getImportedElement();
			}

			/* package imports treatment */
			else if (currentElt instanceof PackageImport) {
				Iterator<EObject> piIter = ((PackageImport) currentElt).getImportedPackage().eAllContents();
				while (piIter.hasNext()) {
					EObject piCurrentElt = piIter.next();
					if (piCurrentElt instanceof Element) {
						if (appliedStereotype != null) {

							Iterator<Stereotype> appStIter = ((Element) piCurrentElt).getAppliedStereotypes().iterator();
							while (appStIter.hasNext()) {
								Stereotype currentSt = appStIter.next();

								if (currentSt.conformsTo(appliedStereotype)) {
									filteredElements.add((T) piCurrentElt);
								}
							}

						} else { // if (appliedStereotype == null)
							if (metaType.isInstance(piCurrentElt)) {
								filteredElements.add((T) piCurrentElt);
							}

							/** add imported meta elements */
							else if (piCurrentElt instanceof ElementImport) {
								Iterator<EObject> eIter = ((ElementImport) piCurrentElt).getImportedElement().eAllContents();
								while (eIter.hasNext()) {
									EObject currentEIelt = eIter.next();
									if (metaType.isInstance(currentEIelt)) {
										filteredElements.add((T) currentEIelt);
									}
								}
							}
						}
					}

				}
			}

			// Filtering elements
			if (currentElt instanceof Element) {

				if (appliedStereotype != null) {

					Iterator<Stereotype> appStIter = ((Element) currentElt).getAppliedStereotypes().iterator();
					while (appStIter.hasNext()) {
						Stereotype currentSt = appStIter.next();

						if (currentSt.conformsTo(appliedStereotype)) {
							filteredElements.add((T) currentElt);
						}
					}

				} else { // if (appliedStereotype == null)
					if (metaType.isInstance(currentElt)) {
						filteredElements.add((T) currentElt);
					}

					/** add imported meta elements */
					else if (currentElt instanceof ElementImport) {
						Iterator<EObject> eIter = ((ElementImport) currentElt).getImportedElement().eAllContents();
						while (eIter.hasNext()) {
							EObject currentEIelt = eIter.next();
							if (metaType.isInstance(currentEIelt)) {
								filteredElements.add((T) currentEIelt);
							}
						}
					}
				}
			}
		}

		return filteredElements;
	}

	/**
	 *
	 * @see org.eclipse.papyrus.infra.widgets.providers.IRestrictedContentProvider#setRestriction(boolean)
	 *
	 * @param isRestricted
	 *            Setter for {@link #isRestricted}
	 */
	@Override
	public void setRestriction(boolean isRestricted) {
		this.isRestricted = isRestricted;
	}


	/**
	 *
	 * @see org.eclipse.papyrus.uml.tools.providers.UMLStereotypePropertyContentProvider#getChildren(java.lang.Object)
	 *
	 * @param parentElement
	 * @return
	 */
	@Override
	public Object[] getChildren(Object parentElement) {
		if (!(parentElement instanceof Package || parentElement instanceof Stereotype)) {
			return new Object[0];
		}
		final Object[] children = super.getChildren(parentElement);
		if (!isRestricted || !(parentElement instanceof Package)) {
			return children;
		} else {
			// we are restricted so we show only the elements available for the current contents of the table
			final Collection<Object> returnedValues = new ArrayList<Object>();
			returnedValues.addAll(Arrays.asList(children));
			returnedValues.retainAll(this.restrictedElements);
			return returnedValues.toArray();
		}
	}


	/**
	 *
	 * @see org.eclipse.papyrus.uml.tools.providers.UMLStereotypePropertyContentProvider#dispose()
	 *
	 */
	@Override
	public void dispose() {
		super.dispose();
		this.umlStereotypePropertyManager = null;
		this.restrictedElements.clear();
	}

	/**
	 *
	 * @see org.eclipse.papyrus.infra.widgets.providers.IRestrictedContentProvider#isRestricted()
	 *
	 * @return
	 */
	@Override
	public boolean isRestricted() {
		return isRestricted;
	}

}

Back to the top