Skip to content

Commit 729878c

Browse files
committed
Use Path to represent file-system paths for ImageData creation
Use java.nio.file.Path to model file-system paths instead of String to represent file-system paths. Add the new way to create ImageData as static factory instead of a constructor, because a constructor is not really suitable to create a copy of the first element of an array of ImageData. Also inline the ImageDataLoader and use the new ImageData.load() factories instead.
1 parent 0e0d38c commit 729878c

File tree

9 files changed

+464
-204
lines changed

9 files changed

+464
-204
lines changed

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

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.eclipse.swt.internal.image.ImageColorTransformer.DEFAULT_DISABLED_IMAGE_TRANSFORMER;
1818

1919
import java.io.*;
20+
import java.nio.file.Path;
2021
import java.util.*;
2122
import java.util.function.*;
2223

@@ -131,9 +132,9 @@ public final class Image extends Resource implements Drawable {
131132
static final int DEFAULT_SCANLINE_PAD = 4;
132133

133134
/**
134-
* ImageFileNameProvider to provide file names at various Zoom levels
135+
* ImageFileProvider to provide files at various Zoom levels
135136
*/
136-
private ImageFileNameProvider imageFileNameProvider;
137+
private ImagePathProvider imageFileProvider;
137138

138139
/**
139140
* ImageDataProvider to provide ImageData at various Zoom levels
@@ -392,11 +393,11 @@ public Image(Device device, Image srcImage, int flag) {
392393
/* Create the 100% representation for the new image from source image & apply flag */
393394
createRepFromSourceAndApplyFlag(srcImage.getRepresentation (100), srcWidth, srcHeight, flag);
394395

395-
imageFileNameProvider = srcImage.imageFileNameProvider;
396+
imageFileProvider = srcImage.imageFileProvider;
396397
imageDataProvider = srcImage.imageDataProvider;
397398
imageGcDrawer = srcImage.imageGcDrawer;
398399
this.styleFlag = srcImage.styleFlag | flag;
399-
if (imageFileNameProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) {
400+
if (imageFileProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) {
400401
/* If source image has 200% representation then create the 200% representation for the new image & apply flag */
401402
NSBitmapImageRep rep200 = srcImage.getRepresentation (200);
402403
if (rep200 != null) createRepFromSourceAndApplyFlag(rep200, srcWidth * 2, srcHeight * 2, flag);
@@ -642,18 +643,11 @@ public Image(Device device, ImageData source, ImageData mask) {
642643
* </p>
643644
* <pre>
644645
* static Image loadImage (Display display, Class clazz, String string) {
645-
* InputStream stream = clazz.getResourceAsStream (string);
646-
* if (stream == null) return null;
647-
* Image image = null;
648-
* try {
649-
* image = new Image (display, stream);
650-
* } catch (SWTException ex) {
651-
* } finally {
652-
* try {
653-
* stream.close ();
654-
* } catch (IOException ex) {}
655-
* }
656-
* return image;
646+
* try (InputStream stream = clazz.getResourceAsStream(string)){
647+
* if (stream == null) return null;
648+
* return new Image (display, stream);
649+
* } catch (SWTException | IOException ex) {
650+
* }
657651
* }
658652
* </pre>
659653
* <p>
@@ -688,8 +682,8 @@ public Image(Device device, InputStream stream) {
688682
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
689683
try {
690684
byte[] input = stream.readAllBytes();
691-
initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom),
692-
zoom -> ImageDataLoader.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element());
685+
initWithSupplier(zoom -> ImageLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom),
686+
zoom -> ImageData.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element());
693687
init();
694688
} catch (IOException e) {
695689
SWT.error(SWT.ERROR_INVALID_ARGUMENT, e);
@@ -736,10 +730,11 @@ public Image(Device device, String filename) {
736730
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
737731
try {
738732
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
739-
initNative(filename);
733+
Path file = Path.of(filename);
734+
initNative(file);
740735
if (this.handle == null) {
741-
initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(filename, FileFormat.DEFAULT_ZOOM, zoom),
742-
zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom).element());
736+
initWithSupplier(zoom -> ImageLoader.canLoadAtZoom(file, FileFormat.DEFAULT_ZOOM, zoom),
737+
zoom -> ImageData.load(file, FileFormat.DEFAULT_ZOOM, zoom).element());
743738
}
744739
init();
745740
} finally {
@@ -775,28 +770,63 @@ public Image(Device device, String filename) {
775770
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
776771
* </ul>
777772
* @since 3.104
773+
* @deprecated Instead use {@link #Image(Device, ImagePathProvider)}
778774
*/
775+
@Deprecated(since = "2025-06")
779776
public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
777+
this(device, ImageData.asImagePathProvider(imageFileNameProvider));
778+
}
779+
780+
/**
781+
* Constructs an instance of this class by loading its representation
782+
* from the file retrieved from the {@link ImagePathProvider}. Throws an
783+
* error if an error occurs while loading the image, or if the result
784+
* is an image of an unsupported type.
785+
* <p>
786+
* This constructor is provided for convenience for loading image as
787+
* per DPI level.
788+
*
789+
* @param device the device on which to create the image
790+
* @param imageFileProvider the {@link ImagePathProvider} object that is
791+
* to be used to get the file
792+
*
793+
* @exception IllegalArgumentException <ul>
794+
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
795+
* <li>ERROR_NULL_ARGUMENT - if the ImageFileNameProvider is null</li>
796+
* <li>ERROR_INVALID_ARGUMENT - if the fileName provided by ImageFileNameProvider is null at 100% zoom</li>
797+
* </ul>
798+
* @exception SWTException <ul>
799+
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
800+
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li>
801+
* <li>ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth</li>
802+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
803+
* </ul>
804+
* @exception SWTError <ul>
805+
* <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
806+
* </ul>
807+
* @since 3.130
808+
*/
809+
public Image(Device device, ImagePathProvider imageFileProvider) {
780810
super(device);
781-
if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
782-
this.imageFileNameProvider = imageFileNameProvider;
783-
String filename = imageFileNameProvider.getImagePath(100);
784-
if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
811+
if (imageFileProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
812+
this.imageFileProvider = imageFileProvider;
813+
Path file = imageFileProvider.getImagePath(100);
814+
if (file == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
785815
NSAutoreleasePool pool = null;
786816
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
787817
try {
788-
initNative(filename);
789-
if (this.handle == null) init(ImageDataLoader.load(filename, 100, 100).element());
818+
initNative(file);
819+
if (this.handle == null) init(ImageData.load(file, 100, 100).element());
790820
init();
791-
String filename2x = imageFileNameProvider.getImagePath(200);
792-
if (filename2x != null) {
821+
Path file2x = imageFileProvider.getImagePath(200);
822+
if (file2x != null) {
793823
alphaInfo_200 = new AlphaInfo();
794-
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x));
824+
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(file2x.toString()));
795825
NSImageRep rep = new NSImageRep(id);
796826
handle.addRepresentation(rep);
797-
} else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) {
827+
} else if (ImageLoader.canLoadAtZoom(file, 100, 200)) {
798828
// Try to natively scale up the image (e.g. possible if it's an SVG)
799-
ImageData imageData2x = ImageDataLoader.load(filename, 100, 200).element();
829+
ImageData imageData2x = ImageData.load(file, 100, 200).element();
800830
alphaInfo_200 = new AlphaInfo();
801831
NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
802832
handle.addRepresentation(rep);
@@ -927,7 +957,7 @@ private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width,
927957

928958
private AlphaInfo _getAlphaInfoAtCurrentZoom (NSBitmapImageRep rep) {
929959
int deviceZoom = DPIUtil.getDeviceZoom();
930-
if (deviceZoom != 100 && (imageFileNameProvider != null || imageDataProvider != null)) {
960+
if (deviceZoom != 100 && (imageFileProvider != null || imageDataProvider != null)) {
931961
if (alphaInfo_100.alphaData != null && alphaInfo_200 != null) {
932962
if (alphaInfo_200.alphaData == null) initAlpha_200(rep);
933963
return alphaInfo_200;
@@ -1201,8 +1231,8 @@ public boolean equals (Object object) {
12011231
if (device != image.device || alphaInfo_100.transparentPixel != image.alphaInfo_100.transparentPixel) return false;
12021232
if (imageDataProvider != null && image.imageDataProvider != null) {
12031233
return styleFlag == image.styleFlag && imageDataProvider.equals (image.imageDataProvider);
1204-
} else if (imageFileNameProvider != null && image.imageFileNameProvider != null) {
1205-
return styleFlag == image.styleFlag && imageFileNameProvider.equals (image.imageFileNameProvider);
1234+
} else if (imageFileProvider != null && image.imageFileProvider != null) {
1235+
return styleFlag == image.styleFlag && imageFileProvider.equals (image.imageFileProvider);
12061236
} else if (imageGcDrawer != null && image.imageGcDrawer != null) {
12071237
return styleFlag == image.styleFlag && imageGcDrawer.equals(image.imageGcDrawer) && width == image.width
12081238
&& height == image.height;
@@ -1440,8 +1470,8 @@ NSBitmapImageRep createImageRep(NSSize targetSize) {
14401470
public int hashCode () {
14411471
if (imageDataProvider != null) {
14421472
return imageDataProvider.hashCode();
1443-
} else if (imageFileNameProvider != null) {
1444-
return imageFileNameProvider.hashCode();
1473+
} else if (imageFileProvider != null) {
1474+
return imageFileProvider.hashCode();
14451475
} else if (imageGcDrawer != null) {
14461476
return Objects.hash(imageGcDrawer, height, width);
14471477
} else {
@@ -1545,7 +1575,8 @@ void initAlpha_100(NSBitmapImageRep nativeRep) {
15451575

15461576
}
15471577

1548-
void initNative(String filename) {
1578+
void initNative(Path file) {
1579+
String filename = file.toString();
15491580
NSAutoreleasePool pool = null;
15501581
NSImage nativeImage = null;
15511582

0 commit comments

Comments
 (0)