3232import org .eclipse .core .runtime .IStatus ;
3333import org .eclipse .core .runtime .Status ;
3434import org .eclipse .jface .internal .InternalPolicy ;
35+ import org .eclipse .jface .resource .URLImageDescriptor .SourceAtZoom ;
3536import org .eclipse .jface .util .Policy ;
3637import org .eclipse .swt .SWT ;
3738import org .eclipse .swt .SWTException ;
3839import org .eclipse .swt .graphics .Device ;
3940import org .eclipse .swt .graphics .Image ;
4041import org .eclipse .swt .graphics .ImageData ;
4142import 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 ;
4246
4347/**
4448 * An image descriptor that loads its image information from a file.
@@ -53,16 +57,16 @@ public String getImagePath(int zoom) {
5357 if (zoom == 100 ) {
5458 return getFilePath (name , logIOException );
5559 }
56- String xName = getxName (name , zoom );
60+ SourceAtZoom < String > xName = getxName (name , zoom );
5761 if (xName != null ) {
58- String xResult = getFilePath (xName , logIOException );
62+ String xResult = getFilePath (xName . source () , logIOException );
5963 if (xResult != null ) {
6064 return xResult ;
6165 }
6266 }
63- String xPath = getxPath (name , zoom );
67+ SourceAtZoom < String > xPath = getxPath (name , zoom );
6468 if (xPath != null ) {
65- String xResult = getFilePath (xPath , logIOException );
69+ String xResult = getFilePath (xPath . source () , logIOException );
6670 if (xResult != null ) {
6771 return xResult ;
6872 }
@@ -121,12 +125,15 @@ public boolean equals(Object o) {
121125 * {@link ImageDescriptor#createImage(boolean, Device)} as of version
122126 * 3.4 so that the SWT OS optimized loading can be used.
123127 */
128+ @ SuppressWarnings ("restriction" )
124129 @ Override
125130 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 ();
130137 } catch (SWTException e ) {
131138 if (e .code != SWT .ERROR_INVALID_IMAGE ) {
132139 throw e ;
@@ -147,17 +154,17 @@ public ImageData getImageData(int zoom) {
147154 * @return the buffered stream on the file or <code>null</code> if the
148155 * file cannot be found
149156 */
150- private InputStream getStream (int zoom ) {
157+ private SourceAtZoom < InputStream > getStream (int zoom ) {
151158 if (zoom == 100 ) {
152- return getStream (name );
159+ return getStream (new SourceAtZoom <>( name , 100 ) );
153160 }
154161
155- InputStream xstream = getStream (getxName (name , zoom ));
162+ SourceAtZoom < InputStream > xstream = getStream (getxName (name , zoom ));
156163 if (xstream != null ) {
157164 return xstream ;
158165 }
159166
160- InputStream xpath = getStream (getxPath (name , zoom ));
167+ SourceAtZoom < InputStream > xpath = getStream (getxPath (name , zoom ));
161168 if (xpath != null ) {
162169 return xpath ;
163170 }
@@ -173,21 +180,22 @@ private InputStream getStream(int zoom) {
173180 * @return an {@link InputStream} to read from, or <code>null</code> if fileName
174181 * does not denotes an existing resource
175182 */
176- private InputStream getStream (String fileName ) {
183+ private SourceAtZoom < InputStream > getStream (SourceAtZoom < String > fileName ) {
177184 if (fileName != null ) {
185+ // TODO DO we need to close these?
178186 if (location != null ) {
179- return location .getResourceAsStream (fileName );
187+ return new SourceAtZoom <>( location .getResourceAsStream (fileName . source ()), fileName . zoom () );
180188 }
181189 try {
182- return new FileInputStream (fileName );
190+ return new SourceAtZoom <>( new FileInputStream (fileName . source ()), fileName . zoom () );
183191 } catch (FileNotFoundException e ) {
184192 return null ;
185193 }
186194 }
187195 return null ;
188196 }
189197
190- static String getxPath (String name , int zoom ) {
198+ static SourceAtZoom < String > getxPath (String name , int zoom ) {
191199 Matcher matcher = XPATH_PATTERN .matcher (name );
192200 if (matcher .find ()) {
193201 try {
@@ -197,24 +205,29 @@ static String getxPath(String name, int zoom) {
197205 int desiredHeight = Math .round ((zoom / 100f ) * currentHeight );
198206 String lead = name .substring (0 , matcher .start (1 ));
199207 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 );
201210 } catch (RuntimeException e ) {
202211 // should never happen but if then we can't use the alternative name...
203212 }
204213 }
205214 return null ;
206215 }
207216
208- static String getxName (String name , int zoom ) {
217+ static SourceAtZoom < String > getxName (String name , int zoom ) {
209218 int dot = name .lastIndexOf ('.' );
210219 if (dot != -1 && (zoom == 150 || zoom == 200 )) {
211220 String lead = name .substring (0 , dot );
212221 String tail = name .substring (dot );
213222 if (InternalPolicy .DEBUG_LOAD_URL_IMAGE_DESCRIPTOR_2x_PNG_FOR_GIF && ".gif" .equalsIgnoreCase (tail )) { //$NON-NLS-1$
214223 tail = ".png" ; //$NON-NLS-1$
215224 }
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 );
218231 }
219232 return null ;
220233 }
0 commit comments