< prev index next >

src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolder2.java

Print this page




  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&nbsp;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 size) {
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 iconSize = (int) Math.sqrt(iconBits.length);

1017                 final BufferedImage img =
1018                         new BufferedImage(iconSize, iconSize, BufferedImage.TYPE_INT_ARGB);
1019                 img.setRGB(0, 0, iconSize, iconSize, iconBits, 0, iconSize);
1020                 return iconSize == size
1021                         ? img
1022                         : new MultiResolutionIconImage(size, img);
1023             }
1024         }
1025         return null;
1026     }
1027 
1028 
1029     /**
1030      * @return The icon image used to display this shell folder
1031      */
1032     public Image getIcon(final boolean getLargeIcon) {
1033         Image icon = getLargeIcon ? largeIcon : smallIcon;
1034         int size = getLargeIcon ? LARGE_ICON_SIZE : SMALL_ICON_SIZE;
1035         if (icon == null) {
1036             icon =
1037                 invoke(new Callable<Image>() {
1038                     public Image call() {
1039                         Image newIcon = null;
1040                         if (isLink()) {
1041                             Win32ShellFolder2 folder = getLinkLocation(false);
1042                             if (folder != null && folder.isLibrary()) {
1043                                 return folder.getIcon(getLargeIcon);
1044                             }
1045                         }
1046                         if (isFileSystem() || isLibrary()) {
1047                             long parentIShellIcon = (parent != null)
1048                                 ? ((Win32ShellFolder2) parent).getIShellIcon()
1049                                 : 0L;
1050                             long relativePIDL = getRelativePIDL();
1051 
1052                             // These are cached per type (using the index in the system image list)
1053                             int index = getIconIndex(parentIShellIcon, relativePIDL);
1054                             if (index > 0) {
1055                                 Map<Integer, Image> imageCache;
1056                                 if (isLink()) {
1057                                     imageCache = getLargeIcon ? largeLinkedSystemImages : smallLinkedSystemImages;
1058                                 } else {
1059                                     imageCache = getLargeIcon ? largeSystemImages : smallSystemImages;
1060                                 }
1061                                 newIcon = imageCache.get(Integer.valueOf(index));
1062                                 if (newIcon == null) {
1063                                     long hIcon = getIcon(getAbsolutePath(), getLargeIcon);
1064                                     newIcon = makeIcon(hIcon, size);
1065                                     disposeIcon(hIcon);
1066                                     if (newIcon != null) {
1067                                         imageCache.put(Integer.valueOf(index), newIcon);
1068                                     }
1069                                 }
1070                             }
1071                         }
1072 
1073                         if (newIcon == null) {
1074                             // These are only cached per object
1075                             long hIcon = extractIcon(getParentIShellFolder(),
1076                                     getRelativePIDL(), size, false);
1077                             // E_PENDING: loading can take time so get the default
1078                             if(hIcon <= 0) {
1079                                 hIcon = extractIcon(getParentIShellFolder(),
1080                                          getRelativePIDL(), size, true);
1081                                 if(hIcon <= 0) {
1082                                     if (isDirectory()) {
1083                                         return getShell32Icon(4, size); // pick folder icon
1084                                     } else {
1085                                         return getShell32Icon(1, size); // pick file icon
1086                                     }
1087                                 }
1088                             }
1089                             newIcon = makeIcon(hIcon, size);
1090                             disposeIcon(hIcon);
1091                         }
1092 
1093                         if (newIcon == null) {
1094                             newIcon = Win32ShellFolder2.super.getIcon(getLargeIcon);
1095                         }
1096                         return newIcon;
1097                     }
1098                 });
1099         }
1100         return icon;
1101     }
1102 
1103     public Image getIcon(int size) {
1104         return invoke(() -> {
1105             Image newIcon = null;
1106             if (isLink()) {
1107                 Win32ShellFolder2 folder = getLinkLocation(false);
1108                 if (folder != null && folder.isLibrary()) {
1109                     return folder.getIcon(size);
1110                 }
1111             }
1112             long hIcon = extractIcon(getParentIShellFolder(),
1113                     getRelativePIDL(), size, false);
1114 
1115             // E_PENDING: loading can take time so get the default
1116             if (hIcon <= 0) {
1117                 hIcon = extractIcon(getParentIShellFolder(),
1118                         getRelativePIDL(), size, true);
1119                 if (hIcon <= 0) {
1120                     if (isDirectory()) {
1121                         return getShell32Icon(4, size);
1122                     } else {
1123                         return getShell32Icon(1, size);
1124                     }
1125                 }
1126             }
1127             newIcon = makeIcon(hIcon, size);
1128             disposeIcon(hIcon);
1129             return newIcon;
1130         });
1131     }
1132 
1133     /**
1134      * Gets an icon from the Windows system icon list as an {@code Image}
1135      */
1136     static Image getSystemIcon(SystemIcon iconType) {
1137         long hIcon = getSystemIcon(iconType.getIconID());
1138         Image icon = makeIcon(hIcon, 32);
1139         disposeIcon(hIcon);
1140         return icon;
1141     }
1142 
1143     /**
1144      * Gets an icon from the Windows system icon list as an {@code Image}
1145      */
1146     static Image getShell32Icon(int iconID, int size) {
1147         boolean useVGAColors = true; // Will be ignored on XP and later



1148         Toolkit toolkit = Toolkit.getDefaultToolkit();
1149         String shellIconBPP = (String)toolkit.getDesktopProperty("win.icon.shellIconBPP");
1150         if (shellIconBPP != null) {
1151             useVGAColors = shellIconBPP.equals("4");
1152         }
1153 
1154         long hIcon = getIconResource("shell32.dll", iconID, size, size, useVGAColors);
1155         if (hIcon != 0) {
1156             Image icon = makeIcon(hIcon, size);
1157             disposeIcon(hIcon);
1158             return icon;
1159         }
1160         return null;
1161     }
1162 
1163     /**
1164      * Returns the canonical form of this abstract pathname.  Equivalent to
1165      * <code>new&nbsp;Win32ShellFolder2(getParentFile(), this.{@link java.io.File#getCanonicalPath}())</code>.
1166      *
1167      * @see java.io.File#getCanonicalFile
1168      */
1169     public File getCanonicalFile() throws IOException {
1170         return this;
1171     }
1172 
1173     /*
1174      * Indicates whether this is a special folder (includes My Documents)
1175      */
1176     public boolean isSpecial() {


< prev index next >