17
17
*******************************************************************************/
18
18
package org .eclipse .jface .resource ;
19
19
20
+ import static org .eclipse .jface .resource .FileImageDescriptor .getImageSource ;
21
+
20
22
import java .io .BufferedInputStream ;
21
23
import java .io .IOException ;
22
24
import java .io .InputStream ;
46
48
*/
47
49
class URLImageDescriptor extends ImageDescriptor implements IAdaptable {
48
50
49
- private static class URLImageFileNameProvider implements ImageFileNameProvider {
50
-
51
- private final String url ;
52
-
53
- public URLImageFileNameProvider (String url ) {
54
- this .url = url ;
55
- }
56
-
57
- @ Override
58
- public String getImagePath (int zoom ) {
59
- URL tempURL = getURL (url );
60
- if (tempURL != null ) {
61
- final boolean logIOException = zoom == 100 ;
62
- if (zoom == 100 ) {
63
- return getFilePath (tempURL , logIOException );
64
- }
65
- URL xUrl = getxURL (tempURL , zoom );
66
- if (xUrl != null ) {
67
- String xResult = getFilePath (xUrl , logIOException );
68
- if (xResult != null ) {
69
- return xResult ;
70
- }
71
- }
72
- String xpath = FileImageDescriptor .getxPath (url , zoom );
73
- if (xpath != null ) {
74
- URL xPathUrl = getURL (xpath );
75
- if (xPathUrl != null ) {
76
- return getFilePath (xPathUrl , logIOException );
77
- }
78
- }
79
- }
80
- return null ;
81
- }
82
-
51
+ private static ImageFileNameProvider createURLImageFileNameProvider (String url ) {
52
+ return zoom -> {
53
+ final boolean logIOException = zoom == 100 ;
54
+ return getImageSource (url , URLImageDescriptor ::getURL , URLImageDescriptor ::getxURL ,
55
+ u -> getFilePath (u , logIOException ), zoom );
56
+ };
83
57
}
84
58
85
- private static class URLImageDataProvider implements ImageDataProvider {
86
-
87
- private final String url ;
88
-
89
- public URLImageDataProvider (String url ) {
90
- this .url = url ;
91
- }
92
-
93
- @ Override
94
- public ImageData getImageData (int zoom ) {
95
- return URLImageDescriptor .getImageData (url , zoom );
96
- }
97
-
59
+ private static ImageDataProvider createURLImageDataProvider (String url ) {
60
+ return zoom -> getImageData (url , zoom );
98
61
}
99
62
100
63
private static long cumulativeTime ;
101
64
102
65
/**
103
66
* Constant for the file protocol for optimized loading
104
67
*/
105
- private static final String FILE_PROTOCOL = "file" ; //$NON-NLS-1$
68
+ private static final String FILE_PROTOCOL = "file" ; //$NON-NLS-1$
106
69
107
70
private final String url ;
108
71
@@ -118,10 +81,10 @@ public ImageData getImageData(int zoom) {
118
81
119
82
@ Override
120
83
public boolean equals (Object o ) {
121
- if (!(o instanceof URLImageDescriptor )) {
84
+ if (!(o instanceof URLImageDescriptor other )) {
122
85
return false ;
123
86
}
124
- return (( URLImageDescriptor ) o ) .url .equals (this .url );
87
+ return other .url .equals (this .url );
125
88
}
126
89
127
90
@ Deprecated
@@ -135,28 +98,9 @@ public ImageData getImageData(int zoom) {
135
98
return getImageData (url , zoom );
136
99
}
137
100
138
- private static ImageData getImageData (String url , int zoom ) {
139
- URL tempURL = getURL (url );
140
- if (tempURL != null ) {
141
- if (zoom == 100 ) {
142
- return getImageData (tempURL );
143
- }
144
- URL xUrl = getxURL (tempURL , zoom );
145
- if (xUrl != null ) {
146
- ImageData xdata = getImageData (xUrl );
147
- if (xdata != null ) {
148
- return xdata ;
149
- }
150
- }
151
- String xpath = FileImageDescriptor .getxPath (url , zoom );
152
- if (xpath != null ) {
153
- URL xPathUrl = getURL (xpath );
154
- if (xPathUrl != null ) {
155
- return getImageData (xPathUrl );
156
- }
157
- }
158
- }
159
- return null ;
101
+ static ImageData getImageData (String url , int zoom ) {
102
+ return getImageSource (url , URLImageDescriptor ::getURL , URLImageDescriptor ::getxURL ,
103
+ URLImageDescriptor ::getImageData , zoom );
160
104
}
161
105
162
106
private static ImageData getImageData (URL url ) {
@@ -176,16 +120,6 @@ private static ImageData getImageData(URL url) {
176
120
return result ;
177
121
}
178
122
179
- /**
180
- * Returns a stream on the image contents. Returns null if a stream could
181
- * not be opened.
182
- *
183
- * @return the stream for loading the data
184
- */
185
- protected InputStream getStream () {
186
- return getStream (getURL (url ));
187
- }
188
-
189
123
private static InputStream getStream (URL url ) {
190
124
if (url == null ) {
191
125
return null ;
@@ -226,17 +160,10 @@ public String toString() {
226
160
}
227
161
228
162
private static URL getxURL (URL url , int zoom ) {
229
- String path = url .getPath ();
230
- int dot = path .lastIndexOf ('.' );
231
- if (dot != -1 && (zoom == 150 || zoom == 200 )) {
232
- String lead = path .substring (0 , dot );
233
- String tail = path .substring (dot );
234
- if (InternalPolicy .DEBUG_LOAD_URL_IMAGE_DESCRIPTOR_2x_PNG_FOR_GIF && ".gif" .equalsIgnoreCase (tail )) { //$NON-NLS-1$
235
- tail = ".png" ; //$NON-NLS-1$
236
- }
237
- String x = zoom == 150 ? "@1.5x" : "@2x" ; //$NON-NLS-1$ //$NON-NLS-2$
163
+ String name = url .getPath ();
164
+ String file = FileImageDescriptor .getxName (name , zoom );
165
+ if (file != null ) {
238
166
try {
239
- String file = lead + x + tail ;
240
167
if (url .getQuery () != null ) {
241
168
file += '?' + url .getQuery ();
242
169
}
@@ -246,7 +173,6 @@ private static URL getxURL(URL url, int zoom) {
246
173
}
247
174
}
248
175
return null ;
249
-
250
176
}
251
177
252
178
/**
@@ -300,7 +226,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
300
226
// We really want a fresh ImageFileNameProvider instance to make
301
227
// sure the code that uses created images can use equals(),
302
228
// see Image#equals
303
- return new Image (device , new URLImageFileNameProvider (url ));
229
+ return new Image (device , createURLImageFileNameProvider (url ));
304
230
} catch (SWTException | IllegalArgumentException exception ) {
305
231
// If we fail fall back to the slower input stream method.
306
232
}
@@ -311,7 +237,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
311
237
// We really want a fresh ImageDataProvider instance to make
312
238
// sure the code that uses created images can use equals(),
313
239
// see Image#equals
314
- image = new Image (device , new URLImageDataProvider (url ));
240
+ image = new Image (device , createURLImageDataProvider (url ));
315
241
} catch (SWTException e ) {
316
242
if (e .code != SWT .ERROR_INVALID_IMAGE ) {
317
243
throw e ;
@@ -371,10 +297,10 @@ public <T> T getAdapter(Class<T> adapter) {
371
297
return adapter .cast (getURL (url ));
372
298
}
373
299
if (adapter == ImageFileNameProvider .class ) {
374
- return adapter .cast (new URLImageFileNameProvider (url ));
300
+ return adapter .cast (createURLImageFileNameProvider (url ));
375
301
}
376
302
if (adapter == ImageDataProvider .class ) {
377
- return adapter .cast (new URLImageDataProvider (url ));
303
+ return adapter .cast (createURLImageDataProvider (url ));
378
304
}
379
305
return null ;
380
306
}
0 commit comments