1 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 ; 16 import java.util.ArrayList ; 17 import java.util.Date ; 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 40 public class IDEResourceInfoUtils { 41 42 private static String BYTES_LABEL = IDEWorkbenchMessages.ResourceInfo_bytes; 43 44 47 public static final String EMPTY_STRING = ""; 49 private static String FILE_LABEL = IDEWorkbenchMessages.ResourceInfo_file; 50 51 private static String FILE_NOT_EXIST_TEXT = IDEWorkbenchMessages.ResourceInfo_fileNotExist; 52 53 private static String FILE_TYPE_FORMAT = IDEWorkbenchMessages.ResourceInfo_fileTypeFormat; 54 55 private static String FOLDER_LABEL = IDEWorkbenchMessages.ResourceInfo_folder; 56 57 private static String LINKED_FILE_LABEL = IDEWorkbenchMessages.ResourceInfo_linkedFile; 58 59 private static String LINKED_FOLDER_LABEL = IDEWorkbenchMessages.ResourceInfo_linkedFolder; 60 61 private static String MISSING_PATH_VARIABLE_TEXT = IDEWorkbenchMessages.ResourceInfo_undefinedPathVariable; 62 63 private static String NOT_EXIST_TEXT = IDEWorkbenchMessages.ResourceInfo_notExist; 64 65 private static String NOT_LOCAL_TEXT = IDEWorkbenchMessages.ResourceInfo_notLocal; 66 67 private static String PROJECT_LABEL = IDEWorkbenchMessages.ResourceInfo_project; 68 69 private static String UNKNOWN_LABEL = IDEWorkbenchMessages.ResourceInfo_unknown; 70 71 77 public static boolean exists(String pathName) { 78 IFileInfo info = getFileInfo(pathName); 79 if (info == null) { 80 return false; 81 } 82 return info.exists(); 83 } 84 85 private static String 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 103 public static String getDateStringValue(IResource resource) { 104 if (!resource.isLocal(IResource.DEPTH_ZERO)) { 105 return NOT_LOCAL_TEXT; 106 } 107 108 if (!isProjectAccessible(resource)) { 110 return UNKNOWN_LABEL; 111 } 112 113 URI 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 (info.getLastModified())); 130 } 131 return NOT_EXIST_TEXT; 132 } 133 134 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 156 public static IFileInfo getFileInfo(String pathName) { 157 IFileStore store = getFileStore(pathName); 158 if (store == null) { 159 return null; 160 } 161 return store.fetchInfo(); 162 } 163 164 171 public static IFileInfo getFileInfo(URI location) { 172 IFileStore store = getFileStore(location); 173 if (store == null) { 174 return null; 175 } 176 return store.fetchInfo(); 177 } 178 179 185 public static IFileStore getFileStore(String string) { 186 return getFileStore(new Path(string).toFile().toURI()); 187 } 188 189 195 public static IFileStore getFileStore(URI uri) { 196 try { 197 return EFS.getStore(uri); 198 } catch (CoreException e) { 199 log(e); 200 return null; 201 } 202 } 203 204 210 public static String getLocationText(IResource resource) { 211 if (!resource.isLocal(IResource.DEPTH_ZERO)) { 212 return NOT_LOCAL_TEXT; 213 } 214 215 URI resolvedLocation = resource.getLocationURI(); 216 URI 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 if (isProjectAccessible(resource) && resolvedLocation != null 227 && !isPathVariable(resource)) { 228 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 250 public static String getResolvedLocationText(IResource resource) { 251 if (!resource.isLocal(IResource.DEPTH_ZERO)) { 252 return NOT_LOCAL_TEXT; 253 } 254 255 URI 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 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 283 public static String getSizeString(IResource resource) { 284 if (resource.getType() != IResource.FILE) { 285 return ""; } 287 288 IFile file = (IFile) resource; 289 if (!file.isLocal(IResource.DEPTH_ZERO)) { 290 return NOT_LOCAL_TEXT; 291 } 292 293 URI 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 321 public static String 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 contentType = getContentTypeString(description); 331 if (contentType != null) { 332 return MessageFormat.format(FILE_TYPE_FORMAT, 333 new String [] { 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 return UNKNOWN_LABEL; 353 } 354 355 366 private static boolean isPathVariable(IResource resource) { 367 if (!resource.isLinked()) { 368 return false; 369 } 370 371 URI resolvedLocation = resource.getLocationURI(); 372 if (resolvedLocation == null) { 373 return true; 375 } 376 URI rawLocation = resource.getRawLocationURI(); 377 if (resolvedLocation.equals(rawLocation)) { 378 return false; 379 } 380 381 return true; 382 } 383 384 387 private static boolean isProjectAccessible(IResource resource) { 388 IProject project = resource.getProject(); 389 return project != null && project.isAccessible(); 390 } 391 392 400 public static IFileStore[] listFileStores(IFileStore store, 401 IFileStoreFilter fileFilter, IProgressMonitor monitor) { 402 ArrayList result = new ArrayList (); 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 |