1 /* @test %W% %E% 2 @bug 6698013 3 @summary JFileChooser can no longer navigate non-local file systems. 4 @author Pavel Porvatov 5 @run applet/manual=done bug6698013.html 6 */ 7 8 import javax.swing.*; 9 import javax.swing.filechooser.FileSystemView; 10 import java.io.File; 11 12 public class bug6698013 extends JApplet { 13 14 final static VirtualFile root = new VirtualFile("testdir", true); 15 16 final static VirtualFile rootFile = new VirtualFile("testdir/test.txt", false); 17 18 final static VirtualFile subdir = new VirtualFile("testdir/subdir", true); 19 20 final static VirtualFile subdirFile = new VirtualFile("testdir/subdir/subtest.txt", false); 21 22 public static void main(String[] args) { 23 JFileChooser chooser = new JFileChooser(new VirtualFileSystemView()); 24 chooser.setCurrentDirectory(root); 25 chooser.showSaveDialog(null); 26 } 27 28 public void init() { 29 JFileChooser chooser = new JFileChooser(new VirtualFileSystemView()); 30 chooser.setCurrentDirectory(root); 31 chooser.showSaveDialog(null); 32 } 33 } 34 35 class VirtualFileSystemView extends FileSystemView { 36 37 public boolean isRoot(File dir) { 38 return bug6698013.root.equals(dir); 39 } 40 41 public File createNewFolder(File dir) { 42 return null; 43 } 44 45 public File[] getRoots() { 46 return new File[]{bug6698013.root}; 47 } 48 49 public boolean isDrive(File dir) { 50 return false; 51 } 52 53 public boolean isFloppyDrive(File dir) { 54 return false; 55 } 56 57 public File getParentDirectory(File dir) { 58 if (dir == null) { 59 return null; 60 } 61 62 return new VirtualFile(dir.getPath(), true).getParentFile(); 63 } 64 65 public File[] getFiles(File dir, boolean hide_hidden) { 66 if (dir.equals(bug6698013.root)) { 67 return new File[]{bug6698013.rootFile, bug6698013.subdir}; 68 } 69 70 if (dir.equals(bug6698013.subdir)) { 71 return new File[]{bug6698013.subdirFile}; 72 } 73 74 return null; 75 } 76 77 public File getHomeDirectory() { 78 return bug6698013.root; 79 } 80 81 public File getDefaultDirectory() { 82 return getHomeDirectory(); 83 } 84 85 public String getSystemDisplayName(File file) { 86 return file.getName(); 87 } 88 89 public Boolean isTraversable(File file) { 90 return Boolean.valueOf(file.isDirectory()); 91 } 92 } 93 94 /** 95 * A Virtual File. Contains a path and a directory flag that 96 * represents the location of a virtual file to be contained in the 97 * Virtual FileSystemView. 98 */ 99 class VirtualFile extends File { 100 101 private static final long serialVersionUID = 0L; 102 103 private String path; 104 105 private boolean directory; 106 107 public VirtualFile(String path, boolean directory) { 108 super(path); 109 this.path = path; 110 this.directory = directory; 111 } 112 113 public File getParentFile() { 114 int index = path.lastIndexOf('/'); 115 116 if (index == -1) { 117 return null; 118 } 119 120 return new VirtualFile(path.substring(0, index), true); 121 } 122 123 public File getCanonicalFile() { 124 return this; 125 } 126 127 public String getParent() { 128 File parent_file = getParentFile(); 129 130 return parent_file == null ? null : parent_file.getPath(); 131 } 132 133 public String getName() { 134 int index = path.lastIndexOf('/'); 135 136 return index == -1 ? path : path.substring(index + 1); 137 } 138 139 public String getPath() { 140 return path; 141 } 142 143 public String getAbsolutePath() { 144 return path; 145 } 146 147 public String getCanonicalPath() { 148 return path; 149 } 150 151 public String toString() { 152 return path; 153 } 154 155 public boolean equals(Object obj) { 156 return obj instanceof VirtualFile && path.equals(obj.toString()); 157 } 158 159 public int hashCode() { 160 return path.hashCode(); 161 } 162 163 public boolean canWrite() { 164 return true; 165 } 166 167 public boolean isDirectory() { 168 return directory; 169 } 170 171 public boolean exists() { 172 return true; 173 } 174 }