changeset 7731:3db7f2778f16

8055304: More boxing for DirectoryComboBoxModel Reviewed-by: serb, prr, skoivu
author alexsch
date Wed, 24 Sep 2014 13:32:29 +0400
parents 6ba008b3b862
children e885a036cc5d
files src/share/classes/com/sun/java/swing/plaf/windows/WindowsFileChooserUI.java src/share/classes/javax/swing/plaf/metal/MetalFileChooserUI.java src/share/classes/sun/swing/WindowsPlacesBar.java src/share/classes/sun/swing/plaf/synth/SynthFileChooserUIImpl.java src/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java
diffstat 5 files changed, 56 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/java/swing/plaf/windows/WindowsFileChooserUI.java	Mon Sep 22 11:12:18 2014 -0700
+++ b/src/share/classes/com/sun/java/swing/plaf/windows/WindowsFileChooserUI.java	Wed Sep 24 13:32:29 2014 +0400
@@ -1067,16 +1067,9 @@
 
             directories.clear();
 
-            File[] baseFolders;
-            if (useShellFolder) {
-                baseFolders = AccessController.doPrivileged(new PrivilegedAction<File[]>() {
-                    public File[] run() {
-                        return (File[]) ShellFolder.get("fileChooserComboBoxFolders");
-                    }
-                });
-            } else {
-                baseFolders = fsv.getRoots();
-            }
+            File[] baseFolders = (useShellFolder)
+                    ? (File[]) ShellFolder.get("fileChooserComboBoxFolders")
+                    : fsv.getRoots();
             directories.addAll(Arrays.asList(baseFolders));
 
             // Get the canonical (full) path. This has the side
--- a/src/share/classes/javax/swing/plaf/metal/MetalFileChooserUI.java	Mon Sep 22 11:12:18 2014 -0700
+++ b/src/share/classes/javax/swing/plaf/metal/MetalFileChooserUI.java	Wed Sep 24 13:32:29 2014 +0400
@@ -941,16 +941,9 @@
 
             directories.clear();
 
-            File[] baseFolders;
-            if (useShellFolder) {
-                baseFolders = AccessController.doPrivileged(new PrivilegedAction<File[]>() {
-                    public File[] run() {
-                        return (File[]) ShellFolder.get("fileChooserComboBoxFolders");
-                    }
-                });
-            } else {
-                baseFolders = fsv.getRoots();
-            }
+            File[] baseFolders = (useShellFolder)
+                    ? (File[]) ShellFolder.get("fileChooserComboBoxFolders")
+                    : fsv.getRoots();
             directories.addAll(Arrays.asList(baseFolders));
 
             // Get the canonical (full) path. This has the side
--- a/src/share/classes/sun/swing/WindowsPlacesBar.java	Mon Sep 22 11:12:18 2014 -0700
+++ b/src/share/classes/sun/swing/WindowsPlacesBar.java	Wed Sep 24 13:32:29 2014 +0400
@@ -81,11 +81,7 @@
         setBackground(bgColor);
         FileSystemView fsv = fc.getFileSystemView();
 
-        files = AccessController.doPrivileged(new PrivilegedAction<File[]>() {
-            public File[] run() {
-                return (File[]) ShellFolder.get("fileChooserShortcutPanelFolders");
-            }
-        });
+        files = (File[]) ShellFolder.get("fileChooserShortcutPanelFolders");
 
         buttons = new JToggleButton[files.length];
         buttonGroup = new ButtonGroup();
--- a/src/share/classes/sun/swing/plaf/synth/SynthFileChooserUIImpl.java	Mon Sep 22 11:12:18 2014 -0700
+++ b/src/share/classes/sun/swing/plaf/synth/SynthFileChooserUIImpl.java	Wed Sep 24 13:32:29 2014 +0400
@@ -771,16 +771,9 @@
                 fireIntervalRemoved(this, 0, oldSize);
             }
 
-            File[] baseFolders;
-            if (useShellFolder) {
-                baseFolders = AccessController.doPrivileged(new PrivilegedAction<File[]>() {
-                    public File[] run() {
-                        return (File[]) ShellFolder.get("fileChooserComboBoxFolders");
-                    }
-                });
-            } else {
-                baseFolders = fsv.getRoots();
-            }
+            File[] baseFolders = (useShellFolder)
+                    ? (File[]) ShellFolder.get("fileChooserComboBoxFolders")
+                    : fsv.getRoots();
             directories.addAll(Arrays.asList(baseFolders));
 
             // Get the canonical (full) path. This has the side
--- a/src/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java	Mon Sep 22 11:12:18 2014 -0700
+++ b/src/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java	Wed Sep 24 13:32:29 2014 +0400
@@ -253,7 +253,7 @@
             if (file == null) {
                 file = getDesktop();
             }
-            return file;
+            return checkFile(file);
         } else if (key.equals("roots")) {
             // Should be "History" and "Desktop" ?
             if (roots == null) {
@@ -264,11 +264,11 @@
                     roots = (File[])super.get(key);
                 }
             }
-            return roots;
+            return checkFiles(roots);
         } else if (key.equals("fileChooserComboBoxFolders")) {
             Win32ShellFolder2 desktop = getDesktop();
 
-            if (desktop != null) {
+            if (desktop != null && checkFile(desktop) != null) {
                 ArrayList<File> folders = new ArrayList<File>();
                 Win32ShellFolder2 drives = getDrives();
 
@@ -297,7 +297,7 @@
                         }
                     }
                 }
-                return folders.toArray(new File[folders.size()]);
+                return checkFiles(folders);
             } else {
                 return super.get(key);
             }
@@ -334,7 +334,7 @@
                     }
                 }
             }
-            return folders.toArray(new File[folders.size()]);
+            return checkFiles(folders);
         } else if (key.startsWith("fileChooserIcon ")) {
             String name = key.substring(key.indexOf(" ") + 1);
 
@@ -380,6 +380,47 @@
         return null;
     }
 
+    private File checkFile(File file) {
+        SecurityManager sm = System.getSecurityManager();
+        return (sm == null || file == null) ? file : checkFile(file, sm);
+    }
+
+    private File checkFile(File file, SecurityManager sm) {
+        try {
+            sm.checkRead(file.getPath());
+            return file;
+        } catch (SecurityException se) {
+            return null;
+        }
+    }
+
+    private File[] checkFiles(File[] files) {
+        SecurityManager sm = System.getSecurityManager();
+        if (sm == null || files == null || files.length == 0) {
+            return files;
+        }
+        return checkFiles(Arrays.asList(files), sm);
+    }
+
+    private File[] checkFiles(List<File> files) {
+        SecurityManager sm = System.getSecurityManager();
+        if (sm == null || files.isEmpty()) {
+            return files.toArray(new File[files.size()]);
+        }
+        return checkFiles(files, sm);
+    }
+
+    private File[] checkFiles(List<File> files, SecurityManager sm) {
+        List<File> checkedFiles = new ArrayList<File>(files.size());
+        for (File file: files) {
+            if(checkFile(file, sm) != null){
+                checkedFiles.add(file);
+            }
+        }
+
+        return checkedFiles.toArray(new File[checkedFiles.size()]);
+    }
+
     /**
      * Does <code>dir</code> represent a "computer" such as a node on the network, or
      * "My Computer" on the desktop.