001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide.dialogs;
011:
012: import com.ibm.icu.text.DateFormat;
013: import com.ibm.icu.text.MessageFormat;
014: import com.ibm.icu.text.NumberFormat;
015:
016: import java.net.URI;
017: import java.util.ArrayList;
018: import java.util.Date;
019: import org.eclipse.core.filesystem.EFS;
020: import org.eclipse.core.filesystem.IFileInfo;
021: import org.eclipse.core.filesystem.IFileStore;
022: import org.eclipse.core.resources.IFile;
023: import org.eclipse.core.resources.IProject;
024: import org.eclipse.core.resources.IResource;
025: import org.eclipse.core.runtime.CoreException;
026: import org.eclipse.core.runtime.IPath;
027: import org.eclipse.core.runtime.IProgressMonitor;
028: import org.eclipse.core.runtime.Path;
029: import org.eclipse.core.runtime.content.IContentDescription;
030: import org.eclipse.core.runtime.content.IContentType;
031: import org.eclipse.osgi.util.NLS;
032: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
033: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
034:
035: /**
036: * Utility class supporting common information required from resources.
037: *
038: * @since 3.2
039: *
040: */
041: public class IDEResourceInfoUtils {
042:
043: private static String BYTES_LABEL = IDEWorkbenchMessages.ResourceInfo_bytes;
044:
045: /**
046: * An empty string to reuse.
047: */
048: public static final String EMPTY_STRING = ""; //$NON-NLS-1$
049:
050: private static String FILE_LABEL = IDEWorkbenchMessages.ResourceInfo_file;
051:
052: private static String FILE_NOT_EXIST_TEXT = IDEWorkbenchMessages.ResourceInfo_fileNotExist;
053:
054: private static String FILE_TYPE_FORMAT = IDEWorkbenchMessages.ResourceInfo_fileTypeFormat;
055:
056: private static String FOLDER_LABEL = IDEWorkbenchMessages.ResourceInfo_folder;
057:
058: private static String LINKED_FILE_LABEL = IDEWorkbenchMessages.ResourceInfo_linkedFile;
059:
060: private static String LINKED_FOLDER_LABEL = IDEWorkbenchMessages.ResourceInfo_linkedFolder;
061:
062: private static String MISSING_PATH_VARIABLE_TEXT = IDEWorkbenchMessages.ResourceInfo_undefinedPathVariable;
063:
064: private static String NOT_EXIST_TEXT = IDEWorkbenchMessages.ResourceInfo_notExist;
065:
066: private static String NOT_LOCAL_TEXT = IDEWorkbenchMessages.ResourceInfo_notLocal;
067:
068: private static String PROJECT_LABEL = IDEWorkbenchMessages.ResourceInfo_project;
069:
070: private static String UNKNOWN_LABEL = IDEWorkbenchMessages.ResourceInfo_unknown;
071:
072: /**
073: * Return whether or not the file called pathName exists.
074: *
075: * @param pathName
076: * @return boolean <code>true</code> if the file exists.
077: * @see IFileInfo#exists()
078: */
079: public static boolean exists(String pathName) {
080: IFileInfo info = getFileInfo(pathName);
081: if (info == null) {
082: return false;
083: }
084: return info.exists();
085: }
086:
087: private static String getContentTypeString(
088: IContentDescription description) {
089: if (description != null) {
090: IContentType contentType = description.getContentType();
091: if (contentType != null) {
092: return contentType.getName();
093: }
094: }
095: return null;
096: }
097:
098: /**
099: * Return the value for the date String for the timestamp of the supplied
100: * resource.
101: *
102: * @param resource
103: * The resource to query
104: * @return String
105: */
106: public static String getDateStringValue(IResource resource) {
107: if (!resource.isLocal(IResource.DEPTH_ZERO)) {
108: return NOT_LOCAL_TEXT;
109: }
110:
111: // don't access the file system for closed projects (bug 151089)
112: if (!isProjectAccessible(resource)) {
113: return UNKNOWN_LABEL;
114: }
115:
116: URI location = resource.getLocationURI();
117: if (location == null) {
118: if (resource.isLinked()) {
119: return MISSING_PATH_VARIABLE_TEXT;
120: }
121: return NOT_EXIST_TEXT;
122: }
123:
124: IFileInfo info = getFileInfo(location);
125: if (info == null) {
126: return UNKNOWN_LABEL;
127: }
128:
129: if (info.exists()) {
130: DateFormat format = DateFormat.getDateTimeInstance(
131: DateFormat.LONG, DateFormat.MEDIUM);
132: return format.format(new Date(info.getLastModified()));
133: }
134: return NOT_EXIST_TEXT;
135: }
136:
137: /**
138: * Return the fileInfo at pathName or <code>null</code> if the format is
139: * invalid or if the file info cannot be determined.
140: *
141: * @param pathName
142: * @return IFileInfo or <code>null</code>
143: */
144: public static IFileInfo getFileInfo(IPath pathName) {
145: IFileStore store = getFileStore(pathName.toFile().toURI());
146: if (store == null) {
147: return null;
148: }
149: return store.fetchInfo();
150: }
151:
152: /**
153: * Return the fileInfo at pathName or <code>null</code> if the format is
154: * invalid or if the file info cannot be determined.
155: *
156: * @param pathName
157: * @return IFileInfo or <code>null</code>
158: */
159: public static IFileInfo getFileInfo(String pathName) {
160: IFileStore store = getFileStore(pathName);
161: if (store == null) {
162: return null;
163: }
164: return store.fetchInfo();
165: }
166:
167: /**
168: * Return the fileInfo for location. Return <code>null</code> if there is
169: * a CoreException looking it up
170: *
171: * @param location
172: * @return String or <code>null</code>
173: */
174: public static IFileInfo getFileInfo(URI location) {
175: IFileStore store = getFileStore(location);
176: if (store == null) {
177: return null;
178: }
179: return store.fetchInfo();
180: }
181:
182: /**
183: * Get the file store for the string.
184: *
185: * @param string
186: * @return IFileStore or <code>null</code> if there is a
187: * {@link CoreException}.
188: */
189: public static IFileStore getFileStore(String string) {
190: return getFileStore(new Path(string).toFile().toURI());
191: }
192:
193: /**
194: * Get the file store for the URI.
195: *
196: * @param uri
197: * @return IFileStore or <code>null</code> if there is a
198: * {@link CoreException}.
199: */
200: public static IFileStore getFileStore(URI uri) {
201: try {
202: return EFS.getStore(uri);
203: } catch (CoreException e) {
204: log(e);
205: return null;
206: }
207: }
208:
209: /**
210: * Get the location of a resource
211: *
212: * @param resource
213: * @return String the text to display the location
214: */
215: public static String getLocationText(IResource resource) {
216: if (!resource.isLocal(IResource.DEPTH_ZERO)) {
217: return NOT_LOCAL_TEXT;
218: }
219:
220: URI resolvedLocation = resource.getLocationURI();
221: URI location = resolvedLocation;
222: if (resource.isLinked()) {
223: location = resource.getRawLocationURI();
224: }
225: if (location == null) {
226: return NOT_EXIST_TEXT;
227: }
228:
229: IFileStore store = getFileStore(location);
230: // don't access the file system for closed projects (bug 151089)
231: if (isProjectAccessible(resource) && resolvedLocation != null
232: && !isPathVariable(resource)) {
233: // No path variable used. Display the file not exist message
234: // in the location. Fixes bug 33318.
235: if (store == null) {
236: return UNKNOWN_LABEL;
237: }
238: if (!store.fetchInfo().exists()) {
239: return NLS.bind(FILE_NOT_EXIST_TEXT, store.toString());
240: }
241: }
242: if (store != null) {
243: return store.toString();
244: }
245: return location.toString();
246: }
247:
248: /**
249: * Get the resolved location of a resource. This resolves path variables if
250: * present in the resource path.
251: *
252: * @param resource
253: * @return String
254: */
255: public static String getResolvedLocationText(IResource resource) {
256: if (!resource.isLocal(IResource.DEPTH_ZERO)) {
257: return NOT_LOCAL_TEXT;
258: }
259:
260: URI location = resource.getLocationURI();
261: if (location == null) {
262: if (resource.isLinked()) {
263: return MISSING_PATH_VARIABLE_TEXT;
264: }
265:
266: return NOT_EXIST_TEXT;
267: }
268:
269: IFileStore store = getFileStore(location);
270: if (store == null) {
271: return UNKNOWN_LABEL;
272: }
273:
274: // don't access the file system for closed projects (bug 151089)
275: if (isProjectAccessible(resource)
276: && !store.fetchInfo().exists()) {
277: return NLS.bind(FILE_NOT_EXIST_TEXT, store.toString());
278: }
279:
280: return store.toString();
281: }
282:
283: /**
284: * Return a String that indicates the size of the supplied file.
285: *
286: * @param resource
287: * @return String
288: */
289: public static String getSizeString(IResource resource) {
290: if (resource.getType() != IResource.FILE) {
291: return ""; //$NON-NLS-1$
292: }
293:
294: IFile file = (IFile) resource;
295: if (!file.isLocal(IResource.DEPTH_ZERO)) {
296: return NOT_LOCAL_TEXT;
297: }
298:
299: URI location = file.getLocationURI();
300: if (location == null) {
301: if (file.isLinked()) {
302: return MISSING_PATH_VARIABLE_TEXT;
303: }
304:
305: return NOT_EXIST_TEXT;
306: }
307:
308: IFileInfo info = getFileInfo(location);
309: if (info == null) {
310: return UNKNOWN_LABEL;
311: }
312:
313: if (info.exists()) {
314: return NLS.bind(BYTES_LABEL, NumberFormat.getInstance()
315: .format(new Long(info.getLength())));
316: }
317:
318: return NOT_EXIST_TEXT;
319: }
320:
321: /**
322: * Get the string that identifies the type of this resource.
323: *
324: * @param resource
325: * @param description
326: * @return String
327: */
328: public static String getTypeString(IResource resource,
329: IContentDescription description) {
330:
331: if (resource.getType() == IResource.FILE) {
332: if (resource.isLinked()) {
333: return LINKED_FILE_LABEL;
334: }
335:
336: if (resource instanceof IFile) {
337: String contentType = getContentTypeString(description);
338: if (contentType != null) {
339: return MessageFormat.format(FILE_TYPE_FORMAT,
340: new String[] { contentType });
341: }
342: }
343: return FILE_LABEL;
344: }
345:
346: if (resource.getType() == IResource.FOLDER) {
347: if (resource.isLinked()) {
348: return LINKED_FOLDER_LABEL;
349: }
350:
351: return FOLDER_LABEL;
352: }
353:
354: if (resource.getType() == IResource.PROJECT) {
355: return PROJECT_LABEL;
356: }
357:
358: // Should not be possible
359: return UNKNOWN_LABEL;
360: }
361:
362: /**
363: * Returns whether the given resource is a linked resource bound to a path
364: * variable.
365: *
366: * @param resource
367: * resource to test
368: * @return boolean <code>true</code> the given resource is a linked
369: * resource bound to a path variable. <code>false</code> the given
370: * resource is either not a linked resource or it is not using a
371: * path variable.
372: */
373: private static boolean isPathVariable(IResource resource) {
374: if (!resource.isLinked()) {
375: return false;
376: }
377:
378: URI resolvedLocation = resource.getLocationURI();
379: if (resolvedLocation == null) {
380: // missing path variable
381: return true;
382: }
383: URI rawLocation = resource.getRawLocationURI();
384: if (resolvedLocation.equals(rawLocation)) {
385: return false;
386: }
387:
388: return true;
389: }
390:
391: /**
392: * Returns whether the resource's project is available
393: */
394: private static boolean isProjectAccessible(IResource resource) {
395: IProject project = resource.getProject();
396: return project != null && project.isAccessible();
397: }
398:
399: /**
400: * Return the file stores that are a child of store that the filter accepts.
401: *
402: * @param store
403: * @param fileFilter
404: * @param monitor
405: * @return IFileStore[]
406: */
407: public static IFileStore[] listFileStores(IFileStore store,
408: IFileStoreFilter fileFilter, IProgressMonitor monitor) {
409: ArrayList result = new ArrayList();
410: IFileStore[] children;
411: try {
412: children = store.childStores(EFS.NONE, monitor);
413: } catch (CoreException e) {
414: log(e);
415: return new IFileStore[0];
416: }
417: for (int i = 0; i < children.length; i++) {
418: if (fileFilter.accept(children[i])) {
419: result.add(children[i]);
420: }
421: }
422: IFileStore[] stores = new IFileStore[result.size()];
423: result.toArray(stores);
424: return stores;
425: }
426:
427: private static void log(CoreException e) {
428: IDEWorkbenchPlugin.log(e.getMessage(), e.getStatus());
429: }
430:
431: }
|