1 | package de.ugoe.cs.swe.bnftools.utils;
|
---|
2 |
|
---|
3 | import java.util.Iterator;
|
---|
4 |
|
---|
5 | import org.eclipse.emf.common.util.URI;
|
---|
6 | import org.eclipse.emf.ecore.EObject;
|
---|
7 | import org.eclipse.emf.ecore.util.EcoreUtil;
|
---|
8 | import org.eclipse.jface.text.ITextSelection;
|
---|
9 | import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
|
---|
10 | import org.eclipse.xtext.resource.IEObjectDescription;
|
---|
11 | import org.eclipse.xtext.resource.IResourceDescription;
|
---|
12 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
13 | import org.eclipse.xtext.resource.XtextResource;
|
---|
14 | import org.eclipse.xtext.util.concurrent.IUnitOfWork;
|
---|
15 |
|
---|
16 | import com.google.common.base.Predicate;
|
---|
17 | import com.google.common.collect.Iterables;
|
---|
18 |
|
---|
19 | public class EObjectSelectionResolver implements
|
---|
20 | IUnitOfWork<IEObjectDescription, XtextResource> {
|
---|
21 | private final ITextSelection selection;
|
---|
22 | private IResourceDescriptions resourceDescriptions;
|
---|
23 |
|
---|
24 | public EObjectSelectionResolver(ITextSelection selection,
|
---|
25 | IResourceDescriptions resourceDescriptions) {
|
---|
26 | this.selection = selection;
|
---|
27 | this.resourceDescriptions = resourceDescriptions;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public IEObjectDescription exec(XtextResource state) throws Exception {
|
---|
31 | EObject element = EObjectAtOffsetHelper.resolveElementAt(state,
|
---|
32 | selection.getOffset(), null);
|
---|
33 | if (element != null) {
|
---|
34 | final URI eObjectURI = EcoreUtil.getURI(element);
|
---|
35 | IResourceDescription resourceDescription = resourceDescriptions
|
---|
36 | .getResourceDescription(eObjectURI.trimFragment());
|
---|
37 | if (resourceDescription != null) {
|
---|
38 | Iterator<IEObjectDescription> eObjectDescriptions = Iterables
|
---|
39 | .filter(resourceDescription.getExportedObjects(),
|
---|
40 | new Predicate<IEObjectDescription>() {
|
---|
41 | public boolean apply(
|
---|
42 | IEObjectDescription input) {
|
---|
43 | return input.getEObjectURI().equals(
|
---|
44 | eObjectURI);
|
---|
45 | }
|
---|
46 | }).iterator();
|
---|
47 | if (eObjectDescriptions.hasNext()) {
|
---|
48 | return eObjectDescriptions.next();
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | return null;
|
---|
53 | }
|
---|
54 | } |
---|