1
1
/*******************************************************************************
2
- * Copyright (c) 2000, 2020 IBM Corporation and others.
2
+ * Copyright (c) 2000, 2025 IBM Corporation and others.
3
3
*
4
4
* This program and the accompanying materials
5
5
* are made available under the terms of the Eclipse Public License 2.0
15
15
16
16
17
17
import java .io .*;
18
+ import java .nio .file .Path ;
18
19
import java .util .*;
19
20
20
21
import org .eclipse .swt .*;
@@ -127,9 +128,9 @@ public final class Image extends Resource implements Drawable {
127
128
static final int DEFAULT_SCANLINE_PAD = 4 ;
128
129
129
130
/**
130
- * ImageFileNameProvider to provide file names at various Zoom levels
131
+ * ImageFileProvider to provide files at various Zoom levels
131
132
*/
132
- private ImageFileNameProvider imageFileNameProvider ;
133
+ private ImageFileProvider imageFileProvider ;
133
134
134
135
/**
135
136
* ImageDataProvider to provide ImageData at various Zoom levels
@@ -388,11 +389,11 @@ public Image(Device device, Image srcImage, int flag) {
388
389
/* Create the 100% representation for the new image from source image & apply flag */
389
390
createRepFromSourceAndApplyFlag (srcImage .getRepresentation (100 ), srcWidth , srcHeight , flag );
390
391
391
- imageFileNameProvider = srcImage .imageFileNameProvider ;
392
+ imageFileProvider = srcImage .imageFileProvider ;
392
393
imageDataProvider = srcImage .imageDataProvider ;
393
394
imageGcDrawer = srcImage .imageGcDrawer ;
394
395
this .styleFlag = srcImage .styleFlag | flag ;
395
- if (imageFileNameProvider != null || imageDataProvider != null ||srcImage .imageGcDrawer != null ) {
396
+ if (imageFileProvider != null || imageDataProvider != null ||srcImage .imageGcDrawer != null ) {
396
397
/* If source image has 200% representation then create the 200% representation for the new image & apply flag */
397
398
NSBitmapImageRep rep200 = srcImage .getRepresentation (200 );
398
399
if (rep200 != null ) createRepFromSourceAndApplyFlag (rep200 , srcWidth * 2 , srcHeight * 2 , flag );
@@ -737,8 +738,9 @@ public Image(Device device, String filename) {
737
738
if (!NSThread .isMainThread ()) pool = (NSAutoreleasePool ) new NSAutoreleasePool ().alloc ().init ();
738
739
try {
739
740
if (filename == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
740
- initNative (filename );
741
- if (this .handle == null ) init (new ImageData (filename ));
741
+ Path file = Path .of (filename );
742
+ initNative (file );
743
+ if (this .handle == null ) init (ImageData .load (file ));
742
744
init ();
743
745
} finally {
744
746
if (pool != null ) pool .release ();
@@ -747,16 +749,16 @@ public Image(Device device, String filename) {
747
749
748
750
/**
749
751
* Constructs an instance of this class by loading its representation
750
- * from the file retrieved from the ImageFileNameProvider . Throws an
752
+ * from the file retrieved from the {@link ImageFileProvider} . Throws an
751
753
* error if an error occurs while loading the image, or if the result
752
754
* is an image of an unsupported type.
753
755
* <p>
754
756
* This constructor is provided for convenience for loading image as
755
757
* per DPI level.
756
758
*
757
759
* @param device the device on which to create the image
758
- * @param imageFileNameProvider the ImageFileNameProvider object that is
759
- * to be used to get the file name
760
+ * @param imageFileProvider the {@link ImageFileProvider} object that is
761
+ * to be used to get the file
760
762
*
761
763
* @exception IllegalArgumentException <ul>
762
764
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
@@ -772,24 +774,24 @@ public Image(Device device, String filename) {
772
774
* @exception SWTError <ul>
773
775
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
774
776
* </ul>
775
- * @since 3.104
777
+ * @since 3.129
776
778
*/
777
- public Image (Device device , ImageFileNameProvider imageFileNameProvider ) {
779
+ public Image (Device device , ImageFileProvider imageFileProvider ) {
778
780
super (device );
779
- if (imageFileNameProvider == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
780
- this .imageFileNameProvider = imageFileNameProvider ;
781
- String filename = imageFileNameProvider .getImagePath (100 );
782
- if (filename == null ) SWT .error (SWT .ERROR_INVALID_ARGUMENT );
781
+ if (imageFileProvider == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
782
+ this .imageFileProvider = imageFileProvider ;
783
+ Path file = imageFileProvider .getImagePath (100 );
784
+ if (file == null ) SWT .error (SWT .ERROR_INVALID_ARGUMENT );
783
785
NSAutoreleasePool pool = null ;
784
786
if (!NSThread .isMainThread ()) pool = (NSAutoreleasePool ) new NSAutoreleasePool ().alloc ().init ();
785
787
try {
786
- initNative (filename );
787
- if (this .handle == null ) init (new ImageData ( filename ));
788
+ initNative (file );
789
+ if (this .handle == null ) init (ImageData . load ( file ));
788
790
init ();
789
- String filename2x = imageFileNameProvider .getImagePath (200 );
790
- if (filename2x != null ) {
791
+ Path file2x = imageFileProvider .getImagePath (200 );
792
+ if (file2x != null ) {
791
793
alphaInfo_200 = new AlphaInfo ();
792
- id id = NSImageRep .imageRepWithContentsOfFile (NSString .stringWith (filename2x ));
794
+ id id = NSImageRep .imageRepWithContentsOfFile (NSString .stringWith (file2x . toString () ));
793
795
NSImageRep rep = new NSImageRep (id );
794
796
handle .addRepresentation (rep );
795
797
}
@@ -798,6 +800,41 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
798
800
}
799
801
}
800
802
803
+ /**
804
+ * Constructs an instance of this class by loading its representation
805
+ * from the file retrieved from the ImageFileNameProvider. Throws an
806
+ * error if an error occurs while loading the image, or if the result
807
+ * is an image of an unsupported type.
808
+ * <p>
809
+ * This constructor is provided for convenience for loading image as
810
+ * per DPI level.
811
+ *
812
+ * @param device the device on which to create the image
813
+ * @param imageFileNameProvider the ImageFileNameProvider object that is
814
+ * to be used to get the file name
815
+ *
816
+ * @exception IllegalArgumentException <ul>
817
+ * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
818
+ * <li>ERROR_NULL_ARGUMENT - if the ImageFileNameProvider is null</li>
819
+ * <li>ERROR_INVALID_ARGUMENT - if the fileName provided by ImageFileNameProvider is null at 100% zoom</li>
820
+ * </ul>
821
+ * @exception SWTException <ul>
822
+ * <li>ERROR_IO - if an IO error occurs while reading from the file</li>
823
+ * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li>
824
+ * <li>ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth</li>
825
+ * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
826
+ * </ul>
827
+ * @exception SWTError <ul>
828
+ * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
829
+ * </ul>
830
+ * @since 3.104
831
+ * @deprecated Instead use {@link #Image(Device, ImageFileProvider)}
832
+ */
833
+ @ Deprecated (since = "2025-03" )
834
+ public Image (Device device , ImageFileNameProvider imageFileNameProvider ) {
835
+ this (device , (ImageFileProvider ) zoom -> Path .of (imageFileNameProvider .getImagePath (zoom )));
836
+ }
837
+
801
838
/**
802
839
* Constructs an instance of this class by loading its representation
803
840
* from the ImageData retrieved from the ImageDataProvider. Throws an
@@ -880,7 +917,7 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
880
917
if (!NSThread .isMainThread ()) pool = (NSAutoreleasePool ) new NSAutoreleasePool ().alloc ().init ();
881
918
try {
882
919
init (data );
883
- init ();
920
+ init ();
884
921
} finally {
885
922
if (pool != null ) pool .release ();
886
923
}
@@ -902,7 +939,7 @@ private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width,
902
939
903
940
private AlphaInfo _getAlphaInfoAtCurrentZoom (NSBitmapImageRep rep ) {
904
941
int deviceZoom = DPIUtil .getDeviceZoom ();
905
- if (deviceZoom != 100 && (imageFileNameProvider != null || imageDataProvider != null )) {
942
+ if (deviceZoom != 100 && (imageFileProvider != null || imageDataProvider != null )) {
906
943
if (alphaInfo_100 .alphaData != null && alphaInfo_200 != null ) {
907
944
if (alphaInfo_200 .alphaData == null ) initAlpha_200 (rep );
908
945
return alphaInfo_200 ;
@@ -1176,8 +1213,8 @@ public boolean equals (Object object) {
1176
1213
if (device != image .device || alphaInfo_100 .transparentPixel != image .alphaInfo_100 .transparentPixel ) return false ;
1177
1214
if (imageDataProvider != null && image .imageDataProvider != null ) {
1178
1215
return styleFlag == image .styleFlag && imageDataProvider .equals (image .imageDataProvider );
1179
- } else if (imageFileNameProvider != null && image .imageFileNameProvider != null ) {
1180
- return styleFlag == image .styleFlag && imageFileNameProvider .equals (image .imageFileNameProvider );
1216
+ } else if (imageFileProvider != null && image .imageFileProvider != null ) {
1217
+ return styleFlag == image .styleFlag && imageFileProvider .equals (image .imageFileProvider );
1181
1218
} else if (imageGcDrawer != null && image .imageGcDrawer != null ) {
1182
1219
return styleFlag == image .styleFlag && imageGcDrawer .equals (image .imageGcDrawer ) && width == image .width
1183
1220
&& height == image .height ;
@@ -1415,8 +1452,8 @@ NSBitmapImageRep createImageRep(NSSize targetSize) {
1415
1452
public int hashCode () {
1416
1453
if (imageDataProvider != null ) {
1417
1454
return imageDataProvider .hashCode ();
1418
- } else if (imageFileNameProvider != null ) {
1419
- return imageFileNameProvider .hashCode ();
1455
+ } else if (imageFileProvider != null ) {
1456
+ return imageFileProvider .hashCode ();
1420
1457
} else if (imageGcDrawer != null ) {
1421
1458
return Objects .hash (imageGcDrawer , height , width );
1422
1459
} else {
@@ -1507,7 +1544,8 @@ void initAlpha_100(NSBitmapImageRep nativeRep) {
1507
1544
1508
1545
}
1509
1546
1510
- void initNative (String filename ) {
1547
+ void initNative (Path file ) {
1548
+ String filename = file .toString ();
1511
1549
NSAutoreleasePool pool = null ;
1512
1550
NSImage nativeImage = null ;
1513
1551
0 commit comments