Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9961cae90eccc4eb2953a323309367b6b6f63c6e (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
/**
 *  Copyright (c) 2011, 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:
 *  	Gregoire Dupe (Mia-Software) - Bug 361794 - [Restructuring] EMF Facet customization meta-model
 *      Gregoire Dupe (Mia-Software) - Bug 369987 - [Restructuring][Table] Switch to the new customization and facet framework
 *      Gregoire Dupe (Mia-Software) - Bug 373078 - API Cleaning
 *      Gregoire Dupe (Mia-Software) - Bug 375087 - [Table] ITableWidget.addColumn(List<ETypedElement>, List<FacetSet>)
 *      Nicolas Bros (Mia-Software) - Bug 379683 - customizable Tree content provider
 *      Christian W. Damus (CEA) - bug 410346
 */
package org.eclipse.papyrus.emf.facet.custom.ui.internal.query;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.ReflectiveItemProvider;
import org.eclipse.papyrus.emf.facet.efacet.core.IFacetManager;
import org.eclipse.papyrus.emf.facet.efacet.core.exception.DerivedTypedElementException;
import org.eclipse.papyrus.emf.facet.query.java.core.IJavaQuery2;
import org.eclipse.papyrus.emf.facet.query.java.core.IParameterValueList2;
import org.eclipse.papyrus.emf.facet.util.emf.core.ModelUtils;

public class LabelQuery implements IJavaQuery2<EObject, String> {

	private static final String NAME = "name"; //$NON-NLS-1$

	public String evaluate(final EObject source,
			final IParameterValueList2 parameterValues,
			final IFacetManager facetManager)
			throws DerivedTypedElementException {
		ETypedElement sfParam = null;
		if (parameterValues != null) {
			sfParam = (ETypedElement) parameterValues
					.getParameterValueByName("eStructuralFeature").getValue(); //$NON-NLS-1$
		}
		String result = null;
		if (sfParam == null) {
			final ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(
					ComposedAdapterFactory.Descriptor.Registry.INSTANCE);

			try {
				final IItemLabelProvider itemLabelProvider = (IItemLabelProvider) adapterFactory.adapt(source, IItemLabelProvider.class);
				// We don't want to use a ReflectiveItemProvider because it provides
				// a string prefixed with the eObject's meta-class name.
				if (itemLabelProvider instanceof ReflectiveItemProvider) {
					result = LabelQuery.getDefaultName(source);
				} else if (itemLabelProvider == null) {
					result = ModelUtils.getDefaultName(source);
				} else {
					result = itemLabelProvider.getText(source);
				}
			} finally {
				// Dispose the adapter factory because it added an adapter that would leak, as nobody else will ever use it
				adapterFactory.dispose();
			}
		} else {
			try {
				if (sfParam instanceof EAttribute && sfParam.getUpperBound() == 1) {
					final Object object = facetManager.getOrInvoke(source, sfParam, Object.class);
					final String objectLabel = getObjectLabel(object, facetManager);
					result = sfParam.getName() + " = " + objectLabel; //$NON-NLS-1$
				} else {
					result = sfParam.getName();
				}
			} catch (final Exception e) {
				throw new DerivedTypedElementException(e);
			}
		}
		return result;
	}

	private String getObjectLabel(final Object object,
			final IFacetManager facetManager)
			throws DerivedTypedElementException {
		String result;
		if (object instanceof EObject) {
			final EObject eObject = (EObject) object;
			result = evaluate(eObject, null, facetManager);
		} else {
			result = String.valueOf(object);
		}
		return result;
	}

	/**
	 * @return a default name based on a string feature of the given {@link EObject}
	 */
	// from org.eclipse.papyrus.emf.facet.infra.common.core.internal.utils.ModelUtils
	public static String getDefaultName(final EObject eObject) {
		String result = ""; //$NON-NLS-1$
		// find a feature that can be used as a name
		final EStructuralFeature feature = LabelQuery.getLabelFeature(eObject
				.eClass());
		if (feature != null) {
			result = (String) eObject.eGet(feature);
		}
		return result;
	}

	/**
	 * This method searches for a structural feature that can be used as a name
	 *
	 * @param eClass
	 *            in which class to search a structural feature that can be used as
	 *            a name
	 * @return an EStructuralFeature
	 */
	// Copied from org.eclipse.emf.edit.provider.ReflectiveItemProvider to org.eclipse.papyrus.emf.facet.infra.common.core.internal.utils.ModelUtils
	// Copied from org.eclipse.papyrus.emf.facet.infra.common.core.internal.utils.ModelUtils
	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 (LabelQuery.NAME.equalsIgnoreCase(eAttribute
						.getName())) {
					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;
	}

}

Back to the top