32
32
import org .eclipse .core .runtime .IStatus ;
33
33
import org .eclipse .core .runtime .Status ;
34
34
import org .eclipse .jface .internal .InternalPolicy ;
35
+ import org .eclipse .jface .resource .URLImageDescriptor .SourceAtZoom ;
35
36
import org .eclipse .jface .util .Policy ;
36
37
import org .eclipse .swt .SWT ;
37
38
import org .eclipse .swt .SWTException ;
38
39
import org .eclipse .swt .graphics .Device ;
39
40
import org .eclipse .swt .graphics .Image ;
40
41
import org .eclipse .swt .graphics .ImageData ;
41
42
import org .eclipse .swt .graphics .ImageFileNameProvider ;
43
+ import org .eclipse .swt .graphics .ImageLoader ;
44
+ import org .eclipse .swt .internal .DPIUtil .ElementAtZoom ;
45
+ import org .eclipse .swt .internal .NativeImageLoader ;
42
46
43
47
/**
44
48
* An image descriptor that loads its image information from a file.
@@ -53,16 +57,16 @@ public String getImagePath(int zoom) {
53
57
if (zoom == 100 ) {
54
58
return getFilePath (name , logIOException );
55
59
}
56
- String xName = getxName (name , zoom );
60
+ SourceAtZoom < String > xName = getxName (name , zoom );
57
61
if (xName != null ) {
58
- String xResult = getFilePath (xName , logIOException );
62
+ String xResult = getFilePath (xName . source () , logIOException );
59
63
if (xResult != null ) {
60
64
return xResult ;
61
65
}
62
66
}
63
- String xPath = getxPath (name , zoom );
67
+ SourceAtZoom < String > xPath = getxPath (name , zoom );
64
68
if (xPath != null ) {
65
- String xResult = getFilePath (xPath , logIOException );
69
+ String xResult = getFilePath (xPath . source () , 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
+ SourceAtZoom <InputStream > inputStreamAtZoom = getStream (zoom );
132
+ if (inputStreamAtZoom != null ) {
133
+ try (BufferedInputStream stream = new BufferedInputStream (inputStreamAtZoom .source ())) {
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,17 @@ 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
+ private SourceAtZoom < InputStream > getStream (int zoom ) {
151
158
if (zoom == 100 ) {
152
- return getStream (name );
159
+ return getStream (new SourceAtZoom <>( name , 100 ) );
153
160
}
154
161
155
- InputStream xstream = getStream (getxName (name , zoom ));
162
+ SourceAtZoom < InputStream > xstream = getStream (getxName (name , zoom ));
156
163
if (xstream != null ) {
157
164
return xstream ;
158
165
}
159
166
160
- InputStream xpath = getStream (getxPath (name , zoom ));
167
+ SourceAtZoom < InputStream > xpath = getStream (getxPath (name , zoom ));
161
168
if (xpath != null ) {
162
169
return xpath ;
163
170
}
@@ -173,21 +180,22 @@ private InputStream getStream(int zoom) {
173
180
* @return an {@link InputStream} to read from, or <code>null</code> if fileName
174
181
* does not denotes an existing resource
175
182
*/
176
- private InputStream getStream (String fileName ) {
183
+ private SourceAtZoom < InputStream > getStream (SourceAtZoom < String > fileName ) {
177
184
if (fileName != null ) {
185
+ // TODO DO we need to close these?
178
186
if (location != null ) {
179
- return location .getResourceAsStream (fileName );
187
+ return new SourceAtZoom <>( location .getResourceAsStream (fileName . source ()), fileName . zoom () );
180
188
}
181
189
try {
182
- return new FileInputStream (fileName );
190
+ return new SourceAtZoom <>( new FileInputStream (fileName . source ()), fileName . zoom () );
183
191
} catch (FileNotFoundException e ) {
184
192
return null ;
185
193
}
186
194
}
187
195
return null ;
188
196
}
189
197
190
- static String getxPath (String name , int zoom ) {
198
+ static SourceAtZoom < String > getxPath (String name , int zoom ) {
191
199
Matcher matcher = XPATH_PATTERN .matcher (name );
192
200
if (matcher .find ()) {
193
201
try {
@@ -197,24 +205,29 @@ static String getxPath(String name, int zoom) {
197
205
int desiredHeight = Math .round ((zoom / 100f ) * currentHeight );
198
206
String lead = name .substring (0 , matcher .start (1 ));
199
207
String tail = name .substring (matcher .end (2 ));
200
- return lead + desiredWidth + "x" + desiredHeight + tail ; //$NON-NLS-1$
208
+ String xPath = lead + desiredWidth + "x" + desiredHeight + tail ; //$NON-NLS-1$
209
+ return new SourceAtZoom <>(xPath , desiredHeight );
201
210
} catch (RuntimeException e ) {
202
211
// should never happen but if then we can't use the alternative name...
203
212
}
204
213
}
205
214
return null ;
206
215
}
207
216
208
- static String getxName (String name , int zoom ) {
217
+ static SourceAtZoom < String > getxName (String name , int zoom ) {
209
218
int dot = name .lastIndexOf ('.' );
210
219
if (dot != -1 && (zoom == 150 || zoom == 200 )) {
211
220
String lead = name .substring (0 , dot );
212
221
String tail = name .substring (dot );
213
222
if (InternalPolicy .DEBUG_LOAD_URL_IMAGE_DESCRIPTOR_2x_PNG_FOR_GIF && ".gif" .equalsIgnoreCase (tail )) { //$NON-NLS-1$
214
223
tail = ".png" ; //$NON-NLS-1$
215
224
}
216
- String x = zoom == 150 ? "@1.5x" : "@2x" ; //$NON-NLS-1$ //$NON-NLS-2$
217
- return lead + x + tail ;
225
+ String x = "@2x" ;//$NON-NLS-1$
226
+ if (zoom == 150 ) {
227
+ x = "@1.5x" ; //$NON-NLS-1$
228
+ return new SourceAtZoom <>(lead + x + tail , 150 );
229
+ }
230
+ return new SourceAtZoom <>(lead + x + tail , 200 );
218
231
}
219
232
return null ;
220
233
}
0 commit comments