source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/utils/EObjectSelectionResolver.java @ 5

Last change on this file since 5 was 5, checked in by zeiss, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 1.9 KB
Line 
1package de.ugoe.cs.swe.bnftools.utils;
2
3import java.util.Iterator;
4
5import org.eclipse.emf.common.util.URI;
6import org.eclipse.emf.ecore.EObject;
7import org.eclipse.emf.ecore.util.EcoreUtil;
8import org.eclipse.jface.text.ITextSelection;
9import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
10import org.eclipse.xtext.resource.IEObjectDescription;
11import org.eclipse.xtext.resource.IResourceDescription;
12import org.eclipse.xtext.resource.IResourceDescriptions;
13import org.eclipse.xtext.resource.XtextResource;
14import org.eclipse.xtext.util.concurrent.IUnitOfWork;
15
16import com.google.common.base.Predicate;
17import com.google.common.collect.Iterables;
18
19public 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}
Note: See TracBrowser for help on using the repository browser.