diff --git a/plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java b/plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java index 4eba14d6..222ef959 100644 --- a/plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java +++ b/plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java @@ -18,16 +18,20 @@ import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IPath; -//import org.eclipse.jdt.internal.core.JarPackageFragmentRoot; -//import org.eclipse.jdt.internal.core.PackageFragment; +import org.eclipse.core.runtime.Platform; +import org.eclipse.jdt.internal.core.JarPackageFragmentRoot; +import org.eclipse.jdt.internal.core.PackageFragment; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.ide.FileStoreEditorInput; +import org.osgi.framework.Bundle; +@SuppressWarnings("restriction") public class ResourceUtils { static public ISelection getResourceSelection(IWorkbenchPart part) { @@ -49,60 +53,52 @@ static public ISelection getResourceSelection(IWorkbenchPart part) { return selection; } - static public Resource getResource(Object myObj) { + static public Resource getResource(Object myObj) { Object object = null; + + // handle IEditorPart if (myObj instanceof IEditorPart) { IEditorPart editorPart = (IEditorPart)myObj; IEditorInput input = editorPart.getEditorInput(); - Object adapter = input.getAdapter(IFile.class); - if(adapter instanceof IFile){ - object = (IFile) adapter; - } else { - adapter = editorPart.getAdapter(IFile.class); - if(adapter instanceof IFile){ - object = (IFile) adapter; - } else { - object = input; - } - } + if (input instanceof IFileEditorInput) { + object = ((IFileEditorInput)input).getFile(); + } else { + Object adapter = input.getAdapter(IFile.class); + if(adapter instanceof IFile){ + object = (IFile) adapter; + } else { + adapter = editorPart.getAdapter(IFile.class); + if(adapter instanceof IFile){ + object = (IFile) adapter; + } else { + object = input; + } + } + } } else { object = myObj; } - /* - IEditorInput input = activeEditorPart.getEditorInput(); - if (input instanceof IFileEditorInput) { - ifile = ((IFileEditorInput)input).getFile(); - } else if (input instanceof IAdaptable) { - IAdaptable adaptable = (IAdaptable) input; - ifile = (IFile) adaptable.getAdapter(IFile.class); - if (ifile == null) { - if (adaptable instanceof FileStoreEditorInput) { - URI fileuri = ((FileStoreEditorInput) adaptable).getURI(); - file = new File(fileuri.getPath()); - } else { - file = (File) adaptable.getAdapter(File.class); - } - } - } - */ - + // the most input types first if (object instanceof Resource) { return new Resource((Resource)object); } - if (object instanceof IFile) { return new Resource(((IFile) object)); } if (object instanceof File) { return new Resource((File) object); } + + // still adaptable if (object instanceof IAdaptable) { IAdaptable adaptable = (IAdaptable) object; + // IFile IFile iFile = (IFile) adaptable.getAdapter(IFile.class); if (iFile != null) { return new Resource(iFile); } + // IResource IResource ires = (IResource) adaptable.getAdapter(IResource.class); if (ires != null) { IPath path = ires.getLocation(); @@ -110,18 +106,23 @@ static public Resource getResource(Object myObj) { return new Resource(ires); } } - /* - if (adaptable instanceof PackageFragment - && ((PackageFragment) adaptable).getPackageFragmentRoot() instanceof JarPackageFragmentRoot) { - return new Resource(getJarFile(((PackageFragment) adaptable) - .getPackageFragmentRoot()),projectName); - } else if (adaptable instanceof JarPackageFragmentRoot) { - return new Resource(getJarFile(adaptable),projectName); - }*/ - else if (adaptable instanceof FileStoreEditorInput) { + // FileStoreEditorInput + if (adaptable instanceof FileStoreEditorInput) { URI fileuri = ((FileStoreEditorInput) adaptable).getURI(); return new Resource(new File(fileuri.getPath())); } + // optional org.eclipse.jdt.core + Bundle bundle = Platform.getBundle("org.eclipse.jdt.core"); + if (bundle != null) { + if ( (adaptable instanceof PackageFragment) && + ( ((PackageFragment) adaptable).getPackageFragmentRoot() instanceof JarPackageFragmentRoot) ) { + return new Resource(getJarFile(((PackageFragment) adaptable) + .getPackageFragmentRoot())); + } else if (adaptable instanceof JarPackageFragmentRoot) { + return new Resource(getJarFile(adaptable)); + } + } + // File File file = (File) adaptable.getAdapter(File.class); if (file != null) { return new Resource(file); @@ -130,21 +131,14 @@ else if (adaptable instanceof FileStoreEditorInput) { return null; } - /* - static public File getJarFile(IAdaptable adaptable) { + static private File getJarFile(IAdaptable adaptable) { JarPackageFragmentRoot jpfr = (JarPackageFragmentRoot) adaptable; - File resource = (File) jpfr.getPath().makeAbsolute().toFile(); - if (!((File) resource).exists()) { - File projectFile = - new File( - jpfr - .getJavaProject() - .getProject() - .getLocation() - .toOSString()); - resource = new File(projectFile.getParent() + resource.toString()); + File file = (File)jpfr.getPath().makeAbsolute().toFile(); + if (!((File)file).exists()) { + File projectFile = new File(jpfr.getJavaProject().getProject().getLocation().toOSString()); + file = new File(projectFile.getParent() + file.toString()); } - return resource; + return file; } - */ + }