eirikbakke commented on code in PR #8114:
URL: https://github.com/apache/netbeans/pull/8114#discussion_r1920728711


##########
platform/openide.util.ui/src/org/openide/util/ImageUtilities.java:
##########
@@ -185,31 +199,87 @@ public static final Image loadImage(String resourceID) {
     }
     
     /**
-     * Loads an image based on resource path.
+     * Loads an image based on a resource path.
      * Exactly like {@link #loadImage(String)} but may do a localized search.
      * For example, requesting 
<code>org/netbeans/modules/foo/resources/foo.gif</code>
      * might actually find 
<code>org/netbeans/modules/foo/resources/foo_ja.gif</code>
      * or <code>org/netbeans/modules/foo/resources/foo_mybranding.gif</code>.
-     * 
-     * <p>Caching of loaded images can be used internally to improve 
performance.
-     * <p> Since version 8.12 the returned image object responds to call
-     * <code>image.getProperty({@link #PROPERTY_URL}, null)</code> by 
returning the internal
-     * {@link URL} of the found and loaded <code>resource</code>. Convenience 
method {@link #findImageBaseURL}
-     * should be used in preference to direct property access.
-     * 
-     * <p>If the current look and feel is 'dark' 
(<code>UIManager.getBoolean("nb.dark.theme")</code>)
-     * then the method first attempts to load image <i>&lt;original file 
name&gt;<b>_dark</b>.&lt;original extension&gt;</i>.
-     * If such file doesn't exist the default one is loaded instead.
-     * </p>
-     * 
+     *
      * @param resource resource path of the image (no initial slash)
      * @param localized true for localized search
-     * @return icon's Image or null if the icon cannot be loaded
+     * @return the icon's Image, or null if the icon cannot be loaded
      */
     public static final Image loadImage(String resource, boolean localized) {
         return loadImageInternal(resource, localized);
     }
 
+    /**
+     * Load an image from a URI/URL. If the URI uses the {@code nbresloc} 
protocol, it is loaded
+     * using the resource loading mechanism provided by {@link 
#loadImage(java.lang.String)}. This
+     * includes handling of SVG icons and dark mode variations.
+     *
+     * <p>This method is intended for use only when a URL or URI must be used 
instead of a resource
+     * path, e.g. in the implementation of pre-existing NetBeans APIs. 
External URLs should be
+     * avoided, as they may be disallowed in the future. Do not use this 
method for new code; prefer
+     * image loading by resource paths instead (e.g. {@link 
#loadImage(String)}).
+     *
+     * @param uri the URI of the image, possibly with the nbresloc protocol
+     * @return the loaded image, or either null or an uninitialized image if 
the image was not
+     *         available
+     * @since 7.36
+     */
+    public static final Image loadImage(URI uri) {
+        Parameters.notNull("icon", uri);
+        String scheme = uri.getScheme();
+        if (scheme.equals("nbresloc")) { // NOI18N
+            // Apply our dedicated handling logic. Omit the initial slash of 
the path.
+            return loadImage(uri.getPath().substring(1), false);
+        } else {
+            if (!(scheme.equals("file") ||
+                scheme.equals("jar") && uri.toString().startsWith("jar:file:") 
||
+                scheme.equals("file")))
+            {
+                LOGGER.log(Level.WARNING, "loadImage(URI) called with unusual 
URI: {0}", uri);
+            }
+            try {
+                /* Observed to return an image with size (-1, -1) if URL 
points to a non-existent
+                file (after ensureLoaded(Image) is called). */
+                return Toolkit.getDefaultToolkit().createImage(uri.toURL());
+            } catch (MalformedURLException e) {
+                LOGGER.log(Level.WARNING, "Malformed URL passed to 
loadImage(URI)", e);
+                return null;
+            }
+        }
+    }
+
+    /**
+     * Convert an {@link Icon} instance to a delegating {@link ImageIcon} 
instance. If the supplied
+     * Icon instance is an SVG icon or has other HiDPI capabilities provided 
by the methods in this
+     * class, they are preserved by the conversion.
+     *
+     * <p>This method is intended for use only when existing APIs require the 
use of ImageIcon. In
+     * most other situations it is preferable to use the more general Icon 
type.
+     *
+     * @param icon the icon to be converted
+     * @return the converted instance, or null if {@code icon} was null
+     * @since 7.36
+     */
+    public static final ImageIcon toImageIcon(Icon icon) {
+        if (icon == null) {
+            return null;
+        }

Review Comment:
   When doing bulk refactoring in the style of 
https://github.com/apache/netbeans/pull/8109, it is useful if `icon` and 
`toImageIcon(icon)` is guaranteed not to change the current behavior. 
Otherwise, existing features could break e.g. if they previously relied on null 
values being quietly accepted.
   
   (Often I can read the existing code and be 95% sure a null value is never 
passed... but getting the last 5% of certainty is very difficult. And I don't 
want to have to insert null checks everywhere, if I'm changing 20 call sites 
and each case is really not supposed to be null in the first place.)
   
   Perhaps I will specify in the Javadoc that null is not a valid input, but 
log a warning if a null does occur, and still return null in that unexpected 
case?
   
   > what do you think about keeping the naming pattern? -> icon2ImageIcon?
   
   Good idea, will rename.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to