Path: blob/master/test/jdk/javax/swing/JFileChooser/6698013/bug6698013.java
41153 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 669801325@summary JFileChooser can no longer navigate non-local file systems.26@run applet/manual=done bug6698013.html27*/2829import java.io.File;3031import javax.swing.JApplet;32import javax.swing.JFileChooser;33import javax.swing.SwingUtilities;34import javax.swing.filechooser.FileSystemView;3536public class bug6698013 extends JApplet {3738final static VirtualFile root = new VirtualFile("testdir", true);3940final static VirtualFile rootFile = new VirtualFile("testdir/test.txt", false);4142final static VirtualFile subdir = new VirtualFile("testdir/subdir", true);4344final static VirtualFile subdirFile = new VirtualFile("testdir/subdir/subtest.txt", false);4546public static void main(String[] args) throws Exception {47SwingUtilities.invokeAndWait(() -> new bug6698013().init());48}4950public void init() {51JFileChooser chooser = new JFileChooser(new VirtualFileSystemView());52chooser.setCurrentDirectory(root);53chooser.showOpenDialog(null);54}55}5657class VirtualFileSystemView extends FileSystemView {5859public boolean isRoot(File dir) {60return bug6698013.root.equals(dir);61}6263public File createNewFolder(File dir) {64return null;65}6667public File[] getRoots() {68return new File[]{bug6698013.root};69}7071public boolean isDrive(File dir) {72return false;73}7475public boolean isFloppyDrive(File dir) {76return false;77}7879public File getParentDirectory(File dir) {80if (dir == null) {81return null;82}8384return new VirtualFile(dir.getPath(), true).getParentFile();85}8687public File[] getFiles(File dir, boolean hide_hidden) {88if (dir.equals(bug6698013.root)) {89return new File[]{bug6698013.rootFile, bug6698013.subdir};90}9192if (dir.equals(bug6698013.subdir)) {93return new File[]{bug6698013.subdirFile};94}9596return null;97}9899public File getHomeDirectory() {100return bug6698013.root;101}102103public File getDefaultDirectory() {104return getHomeDirectory();105}106107public String getSystemDisplayName(File file) {108return file.getName();109}110111public Boolean isTraversable(File file) {112return Boolean.valueOf(file.isDirectory());113}114}115116/**117* A Virtual File. Contains a path and a directory flag that118* represents the location of a virtual file to be contained in the119* Virtual FileSystemView.120*/121class VirtualFile extends File {122123private static final long serialVersionUID = 0L;124125private String path;126127private boolean directory;128129public VirtualFile(String path, boolean directory) {130super(path);131this.path = path;132this.directory = directory;133}134135public File getParentFile() {136int index = path.lastIndexOf('/');137138if (index == -1) {139return null;140}141142return new VirtualFile(path.substring(0, index), true);143}144145public File getCanonicalFile() {146return this;147}148149public String getParent() {150File parent_file = getParentFile();151152return parent_file == null ? null : parent_file.getPath();153}154155public String getName() {156int index = path.lastIndexOf('/');157158return index == -1 ? path : path.substring(index + 1);159}160161public String getPath() {162return path;163}164165public String getAbsolutePath() {166return path;167}168169public String getCanonicalPath() {170return path;171}172173public String toString() {174return path;175}176177public boolean equals(Object obj) {178return obj instanceof VirtualFile && path.equals(obj.toString());179}180181public int hashCode() {182return path.hashCode();183}184185public boolean canWrite() {186return true;187}188189public boolean isDirectory() {190return directory;191}192193public boolean exists() {194return true;195}196}197198199