39
39
import org .eclipse .swt .graphics .Image ;
40
40
import org .eclipse .swt .graphics .ImageData ;
41
41
import org .eclipse .swt .graphics .ImageFileNameProvider ;
42
+ import org .eclipse .swt .graphics .ImageLoader ;
43
+ import org .eclipse .swt .internal .DPIUtil .ElementAtZoom ;
44
+ import org .eclipse .swt .internal .NativeImageLoader ;
42
45
43
46
/**
44
47
* An image descriptor that loads its image information from a file.
@@ -47,22 +50,23 @@ class FileImageDescriptor extends ImageDescriptor implements IAdaptable {
47
50
48
51
private class ImageProvider implements ImageFileNameProvider {
49
52
53
+ @ SuppressWarnings ("restriction" )
50
54
@ Override
51
55
public String getImagePath (int zoom ) {
52
56
final boolean logIOException = zoom == 100 ;
53
57
if (zoom == 100 ) {
54
58
return getFilePath (name , logIOException );
55
59
}
56
- String xName = getxName (name , zoom );
60
+ ElementAtZoom < String > xName = getxName (name , zoom );
57
61
if (xName != null ) {
58
- String xResult = getFilePath (xName , logIOException );
62
+ String xResult = getFilePath (xName . element () , logIOException );
59
63
if (xResult != null ) {
60
64
return xResult ;
61
65
}
62
66
}
63
- String xPath = getxPath (name , zoom );
67
+ ElementAtZoom < String > xPath = getxPath (name , zoom );
64
68
if (xPath != null ) {
65
- String xResult = getFilePath (xPath , logIOException );
69
+ String xResult = getFilePath (xPath . element () , logIOException );
66
70
if (xResult != null ) {
67
71
return xResult ;
68
72
}
@@ -121,12 +125,15 @@ public boolean equals(Object o) {
121
125
* {@link ImageDescriptor#createImage(boolean, Device)} as of version
122
126
* 3.4 so that the SWT OS optimized loading can be used.
123
127
*/
128
+ @ SuppressWarnings ("restriction" )
124
129
@ Override
125
130
public ImageData getImageData (int zoom ) {
126
- InputStream in = getStream (zoom );
127
- if (in != null ) {
128
- try (BufferedInputStream stream = new BufferedInputStream (in )) {
129
- return new ImageData (stream );
131
+ ElementAtZoom <InputStream > inputStreamAtZoom = getStream (zoom );
132
+ if (inputStreamAtZoom != null ) {
133
+ try (BufferedInputStream stream = new BufferedInputStream (inputStreamAtZoom .element ())) {
134
+ ElementAtZoom <ImageData > imageData = NativeImageLoader
135
+ .load (new ElementAtZoom <>(stream , inputStreamAtZoom .zoom ()), new ImageLoader (), zoom ).get (0 );
136
+ return imageData .element ();
130
137
} catch (SWTException e ) {
131
138
if (e .code != SWT .ERROR_INVALID_IMAGE ) {
132
139
throw e ;
@@ -147,17 +154,18 @@ public ImageData getImageData(int zoom) {
147
154
* @return the buffered stream on the file or <code>null</code> if the
148
155
* file cannot be found
149
156
*/
150
- private InputStream getStream (int zoom ) {
157
+ @ SuppressWarnings ("restriction" )
158
+ private ElementAtZoom <InputStream > getStream (int zoom ) {
151
159
if (zoom == 100 ) {
152
- return getStream (name );
160
+ return getStream (new ElementAtZoom <>( name , 100 ) );
153
161
}
154
162
155
- InputStream xstream = getStream (getxName (name , zoom ));
163
+ ElementAtZoom < InputStream > xstream = getStream (getxName (name , zoom ));
156
164
if (xstream != null ) {
157
165
return xstream ;
158
166
}
159
167
160
- InputStream xpath = getStream (getxPath (name , zoom ));
168
+ ElementAtZoom < InputStream > xpath = getStream (getxPath (name , zoom ));
161
169
if (xpath != null ) {
162
170
return xpath ;
163
171
}
@@ -173,21 +181,24 @@ private InputStream getStream(int zoom) {
173
181
* @return an {@link InputStream} to read from, or <code>null</code> if fileName
174
182
* does not denotes an existing resource
175
183
*/
176
- private InputStream getStream (String fileName ) {
184
+ @ SuppressWarnings ("restriction" )
185
+ private ElementAtZoom <InputStream > getStream (ElementAtZoom <String > fileName ) {
177
186
if (fileName != null ) {
187
+ // TODO DO we need to close these?
178
188
if (location != null ) {
179
- return location .getResourceAsStream (fileName );
189
+ return new ElementAtZoom <>( location .getResourceAsStream (fileName . element ()), fileName . zoom () );
180
190
}
181
191
try {
182
- return new FileInputStream (fileName );
192
+ return new ElementAtZoom <>( new FileInputStream (fileName . element ()), fileName . zoom () );
183
193
} catch (FileNotFoundException e ) {
184
194
return null ;
185
195
}
186
196
}
187
197
return null ;
188
198
}
189
199
190
- static String getxPath (String name , int zoom ) {
200
+ @ SuppressWarnings ("restriction" )
201
+ static ElementAtZoom <String > getxPath (String name , int zoom ) {
191
202
Matcher matcher = XPATH_PATTERN .matcher (name );
192
203
if (matcher .find ()) {
193
204
try {
@@ -197,24 +208,30 @@ static String getxPath(String name, int zoom) {
197
208
int desiredHeight = Math .round ((zoom / 100f ) * currentHeight );
198
209
String lead = name .substring (0 , matcher .start (1 ));
199
210
String tail = name .substring (matcher .end (2 ));
200
- return lead + desiredWidth + "x" + desiredHeight + tail ; //$NON-NLS-1$
211
+ String xPath = lead + desiredWidth + "x" + desiredHeight + tail ; //$NON-NLS-1$
212
+ return new ElementAtZoom <>(xPath , desiredHeight );
201
213
} catch (RuntimeException e ) {
202
214
// should never happen but if then we can't use the alternative name...
203
215
}
204
216
}
205
217
return null ;
206
218
}
207
219
208
- static String getxName (String name , int zoom ) {
220
+ @ SuppressWarnings ("restriction" )
221
+ static ElementAtZoom <String > getxName (String name , int zoom ) {
209
222
int dot = name .lastIndexOf ('.' );
210
223
if (dot != -1 && (zoom == 150 || zoom == 200 )) {
211
224
String lead = name .substring (0 , dot );
212
225
String tail = name .substring (dot );
213
226
if (InternalPolicy .DEBUG_LOAD_URL_IMAGE_DESCRIPTOR_2x_PNG_FOR_GIF && ".gif" .equalsIgnoreCase (tail )) { //$NON-NLS-1$
214
227
tail = ".png" ; //$NON-NLS-1$
215
228
}
216
- String x = zoom == 150 ? "@1.5x" : "@2x" ; //$NON-NLS-1$ //$NON-NLS-2$
217
- return lead + x + tail ;
229
+ String x = "@2x" ;//$NON-NLS-1$
230
+ if (zoom == 150 ) {
231
+ x = "@1.5x" ; //$NON-NLS-1$
232
+ return new ElementAtZoom <>(lead + x + tail , 150 );
233
+ }
234
+ return new ElementAtZoom <>(lead + x + tail , 200 );
218
235
}
219
236
return null ;
220
237
}
0 commit comments