Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab94a10c59c01289e14b074be71d7ec5a07626eb (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
/*****************************************************************************
 * Copyright (c) 2012, 2014 CEA LIST 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 410346
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.emf.providers.strategy;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IDisposable;
import org.eclipse.papyrus.emf.facet.custom.core.ICustomizationManager;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.internal.treeproxy.TreeElement;
import org.eclipse.papyrus.emf.facet.custom.ui.internal.CustomizedTreeContentProvider;
import org.eclipse.papyrus.infra.emf.Activator;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.widgets.providers.IAdaptableContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IHierarchicContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;


public class SemanticEMFContentProvider extends CustomizedTreeContentProvider implements IAdaptableContentProvider, IHierarchicContentProvider, IStaticContentProvider {

	protected EObject[] roots;

	protected List<?> metaclasses = new LinkedList<Object>();

	protected List<?> notWantedMetaclasses = new LinkedList<Object>();

	protected EObject eObject;

	protected EStructuralFeature feature;

	protected AdapterFactory factory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);

	protected SemanticEMFContentProvider() {
		super(Activator.getDefault().getCustomizationManager());
	}

	public SemanticEMFContentProvider(EObject editedEObject, EStructuralFeature feature, EObject[] roots, ICustomizationManager customizationManager) {
		super(customizationManager);
		this.roots = roots;

		configureMetaclasses(feature);

		this.eObject = editedEObject;
		this.feature = feature;
	}

	public SemanticEMFContentProvider(EObject editedEObject, EStructuralFeature feature, EObject[] roots) {
		this(editedEObject, feature, roots, Activator.getDefault().getCustomizationManager());
	}

	protected void configureMetaclasses(EStructuralFeature feature) {
		if (feature != null) {
			setWantedMetaclasses(Collections.singletonList(feature.getEType()));
		}
	}

	public SemanticEMFContentProvider(EObject editedEObject, EStructuralFeature feature) {
		this(editedEObject, feature, findRoots(editedEObject));
	}

	public SemanticEMFContentProvider(EObject[] roots) {
		this(null, null, roots);
	}

	public SemanticEMFContentProvider(EObject[] roots, ICustomizationManager customizationManager) {
		this(null, null, roots, customizationManager);
	}

	public SemanticEMFContentProvider(ResourceSet root) {
		this(null, null, root);
	}

	public SemanticEMFContentProvider(EObject editedEObject, EStructuralFeature feature, ResourceSet root) {
		this(editedEObject, feature, getRoots(root));
	}

	@Override
	public void dispose() {
		try {
			// Because we created this adapter factory, we must dispose it
			if (factory instanceof IDisposable) {
				((IDisposable) factory).dispose();
			}
		} finally {
			super.dispose();
		}
	}

	protected static EObject[] getRoots(ResourceSet root) {
		List<EObject> roots = new LinkedList<EObject>();
		if (root != null) {
			for (Resource resource : root.getResources()) {
				roots.addAll(resource.getContents());
			}
		}
		return roots.toArray(new EObject[roots.size()]);
	}

	protected static EObject[] findRoots(EObject source) {

		// The EObject is not contained in a resource : we return the top-level EObject
		if (source.eResource() == null) {
			while (source.eContainer() != null) {
				source = source.eContainer();
			}

			return new EObject[] { source };
		}

		// The resource is not contained in a resource set : we return the resource's contents
		if (source.eResource().getResourceSet() == null) {
			return source.eResource().getContents().toArray(new EObject[0]);
		}

		// We have a full resourceSet : we return its contents
		return getRoots(source.eResource().getResourceSet());
	}

	@Override
	public EObject[] getRootElements(final Object inputElement) {
		return roots;
	}

	@Override
	public Object[] getChildren(Object parentElement) {
		// Sometimes, the ContentProvider is wrapped in a provider which adds new children (Not EMF-Facet compliant)
		if (parentElement instanceof TreeElement) {
			return super.getChildren(parentElement);
		}

		if (parentElement instanceof EObject) {
			return super.getChildren(createEObjectProxy(parentElement, null));
		}

		return new Object[0];
	}

	@Override
	public Object getAdaptedValue(Object containerElement) {
		return EMFHelper.getEObject(containerElement);
	}

	@Override
	public boolean hasChildren(Object parent) {
		// May be expensive
		Object[] children = getChildren(parent);
		return children != null && children.length > 0;
	}

	@Override
	public boolean isValidValue(Object containerElement) {
		// get the semantic object form the element
		Object semanticObject = getAdaptedValue(containerElement);

		// return false for EReference and non-semantic objects
		if (semanticObject instanceof EReference || semanticObject == null) {
			return false;
		}

		// Tests whether the element is compatible with at least one metaclass
		if (metaclasses != null && !metaclasses.isEmpty()) {
			boolean compatible = false;

			for (Object metaclass : metaclasses) {
				if (isCompatibleMetaclass(containerElement, metaclass)) {
					compatible = true;
					break;
				}
			}

			if (!compatible) {
				return false;
			}
		}

		// If the element is compatible with at least one metaclass from notWanted, then it is not valid
		for (Object metaclass : notWantedMetaclasses) {
			if (isCompatibleMetaclass(containerElement, metaclass)) {
				return false;
			}
		}

		return true;
	}

	protected boolean isCompatibleMetaclass(Object containerElement, Object metaclass) {
		if (metaclass instanceof EClassifier) {
			Object semanticElement = getAdaptedValue(containerElement);
			return ((EClassifier) metaclass).isInstance(semanticElement);
		}
		return false;
	}

	public void setWantedMetaclasses(List<?> metaclasses) {
		this.metaclasses = metaclasses;
	}

	public void setNotWantedMetaclasses(List<?> notWantedMetaclasses) {
		assert notWantedMetaclasses != null : "notWantedMetaclasses must be not null"; //$NON-NLS-1$
		this.notWantedMetaclasses = notWantedMetaclasses;
	}

	@Override
	public Object[] getElements() {
		return super.getElements(null);
	}

	public List<?> getWantedMetaclasses() {
		return metaclasses;
	}

	public List<?> getNotWantedMetaclasses() {
		return notWantedMetaclasses;
	}
}

Back to the top