23
23
import java .io .InputStream ;
24
24
import java .net .URL ;
25
25
import java .util .Objects ;
26
- import java .util .function .BiFunction ;
27
- import java .util .function .Function ;
28
26
import java .util .regex .Matcher ;
29
27
import java .util .regex .Pattern ;
30
28
46
44
*/
47
45
class FileImageDescriptor extends ImageDescriptor implements IAdaptable {
48
46
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
+
54
72
}
55
73
56
74
private static final Pattern XPATH_PATTERN = Pattern .compile ("(\\ d+)x(\\ d+)" ); //$NON-NLS-1$
@@ -88,9 +106,10 @@ private ImageFileNameProvider createImageFileNameProvider() {
88
106
89
107
@ Override
90
108
public boolean equals (Object o ) {
91
- if (!(o instanceof FileImageDescriptor other )) {
109
+ if (!(o instanceof FileImageDescriptor )) {
92
110
return false ;
93
111
}
112
+ FileImageDescriptor other = (FileImageDescriptor ) o ;
94
113
return Objects .equals (location , other .location ) && Objects .equals (name , other .name );
95
114
}
96
115
@@ -103,9 +122,9 @@ public boolean equals(Object o) {
103
122
*/
104
123
@ Override
105
124
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 )) {
109
128
return new ImageData (stream );
110
129
} catch (SWTException e ) {
111
130
if (e .code != SWT .ERROR_INVALID_IMAGE ) {
@@ -120,35 +139,28 @@ public ImageData getImageData(int zoom) {
120
139
}
121
140
122
141
/**
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.
125
144
*
126
145
* @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
128
148
*/
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 ;
151
162
}
163
+
152
164
return null ;
153
165
}
154
166
@@ -161,17 +173,20 @@ static <E, R> R getImageSource(String root, Function<String, E> elementParser,
161
173
* does not denotes an existing resource
162
174
*/
163
175
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
+ }
171
185
}
186
+ return null ;
172
187
}
173
188
174
- private static String getxPath (String name , int zoom ) {
189
+ static String getxPath (String name , int zoom ) {
175
190
Matcher matcher = XPATH_PATTERN .matcher (name );
176
191
if (matcher .find ()) {
177
192
try {
@@ -229,7 +244,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
229
244
// We really want a fresh ImageFileNameProvider instance to make
230
245
// sure the code that uses created images can use equals(),
231
246
// see Image#equals
232
- return new Image (device , createImageFileNameProvider ());
247
+ return new Image (device , new ImageProvider ());
233
248
} catch (SWTException | IllegalArgumentException exception ) {
234
249
// If we fail, fall back to the old 1x implementation.
235
250
}
@@ -268,7 +283,7 @@ private Image createDefaultImage(boolean returnMissingImageOnError,
268
283
* @param name the file name
269
284
* @return {@link String} or <code>null</code> if the file cannot be found
270
285
*/
271
- private String getFilePath (String name , boolean logIOException ) {
286
+ String getFilePath (String name , boolean logIOException ) {
272
287
if (location == null )
273
288
return IPath .fromOSString (name ).toOSString ();
274
289
@@ -302,7 +317,7 @@ public <T> T getAdapter(Class<T> adapter) {
302
317
}
303
318
}
304
319
if (adapter == ImageFileNameProvider .class ) {
305
- return adapter .cast (createImageFileNameProvider ());
320
+ return adapter .cast (new ImageProvider ());
306
321
}
307
322
return null ;
308
323
}
0 commit comments