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