960 961 962 // Icons 963 964 private static Map<Integer, Image> smallSystemImages = new HashMap<>(); 965 private static Map<Integer, Image> largeSystemImages = new HashMap<>(); 966 private static Map<Integer, Image> smallLinkedSystemImages = new HashMap<>(); 967 private static Map<Integer, Image> largeLinkedSystemImages = new HashMap<>(); 968 969 // NOTE: this method uses COM and must be called on the 'COM thread'. See ComInvoker for the details 970 private static native long getIShellIcon(long pIShellFolder); 971 972 // NOTE: this method uses COM and must be called on the 'COM thread'. See ComInvoker for the details 973 private static native int getIconIndex(long parentIShellIcon, long relativePIDL); 974 975 // Return the icon of a file system shell folder in the form of an HICON 976 private static native long getIcon(String absolutePath, boolean getLargeIcon); 977 978 // NOTE: this method uses COM and must be called on the 'COM thread'. See ComInvoker for the details 979 private static native long extractIcon(long parentIShellFolder, long relativePIDL, 980 boolean getLargeIcon, boolean getDefaultIcon); 981 982 // Returns an icon from the Windows system icon list in the form of an HICON 983 private static native long getSystemIcon(int iconID); 984 private static native long getIconResource(String libName, int iconID, 985 int cxDesired, int cyDesired, 986 boolean useVGAColors); 987 // Note: useVGAColors is ignored on XP and later 988 989 // Return the bits from an HICON. This has a side effect of setting 990 // the imageHash variable for efficient caching / comparing. 991 private static native int[] getIconBits(long hIcon); 992 // Dispose the HICON 993 private static native void disposeIcon(long hIcon); 994 995 // Get buttons from native toolbar implementation. 996 static native int[] getStandardViewButton0(int iconIndex, boolean small); 997 998 // Should be called from the COM thread 999 private long getIShellIcon() { 1000 if (pIShellIcon == -1L) { 1001 pIShellIcon = getIShellIcon(getIShellFolder()); 1002 } 1003 1004 return pIShellIcon; 1005 } 1006 1007 private static Image makeIcon(long hIcon, boolean getLargeIcon) { 1008 if (hIcon != 0L && hIcon != -1L) { 1009 // Get the bits. This has the side effect of setting the imageHash value for this object. 1010 final int[] iconBits = getIconBits(hIcon); 1011 if (iconBits != null) { 1012 // icons are always square 1013 final int size = (int) Math.sqrt(iconBits.length); 1014 final int baseSize = getLargeIcon ? 32 : 16; 1015 final BufferedImage img = 1016 new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); 1017 img.setRGB(0, 0, size, size, iconBits, 0, size); 1018 return size == baseSize 1019 ? img 1020 : new MultiResolutionIconImage(baseSize, img); 1021 } 1022 } 1023 return null; 1024 } 1025 1026 1027 /** 1028 * @return The icon image used to display this shell folder 1029 */ 1030 public Image getIcon(final boolean getLargeIcon) { 1031 Image icon = getLargeIcon ? largeIcon : smallIcon; 1032 if (icon == null) { 1033 icon = 1034 invoke(new Callable<Image>() { 1035 public Image call() { 1036 Image newIcon = null; 1037 if (isLink()) { 1038 Win32ShellFolder2 folder = getLinkLocation(false); 1039 if (folder != null && folder.isLibrary()) { 1040 return folder.getIcon(getLargeIcon); 1041 } 1042 } 1043 if (isFileSystem() || isLibrary()) { 1044 long parentIShellIcon = (parent != null) 1045 ? ((Win32ShellFolder2) parent).getIShellIcon() 1046 : 0L; 1047 long relativePIDL = getRelativePIDL(); 1048 1049 // These are cached per type (using the index in the system image list) 1050 int index = getIconIndex(parentIShellIcon, relativePIDL); 1051 if (index > 0) { 1052 Map<Integer, Image> imageCache; 1053 if (isLink()) { 1054 imageCache = getLargeIcon ? largeLinkedSystemImages : smallLinkedSystemImages; 1055 } else { 1056 imageCache = getLargeIcon ? largeSystemImages : smallSystemImages; 1057 } 1058 newIcon = imageCache.get(Integer.valueOf(index)); 1059 if (newIcon == null) { 1060 long hIcon = getIcon(getAbsolutePath(), getLargeIcon); 1061 newIcon = makeIcon(hIcon, getLargeIcon); 1062 disposeIcon(hIcon); 1063 if (newIcon != null) { 1064 imageCache.put(Integer.valueOf(index), newIcon); 1065 } 1066 } 1067 } 1068 } 1069 1070 if (newIcon == null) { 1071 // These are only cached per object 1072 long hIcon = extractIcon(getParentIShellFolder(), 1073 getRelativePIDL(), getLargeIcon, false); 1074 // E_PENDING: loading can take time so get the default 1075 if(hIcon <= 0) { 1076 hIcon = extractIcon(getParentIShellFolder(), 1077 getRelativePIDL(), getLargeIcon, true); 1078 if(hIcon <= 0) { 1079 if (isDirectory()) { 1080 return getShell32Icon(4, getLargeIcon); 1081 } else { 1082 return getShell32Icon(1, getLargeIcon); 1083 } 1084 } 1085 } 1086 newIcon = makeIcon(hIcon, getLargeIcon); 1087 disposeIcon(hIcon); 1088 } 1089 1090 if (newIcon == null) { 1091 newIcon = Win32ShellFolder2.super.getIcon(getLargeIcon); 1092 } 1093 return newIcon; 1094 } 1095 }); 1096 if (getLargeIcon) { 1097 largeIcon = icon; 1098 } else { 1099 smallIcon = icon; 1100 } 1101 } 1102 return icon; 1103 } 1104 1105 /** 1106 * Gets an icon from the Windows system icon list as an {@code Image} 1107 */ 1108 static Image getSystemIcon(SystemIcon iconType) { 1109 long hIcon = getSystemIcon(iconType.getIconID()); 1110 Image icon = makeIcon(hIcon, true); 1111 disposeIcon(hIcon); 1112 return icon; 1113 } 1114 1115 /** 1116 * Gets an icon from the Windows system icon list as an {@code Image} 1117 */ 1118 static Image getShell32Icon(int iconID, boolean getLargeIcon) { 1119 boolean useVGAColors = true; // Will be ignored on XP and later 1120 1121 int size = getLargeIcon ? 32 : 16; 1122 1123 Toolkit toolkit = Toolkit.getDefaultToolkit(); 1124 String shellIconBPP = (String)toolkit.getDesktopProperty("win.icon.shellIconBPP"); 1125 if (shellIconBPP != null) { 1126 useVGAColors = shellIconBPP.equals("4"); 1127 } 1128 1129 long hIcon = getIconResource("shell32.dll", iconID, size, size, useVGAColors); 1130 if (hIcon != 0) { 1131 Image icon = makeIcon(hIcon, getLargeIcon); 1132 disposeIcon(hIcon); 1133 return icon; 1134 } 1135 return null; 1136 } 1137 1138 /** 1139 * Returns the canonical form of this abstract pathname. Equivalent to 1140 * <code>new Win32ShellFolder2(getParentFile(), this.{@link java.io.File#getCanonicalPath}())</code>. 1141 * 1142 * @see java.io.File#getCanonicalFile 1143 */ 1144 public File getCanonicalFile() throws IOException { 1145 return this; 1146 } 1147 1148 /* 1149 * Indicates whether this is a special folder (includes My Documents) 1150 */ 1151 public boolean isSpecial() { | 960 961 962 // Icons 963 964 private static Map<Integer, Image> smallSystemImages = new HashMap<>(); 965 private static Map<Integer, Image> largeSystemImages = new HashMap<>(); 966 private static Map<Integer, Image> smallLinkedSystemImages = new HashMap<>(); 967 private static Map<Integer, Image> largeLinkedSystemImages = new HashMap<>(); 968 969 // NOTE: this method uses COM and must be called on the 'COM thread'. See ComInvoker for the details 970 private static native long getIShellIcon(long pIShellFolder); 971 972 // NOTE: this method uses COM and must be called on the 'COM thread'. See ComInvoker for the details 973 private static native int getIconIndex(long parentIShellIcon, long relativePIDL); 974 975 // Return the icon of a file system shell folder in the form of an HICON 976 private static native long getIcon(String absolutePath, boolean getLargeIcon); 977 978 // NOTE: this method uses COM and must be called on the 'COM thread'. See ComInvoker for the details 979 private static native long extractIcon(long parentIShellFolder, long relativePIDL, 980 int size, boolean getDefaultIcon); 981 982 // Returns an icon from the Windows system icon list in the form of an HICON 983 private static native long getSystemIcon(int iconID); 984 private static native long getIconResource(String libName, int iconID, 985 int cxDesired, int cyDesired, 986 boolean useVGAColors); 987 // Note: useVGAColors is ignored on XP and later 988 989 // Return the bits from an HICON. This has a side effect of setting 990 // the imageHash variable for efficient caching / comparing. 991 private static native int[] getIconBits(long hIcon); 992 // Dispose the HICON 993 private static native void disposeIcon(long hIcon); 994 995 // Get buttons from native toolbar implementation. 996 static native int[] getStandardViewButton0(int iconIndex, boolean small); 997 998 // Should be called from the COM thread 999 private long getIShellIcon() { 1000 if (pIShellIcon == -1L) { 1001 pIShellIcon = getIShellIcon(getIShellFolder()); 1002 } 1003 1004 return pIShellIcon; 1005 } 1006 1007 private static Image makeIcon(long hIcon, int bsize) { 1008 if (hIcon != 0L && hIcon != -1L) { 1009 // Get the bits. This has the side effect of setting the imageHash value for this object. 1010 final int[] iconBits = getIconBits(hIcon); 1011 if (iconBits != null) { 1012 // icons are always square 1013 final int size = (int) Math.sqrt(iconBits.length); 1014 final BufferedImage img = 1015 new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); 1016 img.setRGB(0, 0, size, size, iconBits, 0, size); 1017 return size == bsize 1018 ? img 1019 : new MultiResolutionIconImage(bsize, img); 1020 } 1021 } 1022 return null; 1023 } 1024 1025 1026 /** 1027 * @return The icon image used to display this shell folder 1028 */ 1029 public Image getIcon(final boolean getLargeIcon) { 1030 Image icon = getLargeIcon ? largeIcon : smallIcon; 1031 int size = getLargeIcon ? 32 : 16; // large icon is 32 pixels else 16 pixels 1032 if (icon == null) { 1033 icon = 1034 invoke(new Callable<Image>() { 1035 public Image call() { 1036 Image newIcon = null; 1037 if (isLink()) { 1038 Win32ShellFolder2 folder = getLinkLocation(false); 1039 if (folder != null && folder.isLibrary()) { 1040 return folder.getIcon(getLargeIcon); 1041 } 1042 } 1043 if (isFileSystem() || isLibrary()) { 1044 long parentIShellIcon = (parent != null) 1045 ? ((Win32ShellFolder2) parent).getIShellIcon() 1046 : 0L; 1047 long relativePIDL = getRelativePIDL(); 1048 1049 // These are cached per type (using the index in the system image list) 1050 int index = getIconIndex(parentIShellIcon, relativePIDL); 1051 if (index > 0) { 1052 Map<Integer, Image> imageCache; 1053 if (isLink()) { 1054 imageCache = getLargeIcon ? largeLinkedSystemImages : smallLinkedSystemImages; 1055 } else { 1056 imageCache = getLargeIcon ? largeSystemImages : smallSystemImages; 1057 } 1058 newIcon = imageCache.get(Integer.valueOf(index)); 1059 if (newIcon == null) { 1060 long hIcon = getIcon(getAbsolutePath(), getLargeIcon); 1061 newIcon = makeIcon(hIcon, size); 1062 disposeIcon(hIcon); 1063 if (newIcon != null) { 1064 imageCache.put(Integer.valueOf(index), newIcon); 1065 } 1066 } 1067 } 1068 } 1069 1070 if (newIcon == null) { 1071 // These are only cached per object 1072 long hIcon = extractIcon(getParentIShellFolder(), 1073 getRelativePIDL(), size, false); 1074 // E_PENDING: loading can take time so get the default 1075 if(hIcon <= 0) { 1076 hIcon = extractIcon(getParentIShellFolder(), 1077 getRelativePIDL(), size, true); 1078 if(hIcon <= 0) { 1079 if (isDirectory()) { 1080 return getShell32Icon(4, size); // pick folder icon 1081 } else { 1082 return getShell32Icon(1, size); // pick file icon 1083 } 1084 } 1085 } 1086 newIcon = makeIcon(hIcon, size); 1087 disposeIcon(hIcon); 1088 } 1089 1090 if (newIcon == null) { 1091 newIcon = Win32ShellFolder2.super.getIcon(getLargeIcon); 1092 } 1093 return newIcon; 1094 } 1095 }); 1096 } 1097 return icon; 1098 } 1099 1100 public Image getIcon(int size) { 1101 return invoke(() -> { 1102 Image newIcon = null; 1103 if (isLink()) { 1104 Win32ShellFolder2 folder = getLinkLocation(false); 1105 if (folder != null && folder.isLibrary()) { 1106 return folder.getIcon(size); 1107 } 1108 } 1109 long hIcon = extractIcon(getParentIShellFolder(), 1110 getRelativePIDL(), size, false); 1111 1112 // E_PENDING: loading can take time so get the default 1113 if(hIcon <= 0) { 1114 hIcon = extractIcon(getParentIShellFolder(), 1115 getRelativePIDL(), size, true); 1116 if(hIcon <= 0) { 1117 if (isDirectory()) { 1118 return getShell32Icon(4, size); 1119 } else { 1120 return getShell32Icon(1, size); 1121 } 1122 } 1123 } 1124 newIcon = makeIcon(hIcon, size); 1125 disposeIcon(hIcon); 1126 return newIcon; 1127 }); 1128 } 1129 1130 /** 1131 * Gets an icon from the Windows system icon list as an {@code Image} 1132 */ 1133 static Image getSystemIcon(SystemIcon iconType) { 1134 long hIcon = getSystemIcon(iconType.getIconID()); 1135 Image icon = makeIcon(hIcon, 32); 1136 disposeIcon(hIcon); 1137 return icon; 1138 } 1139 1140 /** 1141 * Gets an icon from the Windows system icon list as an {@code Image} 1142 */ 1143 static Image getShell32Icon(int iconID, int size) { 1144 boolean useVGAColors = true; // Will be ignored on XP and later 1145 Toolkit toolkit = Toolkit.getDefaultToolkit(); 1146 String shellIconBPP = (String)toolkit.getDesktopProperty("win.icon.shellIconBPP"); 1147 if (shellIconBPP != null) { 1148 useVGAColors = shellIconBPP.equals("4"); 1149 } 1150 1151 long hIcon = getIconResource("shell32.dll", iconID, size, size, useVGAColors); 1152 if (hIcon != 0) { 1153 Image icon = makeIcon(hIcon, size); 1154 disposeIcon(hIcon); 1155 return icon; 1156 } 1157 return null; 1158 } 1159 1160 /** 1161 * Returns the canonical form of this abstract pathname. Equivalent to 1162 * <code>new Win32ShellFolder2(getParentFile(), this.{@link java.io.File#getCanonicalPath}())</code>. 1163 * 1164 * @see java.io.File#getCanonicalFile 1165 */ 1166 public File getCanonicalFile() throws IOException { 1167 return this; 1168 } 1169 1170 /* 1171 * Indicates whether this is a special folder (includes My Documents) 1172 */ 1173 public boolean isSpecial() { |