Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 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.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.

Author(s):
Jan Koehnlein - Initial contribution and API
public class ProjectUtil {
	private IWorkspace workspace;

Returns:
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();
	}

Returns:
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 },
										null).isOK());
					}
					return false;
				}
			});
			return (IFile) fileStorage.getFirst();
catch (NoSuchElementException e) {
			return null;
		}
	}
New to GrepCode? Check out our FAQ X