Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2013 itemis AG (http://www.itemis.eu) 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 /
  
  package org.eclipse.xtext.ui.generator.trace;
  
 
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.resources.IStorage;
 import  org.eclipse.core.runtime.CoreException;
 import  org.eclipse.jdt.core.IJavaProject;
 import  org.eclipse.jdt.core.IPackageFragmentRoot;
 import  org.eclipse.jdt.core.JavaCore;
 import  org.eclipse.jdt.core.JavaModelException;
 
A contribution that is aware of Java projects and therefore returns the source folder relative path of resources that reside in java projects.

Author(s):
Moritz Eysholdt - Initial contribution and API
Sebastian Zarnekow
 
 
 	private final static Logger LOG = Logger.getLogger(JavaProjectAwareTraceContribution.class);
 	
 	
 	
 	/* @Nullable */
 	public URI getURIForTrace(XtextResource context) {
 		ResourceSet rs = context.getResourceSet();
 		if (!(rs instanceof XtextResourceSet && ((XtextResourceSetrs).getClasspathURIContext() instanceof IJavaProject))
 			return null;
 		IJavaProject javaProject = (IJavaProject) ((XtextResourceSetrs).getClasspathURIContext();
 		try {
 			return computeTraceURI(context.getURI(), javaProject);
 		} catch (JavaModelException e) {
 			.error(e.getMessage(), e);
 		}
 		return null;
 	}
 	
 	/* @Nullable */
 	public URI getURIForTrace(URI uri) {
 		if (uri.isPlatform() && uri.segmentCount() > 1) {
 			Iterator<Pair<IStorage, IProject>> storagesIterator = .getStorages(uri).iterator();
 			if(storagesIterator.hasNext()){
 				Pair<IStorage, IProject> candidate = storagesIterator.next();
 				IProject project = candidate.getSecond();
 				try {
 					if (project.exists()) {
 						if (project.hasNature(JavaCore.NATURE_ID)) {
 							IJavaProject javaProject = JavaCore.create(project);
 							if (javaProject != null)
 								return computeTraceURI(urijavaProject);
 						}
 					}
 				} catch (JavaModelException e) {
 					.error(e.getMessage(), e);
 				} catch (CoreException e) {
 					.error(e.getMessage(), e);
 				}
 			}
 		}
 		return null;
 	}
 	
 	/* @Nullable */
 	protected URI computeTraceURI(URI uri, IJavaProject javaProjectthrows JavaModelException {
 		for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
 			if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
 				URI prefix = URI.createPlatformResourceURI(root.getResource().getFullPath().addTrailingSeparator().toString(), true);
 				if (.isPrefix(prefixuri))
 					return uri.deresolve(prefix).trimFragment().trimQuery();
 			}
 		}
 		return null;
 	}
 	
New to GrepCode? Check out our FAQ X