8081722: Provide public access to sun.awt.shell.ShellFolder methods which are required for implementing javax.swing.JFileChooser
authorssadetsky
Wed, 24 Feb 2016 14:36:53 +0300
changeset 36459 a34194491737
parent 36458 25786a73a5fc
child 36460 44903b87cbf5
8081722: Provide public access to sun.awt.shell.ShellFolder methods which are required for implementing javax.swing.JFileChooser Reviewed-by: prr, serb, alexsch
jdk/src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java
jdk/test/javax/swing/JFileChooser/ShellFolderQueries/ShellFolderQueriesTest.java
--- a/jdk/src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java	Tue Feb 23 22:07:27 2016 +0100
+++ b/jdk/src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java	Wed Feb 24 14:36:53 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2016, 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
@@ -584,6 +584,69 @@
     }
 
     /**
+     * Returns an array of files representing the values to show by default in
+     * the file chooser selector.
+     *
+     * @return an array of {@code File} objects.
+     * @throws SecurityException if the caller does not have necessary
+     *                           permissions
+     * @since 9
+     */
+    public File[] getChooserComboBoxFiles() {
+        return (File[]) ShellFolder.get("fileChooserComboBoxFolders");
+    }
+
+    /**
+     * Returns whether the specified file denotes a shell interpreted link which
+     * can be obtained by the {@link #getLinkLocation(File)}.
+     *
+     * @param file a file
+     * @return whether this is a link
+     * @throws NullPointerException if {@code file} equals {@code null}
+     * @throws SecurityException if the caller does not have necessary
+     *                           permissions
+     * @see #getLinkLocation(File)
+     * @since 9
+     */
+    public boolean isLink(File file) {
+        if (file == null) {
+            throw new NullPointerException("file is null");
+        }
+        try {
+            return ShellFolder.getShellFolder(file).isLink();
+        } catch (FileNotFoundException e) {
+            return false;
+        }
+    }
+
+    /**
+     * Returns the regular file referenced by the specified link file if
+     * the specified file is a shell interpreted link.
+     * Returns {@code null} if the specified file is not
+     * a shell interpreted link.
+     *
+     * @param file a file
+     * @return the linked file or {@code null}.
+     * @throws FileNotFoundException if the linked file does not exist
+     * @throws NullPointerException if {@code file} equals {@code null}
+     * @throws SecurityException if the caller does not have necessary
+     *                           permissions
+     * @since 9
+     */
+    public File getLinkLocation(File file) throws FileNotFoundException {
+        if (file == null) {
+            throw new NullPointerException("file is null");
+        }
+        ShellFolder shellFolder;
+        try {
+            shellFolder = ShellFolder.getShellFolder(file);
+        } catch (FileNotFoundException e) {
+            return null;
+        }
+        return shellFolder.isLink() ? shellFolder.getLinkLocation() : null;
+    }
+
+    /**
      * Throws {@code FileNotFoundException} if file not found or current thread was interrupted
      */
     ShellFolder getShellFolder(File f) throws FileNotFoundException {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JFileChooser/ShellFolderQueries/ShellFolderQueriesTest.java	Wed Feb 24 14:36:53 2016 +0300
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2016, 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
+ * @bug 8081722
+ * @summary Provide public API for file hierarchy provided by
+ * sun.awt.shell.ShellFolder
+ * @author Semyon Sadetsky
+ * @run main ShellFolderQueriesTest
+ */
+
+import sun.awt.OSInfo;
+
+import javax.swing.filechooser.FileSystemView;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class ShellFolderQueriesTest {
+    static final String HOME = System.getProperty("user.home");
+    static final FileSystemView fsv = FileSystemView.getFileSystemView();
+
+
+    static String scriptBeg =
+            "set WshShell = WScript.CreateObject(\"WScript.Shell\")\n" +
+            "set oShellLink = WshShell.CreateShortcut(\"shortcut.lnk\")\n" +
+            "oShellLink.TargetPath = \"";
+    static String scriptEnd = "\"\noShellLink.WindowStyle = 1\noShellLink.Save";
+
+    public static void main(String[] args) throws Exception {
+        if(OSInfo.getOSType() == OSInfo.OSType.WINDOWS) {
+            testGet();
+            testLink();
+        } else {
+            testGet();
+        }
+        System.out.println("ok");
+    }
+
+    private static void testLink() throws IOException, InterruptedException {
+        // Create and execute VBS script to create a link
+        File file = createVbsScript(scriptBeg + HOME + scriptEnd);
+        Runtime.getRuntime().exec("cscript " + file.getName(), null,
+                file.getParentFile()).waitFor();
+        file.delete();
+
+        File link = new File(file.getParentFile(), "shortcut.lnk");
+        if (!fsv.isLink(link)) {
+            link.delete();
+            throw new RuntimeException("Link is not detected");
+        }
+
+        File location = fsv.getLinkLocation(link);
+        if (!location.getAbsolutePath().equals(HOME)) {
+            link.delete();
+            throw new RuntimeException("Link location " + location +
+                    " is wrong");
+        }
+        link.delete();
+
+
+        link = File.createTempFile("test", ".tst");
+
+        if (fsv.isLink(link)) {
+            link.delete();
+            throw new RuntimeException("File is not a link");
+        }
+
+        try {
+            location = fsv.getLinkLocation(link);
+            if (location != null) {
+                link.delete();
+                throw new RuntimeException("Not a link, should return null");
+            }
+        }
+        catch (FileNotFoundException e) {
+        }
+        link.delete();
+    }
+
+    private static File createVbsScript(String script) throws IOException {
+        File file = File.createTempFile("test", ".vbs");
+        file.deleteOnExit();
+        FileOutputStream fos = new FileOutputStream(file);
+        fos.write(script.getBytes());
+        fos.close();
+        return file;
+    }
+
+    private static void testGet() {
+        File[] files = fsv.getChooserComboBoxFiles();
+        for (File file : files) {
+            if (fsv.isLink(file)) {
+                throw new RuntimeException(
+                        "Link shouldn't be in FileChooser combobox, "
+                                + file.getPath());
+            }
+        }
+    }
+}