Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 13475430434e817b1ebbe48ab0db3cd3d511c49f (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
/*****************************************************************************
 * Copyright (c) 2009 Atos Origin.
 *
 *    
 * 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:
 *  Emilien Perico (Atos Origin) emilien.perico@atosorigin.com - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.navigator.internal.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.uml2.common.util.CacheAdapter;

/**
 * Class that manages stereotyped element between model explorer and diagram for profile diagram
 * based
 */
public class StereotypeApplicationUtils {

	/**
	 * Unwraps selection. Gets base element from EObject
	 * 
	 * @param selection
	 *        the selection
	 * 
	 * @return the i selection
	 */
	public static ISelection unwrapStereotypedSelection(ISelection selection) {
		if(selection instanceof StructuredSelection && !selection.isEmpty()) {
			StructuredSelection structuredSelection = (StructuredSelection)selection;
			List<EObject> selectionList = new ArrayList<EObject>();
			for(Iterator<?> iterator = structuredSelection.iterator(); iterator.hasNext();) {
				Object next = iterator.next();
				if(next instanceof EObject) {
					EObject element = getBaseElement((EObject)next);
					if(element != null) {
						selectionList.add(element);
					}
				}
			}
			if(!selectionList.isEmpty()) {
				return new StructuredSelection(selectionList);
			}
		}
		return selection;
	}

	/**
	 * Gets the stereotype application selection of a stereotyped element from common viewer.
	 * 
	 * @param selection
	 *        the selection
	 * 
	 * @return the stereotype application selection
	 */
	public static ISelection getStereotypedSelectionFromCommonViewer(ISelection selection) {
		if(selection instanceof TreeSelection) {
			TreeSelection treeSelectionEvent = (TreeSelection)selection;
			Object firstElement = treeSelectionEvent.getFirstElement();
			if(firstElement instanceof EObject) {
				List<EObject> stereotypedApplications = StereotypeApplicationUtils.getStereotypedApplications((EObject)firstElement);
				return new StructuredSelection(stereotypedApplications);
			}
		}
		return selection;
	}

	/**
	 * Gets the stereotyped applications for an eObject
	 * 
	 * @param eObject
	 *        the eObject
	 * 
	 * @return the stereotyped applications
	 */
	public static List<EObject> getStereotypedApplications(EObject eObject) {
		List<EObject> references = new ArrayList<EObject>();
		if(eObject != null) {
			Collection<Setting> inverseReferences = CacheAdapter.INSTANCE.getNonNavigableInverseReferences(eObject);
			for(Setting ref : inverseReferences) {
				EObject extension = ref.getEObject();
				EObject baseElement = getBaseElement(extension);
				if(eObject.equals(baseElement)) {
					references.add(extension);
				}
			}
		}
		return references;
	}

	/**
	 * Gets the base element of an eObject
	 * 
	 * @param eObject
	 *        the eObject
	 * 
	 * @return the base element
	 */
	private static EObject getBaseElement(EObject eObject) {
		if(eObject != null) {
			for(EStructuralFeature f : eObject.eClass().getEAllStructuralFeatures()) {
				if(f.getName().startsWith("base_")) {
					Object b = eObject.eGet(f);
					if(b instanceof EObject && b != null) {
						return (EObject)b;
					}
				}
			}
		}
		return null;
	}

}

Back to the top