Skip to content

Commit 0d6c6b0

Browse files
committed
Replace Image creating methods accepting Strings as file-system paths
Use File instead of the more modern java.nio.file.Path to avoid confusion with the Path class that already exists in the 'o.e.swt.graphics' package.
1 parent 6d374d9 commit 0d6c6b0

File tree

8 files changed

+376
-43
lines changed

8 files changed

+376
-43
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ public Image(Device device, InputStream stream) {
713713
* </p>
714714
*
715715
* @param device the device on which to create the image
716-
* @param filename the name of the file to load the image from
716+
* @param file the name of the file to load the image from
717717
*
718718
* @exception IllegalArgumentException <ul>
719719
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
@@ -730,21 +730,60 @@ public Image(Device device, InputStream stream) {
730730
* </ul>
731731
*
732732
* @see #dispose()
733+
* @since 3.129
733734
*/
734-
public Image(Device device, String filename) {
735+
public Image(Device device, File file) {
735736
super(device);
736737
NSAutoreleasePool pool = null;
737738
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
738739
try {
739-
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
740-
initNative(filename);
741-
if (this.handle == null) init(new ImageData(filename));
740+
if (file == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
741+
initNative(file.toString());
742+
if (this.handle == null) init(new ImageData(file));
742743
init();
743744
} finally {
744745
if (pool != null) pool.release();
745746
}
746747
}
747748

749+
/**
750+
* Constructs an instance of this class by loading its representation
751+
* from the file with the specified name. Throws an error if an error
752+
* occurs while loading the image, or if the result is an image
753+
* of an unsupported type.
754+
* <p>
755+
* This constructor is provided for convenience when loading
756+
* a single image only. If the specified file contains
757+
* multiple images, only the first one will be used.
758+
* <p>
759+
* You must dispose the image when it is no longer required.
760+
* </p>
761+
*
762+
* @param device the device on which to create the image
763+
* @param filename the name of the file to load the image from
764+
*
765+
* @exception IllegalArgumentException <ul>
766+
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
767+
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
768+
* </ul>
769+
* @exception SWTException <ul>
770+
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
771+
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li>
772+
* <li>ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth</li>
773+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
774+
* </ul>
775+
* @exception SWTError <ul>
776+
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
777+
* </ul>
778+
*
779+
* @see #dispose()
780+
* @deprecated Instead use {@link #Image(Device, File)}
781+
*/
782+
@Deprecated(since = "2025-03")
783+
public Image(Device device, String filename) {
784+
this(device, new File(filename));
785+
}
786+
748787
/**
749788
* Constructs an instance of this class by loading its representation
750789
* from the file retrieved from the ImageFileNameProvider. Throws an
@@ -880,7 +919,7 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
880919
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
881920
try {
882921
init (data);
883-
init ();
922+
init ();
884923
} finally {
885924
if (pool != null) pool.release();
886925
}

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public ImageData[] load(InputStream stream) {
161161
* an error occurs while loading the images, or if the images are
162162
* not of a supported type. Returns the loaded image data array.
163163
*
164-
* @param filename the name of the file to load the images from
164+
* @param file the name of the file to load the images from
165165
* @return an array of <code>ImageData</code> objects loaded from the specified file
166166
*
167167
* @exception IllegalArgumentException <ul>
@@ -172,17 +172,42 @@ public ImageData[] load(InputStream stream) {
172172
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
173173
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
174174
* </ul>
175+
* @since 3.129
175176
*/
176-
public ImageData[] load(String filename) {
177-
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
178-
try (InputStream stream = new FileInputStream(filename)) {
177+
public ImageData[] load(File file) {
178+
if (file == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
179+
try (InputStream stream = new FileInputStream(file)) {
179180
return load(stream);
180181
} catch (IOException e) {
181182
SWT.error(SWT.ERROR_IO, e);
182183
}
183184
return null;
184185
}
185186

187+
/**
188+
* Loads an array of <code>ImageData</code> objects from the
189+
* file with the specified name. Throws an error if either
190+
* an error occurs while loading the images, or if the images are
191+
* not of a supported type. Returns the loaded image data array.
192+
*
193+
* @param filename the name of the file to load the images from
194+
* @return an array of <code>ImageData</code> objects loaded from the specified file
195+
*
196+
* @exception IllegalArgumentException <ul>
197+
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
198+
* </ul>
199+
* @exception SWTException <ul>
200+
* <li>ERROR_IO - if an IO error occurs while reading the file</li>
201+
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
202+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
203+
* </ul>
204+
* @deprecated Instead use {@link #load(File)}
205+
*/
206+
@Deprecated(since = "2025-03")
207+
public ImageData[] load(String filename) {
208+
return load(new File(filename));
209+
}
210+
186211
/**
187212
* Saves the image data in this ImageLoader to the specified stream.
188213
* The format parameter can have one of the following values:
@@ -236,7 +261,7 @@ public void save(OutputStream stream, int format) {
236261
* <dd>PNG file format</dd>
237262
* </dl>
238263
*
239-
* @param filename the name of the file to write the images to
264+
* @param file the name of the file to write the images to
240265
* @param format the format to write the images in
241266
*
242267
* @exception IllegalArgumentException <ul>
@@ -247,16 +272,53 @@ public void save(OutputStream stream, int format) {
247272
* <li>ERROR_INVALID_IMAGE - if the image data contains invalid data</li>
248273
* <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
249274
* </ul>
275+
* @since 3.129
250276
*/
251-
public void save(String filename, int format) {
252-
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
253-
try (OutputStream stream = new FileOutputStream(filename)) {
277+
public void save(File file, int format) {
278+
if (file == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
279+
try (OutputStream stream = new FileOutputStream(file)) {
254280
save(stream, format);
255281
} catch (IOException e) {
256282
SWT.error(SWT.ERROR_IO, e);
257283
}
258284
}
259285

286+
/**
287+
* Saves the image data in this ImageLoader to a file with the specified name.
288+
* The format parameter can have one of the following values:
289+
* <dl>
290+
* <dt><code>IMAGE_BMP</code></dt>
291+
* <dd>Windows BMP file format, no compression</dd>
292+
* <dt><code>IMAGE_BMP_RLE</code></dt>
293+
* <dd>Windows BMP file format, RLE compression if appropriate</dd>
294+
* <dt><code>IMAGE_GIF</code></dt>
295+
* <dd>GIF file format</dd>
296+
* <dt><code>IMAGE_ICO</code></dt>
297+
* <dd>Windows ICO file format</dd>
298+
* <dt><code>IMAGE_JPEG</code></dt>
299+
* <dd>JPEG file format</dd>
300+
* <dt><code>IMAGE_PNG</code></dt>
301+
* <dd>PNG file format</dd>
302+
* </dl>
303+
*
304+
* @param filename the name of the file to write the images to
305+
* @param format the format to write the images in
306+
*
307+
* @exception IllegalArgumentException <ul>
308+
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
309+
* </ul>
310+
* @exception SWTException <ul>
311+
* <li>ERROR_IO - if an IO error occurs while writing to the file</li>
312+
* <li>ERROR_INVALID_IMAGE - if the image data contains invalid data</li>
313+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
314+
* </ul>
315+
* @deprecated Instead use {@link #save(File, int)}
316+
*/
317+
@Deprecated(since = "2025-03")
318+
public void save(String filename, int format) {
319+
save(new File(filename), format);
320+
}
321+
260322
/**
261323
* Adds the listener to the collection of listeners who will be
262324
* notified when image data is either partially or completely loaded.

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public ImageData(InputStream stream) {
365365
* <code>ImageLoader.load()</code>.
366366
* </p>
367367
*
368-
* @param filename the name of the file to load the image from (must not be null)
368+
* @param file the name of the file to load the image from (must not be null)
369369
*
370370
* @exception IllegalArgumentException <ul>
371371
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
@@ -375,9 +375,10 @@ public ImageData(InputStream stream) {
375375
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
376376
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
377377
* </ul>
378+
* @since 3.129
378379
*/
379-
public ImageData(String filename) {
380-
ImageData[] data = ImageDataLoader.load(filename);
380+
public ImageData(File file) {
381+
ImageData[] data = ImageDataLoader.load(file);
381382
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
382383
ImageData i = data[0];
383384
setAllFields(
@@ -400,6 +401,35 @@ public ImageData(String filename) {
400401
i.delayTime);
401402
}
402403

404+
/**
405+
* Constructs an <code>ImageData</code> loaded from a file with the
406+
* specified name. Throws an error if an error occurs loading the
407+
* image, or if the image has an unsupported type.
408+
* <p>
409+
* This constructor is provided for convenience when loading a single
410+
* image only. If the file contains multiple images, only the first
411+
* one will be loaded. To load multiple images, use
412+
* <code>ImageLoader.load()</code>.
413+
* </p>
414+
*
415+
* @param filename the name of the file to load the image from (must not be null)
416+
*
417+
* @exception IllegalArgumentException <ul>
418+
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
419+
* </ul>
420+
* @exception SWTException <ul>
421+
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
422+
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
423+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
424+
* </ul>
425+
* @since 3.129
426+
* @deprecated Instead use {@link #ImageData(File)}
427+
*/
428+
@Deprecated(since = "2025-03")
429+
public ImageData(String filename) {
430+
this(new File(filename));
431+
}
432+
403433
/**
404434
* Prevents uninitialized instances from being created outside the package.
405435
*/

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public static ImageData[] load(InputStream stream) {
2525
return new ImageLoader().load(stream);
2626
}
2727

28-
public static ImageData[] load(String filename) {
29-
return new ImageLoader().load(filename);
28+
public static ImageData[] load(File file) {
29+
return new ImageLoader().load(file);
3030
}
3131

3232
}

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ public Image(Device device, InputStream stream) {
551551
* </p>
552552
*
553553
* @param device the device on which to create the image
554-
* @param filename the name of the file to load the image from
554+
* @param file the name of the file to load the image from
555555
*
556556
* @exception IllegalArgumentException <ul>
557557
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
@@ -568,18 +568,57 @@ public Image(Device device, InputStream stream) {
568568
* </ul>
569569
*
570570
* @see #dispose()
571+
* @since 3.129
571572
*/
572-
public Image(Device device, String filename) {
573+
public Image(Device device, File file) {
573574
super(device);
574-
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
575+
if (file == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
575576

576-
ImageData data = new ImageData(filename);
577+
ImageData data = new ImageData(file);
577578
currentDeviceZoom = DPIUtil.getDeviceZoom();
578579
data = DPIUtil.autoScaleUp (device, data);
579580
init(data);
580581
init();
581582
}
582583

584+
/**
585+
* Constructs an instance of this class by loading its representation
586+
* from the file with the specified name. Throws an error if an error
587+
* occurs while loading the image, or if the result is an image
588+
* of an unsupported type.
589+
* <p>
590+
* This constructor is provided for convenience when loading
591+
* a single image only. If the specified file contains
592+
* multiple images, only the first one will be used.
593+
* <p>
594+
* You must dispose the image when it is no longer required.
595+
* </p>
596+
*
597+
* @param device the device on which to create the image
598+
* @param filename the name of the file to load the image from
599+
*
600+
* @exception IllegalArgumentException <ul>
601+
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
602+
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
603+
* </ul>
604+
* @exception SWTException <ul>
605+
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
606+
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li>
607+
* <li>ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth</li>
608+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
609+
* </ul>
610+
* @exception SWTError <ul>
611+
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
612+
* </ul>
613+
*
614+
* @see #dispose()
615+
* @deprecated Instead use {@link #Image(Device, File)}
616+
*/
617+
@Deprecated(since = "2025-03")
618+
public Image(Device device, String filename) {
619+
this(device, new File(filename));
620+
}
621+
583622
/**
584623
* Constructs an instance of this class by loading its representation
585624
* from the file retrieved from the ImageFileNameProvider. Throws an

0 commit comments

Comments
 (0)