1 /* 2 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.awt.shell; 27 28 import javax.swing.*; 29 import java.awt.Image; 30 import java.awt.Toolkit; 31 import java.io.*; 32 import java.io.FileNotFoundException; 33 import java.nio.file.Files; 34 import java.nio.file.LinkOption; 35 import java.nio.file.Paths; 36 import java.util.*; 37 import java.util.concurrent.Callable; 38 39 /** 40 * @author Michael Martak 41 * @since 1.4 42 */ 43 @SuppressWarnings("serial") // JDK-implementation class 44 public abstract class ShellFolder extends File { 45 public static final String COLUMN_NAME = "FileChooser.fileNameHeaderText"; 46 public static final String COLUMN_SIZE = "FileChooser.fileSizeHeaderText"; 47 public static final String COLUMN_DATE = "FileChooser.fileDateHeaderText"; 48 49 protected ShellFolder parent; 50 51 /** 52 * Create a file system shell folder from a file 53 */ 54 ShellFolder(ShellFolder parent, String pathname) { 55 super((pathname != null) ? pathname : "ShellFolder"); 56 this.parent = parent; 57 } 58 59 /** 60 * @return Whether this is a file system shell folder 61 */ 62 public boolean isFileSystem() { 63 return (!getPath().startsWith("ShellFolder")); 64 } 65 66 /** 67 * This method must be implemented to make sure that no instances 68 * of {@code ShellFolder} are ever serialized. If {@code isFileSystem()} returns 69 * {@code true}, then the object should be representable with an instance of 70 * {@code java.io.File} instead. If not, then the object is most likely 71 * depending on some internal (native) state and cannot be serialized. 72 * 73 * @return a java.io.File replacement object, or null 74 * if no suitable replacement can be found. 75 */ 76 protected abstract Object writeReplace() throws java.io.ObjectStreamException; 77 78 /** 79 * Returns the path for this object's parent, 80 * or {@code null} if this object does not name a parent 81 * folder. 82 * 83 * @return the path as a String for this object's parent, 84 * or {@code null} if this object does not name a parent 85 * folder 86 * 87 * @see java.io.File#getParent() 88 * @since 1.4 89 */ 90 public String getParent() { 91 if (parent == null && isFileSystem()) { 92 return super.getParent(); 93 } 94 if (parent != null) { 95 return (parent.getPath()); 96 } else { 97 return null; 98 } 99 } 100 101 /** 102 * Returns a File object representing this object's parent, 103 * or {@code null} if this object does not name a parent 104 * folder. 105 * 106 * @return a File object representing this object's parent, 107 * or {@code null} if this object does not name a parent 108 * folder 109 * 110 * @see java.io.File#getParentFile() 111 * @since 1.4 112 */ 113 public File getParentFile() { 114 if (parent != null) { 115 return parent; 116 } else if (isFileSystem()) { 117 return super.getParentFile(); 118 } else { 119 return null; 120 } 121 } 122 123 public File[] listFiles() { 124 return listFiles(true); 125 } 126 127 public File[] listFiles(boolean includeHiddenFiles) { 128 File[] files = super.listFiles(); 129 130 if (!includeHiddenFiles) { 131 Vector<File> v = new Vector<>(); 132 int nameCount = (files == null) ? 0 : files.length; 133 for (int i = 0; i < nameCount; i++) { 134 if (!files[i].isHidden()) { 135 v.addElement(files[i]); 136 } 137 } 138 files = v.toArray(new File[v.size()]); 139 } 140 141 return files; 142 } 143 144 145 /** 146 * @return Whether this shell folder is a link 147 */ 148 public abstract boolean isLink(); 149 150 /** 151 * @return The shell folder linked to by this shell folder, or null 152 * if this shell folder is not a link 153 */ 154 public abstract ShellFolder getLinkLocation() throws FileNotFoundException; 155 156 /** 157 * @return The name used to display this shell folder 158 */ 159 public abstract String getDisplayName(); 160 161 /** 162 * @return The type of shell folder as a string 163 */ 164 public abstract String getFolderType(); 165 166 /** 167 * @return The executable type as a string 168 */ 169 public abstract String getExecutableType(); 170 171 /** 172 * Compares this ShellFolder with the specified ShellFolder for order. 173 * 174 * @see #compareTo(Object) 175 */ 176 public int compareTo(File file2) { 177 if (file2 == null || !(file2 instanceof ShellFolder) 178 || ((file2 instanceof ShellFolder) && ((ShellFolder)file2).isFileSystem())) { 179 180 if (isFileSystem()) { 181 return super.compareTo(file2); 182 } else { 183 return -1; 184 } 185 } else { 186 if (isFileSystem()) { 187 return 1; 188 } else { 189 return getName().compareTo(file2.getName()); 190 } 191 } 192 } 193 194 /** 195 * @param getLargeIcon whether to return large icon (ignored in base implementation) 196 * @return The icon used to display this shell folder 197 */ 198 public Image getIcon(boolean getLargeIcon) { 199 return null; 200 } 201 202 /** 203 * @param size size of the icon > 0 204 * @return The icon used to display this shell folder 205 */ 206 public Image getIcon(int size) { 207 return null; 208 } 209 210 // Static 211 212 private static final ShellFolderManager shellFolderManager; 213 214 private static final Invoker invoker; 215 216 static { 217 String managerClassName = (String)Toolkit.getDefaultToolkit(). 218 getDesktopProperty("Shell.shellFolderManager"); 219 Class<?> managerClass = null; 220 try { 221 managerClass = Class.forName(managerClassName, false, null); 222 if (!ShellFolderManager.class.isAssignableFrom(managerClass)) { 223 managerClass = null; 224 } 225 // swallow the exceptions below and use default shell folder 226 } catch(ClassNotFoundException e) { 227 } catch(NullPointerException e) { 228 } catch(SecurityException e) { 229 } 230 231 if (managerClass == null) { 232 managerClass = ShellFolderManager.class; 233 } 234 try { 235 shellFolderManager = 236 (ShellFolderManager)managerClass.getDeclaredConstructor().newInstance(); 237 } catch (ReflectiveOperationException e) { 238 throw new Error("Could not instantiate Shell Folder Manager: " 239 + managerClass.getName()); 240 } 241 242 invoker = shellFolderManager.createInvoker(); 243 } 244 245 /** 246 * Return a shell folder from a file object 247 * @exception FileNotFoundException if file does not exist 248 */ 249 public static ShellFolder getShellFolder(File file) throws FileNotFoundException { 250 if (file instanceof ShellFolder) { 251 return (ShellFolder)file; 252 } 253 254 if (!Files.exists(Paths.get(file.getPath()), LinkOption.NOFOLLOW_LINKS)) { 255 throw new FileNotFoundException(); 256 } 257 return shellFolderManager.createShellFolder(file); 258 } 259 260 /** 261 * @param key a {@code String} 262 * @return An Object matching the string {@code key}. 263 * @see ShellFolderManager#get(String) 264 */ 265 public static Object get(String key) { 266 return shellFolderManager.get(key); 267 } 268 269 /** 270 * Does {@code dir} represent a "computer" such as a node on the network, or 271 * "My Computer" on the desktop. 272 */ 273 public static boolean isComputerNode(File dir) { 274 return shellFolderManager.isComputerNode(dir); 275 } 276 277 /** 278 * @return Whether this is a file system root directory 279 */ 280 public static boolean isFileSystemRoot(File dir) { 281 return shellFolderManager.isFileSystemRoot(dir); 282 } 283 284 /** 285 * Canonicalizes files that don't have symbolic links in their path. 286 * Normalizes files that do, preserving symbolic links from being resolved. 287 */ 288 public static File getNormalizedFile(File f) throws IOException { 289 File canonical = f.getCanonicalFile(); 290 if (f.equals(canonical)) { 291 // path of f doesn't contain symbolic links 292 return canonical; 293 } 294 295 // preserve symbolic links from being resolved 296 return new File(f.toURI().normalize()); 297 } 298 299 // Override File methods 300 301 public static void sort(final List<? extends File> files) { 302 if (files == null || files.size() <= 1) { 303 return; 304 } 305 306 // To avoid loads of synchronizations with Invoker and improve performance we 307 // synchronize the whole code of the sort method once 308 invoke(new Callable<Void>() { 309 public Void call() { 310 // Check that we can use the ShellFolder.sortChildren() method: 311 // 1. All files have the same non-null parent 312 // 2. All files is ShellFolders 313 File commonParent = null; 314 315 for (File file : files) { 316 File parent = file.getParentFile(); 317 318 if (parent == null || !(file instanceof ShellFolder)) { 319 commonParent = null; 320 321 break; 322 } 323 324 if (commonParent == null) { 325 commonParent = parent; 326 } else { 327 if (commonParent != parent && !commonParent.equals(parent)) { 328 commonParent = null; 329 330 break; 331 } 332 } 333 } 334 335 if (commonParent instanceof ShellFolder) { 336 ((ShellFolder) commonParent).sortChildren(files); 337 } else { 338 Collections.sort(files, FILE_COMPARATOR); 339 } 340 341 return null; 342 } 343 }); 344 } 345 346 public void sortChildren(final List<? extends File> files) { 347 // To avoid loads of synchronizations with Invoker and improve performance we 348 // synchronize the whole code of the sort method once 349 invoke(new Callable<Void>() { 350 public Void call() { 351 Collections.sort(files, FILE_COMPARATOR); 352 353 return null; 354 } 355 }); 356 } 357 358 public boolean isAbsolute() { 359 return (!isFileSystem() || super.isAbsolute()); 360 } 361 362 public File getAbsoluteFile() { 363 return (isFileSystem() ? super.getAbsoluteFile() : this); 364 } 365 366 public boolean canRead() { 367 return (isFileSystem() ? super.canRead() : true); // ((Fix?)) 368 } 369 370 /** 371 * Returns true if folder allows creation of children. 372 * True for the "Desktop" folder, but false for the "My Computer" 373 * folder. 374 */ 375 public boolean canWrite() { 376 return (isFileSystem() ? super.canWrite() : false); // ((Fix?)) 377 } 378 379 public boolean exists() { 380 // Assume top-level drives exist, because state is uncertain for 381 // removable drives. 382 return (!isFileSystem() || isFileSystemRoot(this) || super.exists()) ; 383 } 384 385 public boolean isDirectory() { 386 return (isFileSystem() ? super.isDirectory() : true); // ((Fix?)) 387 } 388 389 public boolean isFile() { 390 return (isFileSystem() ? super.isFile() : !isDirectory()); // ((Fix?)) 391 } 392 393 public long lastModified() { 394 return (isFileSystem() ? super.lastModified() : 0L); // ((Fix?)) 395 } 396 397 public long length() { 398 return (isFileSystem() ? super.length() : 0L); // ((Fix?)) 399 } 400 401 public boolean createNewFile() throws IOException { 402 return (isFileSystem() ? super.createNewFile() : false); 403 } 404 405 public boolean delete() { 406 return (isFileSystem() ? super.delete() : false); // ((Fix?)) 407 } 408 409 public void deleteOnExit() { 410 if (isFileSystem()) { 411 super.deleteOnExit(); 412 } else { 413 // Do nothing // ((Fix?)) 414 } 415 } 416 417 public boolean mkdir() { 418 return (isFileSystem() ? super.mkdir() : false); 419 } 420 421 public boolean mkdirs() { 422 return (isFileSystem() ? super.mkdirs() : false); 423 } 424 425 public boolean renameTo(File dest) { 426 return (isFileSystem() ? super.renameTo(dest) : false); // ((Fix?)) 427 } 428 429 public boolean setLastModified(long time) { 430 return (isFileSystem() ? super.setLastModified(time) : false); // ((Fix?)) 431 } 432 433 public boolean setReadOnly() { 434 return (isFileSystem() ? super.setReadOnly() : false); // ((Fix?)) 435 } 436 437 public String toString() { 438 return (isFileSystem() ? super.toString() : getDisplayName()); 439 } 440 441 public static ShellFolderColumnInfo[] getFolderColumns(File dir) { 442 ShellFolderColumnInfo[] columns = null; 443 444 if (dir instanceof ShellFolder) { 445 columns = ((ShellFolder) dir).getFolderColumns(); 446 } 447 448 if (columns == null) { 449 columns = new ShellFolderColumnInfo[]{ 450 new ShellFolderColumnInfo(COLUMN_NAME, 150, 451 SwingConstants.LEADING, true, null, 452 FILE_COMPARATOR), 453 new ShellFolderColumnInfo(COLUMN_SIZE, 75, 454 SwingConstants.RIGHT, true, null, 455 DEFAULT_COMPARATOR, true), 456 new ShellFolderColumnInfo(COLUMN_DATE, 130, 457 SwingConstants.LEADING, true, null, 458 DEFAULT_COMPARATOR, true) 459 }; 460 } 461 462 return columns; 463 } 464 465 public ShellFolderColumnInfo[] getFolderColumns() { 466 return null; 467 } 468 469 public static Object getFolderColumnValue(File file, int column) { 470 if (file instanceof ShellFolder) { 471 Object value = ((ShellFolder)file).getFolderColumnValue(column); 472 if (value != null) { 473 return value; 474 } 475 } 476 477 if (file == null || !file.exists()) { 478 return null; 479 } 480 481 switch (column) { 482 case 0: 483 // By default, file name will be rendered using getSystemDisplayName() 484 return file; 485 486 case 1: // size 487 return file.isDirectory() ? null : Long.valueOf(file.length()); 488 489 case 2: // date 490 if (isFileSystemRoot(file)) { 491 return null; 492 } 493 long time = file.lastModified(); 494 return (time == 0L) ? null : new Date(time); 495 496 default: 497 return null; 498 } 499 } 500 501 public Object getFolderColumnValue(int column) { 502 return null; 503 } 504 505 /** 506 * Invokes the {@code task} which doesn't throw checked exceptions 507 * from its {@code call} method. If invokation is interrupted then Thread.currentThread().isInterrupted() will 508 * be set and result will be {@code null} 509 */ 510 public static <T> T invoke(Callable<T> task) { 511 try { 512 return invoke(task, RuntimeException.class); 513 } catch (InterruptedException e) { 514 return null; 515 } 516 } 517 518 /** 519 * Invokes the {@code task} which throws checked exceptions from its {@code call} method. 520 * If invokation is interrupted then Thread.currentThread().isInterrupted() will 521 * be set and InterruptedException will be thrown as well. 522 */ 523 public static <T, E extends Throwable> T invoke(Callable<T> task, Class<E> exceptionClass) 524 throws InterruptedException, E { 525 try { 526 return invoker.invoke(task); 527 } catch (Exception e) { 528 if (e instanceof RuntimeException) { 529 // Rethrow unchecked exceptions 530 throw (RuntimeException) e; 531 } 532 533 if (e instanceof InterruptedException) { 534 // Set isInterrupted flag for current thread 535 Thread.currentThread().interrupt(); 536 537 // Rethrow InterruptedException 538 throw (InterruptedException) e; 539 } 540 541 if (exceptionClass.isInstance(e)) { 542 throw exceptionClass.cast(e); 543 } 544 545 throw new RuntimeException("Unexpected error", e); 546 } 547 } 548 549 /** 550 * Interface allowing to invoke tasks in different environments on different platforms. 551 */ 552 public static interface Invoker { 553 /** 554 * Invokes a callable task. 555 * 556 * @param task a task to invoke 557 * @throws Exception {@code InterruptedException} or an exception that was thrown from the {@code task} 558 * @return the result of {@code task}'s invokation 559 */ 560 <T> T invoke(Callable<T> task) throws Exception; 561 } 562 563 /** 564 * Provides a default comparator for the default column set 565 */ 566 private static final Comparator<Object> DEFAULT_COMPARATOR = new Comparator<Object>() { 567 public int compare(Object o1, Object o2) { 568 int gt; 569 570 if (o1 == null && o2 == null) { 571 gt = 0; 572 } else if (o1 != null && o2 == null) { 573 gt = 1; 574 } else if (o1 == null && o2 != null) { 575 gt = -1; 576 } else if (o1 instanceof Comparable) { 577 @SuppressWarnings("unchecked") 578 Comparable<Object> o = (Comparable<Object>) o1; 579 gt = o.compareTo(o2); 580 } else { 581 gt = 0; 582 } 583 584 return gt; 585 } 586 }; 587 588 private static final Comparator<File> FILE_COMPARATOR = new Comparator<File>() { 589 public int compare(File f1, File f2) { 590 ShellFolder sf1 = null; 591 ShellFolder sf2 = null; 592 593 if (f1 instanceof ShellFolder) { 594 sf1 = (ShellFolder) f1; 595 if (sf1.isFileSystem()) { 596 sf1 = null; 597 } 598 } 599 if (f2 instanceof ShellFolder) { 600 sf2 = (ShellFolder) f2; 601 if (sf2.isFileSystem()) { 602 sf2 = null; 603 } 604 } 605 606 if (sf1 != null && sf2 != null) { 607 return sf1.compareTo(sf2); 608 } else if (sf1 != null) { 609 // Non-file shellfolders sort before files 610 return -1; 611 } else if (sf2 != null) { 612 return 1; 613 } else { 614 String name1 = f1.getName(); 615 String name2 = f2.getName(); 616 617 // First ignore case when comparing 618 int diff = name1.compareToIgnoreCase(name2); 619 if (diff != 0) { 620 return diff; 621 } else { 622 // May differ in case (e.g. "mail" vs. "Mail") 623 // We need this test for consistent sorting 624 return name1.compareTo(name2); 625 } 626 } 627 } 628 }; 629 }