1 7 8 package javax.swing.filechooser; 9 10 11 import javax.swing.event.*; 12 import javax.swing.*; 13 14 import java.awt.Image ; 15 import java.io.File ; 16 import java.io.FileFilter ; 17 import java.io.FilenameFilter ; 18 import java.io.FileNotFoundException ; 19 import java.io.IOException ; 20 import java.text.MessageFormat ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.List ; 24 import java.util.Vector ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyChangeEvent ; 27 28 29 import java.lang.reflect.*; 30 31 import sun.awt.shell.*; 32 33 49 50 54 public abstract class FileSystemView { 55 56 static FileSystemView windowsFileSystemView = null; 57 static FileSystemView unixFileSystemView = null; 58 static FileSystemView genericFileSystemView = null; 60 static boolean useSystemExtensionsHiding = false; 61 62 public static FileSystemView getFileSystemView() { 63 useSystemExtensionsHiding = UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding"); 64 UIManager.addPropertyChangeListener(new PropertyChangeListener () { 65 public void propertyChange(PropertyChangeEvent e) { 66 if (e.getPropertyName().equals("lookAndFeel")) { 67 useSystemExtensionsHiding = UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding"); 68 } 69 } 70 }); 71 72 if(File.separatorChar == '\\') { 73 if(windowsFileSystemView == null) { 74 windowsFileSystemView = new WindowsFileSystemView(); 75 } 76 return windowsFileSystemView; 77 } 78 79 if(File.separatorChar == '/') { 80 if(unixFileSystemView == null) { 81 unixFileSystemView = new UnixFileSystemView(); 82 } 83 return unixFileSystemView; 84 } 85 86 93 if(genericFileSystemView == null) { 94 genericFileSystemView = new GenericFileSystemView(); 95 } 96 return genericFileSystemView; 97 } 98 99 111 public boolean isRoot(File f) { 112 if (f == null || !f.isAbsolute()) { 113 return false; 114 } 115 116 File [] roots = getRoots(); 117 for (int i = 0; i < roots.length; i++) { 118 if (roots[i].equals(f)) { 119 return true; 120 } 121 } 122 return false; 123 } 124 125 134 public Boolean isTraversable(File f) { 135 return Boolean.valueOf(f.isDirectory()); 136 } 137 138 149 public String getSystemDisplayName(File f) { 150 String name = null; 151 if (f != null) { 152 name = f.getName(); 153 if (!name.equals("..") && !name.equals(".") && 154 (useSystemExtensionsHiding || 155 !isFileSystem(f) || 156 isFileSystemRoot(f)) && 157 ((f instanceof ShellFolder) || 158 f.exists())) { 159 160 name = getShellFolder(f).getDisplayName(); 161 if (name == null || name.length() == 0) { 162 name = f.getPath(); } 164 } 165 } 166 return name; 167 } 168 169 181 public String getSystemTypeDescription(File f) { 182 return null; 183 } 184 185 196 public Icon getSystemIcon(File f) { 197 if (f != null) { 198 ShellFolder sf = getShellFolder(f); 199 Image img = sf.getIcon(false); 200 if (img != null) { 201 return new ImageIcon(img, sf.getFolderType()); 202 } else { 203 return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon"); 204 } 205 } else { 206 return null; 207 } 208 } 209 210 219 public boolean isParent(File folder, File file) { 220 if (folder == null || file == null) { 221 return false; 222 } else if (folder instanceof ShellFolder) { 223 File parent = file.getParentFile(); 224 if (parent != null && parent.equals(folder)) { 225 return true; 226 } 227 File [] children = getFiles(folder, false); 228 for (int i = 0; i < children.length; i++) { 229 if (file.equals(children[i])) { 230 return true; 231 } 232 } 233 return false; 234 } else { 235 return folder.equals(file.getParentFile()); 236 } 237 } 238 239 248 public File getChild(File parent, String fileName) { 249 if (parent instanceof ShellFolder) { 250 File [] children = getFiles(parent, false); 251 for (int i = 0; i < children.length; i++) { 252 if (children[i].getName().equals(fileName)) { 253 return children[i]; 254 } 255 } 256 } 257 return createFileObject(parent, fileName); 258 } 259 260 261 269 public boolean isFileSystem(File f) { 270 if (f instanceof ShellFolder) { 271 ShellFolder sf = (ShellFolder)f; 272 return sf.isFileSystem() && !(sf.isLink() && sf.isDirectory()); 275 } else { 276 return true; 277 } 278 } 279 280 283 public abstract File createNewFolder(File containingDir) throws IOException ; 284 285 288 public boolean isHiddenFile(File f) { 289 return f.isHidden(); 290 } 291 292 293 301 public boolean isFileSystemRoot(File dir) { 302 return ShellFolder.isFileSystemRoot(dir); 303 } 304 305 314 public boolean isDrive(File dir) { 315 return false; 316 } 317 318 327 public boolean isFloppyDrive(File dir) { 328 return false; 329 } 330 331 340 public boolean isComputerNode(File dir) { 341 return ShellFolder.isComputerNode(dir); 342 } 343 344 345 350 public File [] getRoots() { 351 File [] roots = (File [])ShellFolder.get("roots"); 353 354 for (int i = 0; i < roots.length; i++) { 355 if (isFileSystemRoot(roots[i])) { 356 roots[i] = createFileSystemRoot(roots[i]); 357 } 358 } 359 return roots; 360 } 361 362 363 368 public File getHomeDirectory() { 369 return createFileObject(System.getProperty("user.home")); 370 } 371 372 378 public File getDefaultDirectory() { 379 File f = (File )ShellFolder.get("fileChooserDefaultFolder"); 380 if (isFileSystemRoot(f)) { 381 f = createFileSystemRoot(f); 382 } 383 return f; 384 } 385 386 389 public File createFileObject(File dir, String filename) { 390 if(dir == null) { 391 return new File (filename); 392 } else { 393 return new File (dir, filename); 394 } 395 } 396 397 400 public File createFileObject(String path) { 401 File f = new File (path); 402 if (isFileSystemRoot(f)) { 403 f = createFileSystemRoot(f); 404 } 405 return f; 406 } 407 408 409 412 public File [] getFiles(File dir, boolean useFileHiding) { 413 Vector files = new Vector (); 414 415 416 File [] names; 418 if (!(dir instanceof ShellFolder)) { 419 dir = getShellFolder(dir); 420 } 421 422 names = ((ShellFolder)dir).listFiles(!useFileHiding); 423 File f; 424 425 int nameCount = (names == null) ? 0 : names.length; 426 for (int i = 0; i < nameCount; i++) { 427 if (Thread.currentThread().isInterrupted()) { 428 break; 429 } 430 f = names[i]; 431 if (!(f instanceof ShellFolder)) { 432 if (isFileSystemRoot(f)) { 433 f = createFileSystemRoot(f); 434 } 435 try { 436 f = ShellFolder.getShellFolder(f); 437 } catch (FileNotFoundException e) { 438 continue; 441 } catch (InternalError e) { 442 continue; 445 } 446 } 447 if (!useFileHiding || !isHiddenFile(f)) { 448 files.addElement(f); 449 } 450 } 451 452 return (File [])files.toArray(new File [files.size()]); 453 } 454 455 456 457 463 public File getParentDirectory(File dir) { 464 if (dir != null && dir.exists()) { 465 ShellFolder sf = getShellFolder(dir); 466 File psf = sf.getParentFile(); 467 if (psf != null) { 468 if (isFileSystem(psf)) { 469 File f = psf; 470 if (f != null && !f.exists()) { 471 File ppsf = psf.getParentFile(); 473 if (ppsf == null || !isFileSystem(ppsf)) { 474 f = createFileSystemRoot(f); 476 } 477 } 478 return f; 479 } else { 480 return psf; 481 } 482 } 483 } 484 return null; 485 } 486 487 ShellFolder getShellFolder(File f) { 488 if (!(f instanceof ShellFolder) 489 && !(f instanceof FileSystemRoot) 490 && isFileSystemRoot(f)) { 491 492 f = createFileSystemRoot(f); 493 } 494 try { 495 return ShellFolder.getShellFolder(f); 496 } catch (FileNotFoundException e) { 497 System.err.println("FileSystemView.getShellFolder: f="+f); 498 e.printStackTrace(); 499 return null; 500 } catch (InternalError e) { 501 System.err.println("FileSystemView.getShellFolder: f="+f); 502 e.printStackTrace(); 503 return null; 504 } 505 } 506 507 515 protected File createFileSystemRoot(File f) { 516 return new FileSystemRoot(f); 517 } 518 519 520 521 522 static class FileSystemRoot extends File { 523 public FileSystemRoot(File f) { 524 super(f,""); 525 } 526 527 public FileSystemRoot(String s) { 528 super(s); 529 } 530 531 public boolean isDirectory() { 532 return true; 533 } 534 535 public String getName() { 536 return getPath(); 537 } 538 } 539 } 540 541 544 class UnixFileSystemView extends FileSystemView { 545 546 private static final String newFolderString = 547 UIManager.getString("FileChooser.other.newFolder"); 548 private static final String newFolderNextString = 549 UIManager.getString("FileChooser.other.newFolder.subsequent"); 550 551 554 public File createNewFolder(File containingDir) throws IOException { 555 if(containingDir == null) { 556 throw new IOException ("Containing directory is null:"); 557 } 558 File newFolder = null; 559 newFolder = createFileObject(containingDir, newFolderString); 561 int i = 1; 562 while (newFolder.exists() && (i < 100)) { 563 newFolder = createFileObject(containingDir, MessageFormat.format( 564 newFolderNextString, new Object [] { new Integer (i) })); 565 i++; 566 } 567 568 if(newFolder.exists()) { 569 throw new IOException ("Directory already exists:" + newFolder.getAbsolutePath()); 570 } else { 571 newFolder.mkdirs(); 572 } 573 574 return newFolder; 575 } 576 577 public boolean isFileSystemRoot(File dir) { 578 return (dir != null && dir.getAbsolutePath().equals("/")); 579 } 580 581 public boolean isDrive(File dir) { 582 if (isFloppyDrive(dir)) { 583 return true; 584 } else { 585 return false; 586 } 587 } 588 589 public boolean isFloppyDrive(File dir) { 590 return false; 594 } 595 596 public boolean isComputerNode(File dir) { 597 if (dir != null) { 598 String parent = dir.getParent(); 599 if (parent != null && parent.equals("/net")) { 600 return true; 601 } 602 } 603 return false; 604 } 605 } 606 607 608 611 class WindowsFileSystemView extends FileSystemView { 612 613 private static final String newFolderString = 614 UIManager.getString("FileChooser.win32.newFolder"); 615 private static final String newFolderNextString = 616 UIManager.getString("FileChooser.win32.newFolder.subsequent"); 617 618 public Boolean isTraversable(File f) { 619 return Boolean.valueOf(isFileSystemRoot(f) || isComputerNode(f) || f.isDirectory()); 620 } 621 622 public File getChild(File parent, String fileName) { 623 if (fileName.startsWith("\\") 624 && !(fileName.startsWith("\\\\")) 625 && isFileSystem(parent)) { 626 627 String path = parent.getAbsolutePath(); 629 if (path.length() >= 2 630 && path.charAt(1) == ':' 631 && Character.isLetter(path.charAt(0))) { 632 633 return createFileObject(path.substring(0, 2) + fileName); 634 } 635 } 636 return super.getChild(parent, fileName); 637 } 638 639 646 public String getSystemTypeDescription(File f) { 647 if (f != null) { 648 return getShellFolder(f).getFolderType(); 649 } else { 650 return null; 651 } 652 } 653 654 657 public File getHomeDirectory() { 658 return getRoots()[0]; 659 } 660 661 664 public File createNewFolder(File containingDir) throws IOException { 665 if(containingDir == null) { 666 throw new IOException ("Containing directory is null:"); 667 } 668 File newFolder = null; 669 newFolder = createFileObject(containingDir, newFolderString); 671 int i = 2; 672 while (newFolder.exists() && (i < 100)) { 673 newFolder = createFileObject(containingDir, MessageFormat.format( 674 newFolderNextString, new Object [] { new Integer (i) })); 675 i++; 676 } 677 678 if(newFolder.exists()) { 679 throw new IOException ("Directory already exists:" + newFolder.getAbsolutePath()); 680 } else { 681 newFolder.mkdirs(); 682 } 683 684 return newFolder; 685 } 686 687 public boolean isDrive(File dir) { 688 return isFileSystemRoot(dir); 689 } 690 691 public boolean isFloppyDrive(File dir) { 692 String path = dir.getAbsolutePath(); 693 return (path != null && (path.equals("A:\\") || path.equals("B:\\"))); 694 } 695 696 699 public File createFileObject(String path) { 700 if (path.length() >= 2 && path.charAt(1) == ':' && Character.isLetter(path.charAt(0))) { 702 if (path.length() == 2) { 703 path += "\\"; 704 } else if (path.charAt(2) != '\\') { 705 path = path.substring(0, 2) + "\\" + path.substring(2); 706 } 707 } 708 return super.createFileObject(path); 709 } 710 711 protected File createFileSystemRoot(File f) { 712 return new FileSystemRoot(f) { 715 public boolean exists() { 716 return true; 717 } 718 }; 719 } 720 721 } 722 723 726 class GenericFileSystemView extends FileSystemView { 727 728 private static final String newFolderString = 729 UIManager.getString("FileChooser.other.newFolder"); 730 731 734 public File createNewFolder(File containingDir) throws IOException { 735 if(containingDir == null) { 736 throw new IOException ("Containing directory is null:"); 737 } 738 File newFolder = null; 739 newFolder = createFileObject(containingDir, newFolderString); 741 742 if(newFolder.exists()) { 743 throw new IOException ("Directory already exists:" + newFolder.getAbsolutePath()); 744 } else { 745 newFolder.mkdirs(); 746 } 747 748 return newFolder; 749 } 750 751 } 752 | Popular Tags |