<AWT Dev> <Swing Dev> [12] JDK-8182043: Access to Windows Large Icons

Shashidhara Veerabhadraiah shashidhara.veerabhadraiah at oracle.com
Fri Sep 28 08:58:43 UTC 2018

Hi Alexey, Thank you for your thorough review. I have updated the copyrights as well and please see below for my comments:


Here is the new Webrev: http://cr.openjdk.java.net/~sveerabhadra/8182043/webrev.04/


Thanks and regards,



From: Alexey Ivanov 
Sent: Tuesday, September 25, 2018 3:00 AM
To: Shashidhara Veerabhadraiah <shashidhara.veerabhadraiah at oracle.com>; Prasanta Sadhukhan <prasanta.sadhukhan at oracle.com>; swing-dev <swing-dev at openjdk.java.net>; awt-dev <awt-dev at openjdk.java.net>
Subject: Re: <AWT Dev> <Swing Dev> [12] JDK-8182043: Access to Windows Large Icons


Hi Shashi,

Please see my comments inline:

On 21/09/2018 23:22, Shashidhara Veerabhadraiah wrote:

Hi Alexey, Thanks for your review and below is the new Webrev.

HYPERLINK "http://cr.openjdk.java.net/%7Esveerabhadra/8182043/webrev.03/"http://cr.openjdk.java.net/~sveerabhadra/8182043/webrev.03/


Please see below for inline comments.


Thanks and regards,


From: Alexey Ivanov 
Sent: Friday, September 21, 2018 2:09 PM
To: Shashidhara Veerabhadraiah HYPERLINK "mailto:shashidhara.veerabhadraiah at oracle.com"<shashidhara.veerabhadraiah at oracle.com>; Prasanta Sadhukhan HYPERLINK "mailto:prasanta.sadhukhan at oracle.com"<prasanta.sadhukhan at oracle.com>; swing-dev HYPERLINK "mailto:swing-dev at openjdk.java.net"<swing-dev at openjdk.java.net>; awt-dev HYPERLINK "mailto:awt-dev at openjdk.java.net"<awt-dev at openjdk.java.net>
Subject: Re: <AWT Dev> <Swing Dev> [12] JDK-8182043: Access to Windows Large Icons


Hi Shashi,

What is the purpose of new SystemIcon class?
It's not used anywhere but the provided test. Is this class really needed then?
Is it supposed to become the public API for accessing system icons?
Why can't FileSystemView be used for that purpose as it was proposed in Semyon's review?
[Shashi] SystemIcon is going to be the front face to access the icons and that is the purpose of this class. The reason for choosing this is that FileSystemView class can be used internally and did not wanted to expose it externally too. Externally exposing may cause certain restriction in maintaining the classes hence the indirection.

Still, I cannot understand the rationale for a new class the only purpose of which is to provide public access to getSystemIcon(File, int, int).
FileSystemView is already a public class, and it's used internally. (I guess it would not have existed, if it hadn't.) It has a public method getSystemIcon(File). As such, extending its functionality to get access to larger icons seems logical. This is what the new protected getSystemIcon(File f, int size) does.

It can be made public to facilitate access to file icons.
After all, protected method is also a contract, it cannot be changed without affecting backward compatibility.

It is this new protected method that performs the task of getting the icon from the system.

Do we really need other methods?

[Shashi] I think that system icons functions as part of filesystemview class is also a kind of corrupted creation of the filesystemview class. Icons forms a different functionality compared to file system and should have been kept as a separate class in the first place.


Regarding the methods, am not sure does it required or not but I added them to be complete. Some variant is required to provide the original unmodified icon as the system returns and provide the scaled icon as well.

278     public File createNewFolder(File containingDir) throws IOException {
279         return null;
280     }
You had to implement the abstract method from FileSystemView. It's one more point to make system icon available right from FileSystemView.
This implementation should rather throw an exception.

[Shashi] IOException? I think it's not needed as we won't be performing any such actions. But yes I had to implement this place holder to be complete.

60         protected File file;
This field is redundant, in my opinion. It would be quite expensive to instantiate SystemIcon object for each file. It can safely be removed, then only methods which take additional File parameter will be left.
(The field could be final as it cannot be changed and should not be changed.)

[Shashi] Updated.

65     public SystemIcon() {
66         this.file = null;
67     }
Should rather call this(null) constructor.

[Shashi] Correct and updated.

112     public Icon getSystemIcon(int width, int height) {
Are methods with width / height parameters needed? Icons are usually square.

[Shashi] Flexibility is ok I think. It would fall back to the same function though. Though the native does not have the function and since because of scaling we can support that. Am not sure where it can be useful but being flexible is ok I think!!

You repeat checks if f is null, width and height checks in each and every method. I guess parameter validation could be extract into a separate method. You will avoid lots of cope duplication.

[Shashi] Updated

Since it's a completely new API, I suggest throwing IllegalArgumentException with appropriate message in the cases where a parameter (file, width and height) fails validation.

[Shashi] Updated.

210         int size;
211         if(width > height) {
212             size = width;
213         } else {
214             size = height;
215         }
This code can be simplified to
int size = Math.max(width, height);
Concise and clear.
A helper method which validates the parameters could also return this value. Thus, again, avoiding code duplication among many methods in this class.

[Shashi] Updated.

There are lots of tabs in this file. Tabs must be replaced with spaces.
if's are inconsistent throughout the code: some are with space, some are without. Please add the space everyone to align with Java Code Conventions.
Please also sort the imports and remove unused ones.

[Shashi] Updated

 259      * Icon for a file, directory, or folder as it would be displayed in
 260      * a system file browser for the requested size.
For getXXX, it's better to start description with "Returns." so it aligns to other similar methods.
However, I see the new method follows description of getIcon(boolean).

[Shashi] Because as you said rightly it follows the getIcon(boolean)

Is it possible to update documentation to the existing getSystemIcon(File)?
Should I file a separate bug to update the documentation?

Documentation also references a non-public class ShellFolder. Should this reference be removed from documentation as the access to non-public classes is restricted? It does not add much value.

[Shashi] Updated.

 265      * @param size width and height of the icon in pixels to be scaled(valid range: 1 to 256)
Why is it "to be scaled"? I would expect to get the icon of the requested size. At the same time, the icon can be scaled to the requested size if the requested size is not available.

[Shashi] User has no restriction of mentioning any size but the platform may have a limitation of size. Since we are supporting a set of different versions of platforms, platform may limit the size of the icon to a particular size, in which case it will be scaled to the user requested size.

I understand that. However, I think the suggested description does not convey the meaning correctly.
The method will return the icon of the requested size, won't it?
So the correct description is:
@param size width and height of the icon in pixels (valid range: 1 to 256)

The fact the returned icon may be scaled if the requested size is not available must be described in the method documentation as well as in @return line:
@return an icon of the requested size (possibly scaled) as it would be displayed by a native file chooser

[Shashi] Updated

270     protected Icon getSystemIcon(File f, int size) {
Can't the method be public? It was in Semyon's review.

[Shashi] Because of the indirection, this method can stay as protected. I think it is always good to be of using protected than making everything public. Also that is the advantage of adding the SystemIcon class.

Sorry I don't see any advantage of having SystemIcon class over making this method public as I outlined above.

 266      * @return an icon as it would be displayed by a native file chooser
An icon of the requested size (possibly scaled) as.

 275         if(size > 256 || size < 1) {
 276             return null;
 277         }
Please add space between if and the opening parenthesis.
You can throw InvalidArgumentException in this case.
Does size of 1 make any sense?

[Shashi] Done. I can only say that 0 does not make sense. Check is to see that it is not less than 1.

What about throwing InvalidArgumentException when size parameter is invalid?

[Shashi] Updated

I understand that check is to make sure size is at least 1. However, icon of 1 pixel size does not make any sense. Should the minimum be a more sensible of 4?
It's a concern for discussion.
[Shashi] On the native side, there is no restriction so I think we can keep this open.

 202     /**
 203      * @param size size of the icon > 0
 204      * @return The icon used to display this shell folder
 205      */
Can you add a short description of the purpose of this method? "Returns the icon of the specified size used to display this shell folder"?
A similar description can be added to the method above it:
198     public Image getIcon(boolean getLargeIcon) {

[Shashi] Updated. Thank you.

Thank you for updating @return clause of the Javadoc.
My intention was to add a generic description of the method as well:
202     /**
202      * Returns the icon of the specified size used to display this shell folder.
202      *
203      * @param size size of the icon > 0
204      * @return The icon used to display this shell folder
205      */

Such description could also be added to method above getIcon(boolean getLargeIcon), at line 198.

Should the range of size parameter be specified? For example, 1-256 as in FileSystemView.
[Shashi] Updated

 944             hres = pIcon->Extract(szBuf, index, &hIcon, 0, size);
Please use NULL instead of 0. This way it's clear you pass a null pointer rather an integer with value of 0.

[Shashi] Updated.

974     const int MAX_ICON_SIZE = 128;
I also suggest increasing MAX_ICON_SIZE to 256. Otherwise I see no point in allowing 256 as the maximum size at Java level as you'll never have icon of 256×256 even thought the system may have one.

[Shashi] Per me, the problem is that since we support certain older versions of the platforms, it should not cause an exception at the native level. If everyone agrees for the change then we can change that.

This concern was raised in the previous review too:

I think it's safe to update the value of MAX_ICON_SIZE to 256. The oldest supported version of Windows is Windows 7 which supports 256×256 icons.
Windows XP used icons up to 48×48, but it does not imply the API does not allow loading icon of larger size. Both 128 and 256 should be tested on Windows XP if JDK still runs on it.
[Shashi] I will raise a bug on this and work on it later. I have to see fn_GetIconInfo() and what it returns the values and based on it, it is good to update to 256 I think.

1007     private static Image makeIcon(long hIcon, int bsize) {
bsize has no meaning. Prasanta also asked about the name of the parameter.
I suggest using "size" for parameter, and "iconSize" for local variable.

[Shashi] Updated.

1031         int size = getLargeIcon ? 32 : 16; // large icon is 32 pixels else 16 pixels
Create constants for these magic numbers.
HYPERLINK "http://cr.openjdk.java.net/%7Essadetsky/8182043/webrev.01/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolder2.java.udiff.html"http://cr.openjdk.java.net/~ssadetsky/8182043/webrev.01/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolder2.java.udiff.html

[Shashi] Updated.

1080                                         return getShell32Icon(4, size); // pick folder icon
1081                                     } else {
1082                                         return getShell32Icon(1, size); // pick file icon
Also create constants for 4 and 1 as Prasanta suggested.
Creating a constant costs you nothing but makes the code more readable without adding comments.

[Shashi] But my view is that if we start doing for every immediate value, then creation of the object and assignment may have some implications. If it is used more than once, yes definitely create the constant. Please let me know if you think otherwise. This I follow it as a standard by practice. 

Primitive type constants do not have any performance penalty as they're resolved at compile time.
There's no difference if you write getShell32Icon(FOLDER_ICON_INDEX, size) or getShell32Icon(4, size). Yet the former is much clearer, isn't it?

With the constants, the if expression could be replaced with the conditional operator:

if(hIcon <= 0) {
    return getShell32Icon(isDirectory() ? FOLDER_ICON_INDEX : FILE_ICON_INDEX, size);

I agree not every number in code should be defined as a constant. Yet I'm for using constants in this particular piece of code.

These values are used at least twice in Win32ShellFolder2.java: lines 1081-1085 and 1119-1123.

[Shashi] Updated.

1138         Image icon = makeIcon(hIcon, 32);
Now should use LARGE_ICON_SIZE. 

[Shashi] Updated

1113             if(hIcon <= 0) {
1116                 if(hIcon <= 0) {
Please add space between if and the opening parenthesis.

[Shashi] Updated.

1078                             if(hIcon <= 0) {
1081                                 if(hIcon <= 0) {
These (in the method above) could also be updated.

 382                     return Win32ShellFolder2.getShell32Icon(i, key.startsWith("shell32LargeIcon ")?
 383                                                                 32 : 16);
You can use constants declared for icon size in from Win32ShellFolder2 because they're already imported:
  42 import static sun.awt.shell.Win32ShellFolder2.*;

[Shashi] Updated.

 382                     return Win32ShellFolder2.getShell32Icon(i, key.startsWith("shell32LargeIcon ")?
 383                                                                 LARGE_ICON_SIZE : SMALL_ICON_SIZE);

May I suggest updating formatting to:
                    return Win32ShellFolder2.getShell32Icon(i,
                            key.startsWith("shell32LargeIcon ") ? LARGE_ICON_SIZE : SMALL_ICON_SIZE);
or even
                    return Win32ShellFolder2.getShell32Icon(i,
                            key.startsWith("shell32LargeIcon ") ? LARGE_ICON_SIZE
                                                                : SMALL_ICON_SIZE);
(where : aligns with ?)
[Shashi] Updated

Then the code at these lines needs to be updated too:
 129             STANDARD_VIEW_BUTTONS[iconIndex] = (size == 16)
 130                     ? img
 131                     : new MultiResolutionIconImage(16, img);

See HYPERLINK "http://cr.openjdk.java.net/%7Essadetsky/8182043/webrev.01/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java.udiff.html"http://cr.openjdk.java.net/~ssadetsky/8182043/webrev.01/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java.udiff.html


Could you please order the imports? Your IDE would be happy to do it for you.
Could you also add spaces between if and the opening parenthesis?
(Or at least be consistent with it.)

58             ImageIcon icon = (ImageIcon)sysIcon.getSystemIcon(file, size, size);
Casts should be followed by a blank space.

67             else if (icon.getIconWidth() != size) {
else is redundant as the preceding if throws an exception.

[Shashi] Updated.

Could you please organize imports?
There are only three classes used.

41             System.out.println("Windows detected: will run sytem icons test");
typo: system

Since the test is Windows-specific, it can be declared using @requires tag of JTreg:
@requires os.family == "windows"
[Shashi] Updated.



On 04/09/2018 10:39, Shashidhara Veerabhadraiah wrote:

Hi All, Please find the updated Webrev per the discussion:


HYPERLINK "http://cr.openjdk.java.net/%7Esveerabhadra/8182043/webrev.02/"http://cr.openjdk.java.net/~sveerabhadra/8182043/webrev.02/


Thanks and regards,



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/awt-dev/attachments/20180928/36cdb029/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 27030 bytes
Desc: not available
URL: <http://mail.openjdk.java.net/pipermail/awt-dev/attachments/20180928/36cdb029/winmail-0001.dat>

More information about the awt-dev mailing list