package tool.commands;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import tool.model.ToolComponent;
public abstract class CommonHandler extends AbstractHandler{
public IProject getProjectFromSelection(ISelection selection) {
IProject project = null;
if (selection instanceof TreeSelection){
Object object = ((TreeSelection)selection).getFirstElement();
if (object instanceof IResource){
project = ((IResource)object).getProject();
} else if (object instanceof ToolComponent){
/*
* we are going to find the file of the component or its parent, then locate the project
*/
IFile file = null;
ToolComponent subject = (ToolComponent)object;
while (file == null){
file = subject.getFile();
if (file == null)
subject = (ToolComponent) subject.getParent();
}
project = file.getProject();
}
}
return project;
}
public IWorkspace getWorkspace(){
return ResourcesPlugin.getWorkspace();
}
}