57 * a series of identifiers and can be either relative to the desktop 58 * (an absolute PIDL), or relative to the shell folder that contains them. 59 * Some Win32 functions can take absolute or relative PIDL values, and 60 * others can only accept relative values. 61 * <BR> 62 * The second data structure is an IShellFolder COM interface. Using 63 * this interface, one can enumerate the relative PIDLs in a shell 64 * folder, get attributes, etc. 65 * <BR> 66 * All Win32ShellFolder2 objects which are folder types (even non-file 67 * system folders) contain an IShellFolder object. Files are named in 68 * directories via relative PIDLs. 69 * 70 * @author Michael Martak 71 * @author Leif Samuelsson 72 * @author Kenneth Russell 73 * @since 1.4 */ 74 @SuppressWarnings("serial") // JDK-implementation class 75 final class Win32ShellFolder2 extends ShellFolder { 76 77 private static native void initIDs(); 78 79 static { 80 initIDs(); 81 } 82 83 // Win32 Shell Folder Constants 84 public static final int DESKTOP = 0x0000; 85 public static final int INTERNET = 0x0001; 86 public static final int PROGRAMS = 0x0002; 87 public static final int CONTROLS = 0x0003; 88 public static final int PRINTERS = 0x0004; 89 public static final int PERSONAL = 0x0005; 90 public static final int FAVORITES = 0x0006; 91 public static final int STARTUP = 0x0007; 92 public static final int RECENT = 0x0008; 93 public static final int SENDTO = 0x0009; 94 public static final int BITBUCKET = 0x000a; 95 public static final int STARTMENU = 0x000b; 96 public static final int DESKTOPDIRECTORY = 0x0010; 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() { | 57 * a series of identifiers and can be either relative to the desktop 58 * (an absolute PIDL), or relative to the shell folder that contains them. 59 * Some Win32 functions can take absolute or relative PIDL values, and 60 * others can only accept relative values. 61 * <BR> 62 * The second data structure is an IShellFolder COM interface. Using 63 * this interface, one can enumerate the relative PIDLs in a shell 64 * folder, get attributes, etc. 65 * <BR> 66 * All Win32ShellFolder2 objects which are folder types (even non-file 67 * system folders) contain an IShellFolder object. Files are named in 68 * directories via relative PIDLs. 69 * 70 * @author Michael Martak 71 * @author Leif Samuelsson 72 * @author Kenneth Russell 73 * @since 1.4 */ 74 @SuppressWarnings("serial") // JDK-implementation class 75 final class Win32ShellFolder2 extends ShellFolder { 76 77 static final int SMALL_ICON_SIZE = 16; 78 static final int LARGE_ICON_SIZE = 32; 79 80 private static native void initIDs(); 81 82 static { 83 initIDs(); 84 } 85 86 // Win32 Shell Folder Constants 87 public static final int DESKTOP = 0x0000; 88 public static final int INTERNET = 0x0001; 89 public static final int PROGRAMS = 0x0002; 90 public static final int CONTROLS = 0x0003; 91 public static final int PRINTERS = 0x0004; 92 public static final int PERSONAL = 0x0005; 93 public static final int FAVORITES = 0x0006; 94 public static final int STARTUP = 0x0007; 95 public static final int RECENT = 0x0008; 96 public static final int SENDTO = 0x0009; 97 public static final int BITBUCKET = 0x000a; 98 public static final int STARTMENU = 0x000b; 99 public static final int DESKTOPDIRECTORY = 0x0010; 963 964 965 // Icons 966 967 private static Map<Integer, Image> smallSystemImages = new HashMap<>(); 968 private static Map<Integer, Image> largeSystemImages = new HashMap<>(); 969 private static Map<Integer, Image> smallLinkedSystemImages = new HashMap<>(); 970 private static Map<Integer, Image> largeLinkedSystemImages = new HashMap<>(); 971 972 // NOTE: this method uses COM and must be called on the 'COM thread'. See ComInvoker for the details 973 private static native long getIShellIcon(long pIShellFolder); 974 975 // NOTE: this method uses COM and must be called on the 'COM thread'. See ComInvoker for the details 976 private static native int getIconIndex(long parentIShellIcon, long relativePIDL); 977 978 // Return the icon of a file system shell folder in the form of an HICON 979 private static native long getIcon(String absolutePath, boolean getLargeIcon); 980 981 // NOTE: this method uses COM and must be called on the 'COM thread'. See ComInvoker for the details 982 private static native long extractIcon(long parentIShellFolder, long relativePIDL, 983 int size, boolean getDefaultIcon); 984 985 // Returns an icon from the Windows system icon list in the form of an HICON 986 private static native long getSystemIcon(int iconID); 987 private static native long getIconResource(String libName, int iconID, 988 int cxDesired, int cyDesired, 989 boolean useVGAColors); 990 // Note: useVGAColors is ignored on XP and later 991 992 // Return the bits from an HICON. This has a side effect of setting 993 // the imageHash variable for efficient caching / comparing. 994 private static native int[] getIconBits(long hIcon); 995 // Dispose the HICON 996 private static native void disposeIcon(long hIcon); 997 998 // Get buttons from native toolbar implementation. 999 static native int[] getStandardViewButton0(int iconIndex, boolean small); 1000 1001 // Should be called from the COM thread 1002 private long getIShellIcon() { 1003 if (pIShellIcon == -1L) { 1004 pIShellIcon = getIShellIcon(getIShellFolder()); 1005 } 1006 1007 return pIShellIcon; 1008 } 1009 1010 private static Image makeIcon(long hIcon, int bsize) { 1011 if (hIcon != 0L && hIcon != -1L) { 1012 // Get the bits. This has the side effect of setting the imageHash value for this object. 1013 final int[] iconBits = getIconBits(hIcon); 1014 if (iconBits != null) { 1015 // icons are always square 1016 final int size = (int) Math.sqrt(iconBits.length); 1017 final BufferedImage img = 1018 new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); 1019 img.setRGB(0, 0, size, size, iconBits, 0, size); 1020 return size == bsize 1021 ? img 1022 : new MultiResolutionIconImage(bsize, img); 1023 } 1024 } 1025 return null; 1026 } 1027 1028 public Image getIcon(int size) { 1029 return invoke(() -> { 1030 Image newIcon = null; 1031 if (isLink()) { 1032 Win32ShellFolder2 folder = getLinkLocation(false); 1033 if (folder != null && folder.isLibrary()) { 1034 return folder.getIcon(size); 1035 } 1036 } 1037 long hIcon = extractIcon(getParentIShellFolder(), 1038 getRelativePIDL(), size, false); 1039 // E_PENDING: loading can take time so get the default 1040 if(hIcon <= 0) { 1041 hIcon = extractIcon(getParentIShellFolder(), 1042 getRelativePIDL(), size, true); 1043 if(hIcon <= 0) { 1044 if (isDirectory()) { 1045 return getShell32Icon(4, size); 1046 } else { 1047 return getShell32Icon(1, size); 1048 } 1049 } 1050 } 1051 newIcon = makeIcon(hIcon, size); 1052 disposeIcon(hIcon); 1053 return newIcon; 1054 }); 1055 } 1056 1057 /** 1058 * @return The icon image used to display this shell folder 1059 */ 1060 public Image getIcon(final boolean getLargeIcon) { 1061 int size = getLargeIcon ? LARGE_ICON_SIZE : SMALL_ICON_SIZE; 1062 Image icon = getLargeIcon ? largeIcon : smallIcon; 1063 if (icon == null) { 1064 icon = 1065 invoke(new Callable<Image>() { 1066 public Image call() { 1067 Image newIcon = null; 1068 if (isLink()) { 1069 Win32ShellFolder2 folder = getLinkLocation(false); 1070 if (folder != null && folder.isLibrary()) { 1071 return folder.getIcon(getLargeIcon); 1072 } 1073 } 1074 if (isFileSystem() || isLibrary()) { 1075 long parentIShellIcon = (parent != null) 1076 ? ((Win32ShellFolder2) parent).getIShellIcon() 1077 : 0L; 1078 long relativePIDL = getRelativePIDL(); 1079 1080 // These are cached per type (using the index in the system image list) 1081 int index = getIconIndex(parentIShellIcon, relativePIDL); 1082 if (index > 0) { 1083 Map<Integer, Image> imageCache; 1084 if (isLink()) { 1085 imageCache = getLargeIcon ? largeLinkedSystemImages : smallLinkedSystemImages; 1086 } else { 1087 imageCache = getLargeIcon ? largeSystemImages : smallSystemImages; 1088 } 1089 newIcon = imageCache.get(Integer.valueOf(index)); 1090 if (newIcon == null) { 1091 long hIcon = getIcon(getAbsolutePath(), getLargeIcon); 1092 newIcon = makeIcon(hIcon, size); 1093 disposeIcon(hIcon); 1094 if (newIcon != null) { 1095 imageCache.put(Integer.valueOf(index), newIcon); 1096 } 1097 } 1098 } 1099 } 1100 1101 if (newIcon == null) { 1102 // These are only cached per object 1103 long hIcon = extractIcon(getParentIShellFolder(), 1104 getRelativePIDL(), size, false); 1105 // E_PENDING: loading can take time so get the default 1106 if(hIcon <= 0) { 1107 hIcon = extractIcon(getParentIShellFolder(), 1108 getRelativePIDL(), size, true); 1109 if(hIcon <= 0) { 1110 if (isDirectory()) { 1111 return getShell32Icon(4, size); 1112 } else { 1113 return getShell32Icon(1, size); 1114 } 1115 } 1116 } 1117 newIcon = makeIcon(hIcon, size); 1118 disposeIcon(hIcon); 1119 } 1120 1121 return newIcon; 1122 } 1123 }); 1124 } 1125 return icon; 1126 } 1127 1128 /** 1129 * Gets an icon from the Windows system icon list as an {@code Image} 1130 */ 1131 static Image getSystemIcon(SystemIcon iconType) { 1132 long hIcon = getSystemIcon(iconType.getIconID()); 1133 Image icon = makeIcon(hIcon, LARGE_ICON_SIZE); 1134 disposeIcon(hIcon); 1135 return icon; 1136 } 1137 1138 /** 1139 * Gets an icon from the Windows system icon list as an {@code Image} 1140 */ 1141 static Image getShell32Icon(int iconID, int size) { 1142 boolean useVGAColors = true; // Will be ignored on XP and later 1143 1144 Toolkit toolkit = Toolkit.getDefaultToolkit(); 1145 String shellIconBPP = (String)toolkit.getDesktopProperty("win.icon.shellIconBPP"); 1146 if (shellIconBPP != null) { 1147 useVGAColors = shellIconBPP.equals("4"); 1148 } 1149 1150 long hIcon = getIconResource("shell32.dll", iconID, size, size, useVGAColors); 1151 if (hIcon != 0) { 1152 Image icon = makeIcon(hIcon, size); 1153 disposeIcon(hIcon); 1154 return icon; 1155 } 1156 return null; 1157 } 1158 1159 /** 1160 * Returns the canonical form of this abstract pathname. Equivalent to 1161 * <code>new Win32ShellFolder2(getParentFile(), this.{@link java.io.File#getCanonicalPath}())</code>. 1162 * 1163 * @see java.io.File#getCanonicalFile 1164 */ 1165 public File getCanonicalFile() throws IOException { 1166 return this; 1167 } 1168 1169 /* 1170 * Indicates whether this is a special folder (includes My Documents) 1171 */ 1172 public boolean isSpecial() { |