8022057: JFileChooser blocks EDT in Win32ShellFolder2.getIcon
authorssadetsky
Tue, 16 Jun 2015 08:46:41 +0300
changeset 31437 8cebff228261
parent 31436 41c1e00e441f
child 31439 267a86a4cadf
8022057: JFileChooser blocks EDT in Win32ShellFolder2.getIcon Reviewed-by: serb, ant
jdk/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolder2.java
jdk/src/java.desktop/windows/native/libawt/windows/ShellFolder2.cpp
--- a/jdk/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolder2.java	Mon Jun 15 11:01:43 2015 -0700
+++ b/jdk/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolder2.java	Tue Jun 16 08:46:41 2015 +0300
@@ -851,14 +851,14 @@
         return getLinkLocation(true);
     }
 
-    private ShellFolder getLinkLocation(final boolean resolve) {
-        return invoke(new Callable<ShellFolder>() {
-            public ShellFolder call() {
+    private Win32ShellFolder2 getLinkLocation(final boolean resolve) {
+        return invoke(new Callable<Win32ShellFolder2>() {
+            public Win32ShellFolder2 call() {
                 if (!isLink()) {
                     return null;
                 }
 
-                ShellFolder location = null;
+                Win32ShellFolder2 location = null;
                 long linkLocationPIDL = getLinkLocation(getParentIShellFolder(),
                         getRelativePIDL(), resolve);
                 if (linkLocationPIDL != 0) {
@@ -968,7 +968,7 @@
 
     // NOTE: this method uses COM and must be called on the 'COM thread'. See ComInvoker for the details
     private static native long extractIcon(long parentIShellFolder, long relativePIDL,
-                                           boolean getLargeIcon);
+                                           boolean getLargeIcon, boolean getDefaultIcon);
 
     // Returns an icon from the Windows system icon list in the form of an HICON
     private static native long getSystemIcon(int iconID);
@@ -1019,7 +1019,13 @@
                 invoke(new Callable<Image>() {
                     public Image call() {
                         Image newIcon = null;
-                        if (isFileSystem()) {
+                        if (isLink()) {
+                            Win32ShellFolder2 folder = getLinkLocation(false);
+                            if (folder != null && folder.isLibrary()) {
+                                return folder.getIcon(getLargeIcon);
+                            }
+                        }
+                        if (isFileSystem() || isLibrary()) {
                             long parentIShellIcon = (parent != null)
                                 ? ((Win32ShellFolder2) parent).getIShellIcon()
                                 : 0L;
@@ -1049,7 +1055,19 @@
                         if (newIcon == null) {
                             // These are only cached per object
                             long hIcon = extractIcon(getParentIShellFolder(),
-                                getRelativePIDL(), getLargeIcon);
+                                    getRelativePIDL(), getLargeIcon, false);
+                            // E_PENDING: loading can take time so get the default
+                            if(hIcon <= 0) {
+                                hIcon = extractIcon(getParentIShellFolder(),
+                                         getRelativePIDL(), getLargeIcon, true);
+                                if(hIcon <= 0) {
+                                    if (isDirectory()) {
+                                        return getShell32Icon(4, getLargeIcon);
+                                    } else {
+                                        return getShell32Icon(1, getLargeIcon);
+                                    }
+                                }
+                            }
                             newIcon = makeIcon(hIcon, getLargeIcon);
                             disposeIcon(hIcon);
                         }
--- a/jdk/src/java.desktop/windows/native/libawt/windows/ShellFolder2.cpp	Mon Jun 15 11:01:43 2015 -0700
+++ b/jdk/src/java.desktop/windows/native/libawt/windows/ShellFolder2.cpp	Tue Jun 16 08:46:41 2015 +0300
@@ -868,10 +868,11 @@
 /*
  * Class:     sun_awt_shell_Win32ShellFolder2
  * Method:    extractIcon
- * Signature: (JJZ)J
+ * Signature: (JJZZ)J
  */
 JNIEXPORT jlong JNICALL Java_sun_awt_shell_Win32ShellFolder2_extractIcon
-    (JNIEnv* env, jclass cls, jlong pIShellFolderL, jlong relativePIDL, jboolean getLargeIcon)
+    (JNIEnv* env, jclass cls, jlong pIShellFolderL, jlong relativePIDL,
+                                jboolean getLargeIcon, jboolean getDefaultIcon)
 {
     IShellFolder* pIShellFolder = (IShellFolder*)pIShellFolderL;
     LPITEMIDLIST pidl = (LPITEMIDLIST)relativePIDL;
@@ -889,7 +890,8 @@
         WCHAR szBuf[MAX_PATH];
         INT index;
         UINT flags;
-        hres = pIcon->GetIconLocation(GIL_FORSHELL, szBuf, MAX_PATH, &index, &flags);
+        UINT uFlags = getDefaultIcon ? GIL_DEFAULTICON : GIL_FORSHELL | GIL_ASYNC;
+        hres = pIcon->GetIconLocation(uFlags, szBuf, MAX_PATH, &index, &flags);
         if (SUCCEEDED(hres)) {
             HICON hIconLarge;
             hres = pIcon->Extract(szBuf, index, &hIconLarge, &hIcon, (16 << 16) + 32);
@@ -901,6 +903,9 @@
                     fn_DestroyIcon((HICON)hIconLarge);
                 }
             }
+        } else if (hres == E_PENDING) {
+            pIcon->Release();
+            return E_PENDING;
         }
         pIcon->Release();
     }
@@ -1284,7 +1289,6 @@
 JNIEXPORT jobjectArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_loadKnownFolders
     (JNIEnv* env, jclass cls )
 {
-    CoInitialize(NULL);
     IKnownFolderManager* pkfm = NULL;
     HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL,
                                 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));