25
25
import java .net .URL ;
26
26
import java .nio .file .Files ;
27
27
import java .nio .file .Path ;
28
+ import java .util .function .Function ;
28
29
import java .util .function .Supplier ;
29
30
30
31
import org .eclipse .core .runtime .FileLocator ;
52
53
*/
53
54
class URLImageDescriptor extends ImageDescriptor implements IAdaptable {
54
55
55
- private static class URLImageFileNameProvider implements ImageFileNameProvider {
56
-
57
- private final String url ;
58
-
59
- public URLImageFileNameProvider (String url ) {
60
- this .url = url ;
61
- }
62
-
63
- @ Override
64
- public String getImagePath (int zoom ) {
56
+ private ImageFileNameProvider createURLImageFileNameProvider () {
57
+ return zoom -> {
65
58
URL tempURL = getURL (url );
66
59
if (tempURL != null ) {
67
60
final boolean logIOException = zoom == 100 ;
68
61
if (zoom == 100 ) {
62
+ // Do not take this path if the image file can be scaled up dynamically.
63
+ // The calling image will do that itself!
69
64
return getFilePath (tempURL , logIOException );
70
65
}
71
- URL xUrl = getxURL (tempURL , zoom );
72
- if (xUrl != null ) {
73
- String xResult = getFilePath (xUrl , logIOException );
74
- if (xResult != null ) {
75
- return xResult ;
76
- }
77
- }
78
- String xpath = FileImageDescriptor .getxPath (url , zoom );
79
- if (xpath != null ) {
80
- URL xPathUrl = getURL (xpath );
81
- if (xPathUrl != null ) {
82
- return getFilePath (xPathUrl , logIOException );
83
- }
84
- }
66
+ return getZoomedImageSource (tempURL , url , zoom , u -> getFilePath (u , logIOException ));
85
67
}
86
68
return null ;
87
- }
88
-
69
+ };
89
70
}
90
71
91
- private static class URLImageDataProvider implements ImageDataProvider {
92
-
93
- private final String url ;
94
-
95
- public URLImageDataProvider (String url ) {
96
- this .url = url ;
97
- }
98
-
99
- @ Override
100
- public ImageData getImageData (int zoom ) {
101
- return URLImageDescriptor .getImageData (url , zoom );
102
- }
103
-
72
+ private ImageDataProvider createURLImageDataProvider () {
73
+ return zoom -> getImageData (zoom );
104
74
}
105
75
106
76
private static long cumulativeTime ;
@@ -124,36 +94,37 @@ public ImageData getImageData(int zoom) {
124
94
125
95
@ Override
126
96
public boolean equals (Object o ) {
127
- if (!(o instanceof URLImageDescriptor )) {
97
+ if (!(o instanceof URLImageDescriptor other )) {
128
98
return false ;
129
99
}
130
- return (( URLImageDescriptor ) o ) .url .equals (this .url );
100
+ return other .url .equals (this .url );
131
101
}
132
102
133
103
@ Override
134
104
public ImageData getImageData (int zoom ) {
135
- return getImageData (url , zoom );
136
- }
137
-
138
- private static ImageData getImageData (String url , int zoom ) {
139
105
URL tempURL = getURL (url );
140
106
if (tempURL != null ) {
141
107
if (zoom == 100 || canLoadAtZoom (() -> getStream (tempURL ), zoom )) {
142
108
return getImageData (tempURL , 100 , zoom );
143
109
}
144
- URL xUrl = getxURL (tempURL , zoom );
145
- if (xUrl != null ) {
146
- ImageData xdata = getImageData (xUrl , zoom , zoom );
147
- if (xdata != null ) {
148
- return xdata ;
149
- }
110
+ return getZoomedImageSource (tempURL , url , zoom , u -> getImageData (u , zoom , zoom ));
111
+ }
112
+ return null ;
113
+ }
114
+
115
+ private static <R > R getZoomedImageSource (URL url , String urlString , int zoom , Function <URL , R > getImage ) {
116
+ URL xUrl = getxURL (url , zoom );
117
+ if (xUrl != null ) {
118
+ R xdata = getImage .apply (xUrl );
119
+ if (xdata != null ) {
120
+ return xdata ;
150
121
}
151
- String xpath = FileImageDescriptor . getxPath ( url , zoom );
152
- if ( xpath != null ) {
153
- URL xPathUrl = getURL ( xpath );
154
- if ( xPathUrl != null ) {
155
- return getImageData (xPathUrl , zoom , zoom );
156
- }
122
+ }
123
+ String xpath = FileImageDescriptor . getxPath ( urlString , zoom );
124
+ if ( xpath != null ) {
125
+ URL xPathUrl = getURL ( xpath );
126
+ if (xPathUrl != null ) {
127
+ return getImage . apply ( xPathUrl );
157
128
}
158
129
}
159
130
return null ;
@@ -165,7 +136,6 @@ private static ImageData getImageData(URL url, int fileZoom, int targetZoom) {
165
136
}
166
137
167
138
static ImageData loadImageData (InputStream stream , int fileZoom , int targetZoom ) {
168
- ImageData result = null ;
169
139
try (InputStream in = stream ) {
170
140
if (in != null ) {
171
141
return loadImageFromStream (new BufferedInputStream (in ), fileZoom , targetZoom );
@@ -176,9 +146,9 @@ static ImageData loadImageData(InputStream stream, int fileZoom, int targetZoom)
176
146
// fall through otherwise
177
147
}
178
148
} catch (IOException e ) {
179
- Policy .getLog (). log ( Status . error ( e . getLocalizedMessage (), e ) );
149
+ Policy .logException ( e );
180
150
}
181
- return result ;
151
+ return null ;
182
152
}
183
153
184
154
@ SuppressWarnings ("restriction" )
@@ -194,32 +164,15 @@ static boolean canLoadAtZoom(Supplier<InputStream> stream, int zoom) {
194
164
return FileFormat .canLoadAtZoom (new ElementAtZoom <>(in , 100 ), zoom );
195
165
}
196
166
} catch (IOException e ) {
197
- Policy .getLog (). log ( Status . error ( e . getLocalizedMessage (), e ) );
167
+ Policy .logException ( e );
198
168
}
199
169
return false ;
200
170
}
201
171
202
- /**
203
- * Returns a stream on the image contents. Returns null if a stream could
204
- * not be opened.
205
- *
206
- * @return the stream for loading the data
207
- */
208
- protected InputStream getStream () {
209
- return getStream (getURL (url ));
210
- }
211
-
212
172
private static InputStream getStream (URL url ) {
213
- if (url == null ) {
214
- return null ;
215
- }
216
-
217
173
try {
218
174
if (InternalPolicy .OSGI_AVAILABLE ) {
219
- URL platformURL = FileLocator .find (url );
220
- if (platformURL != null ) {
221
- url = platformURL ;
222
- }
175
+ url = resolvePathVariables (url );
223
176
}
224
177
return url .openStream ();
225
178
} catch (IOException e ) {
@@ -265,7 +218,7 @@ private static URL getxURL(URL url, int zoom) {
265
218
}
266
219
return new URL (url .getProtocol (), url .getHost (), url .getPort (), file );
267
220
} catch (MalformedURLException e ) {
268
- Policy .getLog (). log ( Status . error ( e . getLocalizedMessage (), e ) );
221
+ Policy .logException ( e );
269
222
}
270
223
}
271
224
return null ;
@@ -284,11 +237,7 @@ private static String getFilePath(URL url, boolean logIOException) {
284
237
return IPath .fromOSString (url .getFile ()).toOSString ();
285
238
return null ;
286
239
}
287
-
288
- URL platformURL = FileLocator .find (url );
289
- if (platformURL != null ) {
290
- url = platformURL ;
291
- }
240
+ url = resolvePathVariables (url );
292
241
URL locatedURL = FileLocator .toFileURL (url );
293
242
if (FILE_PROTOCOL .equalsIgnoreCase (locatedURL .getProtocol ())) {
294
243
String filePath = IPath .fromOSString (locatedURL .getPath ()).toOSString ();
@@ -310,6 +259,14 @@ private static String getFilePath(URL url, boolean logIOException) {
310
259
}
311
260
}
312
261
262
+ private static URL resolvePathVariables (URL url ) {
263
+ URL platformURL = FileLocator .find (url ); // Resolve variables within URL's path
264
+ if (platformURL != null ) {
265
+ url = platformURL ;
266
+ }
267
+ return url ;
268
+ }
269
+
313
270
@ Override
314
271
public Image createImage (boolean returnMissingImageOnError , Device device ) {
315
272
long start = 0 ;
@@ -323,7 +280,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
323
280
// We really want a fresh ImageFileNameProvider instance to make
324
281
// sure the code that uses created images can use equals(),
325
282
// see Image#equals
326
- return new Image (device , new URLImageFileNameProvider ( url ));
283
+ return new Image (device , createURLImageFileNameProvider ( ));
327
284
} catch (SWTException | IllegalArgumentException exception ) {
328
285
// If we fail fall back to the slower input stream method.
329
286
}
@@ -334,7 +291,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
334
291
// We really want a fresh ImageDataProvider instance to make
335
292
// sure the code that uses created images can use equals(),
336
293
// see Image#equals
337
- image = new Image (device , new URLImageDataProvider ( url ));
294
+ image = new Image (device , createURLImageDataProvider ( ));
338
295
} catch (SWTException e ) {
339
296
if (e .code != SWT .ERROR_INVALID_IMAGE ) {
340
297
throw e ;
@@ -383,7 +340,7 @@ private static URL getURL(String urlString) {
383
340
try {
384
341
result = new URL (urlString );
385
342
} catch (MalformedURLException e ) {
386
- Policy .getLog (). log ( Status . error ( e . getLocalizedMessage (), e ) );
343
+ Policy .logException ( e );
387
344
}
388
345
return result ;
389
346
}
@@ -394,10 +351,10 @@ public <T> T getAdapter(Class<T> adapter) {
394
351
return adapter .cast (getURL (url ));
395
352
}
396
353
if (adapter == ImageFileNameProvider .class ) {
397
- return adapter .cast (new URLImageFileNameProvider ( url ));
354
+ return adapter .cast (createURLImageFileNameProvider ( ));
398
355
}
399
356
if (adapter == ImageDataProvider .class ) {
400
- return adapter .cast (new URLImageDataProvider ( url ));
357
+ return adapter .cast (createURLImageDataProvider ( ));
401
358
}
402
359
return null ;
403
360
}
0 commit comments