blob: 2eb19746f53f6d97b8b15edda1fb1ffd4d4c41b7 [file] [log] [blame]
/*
* Copyright (c) 2012 Google Inc.
*
* 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 com.google.eclipse.protobuf.ui.util;
import org.eclipse.core.resources.*;
import org.eclipse.ui.IEditorPart;
import com.google.inject.Singleton;
/**
* Utility methods related to editors.
*
* @author alruiz@google.com (Alex Ruiz)
*/
@Singleton public class Editors {
/**
* Returns the project owning the file displayed in the given editor.
* @param editor the given editor.
* @return the project owning the file displayed in the given editor.
*/
public IProject projectOwningFileDisplayedIn(IEditorPart editor) {
IResource resource = resourceFrom(editor);
return (resource == null) ? null : resource.getProject();
}
private IResource resourceFrom(IEditorPart editor) {
if (editor == null) {
return null;
}
Object adapter = editor.getEditorInput().getAdapter(IResource.class);
return (adapter == null) ? null : (IResource) adapter;
}
}