diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java index ec2bd5c33b5..9dc50ab74bb 100644 --- a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java @@ -25,6 +25,7 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.function.Function; import java.util.function.Supplier; import org.eclipse.core.runtime.FileLocator; @@ -52,55 +53,24 @@ */ class URLImageDescriptor extends ImageDescriptor implements IAdaptable { - private static class URLImageFileNameProvider implements ImageFileNameProvider { - - private final String url; - - public URLImageFileNameProvider(String url) { - this.url = url; - } - - @Override - public String getImagePath(int zoom) { + private ImageFileNameProvider createURLImageFileNameProvider() { + return zoom -> { URL tempURL = getURL(url); if (tempURL != null) { final boolean logIOException = zoom == 100; if (zoom == 100) { + // Do not take this path if the image file can be scaled up dynamically. + // The calling image will do that itself! return getFilePath(tempURL, logIOException); } - URL xUrl = getxURL(tempURL, zoom); - if (xUrl != null) { - String xResult = getFilePath(xUrl, logIOException); - if (xResult != null) { - return xResult; - } - } - String xpath = FileImageDescriptor.getxPath(url, zoom); - if (xpath != null) { - URL xPathUrl = getURL(xpath); - if (xPathUrl != null) { - return getFilePath(xPathUrl, logIOException); - } - } + return getZoomedImageSource(tempURL, url, zoom, u -> getFilePath(u, logIOException)); } return null; - } - + }; } - private static class URLImageDataProvider implements ImageDataProvider { - - private final String url; - - public URLImageDataProvider(String url) { - this.url = url; - } - - @Override - public ImageData getImageData(int zoom) { - return URLImageDescriptor.getImageData(url, zoom); - } - + private ImageDataProvider createURLImageDataProvider() { + return zoom -> getImageData(zoom); } private static long cumulativeTime; @@ -124,36 +94,37 @@ public ImageData getImageData(int zoom) { @Override public boolean equals(Object o) { - if (!(o instanceof URLImageDescriptor)) { + if (!(o instanceof URLImageDescriptor other)) { return false; } - return ((URLImageDescriptor) o).url.equals(this.url); + return other.url.equals(this.url); } @Override public ImageData getImageData(int zoom) { - return getImageData(url, zoom); - } - - private static ImageData getImageData(String url, int zoom) { URL tempURL = getURL(url); if (tempURL != null) { if (zoom == 100 || canLoadAtZoom(() -> getStream(tempURL), zoom)) { return getImageData(tempURL, 100, zoom); } - URL xUrl = getxURL(tempURL, zoom); - if (xUrl != null) { - ImageData xdata = getImageData(xUrl, zoom, zoom); - if (xdata != null) { - return xdata; - } + return getZoomedImageSource(tempURL, url, zoom, u -> getImageData(u, zoom, zoom)); + } + return null; + } + + private static R getZoomedImageSource(URL url, String urlString, int zoom, Function getImage) { + URL xUrl = getxURL(url, zoom); + if (xUrl != null) { + R xdata = getImage.apply(xUrl); + if (xdata != null) { + return xdata; } - String xpath = FileImageDescriptor.getxPath(url, zoom); - if (xpath != null) { - URL xPathUrl = getURL(xpath); - if (xPathUrl != null) { - return getImageData(xPathUrl, zoom, zoom); - } + } + String xpath = FileImageDescriptor.getxPath(urlString, zoom); + if (xpath != null) { + URL xPathUrl = getURL(xpath); + if (xPathUrl != null) { + return getImage.apply(xPathUrl); } } return null; @@ -165,7 +136,6 @@ private static ImageData getImageData(URL url, int fileZoom, int targetZoom) { } static ImageData loadImageData(InputStream stream, int fileZoom, int targetZoom) { - ImageData result = null; try (InputStream in = stream) { if (in != null) { return loadImageFromStream(new BufferedInputStream(in), fileZoom, targetZoom); @@ -176,9 +146,9 @@ static ImageData loadImageData(InputStream stream, int fileZoom, int targetZoom) // fall through otherwise } } catch (IOException e) { - Policy.getLog().log(Status.error(e.getLocalizedMessage(), e)); + Policy.logException(e); } - return result; + return null; } @SuppressWarnings("restriction") @@ -194,32 +164,15 @@ static boolean canLoadAtZoom(Supplier stream, int zoom) { return FileFormat.canLoadAtZoom(new ElementAtZoom<>(in, 100), zoom); } } catch (IOException e) { - Policy.getLog().log(Status.error(e.getLocalizedMessage(), e)); + Policy.logException(e); } return false; } - /** - * Returns a stream on the image contents. Returns null if a stream could - * not be opened. - * - * @return the stream for loading the data - */ - protected InputStream getStream() { - return getStream(getURL(url)); - } - private static InputStream getStream(URL url) { - if (url == null) { - return null; - } - try { if (InternalPolicy.OSGI_AVAILABLE) { - URL platformURL = FileLocator.find(url); - if (platformURL != null) { - url = platformURL; - } + url = resolvePathVariables(url); } return url.openStream(); } catch (IOException e) { @@ -265,7 +218,7 @@ private static URL getxURL(URL url, int zoom) { } return new URL(url.getProtocol(), url.getHost(), url.getPort(), file); } catch (MalformedURLException e) { - Policy.getLog().log(Status.error(e.getLocalizedMessage(), e)); + Policy.logException(e); } } return null; @@ -284,11 +237,7 @@ private static String getFilePath(URL url, boolean logIOException) { return IPath.fromOSString(url.getFile()).toOSString(); return null; } - - URL platformURL = FileLocator.find(url); - if (platformURL != null) { - url = platformURL; - } + url = resolvePathVariables(url); URL locatedURL = FileLocator.toFileURL(url); if (FILE_PROTOCOL.equalsIgnoreCase(locatedURL.getProtocol())) { String filePath = IPath.fromOSString(locatedURL.getPath()).toOSString(); @@ -310,6 +259,14 @@ private static String getFilePath(URL url, boolean logIOException) { } } + private static URL resolvePathVariables(URL url) { + URL platformURL = FileLocator.find(url); // Resolve variables within URL's path + if (platformURL != null) { + url = platformURL; + } + return url; + } + @Override public Image createImage(boolean returnMissingImageOnError, Device device) { long start = 0; @@ -323,7 +280,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) { // We really want a fresh ImageFileNameProvider instance to make // sure the code that uses created images can use equals(), // see Image#equals - return new Image(device, new URLImageFileNameProvider(url)); + return new Image(device, createURLImageFileNameProvider()); } catch (SWTException | IllegalArgumentException exception) { // If we fail fall back to the slower input stream method. } @@ -334,7 +291,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) { // We really want a fresh ImageDataProvider instance to make // sure the code that uses created images can use equals(), // see Image#equals - image = new Image(device, new URLImageDataProvider(url)); + image = new Image(device, createURLImageDataProvider()); } catch (SWTException e) { if (e.code != SWT.ERROR_INVALID_IMAGE) { throw e; @@ -383,7 +340,7 @@ private static URL getURL(String urlString) { try { result = new URL(urlString); } catch (MalformedURLException e) { - Policy.getLog().log(Status.error(e.getLocalizedMessage(), e)); + Policy.logException(e); } return result; } @@ -394,10 +351,10 @@ public T getAdapter(Class adapter) { return adapter.cast(getURL(url)); } if (adapter == ImageFileNameProvider.class) { - return adapter.cast(new URLImageFileNameProvider(url)); + return adapter.cast(createURLImageFileNameProvider()); } if (adapter == ImageDataProvider.class) { - return adapter.cast(new URLImageDataProvider(url)); + return adapter.cast(createURLImageDataProvider()); } return null; }