Skip to content

Commit 762649b

Browse files
committed
Revert "Unify logic of FileImageDescriptor and URLImageDescriptor"
This reverts commit a1eada2.
1 parent a1eada2 commit 762649b

File tree

2 files changed

+165
-73
lines changed

2 files changed

+165
-73
lines changed

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

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
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;
2826
import java.util.regex.Matcher;
2927
import java.util.regex.Pattern;
3028

@@ -46,11 +44,31 @@
4644
*/
4745
class FileImageDescriptor extends ImageDescriptor implements IAdaptable {
4846

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-
};
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+
5472
}
5573

5674
private static final Pattern XPATH_PATTERN = Pattern.compile("(\\d+)x(\\d+)"); //$NON-NLS-1$
@@ -88,9 +106,10 @@ private ImageFileNameProvider createImageFileNameProvider() {
88106

89107
@Override
90108
public boolean equals(Object o) {
91-
if (!(o instanceof FileImageDescriptor other)) {
109+
if (!(o instanceof FileImageDescriptor)) {
92110
return false;
93111
}
112+
FileImageDescriptor other = (FileImageDescriptor) o;
94113
return Objects.equals(location, other.location) && Objects.equals(name, other.name);
95114
}
96115

@@ -103,9 +122,9 @@ public boolean equals(Object o) {
103122
*/
104123
@Override
105124
public ImageData getImageData(int zoom) {
106-
InputStream inputStream = getImageSource(name, n -> n, FileImageDescriptor::getxName, this::getStream, zoom);
107-
if (inputStream != null) {
108-
try (InputStream stream = new BufferedInputStream(inputStream)) {
125+
InputStream in = getStream(zoom);
126+
if (in != null) {
127+
try (BufferedInputStream stream = new BufferedInputStream(in)) {
109128
return new ImageData(stream);
110129
} catch (SWTException e) {
111130
if (e.code != SWT.ERROR_INVALID_IMAGE) {
@@ -120,35 +139,28 @@ public ImageData getImageData(int zoom) {
120139
}
121140

122141
/**
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
142+
* Returns a stream on the image contents. Returns null if a stream could
143+
* not be opened.
125144
*
126145
* @param zoom the zoom factor
127-
* @return the the file content or {@code null} if the file cannot be found
146+
* @return the buffered stream on the file or <code>null</code> if the
147+
* file cannot be found
128148
*/
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-
}
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;
151162
}
163+
152164
return null;
153165
}
154166

@@ -161,17 +173,20 @@ static <E, R> R getImageSource(String root, Function<String, E> elementParser,
161173
* does not denotes an existing resource
162174
*/
163175
private InputStream getStream(String fileName) {
164-
if (location != null) {
165-
return location.getResourceAsStream(fileName);
166-
}
167-
try {
168-
return new FileInputStream(fileName);
169-
} catch (FileNotFoundException e) {
170-
return null;
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+
}
171185
}
186+
return null;
172187
}
173188

174-
private static String getxPath(String name, int zoom) {
189+
static String getxPath(String name, int zoom) {
175190
Matcher matcher = XPATH_PATTERN.matcher(name);
176191
if (matcher.find()) {
177192
try {
@@ -229,7 +244,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
229244
// We really want a fresh ImageFileNameProvider instance to make
230245
// sure the code that uses created images can use equals(),
231246
// see Image#equals
232-
return new Image(device, createImageFileNameProvider());
247+
return new Image(device, new ImageProvider());
233248
} catch (SWTException | IllegalArgumentException exception) {
234249
// If we fail, fall back to the old 1x implementation.
235250
}
@@ -268,7 +283,7 @@ private Image createDefaultImage(boolean returnMissingImageOnError,
268283
* @param name the file name
269284
* @return {@link String} or <code>null</code> if the file cannot be found
270285
*/
271-
private String getFilePath(String name, boolean logIOException) {
286+
String getFilePath(String name, boolean logIOException) {
272287
if (location == null)
273288
return IPath.fromOSString(name).toOSString();
274289

@@ -302,7 +317,7 @@ public <T> T getAdapter(Class<T> adapter) {
302317
}
303318
}
304319
if (adapter == ImageFileNameProvider.class) {
305-
return adapter.cast(createImageFileNameProvider());
320+
return adapter.cast(new ImageProvider());
306321
}
307322
return null;
308323
}

0 commit comments

Comments
 (0)