Skip to content

Commit

Permalink
Create static variant of ImageDescriptor.imageDescriptorFromURI()
Browse files Browse the repository at this point in the history
This method is intended to be used as a wrapper for createFromURL() to
avoid having to deal with the checked MalformedURLException. However,
this method is effectively unusable, as it requires the user to already
have an instance of the ImageDescriptor they want to created.

Instead, create a new, static createFromURI() method, mark the old
method as deprecated and internally delegate to the new method.

The old method was created as a response to Bug 559656 [1], in order to
match the signature of IResourceUtilities. But because the latter is
accessed via an OSGi service, it doesn't need to be static. Which is
something that doesn't really work for image descriptors.

[1] https://bugs.eclipse.org/bugs/show_bug.cgi?id=559656
  • Loading branch information
ptziegler committed Nov 5, 2024
1 parent 136afb4 commit 7307874
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bundles/org.eclipse.jface/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.jface;singleton:=true
Bundle-Version: 3.35.100.qualifier
Bundle-Version: 3.36.0.qualifier
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.jface,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -205,25 +205,50 @@ public static ImageDescriptor createFromURLSupplier(boolean useMissingImage, Sup
}

/**
* Convenient method to create an ImageDescriptor from an URI
* Convenient method to create an ImageDescriptor from an URI.
*
* Delegates to ImageDescriptor createFromURL
* Delegates to {@link ImageDescriptor#createFromURL(URL)}. <em>Important</em>
* This method should only be used when it's guaranteed that the given
* {@link URI} is also a valid {@link URL}, in order to avoid the
* {@link MalformedURLException} thrown by {@link URI#toURL()}.
*
* If the URI is {@code null} or not a valid {@link URL}, then an image from
* {@link #getMissingImageDescriptor()} will be returned.
*
* @param uriIconPath The URI of the image file.
* @return a new image descriptor
*
* @since 3.19
* @since 3.36
*/
public ImageDescriptor imageDescriptorFromURI(URI uriIconPath) {
public static ImageDescriptor createFromURI(URI uriIconPath) {
if (uriIconPath == null) {
return getMissingImageDescriptor();
}
try {
return ImageDescriptor.createFromURL(new URL(uriIconPath.toString()));
} catch (MalformedURLException | NullPointerException e) {
return ImageDescriptor.createFromURL(uriIconPath.toURL());
} catch (MalformedURLException e) {
// return the missing image placeholder to indicate
// the incorrect call without interfering with the user flow
return getMissingImageDescriptor();
}
}

/**
* Convenient method to create an ImageDescriptor from an URI
*
* Delegates to ImageDescriptor createFromURL
*
* @param uriIconPath The URI of the image file.
* @return a new image descriptor
*
* @since 3.19
* @deprecated Use {@link #createFromURI(URI)} instead.
*/
@Deprecated(since = "3.36", forRemoval = true)
public ImageDescriptor imageDescriptorFromURI(URI uriIconPath) {
return createFromURI(uriIconPath);
}

@Override
public Object createResource(Device device) throws DeviceResourceException {
Image result = createImage(false, device);
Expand Down

0 comments on commit 7307874

Please sign in to comment.