43
43
import org .eclipse .swt .graphics .ImageLoader ;
44
44
import org .eclipse .swt .internal .DPIUtil .ElementAtZoom ;
45
45
import org .eclipse .swt .internal .NativeImageLoader ;
46
+ import org .eclipse .swt .internal .image .FileFormat ;
46
47
47
48
/**
48
49
* An image descriptor that loads its image information from a file.
@@ -54,7 +55,7 @@ private class ImageProvider implements ImageFileNameProvider {
54
55
@ Override
55
56
public String getImagePath (int zoom ) {
56
57
final boolean logIOException = zoom == 100 ;
57
- if (zoom == 100 ) {
58
+ if (canLoadAtZoom ( zoom ) || zoom == 100 ) {
58
59
return getFilePath (name , logIOException );
59
60
}
60
61
SourceAtZoom <String > xName = getxName (name , zoom );
@@ -155,8 +156,8 @@ public ImageData getImageData(int zoom) {
155
156
* file cannot be found
156
157
*/
157
158
private SourceAtZoom <InputStream > getStream (int zoom ) {
158
- if (zoom == 100 ) {
159
- return getStream (new SourceAtZoom <>(name , 100 ));
159
+ if (canLoadAtZoom ( zoom ) || zoom == 100 ) {
160
+ return getStream (new SourceAtZoom <>(name , zoom ));
160
161
}
161
162
162
163
SourceAtZoom <InputStream > xstream = getStream (getxName (name , zoom ));
@@ -172,6 +173,18 @@ private SourceAtZoom<InputStream> getStream(int zoom) {
172
173
return null ;
173
174
}
174
175
176
+ @ SuppressWarnings ("restriction" )
177
+ private boolean canLoadAtZoom (int zoom ) {
178
+ try (InputStream in = getStream (new SourceAtZoom <>(name , zoom )).source ()) {
179
+ if (in != null ) {
180
+ return FileFormat .canLoadAtZoom (new ElementAtZoom <>(in , 100 ), zoom );
181
+ }
182
+ } catch (IOException e ) {
183
+ Policy .getLog ().log (new Status (IStatus .ERROR , Policy .JFACE , e .getLocalizedMessage (), e ));
184
+ }
185
+ return false ;
186
+ }
187
+
175
188
/**
176
189
* try to obtain a stream for a given name, if the name does not match a valid
177
190
* resource null is returned
@@ -180,9 +193,9 @@ private SourceAtZoom<InputStream> getStream(int zoom) {
180
193
* @return an {@link InputStream} to read from, or <code>null</code> if fileName
181
194
* does not denotes an existing resource
182
195
*/
196
+ @ SuppressWarnings ("resource" )
183
197
private SourceAtZoom <InputStream > getStream (SourceAtZoom <String > fileName ) {
184
198
if (fileName != null ) {
185
- // TODO DO we need to close these?
186
199
if (location != null ) {
187
200
return new SourceAtZoom <>(location .getResourceAsStream (fileName .source ()), fileName .zoom ());
188
201
}
0 commit comments