Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3d14746d6d18e9325083bd033c7315a86c290601 (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


/**
 * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
 * 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:
 *         Florian Pirchner - Initial implementation
 */


package org.eclipse.osbp.ecview.uisemantics.scoping;

import java.util.Collections;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.EObjectDescription;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.impl.SimpleScope;
import org.eclipse.xtext.xbase.scoping.batch.XbaseBatchScopeProvider;
import org.eclipse.osbp.ecview.semantic.uisemantics.UiSemanticsPackage;
import org.eclipse.osbp.ecview.semantic.uisemantics.UxElementURI;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;

@SuppressWarnings("restriction")
public class ScopeProvider extends XbaseBatchScopeProvider {

	@Override
	public IScope getScope(EObject context, EReference reference) {

		if (reference == UiSemanticsPackage.Literals.UX_EPACKAGE_IMPORT__EPACKAGE) {
			return createEPackageScope();
		} else if (reference == UiSemanticsPackage.Literals.UX_ELEMENT_URI__ECLASS) {
			UxElementURI elementURI = (UxElementURI) context;
			return new EPackageContentScope(elementURI.getEPackage()
					.getEPackage());
		}

		return super.getScope(context, reference);
	}

	protected IScope createEPackageScope() {
		IScope current = new SimpleScope(IScope.NULLSCOPE, Iterables.transform(
				EPackage.Registry.INSTANCE.keySet(),
				new Function<String, IEObjectDescription>() {
					public IEObjectDescription apply(String from) {
						InternalEObject proxyPackage = (InternalEObject) EcoreFactory.eINSTANCE
								.createEPackage();
						proxyPackage.eSetProxyURI(URI.createURI(from));
						return EObjectDescription.create(
								QualifiedName.create(from), proxyPackage,
								Collections.singletonMap("nsURI", "true"));
					}
				}));
		return current;
	}

}

Back to the top