1717 *******************************************************************************/
1818package org .eclipse .jface .resource ;
1919
20+ import static org .eclipse .jface .resource .FileImageDescriptor .getImageSource ;
21+
2022import java .io .BufferedInputStream ;
2123import java .io .IOException ;
2224import java .io .InputStream ;
4648 */
4749class URLImageDescriptor extends ImageDescriptor implements IAdaptable {
4850
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+ };
8357 }
8458
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 );
9861 }
9962
10063 private static long cumulativeTime ;
10164
10265 /**
10366 * Constant for the file protocol for optimized loading
10467 */
105- private static final String FILE_PROTOCOL = "file" ; //$NON-NLS-1$
68+ private static final String FILE_PROTOCOL = "file" ; //$NON-NLS-1$
10669
10770 private final String url ;
10871
@@ -118,10 +81,10 @@ public ImageData getImageData(int zoom) {
11881
11982 @ Override
12083 public boolean equals (Object o ) {
121- if (!(o instanceof URLImageDescriptor )) {
84+ if (!(o instanceof URLImageDescriptor other )) {
12285 return false ;
12386 }
124- return (( URLImageDescriptor ) o ) .url .equals (this .url );
87+ return other .url .equals (this .url );
12588 }
12689
12790 @ Deprecated
@@ -135,28 +98,9 @@ public ImageData getImageData(int zoom) {
13598 return getImageData (url , zoom );
13699 }
137100
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 );
160104 }
161105
162106 private static ImageData getImageData (URL url ) {
@@ -176,16 +120,6 @@ private static ImageData getImageData(URL url) {
176120 return result ;
177121 }
178122
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-
189123 private static InputStream getStream (URL url ) {
190124 if (url == null ) {
191125 return null ;
@@ -226,17 +160,10 @@ public String toString() {
226160 }
227161
228162 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 ) {
238166 try {
239- String file = lead + x + tail ;
240167 if (url .getQuery () != null ) {
241168 file += '?' + url .getQuery ();
242169 }
@@ -246,7 +173,6 @@ private static URL getxURL(URL url, int zoom) {
246173 }
247174 }
248175 return null ;
249-
250176 }
251177
252178 /**
@@ -300,7 +226,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
300226 // We really want a fresh ImageFileNameProvider instance to make
301227 // sure the code that uses created images can use equals(),
302228 // see Image#equals
303- return new Image (device , new URLImageFileNameProvider (url ));
229+ return new Image (device , createURLImageFileNameProvider (url ));
304230 } catch (SWTException | IllegalArgumentException exception ) {
305231 // If we fail fall back to the slower input stream method.
306232 }
@@ -311,7 +237,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
311237 // We really want a fresh ImageDataProvider instance to make
312238 // sure the code that uses created images can use equals(),
313239 // see Image#equals
314- image = new Image (device , new URLImageDataProvider (url ));
240+ image = new Image (device , createURLImageDataProvider (url ));
315241 } catch (SWTException e ) {
316242 if (e .code != SWT .ERROR_INVALID_IMAGE ) {
317243 throw e ;
@@ -371,10 +297,10 @@ public <T> T getAdapter(Class<T> adapter) {
371297 return adapter .cast (getURL (url ));
372298 }
373299 if (adapter == ImageFileNameProvider .class ) {
374- return adapter .cast (new URLImageFileNameProvider (url ));
300+ return adapter .cast (createURLImageFileNameProvider (url ));
375301 }
376302 if (adapter == ImageDataProvider .class ) {
377- return adapter .cast (new URLImageDataProvider (url ));
303+ return adapter .cast (createURLImageDataProvider (url ));
378304 }
379305 return null ;
380306 }
0 commit comments