Skip to content

Extend existence check in URLImageDescriptor when running without OSGi #3106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -246,24 +246,16 @@ private static String getxPath(String name, int zoom) {
*
* @return {@link String} or <code>null</code> 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();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,20 +123,35 @@ public void testImageFileNameProviderGetxName() {

@Test
public void testImageFileNameProviderGetxName_forFileURL() throws IOException {
URL imageFileURL = tempFolder.newFile("image.png").toURI().toURL();
tempFolder.newFile("[email protected]");
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(), "[email protected]");
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("[email protected]");
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(), "[email protected]");
String imagePath150 = fileNameProvider.getImagePath(150);
assertNull("URLImageDescriptor's ImageFileNameProvider does return a @1.5x path", imagePath150);
} finally {
InternalPolicy.OSGI_AVAILABLE = oldOsgiAvailable;
}
}

@Test
Expand Down
Loading