1 11 package org.eclipse.ui.ide; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.resources.mapping.ResourceMapping; 16 import org.eclipse.core.resources.mapping.ResourceTraversal; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.ui.IEditorInput; 21 import org.eclipse.ui.IEditorPart; 22 import org.eclipse.ui.IEditorReference; 23 import org.eclipse.ui.IWorkbenchPage; 24 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 25 import org.eclipse.ui.part.FileEditorInput; 26 27 37 public final class ResourceUtil { 38 39 private ResourceUtil() { 40 } 42 43 51 public static IFile getFile(IEditorInput editorInput) { 52 if (editorInput == null) { 53 return null; 54 } 55 Object o = editorInput.getAdapter(IFile.class); 58 if (o instanceof IFile) { 59 return (IFile) o; 60 } 61 return null; 62 } 63 64 72 public static IResource getResource(IEditorInput editorInput) { 73 if (editorInput == null) { 74 return null; 75 } 76 Object o = editorInput.getAdapter(IResource.class); 79 if (o instanceof IResource) { 80 return (IResource) o; 81 } 82 return getFile(editorInput); 84 } 85 86 94 public static IEditorPart findEditor(IWorkbenchPage page, IFile file) { 95 IEditorPart editor = page.findEditor(new FileEditorInput(file)); 97 if (editor != null) { 98 return editor; 99 } 100 IEditorReference[] refs = page.getEditorReferences(); 103 for (int i = 0; i < refs.length; i++) { 104 IEditorReference ref = refs[i]; 105 IEditorPart part = ref.getEditor(false); 106 if (part != null) { 107 IFile editorFile = getFile(part.getEditorInput()); 108 if (editorFile != null && file.equals(editorFile)) { 109 return part; 110 } 111 } 112 } 113 return null; 114 } 115 116 124 public static IResource getResource(Object element) { 125 if (element == null) { 126 return null; 127 } 128 if (element instanceof IResource) { 129 return (IResource) element; 130 } 131 return (IResource) getAdapter(element, IResource.class, true); 132 } 133 134 142 public static IFile getFile(Object element) { 143 if (element == null) { 144 return null; 145 } 146 147 if (element instanceof IFile) { 149 return (IFile) element; 150 } 151 152 ResourceMapping mapping = getResourceMapping(element); 154 if (mapping != null) { 155 return getFileFromResourceMapping(mapping); 156 } 157 158 Object adapter = getAdapter(element, IFile.class, true); 160 if (adapter instanceof IFile) { 161 return (IFile) adapter; 162 } 163 164 adapter = getAdapter(element, IResource.class, true); 166 if (adapter instanceof IFile) { 167 return (IFile) adapter; 168 } 169 return null; 170 } 171 172 180 public static ResourceMapping getResourceMapping(Object element) { 181 if (element == null) { 182 return null; 183 } 184 185 if (element instanceof ResourceMapping) { 187 return (ResourceMapping) element; 188 } 189 190 Object adapter = getAdapter(element, ResourceMapping.class, true); 192 if (adapter instanceof ResourceMapping) { 193 return (ResourceMapping) adapter; 194 } 195 return null; 196 } 197 198 206 private static IFile getFileFromResourceMapping(ResourceMapping mapping) { 207 IResource resource = getResourceFromResourceMapping(mapping); 208 if (resource instanceof IFile) { 209 return (IFile) resource; 210 } 211 return null; 212 } 213 214 222 private static IResource getResourceFromResourceMapping(ResourceMapping mapping) { 223 try { 224 ResourceTraversal[] traversals = mapping.getTraversals(null, null); 225 if (traversals.length != 1) { 226 return null; 227 } 228 ResourceTraversal traversal = traversals[0]; 229 IResource[] resources = traversal.getResources(); 231 if (resources.length != 1) { 232 return null; 233 } 234 return resources[0]; 235 } catch (CoreException e) { 236 IDEWorkbenchPlugin.log("Error in ResourceUtil.getFileFromResourceMapping", e); return null; 238 } 239 } 240 241 242 253 public static Object getAdapter(Object element, Class adapterType, boolean forceLoad) { 254 if (element instanceof IAdaptable) { 255 IAdaptable adaptable = (IAdaptable) element; 256 Object o = adaptable.getAdapter(adapterType); 257 if (o != null) { 258 return o; 259 } 260 } 261 if (forceLoad) { 262 return Platform.getAdapterManager().loadAdapter(element, adapterType.getName()); 263 } 264 return Platform.getAdapterManager().getAdapter(element, adapterType); 265 } 266 267 } 268 | Popular Tags |