Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 itemis AG ( 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 /
 package org.eclipse.xtext.ui.refactoring.impl;
import  org.eclipse.core.resources.IFile;
import  org.eclipse.core.resources.IProject;
import  org.eclipse.core.resources.IStorage;
import  org.eclipse.core.resources.IWorkspace;
Converts URIs to IFiles and IProjects.

Jan Koehnlein - Initial contribution and API
public class ProjectUtil {
	private IWorkspace workspace;

null if there is no such project or the file is not accessible
	public IProject getProject(URI uri) {
		IFile file = findFileStorage(urifalse);
		if(file == null)
			return null;
		return file.getProject();

null if there is no such file or the file is not editable
	public IFile findFileStorage(final URI urifinal boolean validateEdit) {
		Iterable<Pair<IStorage, IProject>> storages = .getStorages(uri);
		try {
			Pair<IStorage, IProject> fileStorage = Iterables.find(storagesnew Predicate<Pair<IStorage, IProject>>() {
				public boolean apply(Pair<IStorage, IProject> input) {
					IStorage storage = input.getFirst();
					if (storage instanceof IFile) {
						IFile file = (IFile) storage;
						return file.isAccessible()
								&& (!validateEdit || !file.isReadOnly() || .validateEdit(new IFile[] { file },
					return false;
			return (IFile) fileStorage.getFirst();
catch (NoSuchElementException e) {
			return null;
New to GrepCode? Check out our FAQ X