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

Alexey Ivanov alexey.ivanov at oracle.com
Mon Sep 24 21:30:29 UTC 2018


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.
>
> http://cr.openjdk.java.net/~sveerabhadra/8182043/webrev.03/ 
> <http://cr.openjdk.java.net/%7Esveerabhadra/8182043/webrev.03/>
>
> Please see below for inline comments.
>
> Thanks and regards,
> Shashi
>
> *From:*Alexey Ivanov
> *Sent:* Friday, September 21, 2018 2:09 PM
> *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,
>
> SystemIcon.java
> 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?


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.


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.)

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

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

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.

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.

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.


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.


> FileSystemView.java
>  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)
>

Okay.
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.

>  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

> 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?

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.

>
>
> ShellFolder.java
>  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.

> ShellFolder2.cpp
>  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:
http://mail.openjdk.java.net/pipermail/awt-dev/2017-September/013115.html

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.

>
>
> Win32ShellFolder2.java
> 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.
> http://cr.openjdk.java.net/~ssadetsky/8182043/webrev.01/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolder2.java.udiff.html 
> <http://cr.openjdk.java.net/%7Essadetsky/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.


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

> 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.

> Win32ShellFolderManager2.java
>  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 ?)

>
> 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 
> http://cr.openjdk.java.net/~ssadetsky/8182043/webrev.01/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java.udiff.html 
> <http://cr.openjdk.java.net/%7Essadetsky/8182043/webrev.01/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java.udiff.html>
>
>
> SystemIconTest.java
>
> 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"


Regards,
Alexey

>
>
>
> Regards,
> Alexey
>
> *//*
>
> On 04/09/2018 10:39, Shashidhara Veerabhadraiah wrote:
>
>     Hi All, Please find the updated Webrev per the discussion:
>
>     http://cr.openjdk.java.net/~sveerabhadra/8182043/webrev.02/
>     <http://cr.openjdk.java.net/%7Esveerabhadra/8182043/webrev.02/>
>
>     Thanks and regards,
>
>     Shashi
>
>
>     <SNIP>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/awt-dev/attachments/20180924/306af1bd/attachment-0001.html>


More information about the awt-dev mailing list