1 /*
   2  * Copyright (c) 2008, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test %W% %E%
  25    @bug 6698013
  26    @summary JFileChooser can no longer navigate non-local file systems.
  27    @author Pavel Porvatov
  28    @run applet/manual=done bug6698013.html
  29 */
  30 
  31 import javax.swing.*;
  32 import javax.swing.filechooser.FileSystemView;
  33 import java.io.File;
  34 
  35 public class bug6698013 extends JApplet {
  36 
  37     final static VirtualFile root = new VirtualFile("testdir", true);
  38 
  39     final static VirtualFile rootFile = new VirtualFile("testdir/test.txt", false);
  40 
  41     final static VirtualFile subdir = new VirtualFile("testdir/subdir", true);
  42 
  43     final static VirtualFile subdirFile = new VirtualFile("testdir/subdir/subtest.txt", false);
  44 
  45     public static void main(String[] args) {
  46         JFileChooser chooser = new JFileChooser(new VirtualFileSystemView());
  47         chooser.setCurrentDirectory(root);
  48         chooser.showSaveDialog(null);
  49     }
  50 
  51     public void init() {
  52         JFileChooser chooser = new JFileChooser(new VirtualFileSystemView());
  53         chooser.setCurrentDirectory(root);
  54         chooser.showSaveDialog(null);
  55     }
  56 }
  57 
  58 class VirtualFileSystemView extends FileSystemView {
  59 
  60     public boolean isRoot(File dir) {
  61         return bug6698013.root.equals(dir);
  62     }
  63 
  64     public File createNewFolder(File dir) {
  65         return null;
  66     }
  67 
  68     public File[] getRoots() {
  69         return new File[]{bug6698013.root};
  70     }
  71 
  72     public boolean isDrive(File dir) {
  73         return false;
  74     }
  75 
  76     public boolean isFloppyDrive(File dir) {
  77         return false;
  78     }
  79 
  80     public File getParentDirectory(File dir) {
  81         if (dir == null) {
  82             return null;
  83         }
  84 
  85         return new VirtualFile(dir.getPath(), true).getParentFile();
  86     }
  87 
  88     public File[] getFiles(File dir, boolean hide_hidden) {
  89         if (dir.equals(bug6698013.root)) {
  90             return new File[]{bug6698013.rootFile, bug6698013.subdir};
  91         }
  92 
  93         if (dir.equals(bug6698013.subdir)) {
  94             return new File[]{bug6698013.subdirFile};
  95         }
  96 
  97         return null;
  98     }
  99 
 100     public File getHomeDirectory() {
 101         return bug6698013.root;
 102     }
 103 
 104     public File getDefaultDirectory() {
 105         return getHomeDirectory();
 106     }
 107 
 108     public String getSystemDisplayName(File file) {
 109         return file.getName();
 110     }
 111 
 112     public Boolean isTraversable(File file) {
 113         return Boolean.valueOf(file.isDirectory());
 114     }
 115 }
 116 
 117 /**
 118  * A Virtual File. Contains a path and a directory flag that
 119  * represents the location of a virtual file to be contained in the
 120  * Virtual FileSystemView.
 121  */
 122 class VirtualFile extends File {
 123 
 124     private static final long serialVersionUID = 0L;
 125 
 126     private String path;
 127 
 128     private boolean directory;
 129 
 130     public VirtualFile(String path, boolean directory) {
 131         super(path);
 132         this.path = path;
 133         this.directory = directory;
 134     }
 135 
 136     public File getParentFile() {
 137         int index = path.lastIndexOf('/');
 138 
 139         if (index == -1) {
 140             return null;
 141         }
 142 
 143         return new VirtualFile(path.substring(0, index), true);
 144     }
 145 
 146     public File getCanonicalFile() {
 147         return this;
 148     }
 149 
 150     public String getParent() {
 151         File parent_file = getParentFile();
 152 
 153         return parent_file == null ? null : parent_file.getPath();
 154     }
 155 
 156     public String getName() {
 157         int index = path.lastIndexOf('/');
 158 
 159         return index == -1 ? path : path.substring(index + 1);
 160     }
 161 
 162     public String getPath() {
 163         return path;
 164     }
 165 
 166     public String getAbsolutePath() {
 167         return path;
 168     }
 169 
 170     public String getCanonicalPath() {
 171         return path;
 172     }
 173 
 174     public String toString() {
 175         return path;
 176     }
 177 
 178     public boolean equals(Object obj) {
 179         return obj instanceof VirtualFile && path.equals(obj.toString());
 180     }
 181 
 182     public int hashCode() {
 183         return path.hashCode();
 184     }
 185 
 186     public boolean canWrite() {
 187         return true;
 188     }
 189 
 190     public boolean isDirectory() {
 191         return directory;
 192     }
 193 
 194     public boolean exists() {
 195         return true;
 196     }
 197 }