Skip to content

Commit e6c5232

Browse files
committed
Unify logic of FileImageDescriptor and URLImageDescriptor
Before the logic to determine the filename or URL for regular- and high-dpi resolution was replicated multiple times. Additionally apply a few minor code clean-ups and remove a unused internal method.
1 parent 037c5d7 commit e6c5232

File tree

2 files changed

+68
-157
lines changed

2 files changed

+68
-157
lines changed

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

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.InputStream;
2424
import java.net.URL;
2525
import java.util.Objects;
26+
import java.util.function.BiFunction;
27+
import java.util.function.Function;
2628
import java.util.regex.Matcher;
2729
import java.util.regex.Pattern;
2830

@@ -44,31 +46,11 @@
4446
*/
4547
class FileImageDescriptor extends ImageDescriptor implements IAdaptable {
4648

47-
private class ImageProvider implements ImageFileNameProvider {
48-
49-
@Override
50-
public String getImagePath(int zoom) {
51-
final boolean logIOException = zoom == 100;
52-
if (zoom == 100) {
53-
return getFilePath(name, logIOException);
54-
}
55-
String xName = getxName(name, zoom);
56-
if (xName != null) {
57-
String xResult = getFilePath(xName, logIOException);
58-
if (xResult != null) {
59-
return xResult;
60-
}
61-
}
62-
String xPath = getxPath(name, zoom);
63-
if (xPath != null) {
64-
String xResult = getFilePath(xPath, logIOException);
65-
if (xResult != null) {
66-
return xResult;
67-
}
68-
}
69-
return null;
70-
}
71-
49+
private ImageFileNameProvider createImageFileNameProvider() {
50+
return zoom -> {
51+
boolean logException = zoom == 100;
52+
return getImageSource(name, n -> n, FileImageDescriptor::getxName, n -> getFilePath(n, logException), zoom);
53+
};
7254
}
7355

7456
private static final Pattern XPATH_PATTERN = Pattern.compile("(\\d+)x(\\d+)"); //$NON-NLS-1$
@@ -106,10 +88,9 @@ public String getImagePath(int zoom) {
10688

10789
@Override
10890
public boolean equals(Object o) {
109-
if (!(o instanceof FileImageDescriptor)) {
91+
if (!(o instanceof FileImageDescriptor other)) {
11092
return false;
11193
}
112-
FileImageDescriptor other = (FileImageDescriptor) o;
11394
return Objects.equals(location, other.location) && Objects.equals(name, other.name);
11495
}
11596

@@ -122,9 +103,9 @@ public boolean equals(Object o) {
122103
*/
123104
@Override
124105
public ImageData getImageData(int zoom) {
125-
InputStream in = getStream(zoom);
126-
if (in != null) {
127-
try (BufferedInputStream stream = new BufferedInputStream(in)) {
106+
InputStream inputStream = getImageSource(name, n -> n, FileImageDescriptor::getxName, this::getStream, zoom);
107+
if (inputStream != null) {
108+
try (InputStream stream = new BufferedInputStream(inputStream)) {
128109
return new ImageData(stream);
129110
} catch (SWTException e) {
130111
if (e.code != SWT.ERROR_INVALID_IMAGE) {
@@ -139,28 +120,35 @@ public ImageData getImageData(int zoom) {
139120
}
140121

141122
/**
142-
* Returns a stream on the image contents. Returns null if a stream could
143-
* not be opened.
123+
* Returns a the image contents in the form returned by the given image-source
124+
* factory. Returns null if the content could not be found or accessed
144125
*
145126
* @param zoom the zoom factor
146-
* @return the buffered stream on the file or <code>null</code> if the
147-
* file cannot be found
127+
* @return the the file content or {@code null} if the file cannot be found
148128
*/
149-
private InputStream getStream(int zoom) {
150-
if (zoom == 100) {
151-
return getStream(name);
152-
}
153-
154-
InputStream xstream = getStream(getxName(name, zoom));
155-
if (xstream != null) {
156-
return xstream;
157-
}
158-
159-
InputStream xpath = getStream(getxPath(name, zoom));
160-
if (xpath != null) {
161-
return xpath;
129+
static <E, R> R getImageSource(String root, Function<String, E> elementParser,
130+
BiFunction<E, Integer, E> getXElement, Function<E, R> getImageSource, int zoom) {
131+
E element = elementParser.apply(root);
132+
if (element != null) {
133+
if (zoom == 100) {
134+
return getImageSource.apply(element);
135+
}
136+
@SuppressWarnings("boxing")
137+
E xName = getXElement.apply(element, zoom);
138+
if (xName != null) {
139+
R xResult = getImageSource.apply(xName);
140+
if (xResult != null) {
141+
return xResult;
142+
}
143+
}
144+
String xPath = getxPath(root, zoom);
145+
if (xPath != null) {
146+
E xPathElement = elementParser.apply(xPath);
147+
if (xPathElement != null) {
148+
return getImageSource.apply(xPathElement);
149+
}
150+
}
162151
}
163-
164152
return null;
165153
}
166154

@@ -173,17 +161,14 @@ private InputStream getStream(int zoom) {
173161
* does not denotes an existing resource
174162
*/
175163
private InputStream getStream(String fileName) {
176-
if (fileName != null) {
177-
if (location != null) {
178-
return location.getResourceAsStream(fileName);
179-
}
180-
try {
181-
return new FileInputStream(fileName);
182-
} catch (FileNotFoundException e) {
183-
return null;
184-
}
164+
if (location != null) {
165+
return location.getResourceAsStream(fileName);
166+
}
167+
try {
168+
return new FileInputStream(fileName);
169+
} catch (FileNotFoundException e) {
170+
return null;
185171
}
186-
return null;
187172
}
188173

189174
static String getxPath(String name, int zoom) {
@@ -244,7 +229,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
244229
// We really want a fresh ImageFileNameProvider instance to make
245230
// sure the code that uses created images can use equals(),
246231
// see Image#equals
247-
return new Image(device, new ImageProvider());
232+
return new Image(device, createImageFileNameProvider());
248233
} catch (SWTException | IllegalArgumentException exception) {
249234
// If we fail, fall back to the old 1x implementation.
250235
}
@@ -317,7 +302,7 @@ public <T> T getAdapter(Class<T> adapter) {
317302
}
318303
}
319304
if (adapter == ImageFileNameProvider.class) {
320-
return adapter.cast(new ImageProvider());
305+
return adapter.cast(createImageFileNameProvider());
321306
}
322307
return null;
323308
}

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

Lines changed: 23 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*******************************************************************************/
1818
package org.eclipse.jface.resource;
1919

20+
import static org.eclipse.jface.resource.FileImageDescriptor.getImageSource;
21+
2022
import java.io.BufferedInputStream;
2123
import java.io.IOException;
2224
import java.io.InputStream;
@@ -46,63 +48,24 @@
4648
*/
4749
class 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

Comments
 (0)