Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 023da74e76520b7c9fb103b5bbe0c88d02007604 (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
/*******************************************************************************
 * Copyright (c) 2009, 2014 Mia-Software, CEA, and others.
 * 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:
 * 		Nicolas Bros (Mia-Software) - Bug 366367 - To be able to change the "CanBePresentedInTheTable" query
 *      Gregoire Dupe (Mia-Software) - Bug 369987 - [Restructuring][Table] Switch to the new customization and facet framework
 *      Laurent Pichierri (Soft-Maint) - Bug 371204 - Compatibility with Helios
 *      Gregoire Dupe (Mia-Software) - Bug 371367 - Hierarchical FacetSets
 *      Gregoire Dupe (Mia-Software) - Bug 369987 - [Restructuring][Table] Switch to the new customization and facet framework
 *      Gregoire Dupe (Mia-Software) - Bug 387470 - [EFacet][Custom] Editors
 *      Christian W. Damus (CEA) - bug 410346
 *      
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.util.emf.core;

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

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.papyrus.emf.facet.util.core.Logger;
import org.eclipse.papyrus.emf.facet.util.core.internal.exported.StringUtils;
import org.eclipse.papyrus.emf.facet.util.emf.core.internal.Activator;

/**
 * Utility class for models.
 * @since 0.2
 */
public final class ModelUtils {

	/**
	 * Meta-model qualified name separator
	 */
	private static final String MM_QNAME_SEPARATOR = "::"; //$NON-NLS-1$
	
	/**
	 * Utility classes don't need to (and shouldn't) be instantiated.
	 */
	private ModelUtils() {
		// prevents instantiation
	}

	/**
	 * Computes a list of {@link EObject}s from the same {@link ResourceSet} as
	 * <code>source</code> that can be assigned to the given feature.
	 */
	//Copied from org.eclipse.papyrus.emf.facet.infra.common.core.internal.utils.ModelUtils.computeAssignableElements(EStructuralFeature, EObject)
	public static List<EObject> computeAssignableElements(final EStructuralFeature feature,
			final EObject source) {
		EClassifier eType = feature.getEType();

		// build a list of EObjects assignable to the EReference
		List<EObject> list = new ArrayList<EObject>();
		Resource eResource = source.eResource();
		ResourceSet resourceSet = eResource.getResourceSet();
		TreeIterator<Notifier> allContents = resourceSet.getAllContents();
		while (allContents.hasNext()) {
			Notifier notifier = allContents.next();
			if (notifier instanceof EObject) {
				EObject eObject = (EObject) notifier;
				if (eType.isInstance(eObject)) {
					list.add(eObject);
				}
			}
		}
		return list;
	}
	
	public static boolean isBooleanDataType(final EClassifier classifier) {
		if (classifier instanceof EDataType) {
			EDataType dataType = (EDataType) classifier;
			Class<?> instanceClass = dataType.getInstanceClass();
			return instanceClass == Boolean.class || instanceClass == boolean.class;
		}
		return false;
	}
	
	/**
	 * Find all subclasses of the given metaclass in the package registry
	 * 
	 * @param context
	 *            the metaclass whose subclasses must be found
	 * @return all the subclasses
	 */
	//Copied from org.eclipse.papyrus.emf.facet.infra.common.core.internal.utils.ModelUtils.allSubClasses(final EClass context)
	public static List<EClass> allSubClasses(final EClass context) {
		List<EClass> result = new ArrayList<EClass>();
		// Set<Resource> resources = new HashSet<Resource>();
		Collection<String> uris = new ArrayList<String>(EPackage.Registry.INSTANCE.keySet());
		for (String ePackageURI : uris) {
			try {
				EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(ePackageURI);
				EList<EClassifier> eClassifiers = ePackage.getEClassifiers();
				for (EClassifier eClassifier : eClassifiers) {
					if (eClassifier instanceof EClass) {
						EClass eClass = (EClass) eClassifier;
						if (isSuperTypeOf(context, eClass)) {
							result.add(eClass);
						}
					}
				}
			} catch (Exception e) {
				Logger.logError(e, Activator.getDefault());
			}
		}
		return result;
	}
	
	//Copied from org.eclipse.papyrus.emf.facet.infra.common.core.internal.utils.ModelUtils.isSuperTypeOf(final EClass self, final EClass typeDeclaration)
	public static boolean isSuperTypeOf(final EClass self, final EClass typeDeclaration) {
		return typeDeclaration.getEAllSuperTypes().contains(self);
	}
	
	/** @return the qualified name of the given metaclass */
	//Copied from org.eclipse.papyrus.emf.facet.infra.common.core.internal.utils.ModelUtils.getMetaclassQualifiedName(final EClassifier eClass)
	public static String getMetaclassQualifiedName(final EClassifier eClass) {
		final ArrayList<String> qualifiedNameParts = new ArrayList<String>();
		final StringBuilder builder = new StringBuilder();

		EPackage ePackage = eClass.getEPackage();
		while (ePackage != null) {
			qualifiedNameParts.add(ePackage.getName());
			ePackage = ePackage.getESuperPackage();
		}

		for (int i = qualifiedNameParts.size() - 1; i >= 0; i--) {
			builder.append(qualifiedNameParts.get(i) + "."); //$NON-NLS-1$
		}

		builder.append(eClass.getName());

		return builder.toString();
	}

	/**
	 * Returns a name for the given model element from the EMF global registry,
	 * or use a default name taken from a String attribute.
	 * 
	 * @return a name for the given element
	 */
	//Copied from org.eclipse.papyrus.emf.facet.infra.common.core.internal.utils.ModelUtils.getName(EObject)
	public static String getName(final EObject eObject) {
		ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);

		try {
			IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(eObject, IItemLabelProvider.class);
			if(itemLabelProvider != null) {
				return itemLabelProvider.getText(eObject);
			}
		} finally {
			// Dispose the adapter factory because it added an adapter that would leak, as it will never be reused
			adapterFactory.dispose();
		}

		return ModelUtils.getDefaultName(eObject);
	}

	/**
	 * @return a default name based on a string feature of the given
	 *         {@link EObject}
	 */
	//Copied from org.eclipse.papyrus.emf.facet.infra.common.core.internal.utils.ModelUtils.getDefaultName(EObject)
	public static String getDefaultName(final EObject eObject) {
		// find a feature that can be used as a name
		final EStructuralFeature feature = ModelUtils.getLabelFeature(eObject.eClass());
		if (feature != null) {
			final Object value = eObject.eGet(feature);
			if (value != null) {
				return StringUtils.truncateBeforeNewline(value.toString());
			}
		}
		return ""; //$NON-NLS-1$
	}
	
	//Copied from ReflectiveItemProvider class
	//Copied from org.eclipse.papyrus.emf.facet.infra.common.core.internal.utils.ModelUtils.getLabelFeature(EClass)
	private static EStructuralFeature getLabelFeature(final EClass eClass) {
		EAttribute result = null;
		for (final EAttribute eAttribute : eClass.getEAllAttributes()) {
			if (!eAttribute.isMany()
					&& eAttribute.getEType().getInstanceClass() != FeatureMap.Entry.class) {
				if ("name".equalsIgnoreCase(eAttribute.getName())) { //$NON-NLS-1$
					result = eAttribute;
					break;
				} else if (result == null) {
					result = eAttribute;
				} else if (eAttribute.getEAttributeType().getInstanceClass() == String.class
						&& result.getEAttributeType().getInstanceClass() != String.class) {
					result = eAttribute;
				}
			}
		}
		return result;
	}
	
	/**
	 * @author Gregoire Dupe (Mia-Software)
	 * @param namedElement
	 * @return
	 */
	public static String getQualifiedName(final ENamedElement namedElement) {
		String parentQName = ""; //$NON-NLS-1$
		if (namedElement.eContainer() instanceof ENamedElement) {
			ENamedElement parent = (ENamedElement) namedElement.eContainer();
			parentQName = getQualifiedName(parent) + ModelUtils.MM_QNAME_SEPARATOR;
		}
		return parentQName + namedElement.getName();
	}

	/**
	 * @since 0.3
	 */
	public static ResourceSet getOrCreateResourceSet(final EObject eObject) {
		ResourceSet resourceSet = null;
		if (eObject.eResource() == null) {
			resourceSet = new ResourceSetImpl();
		} else {
			resourceSet = eObject.eResource().getResourceSet();
		}
		return resourceSet;
	}

	/**
	 * @since 0.3
	 */
	public static IProject getProject(final EObject eObject) {
		IProject result = null;
		final Resource resource = eObject.eResource();
		final URI uri = resource.getURI();
		if (uri.isPlatformResource()) {
			final String projectName = uri.segment(1);
			final IWorkspace workspace = ResourcesPlugin.getWorkspace();
			result = workspace.getRoot()
					.getProject(projectName);
		}
		return result;
	}
}

Back to the top