diff --git a/bundles/org.eclipse.jface/META-INF/MANIFEST.MF b/bundles/org.eclipse.jface/META-INF/MANIFEST.MF index 756eac4697e..13debb7c1bc 100644 --- a/bundles/org.eclipse.jface/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.jface/META-INF/MANIFEST.MF @@ -18,7 +18,7 @@ Export-Package: org.eclipse.jface, org.eclipse.jface.fieldassist, org.eclipse.jface.fieldassist.images, org.eclipse.jface.images, - org.eclipse.jface.internal;x-friends:="org.eclipse.ui.workbench,org.eclipse.e4.ui.workbench.renderers.swt", + org.eclipse.jface.internal;x-friends:="org.eclipse.ui.workbench,org.eclipse.e4.ui.workbench.renderers.swt,org.eclipse.jface.tests", org.eclipse.jface.internal.provisional.action;x-friends:="org.eclipse.ui.workbench,org.eclipse.ui.ide", org.eclipse.jface.layout, org.eclipse.jface.menus, 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 5b0368be105..d57d14844a1 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 @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; @@ -31,7 +32,6 @@ import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IAdaptable; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Status; import org.eclipse.jface.internal.InternalPolicy; import org.eclipse.jface.util.Policy; @@ -246,24 +246,16 @@ private static String getxPath(String name, int zoom) { * * @return {@link String} or null if the file cannot be found */ - private static String getFilePath(URL url, boolean logIOException) { + private static String getFilePath(URL url, boolean logException) { try { if (!InternalPolicy.OSGI_AVAILABLE) { - if (FILE_PROTOCOL.equalsIgnoreCase(url.getProtocol())) - return IPath.fromOSString(url.getFile()).toOSString(); - return null; + return getFilePath(url); } url = resolvePathVariables(url); URL locatedURL = FileLocator.toFileURL(url); - if (FILE_PROTOCOL.equalsIgnoreCase(locatedURL.getProtocol())) { - String filePath = IPath.fromOSString(locatedURL.getPath()).toOSString(); - if (Files.exists(Path.of(filePath))) { - return filePath; - } - } - return null; - } catch (IOException e) { - if (logIOException) { + return getFilePath(locatedURL); + } catch (IOException | URISyntaxException e) { + if (logException) { Policy.logException(e); } else if (InternalPolicy.DEBUG_LOG_URL_IMAGE_DESCRIPTOR_MISSING_2x) { String path = url.getPath(); @@ -275,6 +267,16 @@ private static String getFilePath(URL url, boolean logIOException) { } } + private static String getFilePath(URL url) throws URISyntaxException { + if (FILE_PROTOCOL.equalsIgnoreCase(url.getProtocol())) { + Path filePath = Path.of(url.toURI()); + if (Files.exists(filePath)) { + return filePath.toString(); + } + } + return null; + } + private static URL resolvePathVariables(URL url) { URL platformURL = FileLocator.find(url); // Resolve variables within URL's path if (platformURL != null) { diff --git a/tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/images/UrlImageDescriptorTest.java b/tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/images/UrlImageDescriptorTest.java index c86db5d3a34..1e6cfb8a437 100644 --- a/tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/images/UrlImageDescriptorTest.java +++ b/tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/images/UrlImageDescriptorTest.java @@ -26,6 +26,7 @@ import org.eclipse.core.runtime.Adapters; import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.internal.InternalPolicy; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; @@ -122,20 +123,35 @@ public void testImageFileNameProviderGetxName() { @Test public void testImageFileNameProviderGetxName_forFileURL() throws IOException { - URL imageFileURL = tempFolder.newFile("image.png").toURI().toURL(); - tempFolder.newFile("image@2x.png"); - ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageFileURL); + testImageFileNameProviderGetxName_forFileURL(true); + } - ImageFileNameProvider fileNameProvider = Adapters.adapt(descriptor, ImageFileNameProvider.class); - assertNotNull("URLImageDescriptor does not adapt to ImageFileNameProvider", fileNameProvider); - String imagePath100 = fileNameProvider.getImagePath(100); - assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the 100% path", imagePath100); - assertEquals(IPath.fromOSString(imagePath100).lastSegment(), "image.png"); - String imagePath200 = fileNameProvider.getImagePath(200); - assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the @2x path", imagePath200); - assertEquals(IPath.fromOSString(imagePath200).lastSegment(), "image@2x.png"); - String imagePath150 = fileNameProvider.getImagePath(150); - assertNull("URLImageDescriptor's ImageFileNameProvider does return a @1.5x path", imagePath150); + @Test + public void testImageFileNameProviderGetxName_forFileURL_noOSGi() throws IOException { + testImageFileNameProviderGetxName_forFileURL(false); + } + + private void testImageFileNameProviderGetxName_forFileURL(boolean osgiAvailable) throws IOException { + boolean oldOsgiAvailable = InternalPolicy.OSGI_AVAILABLE; + InternalPolicy.OSGI_AVAILABLE = osgiAvailable; + try { + URL imageFileURL = tempFolder.newFile("image.png").toURI().toURL(); + tempFolder.newFile("image@2x.png"); + ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageFileURL); + + ImageFileNameProvider fileNameProvider = Adapters.adapt(descriptor, ImageFileNameProvider.class); + assertNotNull("URLImageDescriptor does not adapt to ImageFileNameProvider", fileNameProvider); + String imagePath100 = fileNameProvider.getImagePath(100); + assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the 100% path", imagePath100); + assertEquals(IPath.fromOSString(imagePath100).lastSegment(), "image.png"); + String imagePath200 = fileNameProvider.getImagePath(200); + assertNotNull("URLImageDescriptor ImageFileNameProvider does not return the @2x path", imagePath200); + assertEquals(IPath.fromOSString(imagePath200).lastSegment(), "image@2x.png"); + String imagePath150 = fileNameProvider.getImagePath(150); + assertNull("URLImageDescriptor's ImageFileNameProvider does return a @1.5x path", imagePath150); + } finally { + InternalPolicy.OSGI_AVAILABLE = oldOsgiAvailable; + } } @Test