Skip to content

Commit 5e07d2c

Browse files
committed
Merge FileImageDescriptor into URLImageDescriptor and clean it up
In it's core the FileImageDescriptor queries the given context class for a resource at the given path and then operates on the returned URL or directly fetches the resource's stream. By obtaining the resource's URL immediately and using an URLImageDescriptor with it a lot of similar code and logic from the FileImageDescriptor can be saved. Additionally apply a few minor code clean-ups and remove a unused internal method.
1 parent d2c3469 commit 5e07d2c

File tree

3 files changed

+91
-410
lines changed

3 files changed

+91
-410
lines changed

bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FileImageDescriptor.java

-313
This file was deleted.

bundles/org.eclipse.jface/src/org/eclipse/jface/resource/ImageDescriptor.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import java.net.MalformedURLException;
1717
import java.net.URI;
1818
import java.net.URL;
19+
import java.nio.file.Path;
1920
import java.util.function.Supplier;
2021

22+
import org.eclipse.jface.util.Policy;
2123
import org.eclipse.swt.SWTException;
2224
import org.eclipse.swt.graphics.Device;
2325
import org.eclipse.swt.graphics.Image;
@@ -76,6 +78,9 @@ protected ImageDescriptor() {
7678
ImageDescriptor(boolean shouldBeCached) {
7779
super(shouldBeCached);
7880
}
81+
82+
private static final ImageDescriptor NULL_IMAGE = createFromImageDataProvider(z -> null);
83+
7984
/**
8085
* Creates and returns a new image descriptor from a file.
8186
*
@@ -84,7 +89,21 @@ protected ImageDescriptor() {
8489
* @return a new image descriptor
8590
*/
8691
public static ImageDescriptor createFromFile(Class<?> location, String filename) {
87-
return new FileImageDescriptor(location, filename);
92+
URL url;
93+
if (location == null) {
94+
try {
95+
url = Path.of(filename).toUri().toURL();
96+
} catch (MalformedURLException e) {
97+
Policy.logException(e);
98+
url = null;
99+
}
100+
} else {
101+
url = location.getResource(filename);
102+
}
103+
if (url == null) {
104+
return NULL_IMAGE; // Defer failure to the time when the image is created
105+
}
106+
return new URLImageDescriptor(url);
88107
}
89108

90109
/**

0 commit comments

Comments
 (0)