KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > ide > ResourceUtil


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
28  * Utility class for manipulating resources and determining correspondences
29  * between resources and workbench objects.
30  * <p>
31  * This class provides all its functionality via static methods.
32  * It is not intended to be instantiated or subclassed.
33  * </p>
34  *
35  * @since 3.1
36  */

37 public final class ResourceUtil {
38
39     private ResourceUtil() {
40         // prevent instantiation
41
}
42
43     /**
44      * Returns the file corresponding to the given editor input, or <code>null</code>
45      * if there is no applicable file.
46      * Returns <code>null</code> if the given editor input is <code>null</code>.
47      *
48      * @param editorInput the editor input, or <code>null</code>
49      * @return the file corresponding to the editor input, or <code>null</code>
50      */

51     public static IFile getFile(IEditorInput editorInput) {
52         if (editorInput == null) {
53             return null;
54         }
55         // Note: do not treat IFileEditorInput as a special case. Use the adapter mechanism instead.
56
// See Bug 87288 [IDE] [EditorMgmt] Should avoid explicit checks for [I]FileEditorInput
57
Object JavaDoc o = editorInput.getAdapter(IFile.class);
58         if (o instanceof IFile) {
59             return (IFile) o;
60         }
61         return null;
62     }
63
64     /**
65      * Returns the resource corresponding to the given editor input, or <code>null</code>
66      * if there is no applicable resource.
67      * Returns <code>null</code> if the given editor input is <code>null</code>.
68      *
69      * @param editorInput the editor input
70      * @return the file corresponding to the editor input, or <code>null</code>
71      */

72     public static IResource getResource(IEditorInput editorInput) {
73         if (editorInput == null) {
74             return null;
75         }
76         // Note: do not treat IFileEditorInput as a special case. Use the adapter mechanism instead.
77
// See Bug 87288 [IDE] [EditorMgmt] Should avoid explicit checks for [I]FileEditorInput
78
Object JavaDoc o = editorInput.getAdapter(IResource.class);
79         if (o instanceof IResource) {
80             return (IResource) o;
81         }
82         // the input may adapt to IFile but not IResource
83
return getFile(editorInput);
84     }
85
86     /**
87      * Returns the editor in the given page whose input represents the given file,
88      * or <code>null</code> if there is no such editor.
89      *
90      * @param page the workbench page
91      * @param file the file
92      * @return the matching editor, or <code>null</code>
93      */

94     public static IEditorPart findEditor(IWorkbenchPage page, IFile file) {
95         // handle the common case where the editor input is a FileEditorInput
96
IEditorPart editor = page.findEditor(new FileEditorInput(file));
97         if (editor != null) {
98             return editor;
99         }
100         // check for editors that have their own kind of input that adapts to IFile,
101
// being careful not to force loading of the editor
102
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     /**
117      * Returns the resource corresponding to the given model element, or <code>null</code>
118      * if there is no applicable resource.
119      *
120      * @param element the model element, or <code>null</code>
121      * @return the resource corresponding to the model element, or <code>null</code>
122      * @since 3.2
123      */

124     public static IResource getResource(Object JavaDoc 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     /**
135      * Returns the file corresponding to the given model element, or <code>null</code>
136      * if there is no applicable file.
137      *
138      * @param element the model element, or <code>null</code>
139      * @return the resource corresponding to the model element, or <code>null</code>
140      * @since 3.2
141      */

142     public static IFile getFile(Object JavaDoc element) {
143         if (element == null) {
144             return null;
145         }
146         
147         // try direct instanceof check
148
if (element instanceof IFile) {
149             return (IFile) element;
150         }
151         
152         // try for ResourceMapping
153
ResourceMapping mapping = getResourceMapping(element);
154         if (mapping != null) {
155             return getFileFromResourceMapping(mapping);
156         }
157         
158         // try for IFile adapter (before IResource adapter, since it's more specific)
159
Object JavaDoc adapter = getAdapter(element, IFile.class, true);
160         if (adapter instanceof IFile) {
161             return (IFile) adapter;
162         }
163         
164         // try for IResource adapter
165
adapter = getAdapter(element, IResource.class, true);
166         if (adapter instanceof IFile) {
167             return (IFile) adapter;
168         }
169         return null;
170     }
171
172     /**
173      * Returns the resource mapping corresponding to the given model element, or <code>null</code>
174      * if there is no applicable resource mapping.
175      *
176      * @param element the model element, or <code>null</code>
177      * @return the resource mapping corresponding to the model element, or <code>null</code>
178      * @since 3.2
179      */

180     public static ResourceMapping getResourceMapping(Object JavaDoc element) {
181         if (element == null) {
182             return null;
183         }
184         
185         // try direct instanceof check
186
if (element instanceof ResourceMapping) {
187             return (ResourceMapping) element;
188         }
189         
190         // try for ResourceMapping adapter
191
Object JavaDoc adapter = getAdapter(element, ResourceMapping.class, true);
192         if (adapter instanceof ResourceMapping) {
193             return (ResourceMapping) adapter;
194         }
195         return null;
196     }
197     
198     /**
199      * Tries to extra a single file from the given resource mapping.
200      * Returns the file if the mapping maps to a single file, or <code>null</code>
201      * if it maps to zero or multiple files.
202      *
203      * @param mapping the resource mapping
204      * @return the file, or <code>null</code>
205      */

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     /**
215      * Tries to extra a single resource from the given resource mapping.
216      * Returns the resource if the mapping maps to a single resource, or <code>null</code>
217      * if it maps to zero or multiple resources.
218      *
219      * @param mapping the resource mapping
220      * @return the resource, or <code>null</code>
221      */

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             // TODO: need to honour traversal flags
230
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); //$NON-NLS-1$
237
return null;
238         }
239     }
240
241
242     /**
243      * Returns the specified adapter for the given element, or <code>null</code>
244      * if no such adapter was found.
245      *
246      * @param element the model element
247      * @param adapterType the type of adapter to look up
248      * @param forceLoad <code>true</code> to force loading of the plug-in providing the adapter,
249      * <code>false</code> otherwise
250      * @return the adapter
251      * @since 3.2
252      */

253     public static Object JavaDoc getAdapter(Object JavaDoc element, Class JavaDoc adapterType, boolean forceLoad) {
254         if (element instanceof IAdaptable) {
255             IAdaptable adaptable = (IAdaptable) element;
256             Object JavaDoc 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