KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > IDEResourceInfoUtils


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.internal.ide.dialogs;
12
13 import com.ibm.icu.text.DateFormat;
14 import com.ibm.icu.text.MessageFormat;
15 import java.net.URI JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Date JavaDoc;
18 import org.eclipse.core.filesystem.EFS;
19 import org.eclipse.core.filesystem.IFileInfo;
20 import org.eclipse.core.filesystem.IFileStore;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.core.runtime.content.IContentDescription;
29 import org.eclipse.core.runtime.content.IContentType;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
32 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
33
34 /**
35  * Utility class supporting common information required from resources.
36  *
37  * @since 3.2
38  *
39  */

40 public class IDEResourceInfoUtils {
41
42     private static String JavaDoc BYTES_LABEL = IDEWorkbenchMessages.ResourceInfo_bytes;
43
44     /**
45      * An empty string to reuse.
46      */

47     public static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
48

49     private static String JavaDoc FILE_LABEL = IDEWorkbenchMessages.ResourceInfo_file;
50
51     private static String JavaDoc FILE_NOT_EXIST_TEXT = IDEWorkbenchMessages.ResourceInfo_fileNotExist;
52
53     private static String JavaDoc FILE_TYPE_FORMAT = IDEWorkbenchMessages.ResourceInfo_fileTypeFormat;
54
55     private static String JavaDoc FOLDER_LABEL = IDEWorkbenchMessages.ResourceInfo_folder;
56
57     private static String JavaDoc LINKED_FILE_LABEL = IDEWorkbenchMessages.ResourceInfo_linkedFile;
58
59     private static String JavaDoc LINKED_FOLDER_LABEL = IDEWorkbenchMessages.ResourceInfo_linkedFolder;
60
61     private static String JavaDoc MISSING_PATH_VARIABLE_TEXT = IDEWorkbenchMessages.ResourceInfo_undefinedPathVariable;
62
63     private static String JavaDoc NOT_EXIST_TEXT = IDEWorkbenchMessages.ResourceInfo_notExist;
64
65     private static String JavaDoc NOT_LOCAL_TEXT = IDEWorkbenchMessages.ResourceInfo_notLocal;
66
67     private static String JavaDoc PROJECT_LABEL = IDEWorkbenchMessages.ResourceInfo_project;
68
69     private static String JavaDoc UNKNOWN_LABEL = IDEWorkbenchMessages.ResourceInfo_unknown;
70
71     /**
72      * Return whether or not the file called pathName exists.
73      * @param pathName
74      * @return boolean <code>true</code> if the file exists.
75      * @see IFileInfo#exists()
76      */

77     public static boolean exists(String JavaDoc pathName) {
78         IFileInfo info = getFileInfo(pathName);
79         if (info == null) {
80             return false;
81         }
82         return info.exists();
83     }
84
85     private static String JavaDoc getContentTypeString(IContentDescription description) {
86         if (description != null) {
87             IContentType contentType = description.getContentType();
88             if (contentType != null) {
89                 return contentType.getName();
90             }
91         }
92         return null;
93     }
94
95     /**
96      * Return the value for the date String for the timestamp of the supplied
97      * resource.
98      *
99      * @param resource
100      * The resource to query
101      * @return String
102      */

103     public static String JavaDoc getDateStringValue(IResource resource) {
104         if (!resource.isLocal(IResource.DEPTH_ZERO)) {
105             return NOT_LOCAL_TEXT;
106         }
107
108         //don't access the file system for closed projects (bug 151089)
109
if (!isProjectAccessible(resource)) {
110             return UNKNOWN_LABEL;
111         }
112
113         URI JavaDoc location = resource.getLocationURI();
114         if (location == null) {
115             if (resource.isLinked()) {
116                 return MISSING_PATH_VARIABLE_TEXT;
117             }
118             return NOT_EXIST_TEXT;
119         }
120
121         IFileInfo info = getFileInfo(location);
122         if (info == null) {
123             return UNKNOWN_LABEL;
124         }
125
126         if (info.exists()) {
127             DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG,
128                     DateFormat.MEDIUM);
129             return format.format(new Date JavaDoc(info.getLastModified()));
130         }
131         return NOT_EXIST_TEXT;
132     }
133
134     /**
135      * Return the fileInfo at pathName or <code>null</code> if the format is
136      * invalid or if the file info cannot be determined.
137      *
138      * @param pathName
139      * @return IFileInfo or <code>null</code>
140      */

141     public static IFileInfo getFileInfo(IPath pathName) {
142         IFileStore store = getFileStore(pathName.toFile().toURI());
143         if (store == null) {
144             return null;
145         }
146         return store.fetchInfo();
147     }
148
149     /**
150      * Return the fileInfo at pathName or <code>null</code> if the format is
151      * invalid or if the file info cannot be determined.
152      *
153      * @param pathName
154      * @return IFileInfo or <code>null</code>
155      */

156     public static IFileInfo getFileInfo(String JavaDoc pathName) {
157         IFileStore store = getFileStore(pathName);
158         if (store == null) {
159             return null;
160         }
161         return store.fetchInfo();
162     }
163
164     /**
165      * Return the fileInfo for location. Return <code>null</code> if there is
166      * a CoreException looking it up
167      *
168      * @param location
169      * @return String or <code>null</code>
170      */

171     public static IFileInfo getFileInfo(URI JavaDoc location) {
172         IFileStore store = getFileStore(location);
173         if (store == null) {
174             return null;
175         }
176         return store.fetchInfo();
177     }
178
179     /**
180      * Get the file store for the string.
181      * @param string
182      * @return IFileStore or <code>null</code> if there is a
183      * {@link CoreException}.
184      */

185     public static IFileStore getFileStore(String JavaDoc string) {
186         return getFileStore(new Path(string).toFile().toURI());
187     }
188
189     /**
190      * Get the file store for the URI.
191      * @param uri
192      * @return IFileStore or <code>null</code> if there is a
193      * {@link CoreException}.
194      */

195     public static IFileStore getFileStore(URI JavaDoc uri) {
196         try {
197             return EFS.getStore(uri);
198         } catch (CoreException e) {
199             log(e);
200             return null;
201         }
202     }
203
204     /**
205      * Get the location of a resource
206      *
207      * @param resource
208      * @return String the text to display the location
209      */

210     public static String JavaDoc getLocationText(IResource resource) {
211         if (!resource.isLocal(IResource.DEPTH_ZERO)) {
212             return NOT_LOCAL_TEXT;
213         }
214
215         URI JavaDoc resolvedLocation = resource.getLocationURI();
216         URI JavaDoc location = resolvedLocation;
217         if (resource.isLinked()) {
218             location = resource.getRawLocationURI();
219         }
220         if (location == null) {
221             return NOT_EXIST_TEXT;
222         }
223
224         IFileStore store = getFileStore(location);
225         //don't access the file system for closed projects (bug 151089)
226
if (isProjectAccessible(resource) && resolvedLocation != null
227                 && !isPathVariable(resource)) {
228             // No path variable used. Display the file not exist message
229
// in the location. Fixes bug 33318.
230
if (store == null) {
231                 return UNKNOWN_LABEL;
232             }
233             if (!store.fetchInfo().exists()) {
234                 return NLS.bind(FILE_NOT_EXIST_TEXT, store.toString());
235             }
236         }
237         if (store != null) {
238             return store.toString();
239         }
240         return location.toString();
241     }
242
243     /**
244      * Get the resolved location of a resource. This resolves path variables if
245      * present in the resource path.
246      *
247      * @param resource
248      * @return String
249      */

250     public static String JavaDoc getResolvedLocationText(IResource resource) {
251         if (!resource.isLocal(IResource.DEPTH_ZERO)) {
252             return NOT_LOCAL_TEXT;
253         }
254
255         URI JavaDoc location = resource.getLocationURI();
256         if (location == null) {
257             if (resource.isLinked()) {
258                 return MISSING_PATH_VARIABLE_TEXT;
259             }
260
261             return NOT_EXIST_TEXT;
262         }
263
264         IFileStore store = getFileStore(location);
265         if (store == null) {
266             return UNKNOWN_LABEL;
267         }
268
269         //don't access the file system for closed projects (bug 151089)
270
if (isProjectAccessible(resource) && !store.fetchInfo().exists()) {
271             return NLS.bind(FILE_NOT_EXIST_TEXT, store.toString());
272         }
273
274         return store.toString();
275     }
276
277     /**
278      * Return a String that indicates the size of the supplied file.
279      *
280      * @param resource
281      * @return String
282      */

283     public static String JavaDoc getSizeString(IResource resource) {
284         if (resource.getType() != IResource.FILE) {
285             return ""; //$NON-NLS-1$
286
}
287
288         IFile file = (IFile) resource;
289         if (!file.isLocal(IResource.DEPTH_ZERO)) {
290             return NOT_LOCAL_TEXT;
291         }
292
293         URI JavaDoc location = file.getLocationURI();
294         if (location == null) {
295             if (file.isLinked()) {
296                 return MISSING_PATH_VARIABLE_TEXT;
297             }
298
299             return NOT_EXIST_TEXT;
300         }
301
302         IFileInfo info = getFileInfo(location);
303         if (info == null) {
304             return UNKNOWN_LABEL;
305         }
306
307         if (info.exists()) {
308             return NLS.bind(BYTES_LABEL, Long.toString(info.getLength()));
309         }
310
311         return NOT_EXIST_TEXT;
312     }
313
314     /**
315      * Get the string that identifies the type of this resource.
316      *
317      * @param resource
318      * @param description
319      * @return String
320      */

321     public static String JavaDoc getTypeString(IResource resource,
322             IContentDescription description) {
323
324         if (resource.getType() == IResource.FILE) {
325             if (resource.isLinked()) {
326                 return LINKED_FILE_LABEL;
327             }
328
329             if (resource instanceof IFile) {
330                 String JavaDoc contentType = getContentTypeString(description);
331                 if (contentType != null) {
332                     return MessageFormat.format(FILE_TYPE_FORMAT,
333                             new String JavaDoc[] { contentType });
334                 }
335             }
336             return FILE_LABEL;
337         }
338
339         if (resource.getType() == IResource.FOLDER) {
340             if (resource.isLinked()) {
341                 return LINKED_FOLDER_LABEL;
342             }
343
344             return FOLDER_LABEL;
345         }
346
347         if (resource.getType() == IResource.PROJECT) {
348             return PROJECT_LABEL;
349         }
350
351         // Should not be possible
352
return UNKNOWN_LABEL;
353     }
354
355     /**
356      * Returns whether the given resource is a linked resource bound to a path
357      * variable.
358      *
359      * @param resource
360      * resource to test
361      * @return boolean <code>true</code> the given resource is a linked
362      * resource bound to a path variable. <code>false</code> the given
363      * resource is either not a linked resource or it is not using a
364      * path variable.
365      */

366     private static boolean isPathVariable(IResource resource) {
367         if (!resource.isLinked()) {
368             return false;
369         }
370
371         URI JavaDoc resolvedLocation = resource.getLocationURI();
372         if (resolvedLocation == null) {
373             // missing path variable
374
return true;
375         }
376         URI JavaDoc rawLocation = resource.getRawLocationURI();
377         if (resolvedLocation.equals(rawLocation)) {
378             return false;
379         }
380
381         return true;
382     }
383
384     /**
385      * Returns whether the resource's project is available
386      */

387     private static boolean isProjectAccessible(IResource resource) {
388         IProject project = resource.getProject();
389         return project != null && project.isAccessible();
390     }
391
392     /**
393      * Return the file stores that are a child of store that the filter
394      * accepts.
395      * @param store
396      * @param fileFilter
397      * @param monitor
398      * @return IFileStore[]
399      */

400     public static IFileStore[] listFileStores(IFileStore store,
401             IFileStoreFilter fileFilter, IProgressMonitor monitor) {
402         ArrayList JavaDoc result = new ArrayList JavaDoc();
403         IFileStore[] children;
404         try {
405             children = store.childStores(EFS.NONE, monitor);
406         } catch (CoreException e) {
407             log(e);
408             return new IFileStore[0];
409         }
410         for (int i = 0; i < children.length; i++) {
411             if (fileFilter.accept(children[i])) {
412                 result.add(children[i]);
413             }
414         }
415         IFileStore[] stores = new IFileStore[result.size()];
416         result.toArray(stores);
417         return stores;
418     }
419
420     private static void log(CoreException e) {
421         IDEWorkbenchPlugin.log(e.getMessage(), e.getStatus());
422     }
423
424 }
425
Popular Tags