diff --git a/binaries/org.eclipse.swt.cocoa.macosx.aarch64/.settings/.api_filters b/binaries/org.eclipse.swt.cocoa.macosx.aarch64/.settings/.api_filters index ad4abeb951b..59c3e6cc9f7 100644 --- a/binaries/org.eclipse.swt.cocoa.macosx.aarch64/.settings/.api_filters +++ b/binaries/org.eclipse.swt.cocoa.macosx.aarch64/.settings/.api_filters @@ -478,12 +478,4 @@ - - - - - - - - diff --git a/binaries/org.eclipse.swt.cocoa.macosx.x86_64/.settings/.api_filters b/binaries/org.eclipse.swt.cocoa.macosx.x86_64/.settings/.api_filters index 3e161f4e933..b9e9bacdd89 100644 --- a/binaries/org.eclipse.swt.cocoa.macosx.x86_64/.settings/.api_filters +++ b/binaries/org.eclipse.swt.cocoa.macosx.x86_64/.settings/.api_filters @@ -478,12 +478,4 @@ - - - - - - - - diff --git a/binaries/org.eclipse.swt.gtk.linux.aarch64/.settings/.api_filters b/binaries/org.eclipse.swt.gtk.linux.aarch64/.settings/.api_filters index a13db65355a..29eed087e1d 100644 --- a/binaries/org.eclipse.swt.gtk.linux.aarch64/.settings/.api_filters +++ b/binaries/org.eclipse.swt.gtk.linux.aarch64/.settings/.api_filters @@ -478,12 +478,4 @@ - - - - - - - - diff --git a/binaries/org.eclipse.swt.gtk.linux.loongarch64/.settings/.api_filters b/binaries/org.eclipse.swt.gtk.linux.loongarch64/.settings/.api_filters index 32d58add358..c318b164f40 100644 --- a/binaries/org.eclipse.swt.gtk.linux.loongarch64/.settings/.api_filters +++ b/binaries/org.eclipse.swt.gtk.linux.loongarch64/.settings/.api_filters @@ -478,12 +478,4 @@ - - - - - - - - diff --git a/binaries/org.eclipse.swt.gtk.linux.ppc64le/.settings/.api_filters b/binaries/org.eclipse.swt.gtk.linux.ppc64le/.settings/.api_filters index fe0b7c72db9..cfbb261cd1b 100644 --- a/binaries/org.eclipse.swt.gtk.linux.ppc64le/.settings/.api_filters +++ b/binaries/org.eclipse.swt.gtk.linux.ppc64le/.settings/.api_filters @@ -478,12 +478,4 @@ - - - - - - - - diff --git a/binaries/org.eclipse.swt.gtk.linux.riscv64/.settings/.api_filters b/binaries/org.eclipse.swt.gtk.linux.riscv64/.settings/.api_filters index ac69e64af61..d94b0fc9730 100644 --- a/binaries/org.eclipse.swt.gtk.linux.riscv64/.settings/.api_filters +++ b/binaries/org.eclipse.swt.gtk.linux.riscv64/.settings/.api_filters @@ -478,12 +478,4 @@ - - - - - - - - diff --git a/binaries/org.eclipse.swt.gtk.linux.x86_64/.settings/.api_filters b/binaries/org.eclipse.swt.gtk.linux.x86_64/.settings/.api_filters index e218c3dcef1..13a7e5af94c 100644 --- a/binaries/org.eclipse.swt.gtk.linux.x86_64/.settings/.api_filters +++ b/binaries/org.eclipse.swt.gtk.linux.x86_64/.settings/.api_filters @@ -24,12 +24,4 @@ - - - - - - - - diff --git a/binaries/org.eclipse.swt.win32.win32.aarch64/.settings/.api_filters b/binaries/org.eclipse.swt.win32.win32.aarch64/.settings/.api_filters index f6b4725ec0d..e8c5426272d 100644 --- a/binaries/org.eclipse.swt.win32.win32.aarch64/.settings/.api_filters +++ b/binaries/org.eclipse.swt.win32.win32.aarch64/.settings/.api_filters @@ -614,12 +614,4 @@ - - - - - - - - diff --git a/binaries/org.eclipse.swt.win32.win32.x86_64/.settings/.api_filters b/binaries/org.eclipse.swt.win32.win32.x86_64/.settings/.api_filters index 5d0ce47678f..7b325e165d6 100644 --- a/binaries/org.eclipse.swt.win32.win32.x86_64/.settings/.api_filters +++ b/binaries/org.eclipse.swt.win32.win32.x86_64/.settings/.api_filters @@ -614,12 +614,4 @@ - - - - - - - - diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java index 8ef4c8ff4bb..b0e720e1110 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java @@ -17,6 +17,7 @@ import static org.eclipse.swt.internal.image.ImageColorTransformer.DEFAULT_DISABLED_IMAGE_TRANSFORMER; import java.io.*; +import java.nio.file.Path; import java.util.*; import java.util.function.*; @@ -131,9 +132,9 @@ public final class Image extends Resource implements Drawable { static final int DEFAULT_SCANLINE_PAD = 4; /** - * ImageFileNameProvider to provide file names at various Zoom levels + * ImageFileProvider to provide files at various Zoom levels */ - private ImageFileNameProvider imageFileNameProvider; + private ImagePathProvider imageFileProvider; /** * ImageDataProvider to provide ImageData at various Zoom levels @@ -392,11 +393,11 @@ public Image(Device device, Image srcImage, int flag) { /* Create the 100% representation for the new image from source image & apply flag */ createRepFromSourceAndApplyFlag(srcImage.getRepresentation (100), srcWidth, srcHeight, flag); - imageFileNameProvider = srcImage.imageFileNameProvider; + imageFileProvider = srcImage.imageFileProvider; imageDataProvider = srcImage.imageDataProvider; imageGcDrawer = srcImage.imageGcDrawer; this.styleFlag = srcImage.styleFlag | flag; - if (imageFileNameProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) { + if (imageFileProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) { /* If source image has 200% representation then create the 200% representation for the new image & apply flag */ NSBitmapImageRep rep200 = srcImage.getRepresentation (200); if (rep200 != null) createRepFromSourceAndApplyFlag(rep200, srcWidth * 2, srcHeight * 2, flag); @@ -645,18 +646,11 @@ public Image(Device device, ImageData source, ImageData mask) { *

*
  *     static Image loadImage (Display display, Class clazz, String string) {
- *          InputStream stream = clazz.getResourceAsStream (string);
- *          if (stream == null) return null;
- *          Image image = null;
- *          try {
- *               image = new Image (display, stream);
- *          } catch (SWTException ex) {
- *          } finally {
- *               try {
- *                    stream.close ();
- *               } catch (IOException ex) {}
- *          }
- *          return image;
+ *         try (InputStream stream = clazz.getResourceAsStream(string)){
+ *             if (stream == null) return null;
+*              return new Image (display, stream);
+ *         } catch (SWTException | IOException ex) {
+ *         }
  *     }
  * 
*

@@ -691,8 +685,8 @@ public Image(Device device, InputStream stream) { if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { byte[] input = stream.readAllBytes(); - initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom), - zoom -> ImageDataLoader.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element()); + initWithSupplier(zoom -> ImageLoader.canLoadAtZoom(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom), + zoom -> ImageData.load(new ByteArrayInputStream(input), FileFormat.DEFAULT_ZOOM, zoom).element()); init(); } catch (IOException e) { SWT.error(SWT.ERROR_INVALID_ARGUMENT, e); @@ -739,10 +733,11 @@ public Image(Device device, String filename) { if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - initNative(filename); + Path file = Path.of(filename); + initNative(file); if (this.handle == null) { - initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(filename, FileFormat.DEFAULT_ZOOM, zoom), - zoom -> ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, zoom).element()); + initWithSupplier(zoom -> ImageLoader.canLoadAtZoom(file, FileFormat.DEFAULT_ZOOM, zoom), + zoom -> ImageData.load(file, FileFormat.DEFAULT_ZOOM, zoom).element()); } init(); } finally { @@ -778,28 +773,63 @@ public Image(Device device, String filename) { *

  • ERROR_NO_HANDLES if a handle could not be obtained for image creation
  • * * @since 3.104 + * @deprecated Instead use {@link #Image(Device, ImagePathProvider)} */ +@Deprecated(since = "2025-06") public Image(Device device, ImageFileNameProvider imageFileNameProvider) { + this(device, ImageData.asImagePathProvider(imageFileNameProvider)); +} + +/** + * Constructs an instance of this class by loading its representation + * from the file retrieved from the {@link ImagePathProvider}. Throws an + * error if an error occurs while loading the image, or if the result + * is an image of an unsupported type. + *

    + * This constructor is provided for convenience for loading image as + * per DPI level. + * + * @param device the device on which to create the image + * @param imageFileProvider the {@link ImagePathProvider} object that is + * to be used to get the file + * + * @exception IllegalArgumentException

    + * @exception SWTException + * @exception SWTError + * @since 3.130 + */ +public Image(Device device, ImagePathProvider imageFileProvider) { super(device); - if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - this.imageFileNameProvider = imageFileNameProvider; - String filename = imageFileNameProvider.getImagePath(100); - if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + if (imageFileProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + this.imageFileProvider = imageFileProvider; + Path file = imageFileProvider.getImagePath(100); + if (file == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); NSAutoreleasePool pool = null; if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { - initNative(filename); - if (this.handle == null) init(ImageDataLoader.load(filename, 100, 100).element()); + initNative(file); + if (this.handle == null) init(ImageData.load(file, 100, 100).element()); init(); - String filename2x = imageFileNameProvider.getImagePath(200); - if (filename2x != null) { + Path file2x = imageFileProvider.getImagePath(200); + if (file2x != null) { alphaInfo_200 = new AlphaInfo(); - id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x)); + id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(file2x.toString())); NSImageRep rep = new NSImageRep(id); handle.addRepresentation(rep); - } else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) { + } else if (ImageLoader.canLoadAtZoom(file, 100, 200)) { // Try to natively scale up the image (e.g. possible if it's an SVG) - ImageData imageData2x = ImageDataLoader.load(filename, 100, 200).element(); + ImageData imageData2x = ImageData.load(file, 100, 200).element(); alphaInfo_200 = new AlphaInfo(); NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200); handle.addRepresentation(rep); @@ -930,7 +960,7 @@ private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width, private AlphaInfo _getAlphaInfoAtCurrentZoom (NSBitmapImageRep rep) { int deviceZoom = DPIUtil.getDeviceZoom(); - if (deviceZoom != 100 && (imageFileNameProvider != null || imageDataProvider != null)) { + if (deviceZoom != 100 && (imageFileProvider != null || imageDataProvider != null)) { if (alphaInfo_100.alphaData != null && alphaInfo_200 != null) { if (alphaInfo_200.alphaData == null) initAlpha_200(rep); return alphaInfo_200; @@ -1204,8 +1234,8 @@ public boolean equals (Object object) { if (device != image.device || alphaInfo_100.transparentPixel != image.alphaInfo_100.transparentPixel) return false; if (imageDataProvider != null && image.imageDataProvider != null) { return styleFlag == image.styleFlag && imageDataProvider.equals (image.imageDataProvider); - } else if (imageFileNameProvider != null && image.imageFileNameProvider != null) { - return styleFlag == image.styleFlag && imageFileNameProvider.equals (image.imageFileNameProvider); + } else if (imageFileProvider != null && image.imageFileProvider != null) { + return styleFlag == image.styleFlag && imageFileProvider.equals (image.imageFileProvider); } else if (imageGcDrawer != null && image.imageGcDrawer != null) { return styleFlag == image.styleFlag && imageGcDrawer.equals(image.imageGcDrawer) && width == image.width && height == image.height; @@ -1443,8 +1473,8 @@ NSBitmapImageRep createImageRep(NSSize targetSize) { public int hashCode () { if (imageDataProvider != null) { return imageDataProvider.hashCode(); - } else if (imageFileNameProvider != null) { - return imageFileNameProvider.hashCode(); + } else if (imageFileProvider != null) { + return imageFileProvider.hashCode(); } else if (imageGcDrawer != null) { return Objects.hash(imageGcDrawer, height, width); } else { @@ -1548,7 +1578,8 @@ void initAlpha_100(NSBitmapImageRep nativeRep) { } -void initNative(String filename) { +void initNative(Path file) { + String filename = file.toString(); NSAutoreleasePool pool = null; NSImage nativeImage = null; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java index c5515b3fe74..a888d884634 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -15,9 +15,12 @@ import java.io.*; +import java.nio.file.Path; +import java.util.*; import org.eclipse.swt.*; import org.eclipse.swt.internal.*; +import org.eclipse.swt.internal.DPIUtil.*; /** * Instances of this class are device-independent descriptions @@ -239,6 +242,90 @@ public final class ImageData implements Cloneable { { 0x000000, 0x800000, 0x200000, 0xa00000, 0x080000, 0x880000, 0x280000, 0xa80000 } }; + /** + * Loads the {@code ImageData} from the specified file. Throws an error + * if an error occurs loading the image, or if the image has an unsupported + * type. + *

    + * This factory is provided for convenience when loading a single image only. If + * the file contains multiple images, only the first one will be loaded. To load + * multiple images, use {@link ImageLoader#load(Path)}. + *

    + * + * @param file the file to load the image from (must not be null) + * + * @exception IllegalArgumentException + * @exception SWTException + * @since 3.130 + */ + public static ImageData load(Path file) { + return first(Arrays.asList(new ImageLoader().load(file))); + } + + /** + * Loads the ImageData the specified input stream. + * Throws an error if an error occurs while loading the image, or if the image + * has an unsupported type. Application code is still responsible for closing + * the input stream. + *

    + * This factory is provided for convenience when loading a single image only. If + * the stream contains multiple images, only the first one will be loaded. To + * load multiple images, use {@link ImageLoader#load(InputStream)}. + *

    + *

    + * This factory may be used to load a resource as follows: + *

    + * + *
    +	 * static ImageData loadImageData(Class clazz, String string) {
    +	 *     try (InputStream stream = clazz.getResourceAsStream(string)) {
    +	 *         if (stream == null) return null;
    +	 *         return ImageData.load(stream);
    +	 *     } catch (SWTException | IOException ex) {
    +	 *         // handle exception appropriately
    +	 *     }
    +	 * }
    +	 * 
    + * + * @param stream the input stream to load the image from (must not be null) + * + * @exception IllegalArgumentException + * @exception SWTException + * + * @see ImageLoader#load(InputStream) + * @since 3.130 + */ + public static ImageData load(InputStream stream) { + return first(Arrays.asList(new ImageLoader().load(stream))); + } + + static ElementAtZoom load(InputStream stream, int fileZoom, int targetZoom) { + return first(new ImageLoader().load(stream, fileZoom, targetZoom)); + } + + static ElementAtZoom load(Path file, int fileZoom, int targetZoom) { + return first(new ImageLoader().load(file, fileZoom, targetZoom)); + } + + private static T first(List data) { + if (data.isEmpty()) { + SWT.error(SWT.ERROR_INVALID_IMAGE); + } + return data.get(0); + } + /** * Constructs a new, empty ImageData with the given width, height, * depth and palette. The data will be initialized to an (all zero) @@ -296,24 +383,18 @@ scanlinePad, checkData(data), 0, null, * This constructor is provided for convenience when loading a single * image only. If the stream contains multiple images, only the first * one will be loaded. To load multiple images, use - * ImageLoader.load(). + * {@link ImageLoader#load(InputStream)}. *

    * This constructor may be used to load a resource as follows: *

    *
      *     static ImageData loadImageData (Class clazz, String string) {
    - *          InputStream stream = clazz.getResourceAsStream (string);
    - *          if (stream == null) return null;
    - *          ImageData imageData = null;
    - *          try {
    - *               imageData = new ImageData (stream);
    - *          } catch (SWTException ex) {
    - *          } finally {
    - *               try {
    - *                    stream.close ();
    - *               } catch (IOException ex) {}
    + *          try (InputStream stream = clazz.getResourceAsStream (string)) {
    + *              if (stream == null) return null;
    + *              return ImageData.load(stream);
    + *          } catch (SWTException |IOException ex) {
    + *              // handle exception appropriately
      *          }
    - *          return imageData;
      *     }
      * 
    * @@ -329,9 +410,11 @@ scanlinePad, checkData(data), 0, null, * * * @see ImageLoader#load(InputStream) + * @deprecated Instead use {@link #load(InputStream)} */ +@Deprecated(since = "2025-06") public ImageData(InputStream stream) { - ImageData i = ImageDataLoader.load(stream); + ImageData i = load(stream); setAllFields( i.width, i.height, @@ -360,7 +443,7 @@ public ImageData(InputStream stream) { * This constructor is provided for convenience when loading a single * image only. If the file contains multiple images, only the first * one will be loaded. To load multiple images, use - * ImageLoader.load(). + * {@link ImageLoader#load(String)}. *

    * * @param filename the name of the file to load the image from (must not be null) @@ -373,9 +456,12 @@ public ImageData(InputStream stream) { *
  • ERROR_INVALID_IMAGE - if the image file contains invalid data
  • *
  • ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format
  • * + * @deprecated Instead use {@link #load(Path)} */ +@Deprecated(since = "2025-06") public ImageData(String filename) { - ImageData i = ImageDataLoader.load(filename); + if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + ImageData i = load(Path.of(filename)); setAllFields( i.width, i.height, @@ -2644,4 +2730,31 @@ static void fillGradientRectangle(GC gc, Device device, image.dispose(); } +@SuppressWarnings("deprecation") +static ImagePathProvider asImagePathProvider(ImageFileNameProvider provider) { + class WrappingImageFileProvider implements ImagePathProvider { + ImageFileNameProvider filenameProvider = provider; + + @Override + public Path getImagePath(int zoom) { + String path = filenameProvider.getImagePath(zoom); + return path != null ? Path.of(path) : null; + } + + @Override + public int hashCode() { + return filenameProvider.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof WrappingImageFileProvider wrappingProvider) { + return this.filenameProvider.equals(wrappingProvider.filenameProvider); + } + return this.filenameProvider.equals(obj); + } + } + return provider == null ? null : new WrappingImageFileProvider(); +} + } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java deleted file mode 100644 index 29f517b45c0..00000000000 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java +++ /dev/null @@ -1,60 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2025 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.swt.graphics; - -import java.io.*; -import java.util.*; - -import org.eclipse.swt.*; -import org.eclipse.swt.internal.DPIUtil.*; - -/** - * Internal class that separates ImageData from ImageLoader - * to allow removal of ImageLoader from the toolkit. - */ -class ImageDataLoader { - - public static ImageData load(InputStream stream) { - ImageData[] data = new ImageLoader().load(stream); - if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE); - return data[0]; - } - - public static ImageData load(String filename) { - ImageData[] data = new ImageLoader().load(filename); - if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE); - return data[0]; - } - - public static boolean canLoadAtZoom(InputStream stream, int fileZoom, int targetZoom) { - return ImageLoader.canLoadAtZoom(stream, fileZoom, targetZoom); - } - - public static ElementAtZoom load(InputStream stream, int fileZoom, int targetZoom) { - List> data = new ImageLoader().load(stream, fileZoom, targetZoom); - if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE); - return data.get(0); - } - - public static boolean canLoadAtZoom(String filename, int fileZoom, int targetZoom) { - return ImageLoader.canLoadAtZoom(filename, fileZoom, targetZoom); - } - - public static ElementAtZoom load(String filename, int fileZoom, int targetZoom) { - List> data = new ImageLoader().load(filename, fileZoom, targetZoom); - if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE); - return data.get(0); - } - -} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageFileNameProvider.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageFileNameProvider.java index 2413cda3231..d3c36eaf68b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageFileNameProvider.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageFileNameProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2018 IBM Corporation and others. + * Copyright (c) 2018, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -22,7 +22,9 @@ * image information on demand. * * @since 3.104 + * @deprecated Instead use {@link ImagePathProvider} */ +@Deprecated(since = "2025-06") public interface ImageFileNameProvider { /** diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageLoader.java index 9973b8f06e3..6fd30a48fdc 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageLoader.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageLoader.java @@ -15,6 +15,8 @@ package org.eclipse.swt.graphics; import java.io.*; +import java.nio.file.*; +import java.nio.file.Path; import java.util.*; import org.eclipse.swt.*; @@ -133,13 +135,13 @@ void reset() { } /** - * Loads an array of ImageData objects from the + * Loads an array of {@code ImageData} objects from the * specified input stream. Throws an error if either an error * occurs while loading the images, or if the images are not * of a supported type. Returns the loaded image data array. * * @param stream the input stream to load the images from - * @return an array of ImageData objects loaded from the specified input stream + * @return an array of {@code ImageData} objects loaded from the specified input stream * * @exception IllegalArgumentException
      *
    • ERROR_NULL_ARGUMENT - if the stream is null
    • @@ -169,13 +171,13 @@ static boolean canLoadAtZoom(InputStream stream, int fileZoom, int targetZoom) { } /** - * Loads an array of ImageData objects from the + * Loads an array of {@code ImageData} objects from the * file with the specified name. Throws an error if either * an error occurs while loading the images, or if the images are * not of a supported type. Returns the loaded image data array. * * @param filename the name of the file to load the images from - * @return an array of ImageData objects loaded from the specified file + * @return an array of {@code ImageData} objects loaded from the specified file * * @exception IllegalArgumentException
        *
      • ERROR_NULL_ARGUMENT - if the file name is null
      • @@ -185,15 +187,41 @@ static boolean canLoadAtZoom(InputStream stream, int fileZoom, int targetZoom) { *
      • ERROR_INVALID_IMAGE - if the image file contains invalid data
      • *
      • ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format
      • *
      + * @deprecated Instead use {@link #load(Path)} */ +@Deprecated(since = "2025-06") public ImageData[] load(String filename) { - load(filename, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM); + if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + return load(Path.of(filename)); +} + +/** + * Loads an array of {@code ImageData} objects from the + * file with the specified name. Throws an error if either + * an error occurs while loading the images, or if the images are + * not of a supported type. Returns the loaded image data array. + * + * @param file the name of the file to load the images from + * @return an array of {@code ImageData} objects loaded from the specified file + * + * @exception IllegalArgumentException
        + *
      • ERROR_NULL_ARGUMENT - if the file name is null
      • + *
      + * @exception SWTException
        + *
      • ERROR_IO - if an IO error occurs while reading the file
      • + *
      • ERROR_INVALID_IMAGE - if the image file contains invalid data
      • + *
      • ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format
      • + *
      + * @since 3.130 + */ +public ImageData[] load(Path file) { + load(file, FileFormat.DEFAULT_ZOOM, FileFormat.DEFAULT_ZOOM); return data; } -List> load(String filename, int fileZoom, int targetZoom) { - if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - try (InputStream stream = new FileInputStream(filename)) { +List> load(Path file, int fileZoom, int targetZoom) { + if (file == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + try (InputStream stream = Files.newInputStream(file)) { return load(stream, fileZoom, targetZoom); } catch (IOException e) { SWT.error(SWT.ERROR_IO, e); @@ -201,9 +229,9 @@ List> load(String filename, int fileZoom, int targetZoo return null; } -static boolean canLoadAtZoom(String filename, int fileZoom, int targetZoom) { - if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - try (InputStream stream = new FileInputStream(filename)) { +static boolean canLoadAtZoom(Path file, int fileZoom, int targetZoom) { + if (file == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + try (InputStream stream = Files.newInputStream(file)) { return canLoadAtZoom(stream, fileZoom, targetZoom); } catch (IOException e) { SWT.error(SWT.ERROR_IO, e); @@ -279,10 +307,50 @@ public void save(OutputStream stream, int format) { *
    • ERROR_INVALID_IMAGE - if the image data contains invalid data
    • *
    • ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format
    • *
    + * @deprecated Instead use {@link #save(Path, int)} */ +@Deprecated(since = "2025-06") public void save(String filename, int format) { if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - try (OutputStream stream = new FileOutputStream(filename)) { + save(Path.of(filename), format); +} + +/** + * Saves the image data in this ImageLoader to a file with the specified name. + * The format parameter can have one of the following values: + *
    + *
    {@link SWT#IMAGE_BMP}
    + *
    Windows BMP file format, no compression
    + *
    {@link SWT#IMAGE_BMP_RLE}
    + *
    Windows BMP file format, RLE compression if appropriate
    + *
    {@link SWT#IMAGE_GIF}
    + *
    GIF file format
    + *
    {@link SWT#IMAGE_ICO}
    + *
    Windows ICO file format
    + *
    {@link SWT#IMAGE_JPEG}
    + *
    JPEG file format
    + *
    {@link SWT#IMAGE_PNG}
    + *
    PNG file format
    + *
    {@link SWT#IMAGE_TIFF}
    + *
    TIFF file format
    + *
    + * + * @param file the name of the file to write the images to + * @param format the format to write the images in + * + * @exception IllegalArgumentException
      + *
    • ERROR_NULL_ARGUMENT - if the file name is null
    • + *
    + * @exception SWTException
      + *
    • ERROR_IO - if an IO error occurs while writing to the file
    • + *
    • ERROR_INVALID_IMAGE - if the image data contains invalid data
    • + *
    • ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format
    • + *
    + * @since 3.130 + */ +public void save(Path file, int format) { + if (file == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + try (OutputStream stream = Files.newOutputStream(file)) { save(stream, format); } catch (IOException e) { SWT.error(SWT.ERROR_IO, e); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImagePathProvider.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImagePathProvider.java new file mode 100644 index 00000000000..ed9a8e8fc5c --- /dev/null +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImagePathProvider.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2018, 2025 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + * Hannes Wellmann - introduce ImageFileProvider as replacement for ImageFileNameProvider + *******************************************************************************/ +package org.eclipse.swt.graphics; + +import java.nio.file.Path; + +/** + * Interface to provide a callback mechanism to get information about images + * when the application is moved from a low DPI monitor to a high DPI monitor. + * This provides API which will be called by SWT during the image rendering. + * + * This interface needs to be implemented by client code to provide the image + * information on demand. + * + * @since 3.130 + */ +@FunctionalInterface +public interface ImagePathProvider { + + /** + * Returns the image filePath for the given zoom level. + *

    + * If no image is available for a particular zoom level, this method should + * return null. For zoom == 100, returning + * null is not allowed, and SWT will throw an exception. + * + * @param zoom The zoom level in % of the standard resolution (which is 1 + * physical monitor pixel == 1 SWT logical point). Typically 100, + * 150, or 200. + * @return the image filePath, or null if zoom != 100 + * and no image is available for the given zoom level. + * @since 3.129 + */ + Path getImagePath(int zoom); + +} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java index a15f4683ae3..5f94efa4480 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2022 IBM Corporation and others. + * Copyright (c) 2022, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -15,6 +15,7 @@ *******************************************************************************/ package org.eclipse.swt.internal; +import java.nio.file.Path; import java.util.*; import java.util.function.*; @@ -567,16 +568,16 @@ public static ElementAtZoom validateAndGetImageDataAtZoom(ImageDataPr * the 100% image is returned as a fallback. If provider or fallback image is * not available, an error is thrown. */ -public static ElementAtZoom validateAndGetImagePathAtZoom(ImageFileNameProvider provider, int zoom) { +public static ElementAtZoom validateAndGetImagePathAtZoom(ImagePathProvider provider, int zoom) { if (provider == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } - ElementAtZoom imagePathAtZoom = getElementAtZoom(z -> provider.getImagePath(z), zoom); - if (imagePathAtZoom == null) { + ElementAtZoom imageAtZoom = getElementAtZoom(z -> provider.getImagePath(z), zoom); + if (imageAtZoom == null) { SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, - ": ImageFileNameProvider [" + provider + "] returns null filename at 100% zoom."); + ": ImageFileNameProvider [" + provider + "] returns null file at 100% zoom."); } - return imagePathAtZoom; + return imageAtZoom; } private static ElementAtZoom getElementAtZoom(Function elementForZoomProvider, int zoom) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java index 74158802a5e..5da399f3df8 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java @@ -17,6 +17,7 @@ import static org.eclipse.swt.internal.image.ImageColorTransformer.DEFAULT_DISABLED_IMAGE_TRANSFORMER; import java.io.*; +import java.nio.file.Path; import java.util.*; import org.eclipse.swt.*; @@ -146,9 +147,9 @@ public final class Image extends Resource implements Drawable { static final int DEFAULT_SCANLINE_PAD = 4; /** - * ImageFileNameProvider to provide file names at various Zoom levels + * ImageFileProvider to provide files at various Zoom levels */ - private ImageFileNameProvider imageFileNameProvider; + private ImagePathProvider imageFileProvider; /** * ImageDataProvider to provide ImageData at various Zoom levels @@ -271,7 +272,7 @@ public Image(Device device, Image srcImage, int flag) { device = this.device; this.type = srcImage.type; this.imageDataProvider = srcImage.imageDataProvider; - this.imageFileNameProvider = srcImage.imageFileNameProvider; + this.imageFileProvider = srcImage.imageFileProvider; this.imageGcDrawer = srcImage.imageGcDrawer; this.styleFlag = srcImage.styleFlag | flag; this.currentDeviceZoom = srcImage.currentDeviceZoom; @@ -515,18 +516,11 @@ public Image(Device device, ImageData source, ImageData mask) { *

    *
      *     static Image loadImage (Display display, Class clazz, String string) {
    - *          InputStream stream = clazz.getResourceAsStream (string);
    - *          if (stream == null) return null;
    - *          Image image = null;
    - *          try {
    - *               image = new Image (display, stream);
    - *          } catch (SWTException ex) {
    - *          } finally {
    - *               try {
    - *                    stream.close ();
    - *               } catch (IOException ex) {}
    - *          }
    - *          return image;
    + *         try (InputStream stream = clazz.getResourceAsStream(string)){
    + *             if (stream == null) return null;
    +*              return new Image (display, stream);
    + *         } catch (SWTException | IOException ex) {
    + *         }
      *     }
      * 
    *

    @@ -555,7 +549,7 @@ public Image(Device device, ImageData source, ImageData mask) { public Image(Device device, InputStream stream) { super(device); currentDeviceZoom = DPIUtil.getDeviceZoom(); - ElementAtZoom image = ImageDataLoader.load(stream, FileFormat.DEFAULT_ZOOM, currentDeviceZoom); + ElementAtZoom image = ImageData.load(stream, FileFormat.DEFAULT_ZOOM, currentDeviceZoom); ImageData data = DPIUtil.scaleImageData(device, image, currentDeviceZoom); init(data); init(); @@ -597,7 +591,7 @@ public Image(Device device, String filename) { super(device); if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); currentDeviceZoom = DPIUtil.getDeviceZoom(); - ElementAtZoom image = ImageDataLoader.load(filename, FileFormat.DEFAULT_ZOOM, currentDeviceZoom); + ElementAtZoom image = ImageData.load(Path.of(filename), FileFormat.DEFAULT_ZOOM, currentDeviceZoom); ImageData data = DPIUtil.scaleImageData(device, image, currentDeviceZoom); init(data); init(); @@ -631,10 +625,45 @@ public Image(Device device, String filename) { *

  • ERROR_NO_HANDLES if a handle could not be obtained for image creation
  • * * @since 3.104 + * @deprecated Instead use {@link #Image(Device, ImagePathProvider)} */ +@Deprecated(since = "2025-06") public Image(Device device, ImageFileNameProvider imageFileNameProvider) { + this(device, ImageData.asImagePathProvider(imageFileNameProvider)); +} + +/** + * Constructs an instance of this class by loading its representation + * from the file retrieved from the {@link ImagePathProvider}. Throws an + * error if an error occurs while loading the image, or if the result + * is an image of an unsupported type. + *

    + * This constructor is provided for convenience for loading image as + * per DPI level. + * + * @param device the device on which to create the image + * @param imageFileProvider the {@link ImagePathProvider} object that is + * to be used to get the file + * + * @exception IllegalArgumentException

      + *
    • ERROR_NULL_ARGUMENT - if device is null and there is no current device
    • + *
    • ERROR_NULL_ARGUMENT - if the ImageFileNameProvider is null
    • + *
    • ERROR_INVALID_ARGUMENT - if the fileName provided by ImageFileNameProvider is null at 100% zoom
    • + *
    + * @exception SWTException
      + *
    • ERROR_IO - if an IO error occurs while reading from the file
    • + *
    • ERROR_INVALID_IMAGE - if the image file contains invalid data
    • + *
    • ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth
    • + *
    • ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format
    • + *
    + * @exception SWTError
      + *
    • ERROR_NO_HANDLES if a handle could not be obtained for image creation
    • + *
    + * @since 3.130 + */ +public Image(Device device, ImagePathProvider imageFileProvider) { super(device); - this.imageFileNameProvider = imageFileNameProvider; + this.imageFileProvider = imageFileProvider; currentDeviceZoom = DPIUtil.getDeviceZoom(); initFromFileNameProvider(currentDeviceZoom); init (); @@ -693,7 +722,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) { *
  • ERROR_NULL_ARGUMENT - if device is null and there is no current device
  • *
  • ERROR_NULL_ARGUMENT - if the ImageGcDrawer is null
  • * - * @since 3.129 + * @since 3.130 */ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) { super(device); @@ -730,7 +759,7 @@ public boolean internal_gtk_refreshImageForZoom() { boolean refreshImageForZoom () { boolean refreshed = false; int deviceZoom = DPIUtil.getDeviceZoom(); - if (imageFileNameProvider != null) { + if (imageFileProvider != null) { int deviceZoomLevel = deviceZoom; if (deviceZoomLevel != currentDeviceZoom) { /* Release current native resources */ @@ -765,9 +794,9 @@ boolean refreshImageForZoom () { return refreshed; } -void initNative(String filename) { +void initNative(Path file) { try { - byte[] fileNameBuffer = Converter.javaStringToCString(filename); + byte[] fileNameBuffer = Converter.javaStringToCString(file.toString()); long pixbuf = GDK.gdk_pixbuf_new_from_file(fileNameBuffer, null); if (pixbuf != 0) { try { @@ -780,12 +809,12 @@ void initNative(String filename) { } private void initFromFileNameProvider(int zoom) { - ElementAtZoom fileForZoom = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom); + ElementAtZoom fileForZoom = DPIUtil.validateAndGetImagePathAtZoom(imageFileProvider, zoom); if (fileForZoom.zoom() == zoom) { initNative(fileForZoom.element()); } if (this.surface == 0) { - ElementAtZoom imageDataAtZoom = ImageDataLoader.load(fileForZoom.element(), fileForZoom.zoom(), zoom); + ElementAtZoom imageDataAtZoom = ImageData.load(fileForZoom.element(), fileForZoom.zoom(), zoom); ImageData imageData = imageDataAtZoom.element(); if (imageDataAtZoom.zoom() != zoom) { imageData = DPIUtil.scaleImageData(device, imageDataAtZoom, zoom); @@ -949,8 +978,8 @@ public boolean equals (Object object) { if (device != image.device || transparentPixel != image.transparentPixel) return false; if (imageDataProvider != null && image.imageDataProvider != null) { return (styleFlag == image.styleFlag) && imageDataProvider.equals (image.imageDataProvider); - } else if (imageFileNameProvider != null && image.imageFileNameProvider != null) { - return (styleFlag == image.styleFlag) && imageFileNameProvider.equals (image.imageFileNameProvider); + } else if (imageFileProvider != null && image.imageFileProvider != null) { + return (styleFlag == image.styleFlag) && imageFileProvider.equals(image.imageFileProvider); } else if (imageGcDrawer != null && image.imageGcDrawer != null) { return styleFlag == image.styleFlag && imageGcDrawer.equals(image.imageGcDrawer) && width == image.width && height == image.height; @@ -1157,9 +1186,9 @@ public ImageData getImageData (int zoom) { } else if (imageDataProvider != null) { ElementAtZoom data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, zoom); return DPIUtil.scaleImageData (device, data.element(), zoom, data.zoom()); - } else if (imageFileNameProvider != null) { - ElementAtZoom fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom); - return DPIUtil.scaleImageData (device, new ImageData (fileName.element()), zoom, fileName.zoom()); + } else if (imageFileProvider != null) { + ElementAtZoom file = DPIUtil.validateAndGetImagePathAtZoom(imageFileProvider, zoom); + return DPIUtil.scaleImageData(device, ImageData.load(file.element()), zoom, file.zoom()); } else if (imageGcDrawer != null) { return drawWithImageGcDrawer(width, height, zoom); } else { @@ -1252,8 +1281,8 @@ public static Image gtk_new_from_pixbuf(Device device, int type, long pixbuf) { public int hashCode () { if (imageDataProvider != null) { return imageDataProvider.hashCode(); - } else if (imageFileNameProvider != null) { - return imageFileNameProvider.hashCode(); + } else if (imageFileProvider != null) { + return imageFileProvider.hashCode(); } else if (imageGcDrawer != null) { return Objects.hash(imageGcDrawer, width, height); } else { @@ -1563,8 +1592,8 @@ public void setBackground(Color color) { public String toString () { if (isDisposed()) return "Image {*DISPOSED*}"; - if (imageFileNameProvider != null) { - return "Image {" + imageFileNameProvider.getImagePath(100) + "}"; + if (imageFileProvider != null) { + return "Image {" + imageFileProvider.getImagePath(100) + "}"; } return "Image {" + surface + "}"; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java index 272cedb319f..93a94c7c478 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java @@ -17,6 +17,7 @@ import static org.eclipse.swt.internal.image.ImageColorTransformer.DEFAULT_DISABLED_IMAGE_TRANSFORMER; import java.io.*; +import java.nio.file.Path; import java.util.*; import java.util.Map.*; import java.util.function.*; @@ -444,18 +445,11 @@ public Image(Device device, ImageData source, ImageData mask) { *

    *
      *     static Image loadImage (Display display, Class clazz, String string) {
    - *          InputStream stream = clazz.getResourceAsStream (string);
    - *          if (stream == null) return null;
    - *          Image image = null;
    - *          try {
    - *               image = new Image (display, stream);
    - *          } catch (SWTException ex) {
    - *          } finally {
    - *               try {
    - *                    stream.close ();
    - *               } catch (IOException ex) {}
    - *          }
    - *          return image;
    + *         try (InputStream stream = clazz.getResourceAsStream(string)){
    + *             if (stream == null) return null;
    +*              return new Image (display, stream);
    + *         } catch (SWTException | IOException ex) {
    + *         }
      *     }
      * 
    *

    @@ -524,11 +518,8 @@ public Image (Device device, InputStream stream) { public Image (Device device, String filename) { super(device); if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - this.imageProvider = new ImageFileNameProviderWrapper(zoom -> { - if (zoom == 100) { - return filename; - } - return null; + this.imageProvider = new ImagePathProviderWrapper(zoom -> { + return zoom == 100 ? Path.of(filename) : null; }); init(); this.device.registerResourceWithZoomSupport(this); @@ -562,13 +553,48 @@ public Image (Device device, String filename) { *

  • ERROR_NO_HANDLES if a handle could not be obtained for image creation
  • * * @since 3.104 + * @deprecated Instead use {@link #Image(Device, ImagePathProvider)} */ +@Deprecated(since = "2025-06") public Image(Device device, ImageFileNameProvider imageFileNameProvider) { + this(device, ImageData.asImagePathProvider(imageFileNameProvider)); +} + +/** + * Constructs an instance of this class by loading its representation + * from the file retrieved from the {@link ImagePathProvider}. Throws an + * error if an error occurs while loading the image, or if the result + * is an image of an unsupported type. + *

    + * This constructor is provided for convenience for loading image as + * per DPI level. + * + * @param device the device on which to create the image + * @param imageFileProvider the {@link ImagePathProvider} object that is + * to be used to get the file + * + * @exception IllegalArgumentException

      + *
    • ERROR_NULL_ARGUMENT - if device is null and there is no current device
    • + *
    • ERROR_NULL_ARGUMENT - if the ImageFileNameProvider is null
    • + *
    • ERROR_INVALID_ARGUMENT - if the fileName provided by ImageFileNameProvider is null at 100% zoom
    • + *
    + * @exception SWTException
      + *
    • ERROR_IO - if an IO error occurs while reading from the file
    • + *
    • ERROR_INVALID_IMAGE - if the image file contains invalid data
    • + *
    • ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth
    • + *
    • ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format
    • + *
    + * @exception SWTError
      + *
    • ERROR_NO_HANDLES if a handle could not be obtained for image creation
    • + *
    + * @since 3.130 + */ +public Image(Device device, ImagePathProvider imageFileProvider) { super(device); - this.imageProvider = new ImageFileNameProviderWrapper(imageFileNameProvider); - if (imageFileNameProvider.getImagePath(100) == null) { + this.imageProvider = new ImagePathProviderWrapper(imageFileProvider); + if (imageFileProvider.getImagePath(100) == null) { SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, - ": ImageFileNameProvider [" + imageFileNameProvider + "] returns null fileName at 100% zoom."); + ": ImageFileNameProvider [" + imageFileProvider + "] returns null fileName at 100% zoom."); } init(); this.device.registerResourceWithZoomSupport(this); @@ -2053,7 +2079,7 @@ private ImageDataLoaderStreamProviderWrapper(byte[] inputStreamData) { @Override protected ElementAtZoom loadImageData(int zoom) { - return ImageDataLoader.load(new ByteArrayInputStream(inputStreamData), FileFormat.DEFAULT_ZOOM, zoom); + return ImageData.load(new ByteArrayInputStream(inputStreamData), FileFormat.DEFAULT_ZOOM, zoom); } @Override @@ -2228,9 +2254,9 @@ protected Rectangle getBounds(int zoom) { } } -private class ImageFileNameProviderWrapper extends BaseImageProviderWrapper { - ImageFileNameProviderWrapper(ImageFileNameProvider provider) { - super(provider, ImageFileNameProvider.class); +private class ImagePathProviderWrapper extends BaseImageProviderWrapper { + ImagePathProviderWrapper(ImagePathProvider provider) { + super(provider, ImagePathProvider.class); // Checks for the contract of the passed provider require // checking for valid image data creation newImageData(DPIUtil.getDeviceZoom()); @@ -2238,11 +2264,11 @@ private class ImageFileNameProviderWrapper extends BaseImageProviderWrapper loadImageData(int zoom) { - ElementAtZoom fileForZoom = DPIUtil.validateAndGetImagePathAtZoom(provider, zoom); + ElementAtZoom fileForZoom = DPIUtil.validateAndGetImagePathAtZoom(provider, zoom); // Load at appropriate zoom via loader - if (fileForZoom.zoom() != zoom && ImageDataLoader.canLoadAtZoom(fileForZoom.element(), fileForZoom.zoom(), zoom)) { - ElementAtZoom imageDataAtZoom = ImageDataLoader.load(fileForZoom.element(), fileForZoom.zoom(), zoom); + if (fileForZoom.zoom() != zoom && ImageLoader.canLoadAtZoom(fileForZoom.element(), fileForZoom.zoom(), zoom)) { + ElementAtZoom imageDataAtZoom = ImageData.load(fileForZoom.element(), fileForZoom.zoom(), zoom); return new ElementAtZoom<>(imageDataAtZoom.element(), zoom); } @@ -2255,7 +2281,7 @@ protected ElementAtZoom loadImageData(int zoom) { } ElementAtZoom imageDataAtZoom; if (nativeInitializedImage == null) { - imageDataAtZoom = ImageDataLoader.load(fileForZoom.element(), fileForZoom.zoom(), zoom); + imageDataAtZoom = ImageData.load(fileForZoom.element(), fileForZoom.zoom(), zoom); } else { imageDataAtZoom = new ElementAtZoom<>(nativeInitializedImage.getImageData(), fileForZoom.zoom()); nativeInitializedImage.destroy(); @@ -2269,42 +2295,44 @@ public int hashCode() { } @Override - ImageFileNameProviderWrapper createCopy(Image image) { - return image.new ImageFileNameProviderWrapper(provider); + ImagePathProviderWrapper createCopy(Image image) { + return image.new ImagePathProviderWrapper(provider); } - ImageHandle initNative(String filename, int zoom) { + ImageHandle initNative(Path file, int zoom) { ImageHandle imageMetadata = null; long handle = 0; int width = -1; int height = -1; device.checkGDIP(); boolean gdip = true; + String filenameLowerCase = file.getFileName().toString().toLowerCase(); /* * Bug in GDI+. For some reason, Bitmap.LockBits() segment faults * when loading GIF files in 64-bit Windows. The fix is to not use * GDI+ image loading in this case. */ - if (gdip && C.PTR_SIZEOF == 8 && filename.toLowerCase().endsWith(".gif")) gdip = false; + if (gdip && C.PTR_SIZEOF == 8 && filenameLowerCase.endsWith(".gif")) gdip = false; /* * Bug in GDI+. Bitmap.LockBits() fails to load GIF files in * Windows 7 when the image has a position offset in the first frame. * The fix is to not use GDI+ image loading in this case. */ - if (filename.toLowerCase().endsWith(".gif")) gdip = false; + if (filenameLowerCase.endsWith(".gif")) gdip = false; if(!gdip) return null; - int length = filename.length(); - char[] chars = new char[length+1]; - filename.getChars(0, length, chars, 0); - long bitmap = Gdip.Bitmap_new(chars, false); + String filePath = file.toString(); + int length = filePath.length(); + char[] nullTerminatedChars = new char[length + 1]; + filePath.getChars(0, length, nullTerminatedChars, 0); + long bitmap = Gdip.Bitmap_new(nullTerminatedChars, false); if (bitmap == 0) return null; int error = SWT.ERROR_NO_HANDLES; int status = Gdip.Image_GetLastStatus(bitmap); if (status == 0) { - if (filename.toLowerCase().endsWith(".ico")) { + if (filenameLowerCase.endsWith(".ico")) { type = SWT.ICON; long[] hicon = new long[1]; status = Gdip.Bitmap_GetHICON(bitmap, hicon); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TaskBar.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TaskBar.java index 95908c488f9..89032db1ba7 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TaskBar.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TaskBar.java @@ -16,6 +16,7 @@ import java.io.*; +import java.nio.file.Path; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; @@ -175,7 +176,7 @@ IShellLink createShellLink (MenuItem item) { } ImageLoader loader = new ImageLoader (); loader.data = new ImageData [] {data}; - loader.save (icon, SWT.IMAGE_ICO); + loader.save(Path.of(icon), SWT.IMAGE_ICO); } } if (icon != null) { diff --git a/examples/org.eclipse.swt.examples.launcher/src/org/eclipse/swt/examples/launcher/LauncherPlugin.java b/examples/org.eclipse.swt.examples.launcher/src/org/eclipse/swt/examples/launcher/LauncherPlugin.java index 8d98dce3474..059f89cfe5b 100644 --- a/examples/org.eclipse.swt.examples.launcher/src/org/eclipse/swt/examples/launcher/LauncherPlugin.java +++ b/examples/org.eclipse.swt.examples.launcher/src/org/eclipse/swt/examples/launcher/LauncherPlugin.java @@ -59,7 +59,7 @@ public class LauncherPlugin extends AbstractUIPlugin { LAUNCH_ITEMS_XML_ATTRIB_ENABLED = "enabled", LAUNCH_ITEMS_XML_ATTRIB_CATEGORY = "category", LAUNCH_ITEMS_XML_VALUE_TRUE = "true", - LAUNCH_ITEMS_XML_VALUE_FALSE = "false"; + LAUNCH_ITEMS_XML_VALUE_FALSE = "false"; static final int liClosedFolder = 0, @@ -84,7 +84,7 @@ public void start(BundleContext context) throws Exception { super.start(context); resourceBundle = Platform.getResourceBundle(getBundle()); } - + /** * Clean up */ @@ -107,7 +107,7 @@ public static LauncherPlugin getDefault() { public static void initResources() { if (images == null) { images = new Image[imageLocations.length]; - + for (int i = 0; i < imageLocations.length; ++i) { images[i] = getImageFromPlugin(plugin.getBundle(), imageLocations[i]); if (images[i] == null) { @@ -116,7 +116,7 @@ public static void initResources() { throw new IllegalStateException(); } } - } + } } /** @@ -133,7 +133,7 @@ public static void freeResources() { /** * Log an error to the ILog for this plugin - * + * * @param message the localized error message text * @param exception the associated exception, or null */ @@ -154,7 +154,7 @@ public static String getResourceString(String key) { return key; } catch (NullPointerException e) { return "!" + key + "!"; - } + } } /** @@ -174,7 +174,7 @@ public static String getResourceString(String key, Object[] args) { /** * Constructs a list of available programs from registered extensions. - * + * * @return an ItemTreeNode representing the root of a tree of items (the root is not to be displayed) */ public static ItemTreeNode getLaunchItemTree() { @@ -186,33 +186,33 @@ public static ItemTreeNode getLaunchItemTree() { // retrieve all configuration elements registered at our launchItems extension-point IConfigurationElement[] configurationElements = extensionRegistry.getConfigurationElementsFor(LAUNCH_ITEMS_POINT_ID); - + if (configurationElements == null || configurationElements.length == 0) { logError(getResourceString("error.CouldNotFindRegisteredExtensions"), null); return categoryTree; } - + /* Collect all launch categories -- coalesce those with same ID */ HashMap idMap = new HashMap<>(); for (IConfigurationElement ce: configurationElements) { final String ceName = ce.getName(); final String attribId = getItemAttribute(ce, LAUNCH_ITEMS_XML_ATTRIB_ID, null); - + if (idMap.containsKey(attribId)) continue; if (ceName.equalsIgnoreCase(LAUNCH_ITEMS_XML_CATEGORY)) { - final String attribName = getItemName(ce); + final String attribName = getItemName(ce); ItemDescriptor theDescriptor = new ItemDescriptor(attribId, attribName, getItemDescription(ce), null, null, null, null, ce); idMap.put(attribId, new ItemTreeNode(theDescriptor)); } } - + /* Generate launch category hierarchy */ Set tempIdSet = new HashSet<>(); // used to prevent duplicates from being entered into the tree for (IConfigurationElement ce : configurationElements) { final String ceName = ce.getName(); final String attribId = getItemAttribute(ce, LAUNCH_ITEMS_XML_ATTRIB_ID, null); - + if (tempIdSet.contains(attribId)) continue; if (ceName.equalsIgnoreCase(LAUNCH_ITEMS_XML_CATEGORY)) { final ItemTreeNode theNode = idMap.get(attribId); @@ -220,7 +220,7 @@ public static ItemTreeNode getLaunchItemTree() { tempIdSet.add(attribId); } } - + /* Generate program tree */ for (IConfigurationElement ce : configurationElements) { final String ceName = ce.getName(); @@ -230,11 +230,11 @@ public static ItemTreeNode getLaunchItemTree() { if (ceName.equalsIgnoreCase(LAUNCH_ITEMS_XML_CATEGORY)) { // ignore } else if (ceName.equalsIgnoreCase(LAUNCH_ITEMS_XML_ITEM)) { - final String enabled = getItemAttribute(ce, LAUNCH_ITEMS_XML_ATTRIB_ENABLED, + final String enabled = getItemAttribute(ce, LAUNCH_ITEMS_XML_ATTRIB_ENABLED, LAUNCH_ITEMS_XML_VALUE_TRUE); if (enabled.equalsIgnoreCase(LAUNCH_ITEMS_XML_VALUE_FALSE)) continue; - ItemDescriptor theDescriptor = createItemDescriptor(ce, attribId); - + ItemDescriptor theDescriptor = createItemDescriptor(ce, attribId); + if (theDescriptor != null) { final ItemTreeNode theNode = new ItemTreeNode(theDescriptor); addItemByCategory(ce, categoryTree, theNode, idMap); @@ -245,28 +245,28 @@ public static ItemTreeNode getLaunchItemTree() { return categoryTree; } - + /** * Adds an item to the category tree. */ private static void addItemByCategory(IConfigurationElement ce, ItemTreeNode root, ItemTreeNode theNode, HashMap idMap) { final String attribCategory = getItemAttribute(ce, LAUNCH_ITEMS_XML_ATTRIB_CATEGORY, null); - + // locate the parent node ItemTreeNode parentNode = null; if (attribCategory != null) { parentNode = idMap.get(attribCategory); } if (parentNode == null) parentNode = root; - + // add the item parentNode.addSortedNode(theNode); } /** * Creates an ItemDescriptor from an XML definition. - * + * * @param ce the IConfigurationElement describing the item * @param attribId the attribute id * @return a new ItemDescriptor, or null if an error occurs @@ -279,7 +279,7 @@ private static ItemDescriptor createItemDescriptor(IConfigurationElement ce, Str IConfigurationElement viewCE = getItemElement(ce, LAUNCH_ITEMS_XML_VIEW); if (viewCE != null) { //Item is a view - final String attribView = getItemAttribute(viewCE, LAUNCH_ITEMS_XML_VIEW_VIEWID, null); + final String attribView = getItemAttribute(viewCE, LAUNCH_ITEMS_XML_VIEW_VIEWID, null); if (attribView == null) { logError(getResourceString("error.IncompleteViewLaunchItem", new Object[] { attribId } ), null); @@ -310,7 +310,7 @@ private static ItemDescriptor createItemDescriptor(IConfigurationElement ce, Str /** * Returns the first instance of a particular child XML element. - * + * * @param ce the IConfigurationElement parent * @param element the name of the element to fetch * @return the element's IConfigurationElement, or null if not found @@ -322,7 +322,7 @@ private static IConfigurationElement getItemElement(IConfigurationElement ce, St /** * Returns the value of an XML attribute for an item. - * + * * @param ce the IConfigurationElement describing the item * @param attribute the attribute to fetch * @param defaultValue the value to return if the attribute is not found @@ -335,7 +335,7 @@ private static String getItemAttribute(IConfigurationElement ce, String attribut /** * Returns the description string given the IConfigurationElement for an item. - * + * * @param ce the IConfigurationElement describing the item * @return a newline-delimited string that describes this item, or null if none */ @@ -346,7 +346,7 @@ private static String getItemDescription(IConfigurationElement ce) { /** * Returns the name of an item. - * + * * @param ce the IConfigurationElement describing the item * @return the attribute value */ @@ -358,7 +358,7 @@ private static String getItemName(IConfigurationElement ce) { /** * Returns the icon for an item. - * + * * @param ce the IConfigurationElement describing the item * @return an icon */ @@ -392,7 +392,7 @@ private static Image getImageFromPlugin(Bundle bundle, String iconPath) { URL installUrl = bundle.getEntry("/"); URL url = new URL(installUrl, iconPath); is = url.openConnection().getInputStream(); - ImageData source = new ImageData(is); + ImageData source = ImageData.load(is); ImageData mask = source.getTransparencyMask(); Image image = new Image(null, source, mask); return image; diff --git a/examples/org.eclipse.swt.examples.ole.win32/src/org/eclipse/swt/examples/ole/win32/OlePlugin.java b/examples/org.eclipse.swt.examples.ole.win32/src/org/eclipse/swt/examples/ole/win32/OlePlugin.java index 43051d05c3d..884438f81cc 100644 --- a/examples/org.eclipse.swt.examples.ole.win32/src/org/eclipse/swt/examples/ole/win32/OlePlugin.java +++ b/examples/org.eclipse.swt.examples.ole.win32/src/org/eclipse/swt/examples/ole/win32/OlePlugin.java @@ -14,16 +14,22 @@ package org.eclipse.swt.examples.ole.win32; -import java.io.*; -import java.net.*; -import java.text.*; -import java.util.*; +import java.io.InputStream; +import java.net.URL; +import java.text.MessageFormat; +import java.util.MissingResourceException; +import java.util.ResourceBundle; -import org.eclipse.core.runtime.*; -import org.eclipse.swt.*; -import org.eclipse.swt.graphics.*; -import org.eclipse.ui.plugin.*; -import org.osgi.framework.*; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; /** * The main plugin class to be used in the desktop. @@ -174,7 +180,7 @@ public static void freeResources() { private static Image getImageFromPlugin(Bundle bundle, String iconPath) { URL installUrl = bundle.getEntry("/"); try (InputStream is = new URL(installUrl, iconPath).openConnection().getInputStream()) { - ImageData source = new ImageData(is); + ImageData source = ImageData.load(is); ImageData mask = source.getTransparencyMask(); Image image = new Image(null, source, mask); return image; diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/browserexample/BrowserExample.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/browserexample/BrowserExample.java index f9d4abde220..b455f1a8bd6 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/browserexample/BrowserExample.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/browserexample/BrowserExample.java @@ -324,7 +324,7 @@ void initResources() { images = new Image[imageLocations.length]; for (int i = 0; i < imageLocations.length; ++i) { try (InputStream sourceStream = clazz.getResourceAsStream(imageLocations[i])) { - ImageData source = new ImageData(sourceStream); + ImageData source = ImageData.load(sourceStream); ImageData mask = source.getTransparencyMask(); images[i] = new Image(null, source, mask); } diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/ControlExample.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/ControlExample.java index 639ba59eae7..bfe28dbbb30 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/ControlExample.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/ControlExample.java @@ -204,7 +204,7 @@ void initResources() { for (int i = 0; i < imageLocations.length; ++i) { try (InputStream sourceStream = clazz.getResourceAsStream(imageLocations[i])) { - ImageData source = new ImageData(sourceStream); + ImageData source = ImageData.load(sourceStream); if (imageTypes[i] == SWT.ICON) { ImageData mask = source.getTransparencyMask(); images[i] = new Image(null, source, mask); diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/StyledTextTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/StyledTextTab.java index 44e1297b929..eb352422a3e 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/StyledTextTab.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/StyledTextTab.java @@ -75,8 +75,8 @@ class StyledTextTab extends ScrollableTab { Image createBitmapImage(Display display, String name) { try (InputStream sourceStream = ControlExample.class.getResourceAsStream(name + ".bmp"); InputStream maskStream = ControlExample.class.getResourceAsStream(name + "_mask.bmp")) { - ImageData source = new ImageData(sourceStream); - ImageData mask = new ImageData(maskStream); + ImageData source = ImageData.load(sourceStream); + ImageData mask = ImageData.load(maskStream); Image result = new Image(display, source, mask); return result; } catch (IOException e) { diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/fileviewer/IconCache.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/fileviewer/IconCache.java index 26cf621db99..38f1ae4ed3f 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/fileviewer/IconCache.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/fileviewer/IconCache.java @@ -130,7 +130,7 @@ public void freeResources() { */ private Image createStockImage(Display display, String path) { try (InputStream stream = IconCache.class.getResourceAsStream (path)) { - ImageData imageData = new ImageData (stream); + ImageData imageData = ImageData.load(stream); ImageData mask = imageData.getTransparencyMask (); Image result = new Image (display, imageData, mask); return result; diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/hoverhelp/HoverHelp.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/hoverhelp/HoverHelp.java index 9451b18d44a..e1ca7eefb1a 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/hoverhelp/HoverHelp.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/hoverhelp/HoverHelp.java @@ -88,7 +88,7 @@ public Shell open(Display display) { for (int i = 0; i < imageLocations.length; ++i) { try (InputStream stream = clazz.getResourceAsStream(imageLocations[i])) { - ImageData source = new ImageData(stream); + ImageData source = ImageData.load(stream); ImageData mask = source.getTransparencyMask(); images[i] = new Image(display, source, mask); } catch (IOException e) { diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java index 5daa74431ee..4c410ebb841 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java @@ -17,6 +17,8 @@ import java.io.InputStream; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; @@ -103,7 +105,7 @@ public class ImageAnalyzer { Thread incrementalThread; // draws incremental images String lastPath; // used to seed the file dialog String currentName; // the current image file or URL name - String fileName; // the current image file + Path fileName; // the current image file ImageLoader loader; // the loader for the current image file ImageData[] imageDataArray; // all image data read from the current file int imageDataIndex; // the index of the current image data @@ -793,7 +795,7 @@ void menuLoad() { // Cache the filename. currentName = filename; - fileName = filename; + fileName = Path.of(filename); // Fill in array and loader data. loader = new ImageLoader(); @@ -838,12 +840,12 @@ void menuOpenFile() { } // Read the new image(s) from the chosen file. long startTime = System.currentTimeMillis(); - imageDataArray = loader.load(filename); + imageDataArray = loader.load(Path.of(filename)); loadTime = System.currentTimeMillis() - startTime; if (imageDataArray.length > 0) { // Cache the filename. currentName = filename; - fileName = filename; + fileName = Path.of(filename); // If there are multiple images in the file (typically GIF) // then enable the Previous, Next and Animate buttons. @@ -1005,11 +1007,7 @@ void menuSaveAs() { FileDialog fileChooser = new FileDialog(shell, SWT.SAVE); fileChooser.setFilterPath(lastPath); if (fileName != null) { - String name = fileName; - int nameStart = name.lastIndexOf(java.io.File.separatorChar); - if (nameStart > -1) { - name = name.substring(nameStart + 1); - } + String name = fileName.getFileName().toString(); fileChooser.setFileName(name.substring(0, name.indexOf(".")) + "." + imageTypeCombo.getText().toLowerCase()); } fileChooser.setFilterExtensions(SAVE_FILTER_EXTENSIONS); @@ -1055,9 +1053,10 @@ void menuSaveAs() { } } - if (new java.io.File(filename).exists()) { + Path file = Path.of(filename); + if (Files.exists(file)) { MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL); - box.setMessage(createMsg(bundle.getString("Overwrite"), filename)); + box.setMessage(createMsg(bundle.getString("Overwrite"), file)); if (box.open() == SWT.CANCEL) return; } @@ -1084,7 +1083,7 @@ void menuSaveAs() { if (!multi) loader.data = new ImageData[] {imageData}; loader.compression = compressionCombo.indexOf(compressionCombo.getText()); - loader.save(filename, filetype); + loader.save(file, filetype); /* Restore the previous transparency setting. */ if (!multi && transparentPixel != -1 && !transparent) { @@ -1093,12 +1092,12 @@ void menuSaveAs() { // Update the shell title and file type label, // and use the new file. - fileName = filename; - shell.setText(createMsg(bundle.getString("Analyzer_on"), filename)); + fileName = file; + shell.setText(createMsg(bundle.getString("Analyzer_on"), file)); typeLabel.setText(createMsg(bundle.getString("Type_string"), fileTypeString(filetype))); } catch (SWTException | SWTError e) { - showErrorDialog(bundle.getString("Saving_lc"), filename, e); + showErrorDialog(bundle.getString("Saving_lc"), file, e); } finally { shell.setCursor(null); imageCanvas.setCursor(crossCursor); @@ -1113,7 +1112,7 @@ void menuSaveMaskAs() { // Get the user to choose a file name and type to save. FileDialog fileChooser = new FileDialog(shell, SWT.SAVE); fileChooser.setFilterPath(lastPath); - if (fileName != null) fileChooser.setFileName(fileName); + if (fileName != null) fileChooser.setFileName(fileName.toString()); fileChooser.setFilterExtensions(SAVE_FILTER_EXTENSIONS); fileChooser.setFilterNames(SAVE_FILTER_NAMES); String filename = fileChooser.open(); @@ -1137,9 +1136,10 @@ void menuSaveMaskAs() { } } - if (new java.io.File(filename).exists()) { + Path file = Path.of(filename); + if (Files.exists(file)) { MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL); - box.setMessage(createMsg(bundle.getString("Overwrite"), filename)); + box.setMessage(createMsg(bundle.getString("Overwrite"), file)); if (box.open() == SWT.CANCEL) return; } @@ -1151,10 +1151,10 @@ void menuSaveMaskAs() { // Save the mask of the current image to the specified file. ImageData maskImageData = imageData.getTransparencyMask(); loader.data = new ImageData[] {maskImageData}; - loader.save(filename, filetype); + loader.save(file, filetype); } catch (SWTException | SWTError e) { - showErrorDialog(bundle.getString("Saving_lc"), filename, e); + showErrorDialog(bundle.getString("Saving_lc"), file, e); } finally { shell.setCursor(null); imageCanvas.setCursor(crossCursor); @@ -2077,7 +2077,7 @@ String dataHexDump(String lineDelimiter) { /* * Open an error dialog displaying the specified information. */ - void showErrorDialog(String operation, String filename, Throwable e) { + void showErrorDialog(String operation, Object filename, Throwable e) { MessageBox box = new MessageBox(shell, SWT.ICON_ERROR); String message = createMsg(bundle.getString("Error"), operation, filename); String errorMessage = ""; diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/paint/PaintExample.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/paint/PaintExample.java index 625984985d8..208c14237ce 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/paint/PaintExample.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/paint/PaintExample.java @@ -474,7 +474,7 @@ public void initResources() { Tool tool = tool2; String id = tool.group + '.' + tool.name; try (InputStream sourceStream = clazz.getResourceAsStream(getResourceString(id + ".image"))) { - ImageData source = new ImageData(sourceStream); + ImageData source = ImageData.load(sourceStream); ImageData mask = source.getTransparencyMask(); tool.image = new Image(null, source, mask); } diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/texteditor/TextEditor.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/texteditor/TextEditor.java index c4d5d9ab137..30ec37d6adf 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/texteditor/TextEditor.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/texteditor/TextEditor.java @@ -1120,7 +1120,7 @@ void installListeners() { Image loadImage(Display display, String fileName) { Image image = null; try (InputStream sourceStream = getClass().getResourceAsStream(fileName + ".ico")) { //$NON-NLS-1$ - ImageData source = new ImageData(sourceStream); + ImageData source = ImageData.load(sourceStream); ImageData mask = source.getTransparencyMask(); image = new Image(display, source, mask); } catch (IOException e) { diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet118.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet118.java index 2c3a3b7ef96..a35fc9bb9e7 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet118.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet118.java @@ -21,6 +21,8 @@ * * @since 3.0 */ +import java.nio.file.Path; + import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; @@ -42,7 +44,7 @@ public static void main (String [] args) { dialog.setFilterExtensions(new String[] {"*.ico", "*.gif", "*.*"}); String name = dialog.open(); if (name == null) return; - ImageData image = new ImageData(name); + ImageData image = ImageData.load(Path.of(name)); Cursor oldCursor = cursor[0]; cursor[0] = new Cursor(display, image, 0, 0); shell.setCursor(cursor[0]); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java index d3d2ce755a3..ec59e16031c 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.eclipse.swt.snippets; +import java.nio.file.Path; import java.util.concurrent.atomic.*; import org.eclipse.swt.*; @@ -52,7 +53,7 @@ public static void main(String[] args) { if (fileName != null) { loader = new ImageLoader(); try { - imageDataArray = loader.load(fileName); + imageDataArray = loader.load(Path.of(fileName)); if (imageDataArray.length > 1) { animateThread = new Thread("Animation") { @Override diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java index 156864359c3..db95bd6ad20 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java @@ -21,6 +21,7 @@ */ import java.awt.*; import java.awt.image.*; +import java.nio.file.Path; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; @@ -143,8 +144,8 @@ public static void main(String[] args) { shell.setText("SWT Image"); ImageData data; if (args.length > 0) { - String fileName = args[0]; - data = new ImageData(fileName); + Path file = Path.of(args[0]); + data = ImageData.load(file); } else { data = createSampleImage(display); } diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet194.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet194.java index 53bd4db14fd..6d4e359cefc 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet194.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet194.java @@ -19,6 +19,8 @@ * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ */ +import java.nio.file.Path; + import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; @@ -62,7 +64,7 @@ public void run() { loader.logicalScreenWidth = data[0].width; loader.repeatCount = 0; // run forever try { - loader.save("swt.gif", SWT.IMAGE_GIF); + loader.save(Path.of("swt.gif"), SWT.IMAGE_GIF); } catch (SWTException ex) { System.out.println("Error saving GIF: " + ex); } finally { diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet246.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet246.java index 331228deac4..aeb45145635 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet246.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet246.java @@ -19,6 +19,8 @@ * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ */ +import java.nio.file.Path; + import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; @@ -64,7 +66,7 @@ private static void createImage() { ImageLoader loader = new ImageLoader(); loader.data = new ImageData[] { image.getImageData() }; - loader.save("swt.png", SWT.IMAGE_PNG); + loader.save(Path.of("swt.png"), SWT.IMAGE_PNG); image.dispose(); font.dispose(); } diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet288.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet288.java index 4065a003fb9..470560622c3 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet288.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet288.java @@ -13,13 +13,7 @@ *******************************************************************************/ package org.eclipse.swt.snippets; -/* - * Create a ToolBar containing animated GIFs - * - * For a list of all SWT example snippets see - * http://www.eclipse.org/swt/snippets/ - */ -import java.io.*; +import java.nio.file.Path; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; @@ -46,12 +40,12 @@ public static void main (String [] args) { FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI); dialog.setText("Select Multiple Animated GIFs"); dialog.setFilterExtensions(new String[] {"*.gif"}); - String filename = dialog.open(); + Path filename = Path.of(dialog.open()); String filenames[] = dialog.getFileNames(); int numToolBarItems = filenames.length; if (numToolBarItems > 0) { try { - loadAllImages(new File(filename).getParent(), filenames); + loadAllImages(filename.getParent(), filenames); } catch (SWTException e) { System.err.println("There was an error loading an image."); e.printStackTrace(); @@ -81,7 +75,7 @@ public static void main (String [] args) { } @SuppressWarnings("unused") - private static void loadAllImages(String directory, String[] filenames) throws SWTException { + private static void loadAllImages(Path directory, String[] filenames) throws SWTException { int numItems = filenames.length; loader = new ImageLoader[numItems]; imageDataArray = new ImageData[numItems][]; @@ -90,7 +84,7 @@ private static void loadAllImages(String directory, String[] filenames) throws S loader[i] = new ImageLoader(); int fullWidth = loader[i].logicalScreenWidth; int fullHeight = loader[i].logicalScreenHeight; - imageDataArray[i] = loader[i].load(directory + File.separator + filenames[i]); + imageDataArray[i] = loader[i].load(directory.resolve(filenames[i])); int numFramesOfAnimation = imageDataArray[i].length; image[i] = new Image[numFramesOfAnimation]; for (int j = 0; j < numFramesOfAnimation; j++) { diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java index 54b53dc1d39..66479b0e0eb 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java @@ -13,6 +13,8 @@ *******************************************************************************/ package org.eclipse.swt.snippets; +import java.nio.file.Path; + import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; @@ -31,36 +33,24 @@ public class Snippet367 { private static final String IMAGE_200 = "eclipse32.png"; private static final String IMAGES_ROOT = "bin/org/eclipse/swt/snippets/"; - private static final String IMAGE_PATH_100 = IMAGES_ROOT + IMAGE_100; - private static final String IMAGE_PATH_150 = IMAGES_ROOT + IMAGE_150; - private static final String IMAGE_PATH_200 = IMAGES_ROOT + IMAGE_200; + private static final Path IMAGE_PATH_100 = Path.of(IMAGES_ROOT, IMAGE_100); + private static final Path IMAGE_PATH_150 = Path.of(IMAGES_ROOT, IMAGE_150); + private static final Path IMAGE_PATH_200 = Path.of(IMAGES_ROOT, IMAGE_200); public static void main (String [] args) { final Display display = new Display (); - final ImageFileNameProvider filenameProvider = zoom -> { - switch (zoom) { - case 100: - return IMAGE_PATH_100; - case 150: - return IMAGE_PATH_150; - case 200: - return IMAGE_PATH_200; - default: - return null; - } + ImagePathProvider filenameProvider = zoom -> switch (zoom) { + case 100 -> IMAGE_PATH_100; + case 150 -> IMAGE_PATH_150; + case 200 -> IMAGE_PATH_200; + default -> null; }; - final ImageDataProvider imageDataProvider = zoom -> { - switch (zoom) { - case 100: - return new ImageData (IMAGE_PATH_100); - case 150: - return new ImageData (IMAGE_PATH_150); - case 200: - return new ImageData (IMAGE_PATH_200); - default: - return null; - } + ImageDataProvider imageDataProvider = zoom -> switch (zoom) { + case 100 -> ImageData.load(IMAGE_PATH_100); + case 150 -> ImageData.load(IMAGE_PATH_150); + case 200 -> ImageData.load(IMAGE_PATH_200); + default -> null; }; final ImageGcDrawer imageGcDrawer = (gc, width, height) -> { gc.drawRectangle(1, 1, width - 2, height - 2); @@ -93,16 +83,16 @@ public int getGcStyle() { exitItem.addListener(SWT.Selection, e -> shell.close()); new Label (shell, SWT.NONE).setText (IMAGE_200 + ":"); - new Label (shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_200)); - new Button(shell, SWT.PUSH).setImage (new Image (display, IMAGE_PATH_200)); + new Label (shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_200.toString())); + new Button(shell, SWT.PUSH).setImage (new Image (display, IMAGE_PATH_200.toString())); new Label (shell, SWT.NONE).setText (IMAGE_150 + ":"); - new Label (shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_150)); - new Button(shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_150)); + new Label (shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_150.toString())); + new Button(shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_150.toString())); new Label (shell, SWT.NONE).setText (IMAGE_100 + ":"); - new Label (shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_100)); - new Button(shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_100)); + new Label (shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_100.toString())); + new Button(shell, SWT.NONE).setImage (new Image (display, IMAGE_PATH_100.toString())); createSeparator(shell); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet373.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet373.java index 8ed82b1d82f..be2573e3fbd 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet373.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet373.java @@ -13,6 +13,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ +import java.nio.file.Path; import org.eclipse.swt.*; import org.eclipse.swt.custom.*; @@ -46,19 +47,13 @@ public class Snippet373 { private static final String IMAGE_PATH_100 = IMAGES_ROOT + IMAGE_100; private static final String IMAGE_PATH_150 = IMAGES_ROOT + IMAGE_150; private static final String IMAGE_PATH_200 = IMAGES_ROOT + IMAGE_200; - static final ImageFileNameProvider filenameProvider = zoom -> { - String path = null; - switch (zoom) { - case 150: - path = IMAGE_PATH_150; - break; - case 200: - path = IMAGE_PATH_200; - break; - default: - path = IMAGE_PATH_100; - } - return path; + static final ImagePathProvider filenameProvider = zoom -> { + String path = switch (zoom) { + case 150 -> IMAGE_PATH_150; + case 200 -> IMAGE_PATH_200; + default -> IMAGE_PATH_100; + }; + return Path.of(path); }; public static void main(String[] args) { diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet382.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet382.java index 502f14dfe65..0fdef8a1fd2 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet382.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet382.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.eclipse.swt.snippets; +import java.nio.file.Path; import java.util.function.*; import org.eclipse.swt.*; @@ -34,34 +35,22 @@ public class Snippet382 { private static final String IMAGE_200 = "eclipse32.png"; private static final String IMAGES_ROOT = "bin/org/eclipse/swt/snippets/"; - private static final String IMAGE_PATH_100 = IMAGES_ROOT + IMAGE_100; - private static final String IMAGE_PATH_150 = IMAGES_ROOT + IMAGE_150; - private static final String IMAGE_PATH_200 = IMAGES_ROOT + IMAGE_200; + private static final Path IMAGE_PATH_100 = Path.of(IMAGES_ROOT, IMAGE_100); + private static final Path IMAGE_PATH_150 = Path.of(IMAGES_ROOT, IMAGE_150); + private static final Path IMAGE_PATH_200 = Path.of(IMAGES_ROOT, IMAGE_200); public static void main (String [] args) { - final ImageFileNameProvider filenameProvider = zoom -> { - switch (zoom) { - case 100: - return IMAGE_PATH_100; - case 150: - return IMAGE_PATH_150; - case 200: - return IMAGE_PATH_200; - default: - return null; - } + ImagePathProvider filenameProvider = zoom -> switch (zoom) { + case 100 -> IMAGE_PATH_100; + case 150 -> IMAGE_PATH_150; + case 200 -> IMAGE_PATH_200; + default -> null; }; - final ImageDataProvider imageDataProvider = zoom -> { - switch (zoom) { - case 100: - return new ImageData (IMAGE_PATH_100); - case 150: - return new ImageData (IMAGE_PATH_150); - case 200: - return new ImageData (IMAGE_PATH_200); - default: - return null; - } + ImageDataProvider imageDataProvider = zoom -> switch (zoom) { + case 100 -> ImageData.load(IMAGE_PATH_100); + case 150 -> ImageData.load(IMAGE_PATH_150); + case 200 -> ImageData.load(IMAGE_PATH_200); + default -> null; }; final Display display = new Display (); @@ -74,7 +63,7 @@ public static void main (String [] args) { final Image disabledImageWithDataProvider = new Image (display,imageWithDataProvider, SWT.IMAGE_DISABLE); final Image greyImageWithDataProvider = new Image (display,imageWithDataProvider, SWT.IMAGE_GRAY); - final Image imageWithData = new Image (display, IMAGE_PATH_100); + final Image imageWithData = new Image (display, IMAGE_PATH_100.toString()); final Image disabledImageWithData = new Image (display,imageWithData, SWT.IMAGE_DISABLE); final Image greyImageWithData = new Image (display,imageWithData, SWT.IMAGE_GRAY); diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug302918_ToolItemDisabled.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug302918_ToolItemDisabled.java index 49d838088e3..10cbac0f8b7 100644 --- a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug302918_ToolItemDisabled.java +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug302918_ToolItemDisabled.java @@ -13,9 +13,11 @@ *******************************************************************************/ package org.eclipse.swt.tests.gtk.snippets; +import java.nio.file.Path; + import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.ImageLoader; +import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.CoolBar; @@ -39,11 +41,11 @@ public static void main(String[] args) { item.setPreferredSize(item.computeSize(size.x, size.y)); item.setControl(button); } - ImageLoader loader = new ImageLoader (); - loader.load("./images/next_nav.gif"); - Image nav = new Image (display, loader.data[0]); - loader.load("./images/next_nav_dis.gif"); - Image nav_dis = new Image (display, loader.data[0]); + + ImageData data = ImageData.load(Path.of("./images/next_nav.gif")); + Image nav = new Image(display, data); + data = ImageData.load(Path.of("./images/next_nav_dis.gif")); + Image nav_dis = new Image(display, data); ToolBar toolbar = new ToolBar(bar, SWT.NONE); ToolItem item1 = new ToolItem(toolbar, SWT.PUSH); diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug531667_CanvasPrint_does_not_work.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug531667_CanvasPrint_does_not_work.java index f7ac986b7cf..3dafe61896a 100644 --- a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug531667_CanvasPrint_does_not_work.java +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug531667_CanvasPrint_does_not_work.java @@ -14,7 +14,9 @@ *******************************************************************************/ package org.eclipse.swt.tests.gtk.snippets; -import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; @@ -41,8 +43,8 @@ */ public class Bug531667_CanvasPrint_does_not_work { - public static void main(String[] args) { - String filename = "some_canvas.png"; + public static void main(String[] args) throws IOException { + Path filename = Path.of("some_canvas.png"); Display display = new Display(); Shell shell = new Shell(display); @@ -79,7 +81,7 @@ private static Composite canvas(Display display, Shell shell) { return composite; } - private static void snapshot(Display display, Composite composite, String filename) { + private static void snapshot(Display display, Composite composite, Path output) throws IOException { Rectangle bounds = composite.getBounds(); Image image = new Image(display, bounds.width, bounds.height); GC gc = new GC(image); @@ -88,9 +90,8 @@ private static void snapshot(Display display, Composite composite, String filena ImageLoader loader = new ImageLoader(); loader.data = new ImageData[] { image.getImageData() }; - File output = new File(filename); - output.delete(); - loader.save(filename, SWT.IMAGE_PNG); - System.out.println("Image saved to: " + output.getAbsolutePath()); + Files.delete(output); + loader.save(output, SWT.IMAGE_PNG); + System.out.println("Image saved to: " + output.toAbsolutePath()); } } diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug545032_ImageLoaderTesting.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug545032_ImageLoaderTesting.java index 2f63538bbab..97a1acd149c 100644 --- a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug545032_ImageLoaderTesting.java +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug545032_ImageLoaderTesting.java @@ -13,7 +13,8 @@ *******************************************************************************/ package org.eclipse.swt.tests.gtk.snippets; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; @@ -36,7 +37,7 @@ public class Bug545032_ImageLoaderTesting { public static void main(String[] args) throws Exception { - File file = File.createTempFile("swt", "example"); + Path file = Files.createTempFile("swt", "example").toAbsolutePath(); Display display = new Display(); Shell shell = new Shell(display); @@ -77,18 +78,16 @@ public static void main(String[] args) throws Exception { ImageLoader saver = new ImageLoader(); saver.data = new ImageData[] { image.getImageData() }; - file.delete(); - saver.save(file.getAbsolutePath(), SWT.IMAGE_PNG); + saver.save(file, SWT.IMAGE_PNG); + + + ImageData imageLoadData = ImageData.load(file); + Label l = new Label(shell, SWT.BORDER); + image = new Image(display, imageLoadData); + l.setImage(image); - ImageLoader loader = new ImageLoader(); - ImageData[] loaded = loader.load(file.getAbsolutePath()); - for (ImageData imageLoadData : loaded) { - Label l = new Label(shell, SWT.BORDER); - image = new Image(display, imageLoadData); - l.setImage(image); - } shell.pack(); - file.delete(); + Files.delete(file); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug545804_SVGSupport.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug545804_SVGSupport.java index adcc0e6b6e7..3fad231eae8 100644 --- a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug545804_SVGSupport.java +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug545804_SVGSupport.java @@ -13,9 +13,11 @@ *******************************************************************************/ package org.eclipse.swt.tests.gtk.snippets; +import java.nio.file.Path; + import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.ImageLoader; +import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; @@ -25,9 +27,8 @@ public class Bug545804_SVGSupport { public static void main(String[] args) { Display display = new Display(); - ImageLoader loader = new ImageLoader(); - loader.load("./images/red_hat_fedora.svg"); - Image image = new Image(display, loader.data[0]); + ImageData data = ImageData.load(Path.of("./images/red_hat_fedora.svg")); + Image image = new Image(display, data); Shell shell = new Shell(display); shell.setLayout (new GridLayout()); Button button = new Button(shell, SWT.PUSH); diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ControlPrintBroken.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ControlPrintBroken.java index c24c3620e8b..84e78338647 100644 --- a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ControlPrintBroken.java +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ControlPrintBroken.java @@ -12,7 +12,7 @@ * Andrey Loskutov - initial API and implementation *******************************************************************************/ package org.eclipse.swt.tests.gtk.snippets; -import java.io.File; +import java.nio.file.Path; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; @@ -91,21 +91,18 @@ private static void snapshot(Display display, Composite composite, String filena ImageLoader loader = new ImageLoader(); loader.data = new ImageData[] { image.getImageData() }; - File file = new File(filename + ".png"); - file.delete(); - loader.save(filename + ".png", SWT.IMAGE_PNG); - + Path file = Path.of(filename + ".png"); + loader.save(file, SWT.IMAGE_PNG); loader = new ImageLoader(); - ImageData[] loaded = loader.load(file.getAbsolutePath()); + + ImageData loadedImage = ImageData.load(file); Shell shell = display.getShells()[0]; - for (ImageData d : loaded) { - Label l = new Label(shell, SWT.NONE); - image = new Image(display, d); - l.setImage(image); - } + Label l = new Label(shell, SWT.NONE); + image = new Image(display, loadedImage); + l.setImage(image); - loader.save(filename + "_2.png", SWT.IMAGE_PNG); + loader.save(Path.of(filename + "_2.png"), SWT.IMAGE_PNG); shell.pack(); } } diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ImageLoaderStriping.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ImageLoaderStriping.java index 0ad5c1f5088..c217c1060cd 100644 --- a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ImageLoaderStriping.java +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547529_ImageLoaderStriping.java @@ -13,6 +13,8 @@ *******************************************************************************/ package org.eclipse.swt.tests.gtk.snippets; +import java.nio.file.Path; + import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; @@ -57,7 +59,7 @@ public void widgetSelected(SelectionEvent e) { dialog.setFileName("Untitled.png"); String filename = dialog.open(); if ((filename != null) && !filename.isEmpty()) { - saveImage(composite, filename, SWT.IMAGE_PNG); + saveImage(composite, Path.of(filename), SWT.IMAGE_PNG); } } }); @@ -72,7 +74,7 @@ public void widgetSelected(SelectionEvent e) { display.dispose(); } - private static void saveImage(Control control, String filename, int format) { + private static void saveImage(Control control, Path file, int format) { Rectangle bounds = control.getBounds(); Image image = new Image(control.getDisplay(), bounds.width, bounds.height); GC gc = new GC(image); @@ -81,7 +83,7 @@ private static void saveImage(Control control, String filename, int format) { ImageData data = image.getImageData(); ImageLoader loader = new ImageLoader(); loader.data = new ImageData[] { data }; - loader.save(filename, format); + loader.save(file, format); image.dispose(); } } diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547557_ShellPrintGC.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547557_ShellPrintGC.java index d28589f9d02..277c20100e6 100644 --- a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547557_ShellPrintGC.java +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug547557_ShellPrintGC.java @@ -14,6 +14,8 @@ package org.eclipse.swt.tests.gtk.snippets; +import java.nio.file.Path; + import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; @@ -50,7 +52,7 @@ public void widgetSelected(SelectionEvent e) { // String filename = "/home//shell_test.png"; String filename = ""; if ((filename != null) && !filename.isEmpty()) { - saveImage(shell, filename, SWT.IMAGE_PNG); + saveImage(shell, Path.of(filename), SWT.IMAGE_PNG); } } }); @@ -64,7 +66,7 @@ public void widgetSelected(SelectionEvent e) { display.dispose(); } - private static void saveImage(Shell shell, String filename, int format) { + private static void saveImage(Shell shell, Path file, int format) { Rectangle bounds = shell.getBounds(); Image image = new Image(shell.getDisplay(), bounds.width, bounds.height); // Printing the client area will result in a warning and only the client area being printed @@ -75,7 +77,7 @@ private static void saveImage(Shell shell, String filename, int format) { ImageData data = image.getImageData(); ImageLoader loader = new ImageLoader(); loader.data = new ImageData[] { data }; - loader.save(filename, format); + loader.save(file, format); image.dispose(); } } \ No newline at end of file diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug553240_ImageLoaderSavingStriped.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug553240_ImageLoaderSavingStriped.java index bd356fb4291..5a64f97a85d 100644 --- a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug553240_ImageLoaderSavingStriped.java +++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug553240_ImageLoaderSavingStriped.java @@ -13,7 +13,8 @@ *******************************************************************************/ package org.eclipse.swt.tests.gtk.snippets; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; @@ -39,7 +40,7 @@ public static void main(final String[] args) { } static void initUI() throws Exception { - final File file = File.createTempFile("swt", "example"); + Path file = Files.createTempFile("swt", "example").toAbsolutePath(); final Display display = new Display(); final Shell shell = new Shell(); @@ -53,17 +54,13 @@ static void initUI() throws Exception { final Text text = new Text(shell, SWT.BORDER); text.setBounds(0, 50, 450, 75); - ImageLoader loader = new ImageLoader(); - final ImageData[] loadedImageData = loader.load("./images/map.png"); - final ImageData tileImageData = loadedImageData[0]; + ImageData tileImageData = ImageData.load(Path.of("./images/map.png")); final ImageLoader imageLoader = new ImageLoader(); imageLoader.data = new ImageData[] { tileImageData }; final int imageType = tileImageData.type; - file.delete(); - final String imageFilePath = file.getAbsolutePath(); - imageLoader.save(imageFilePath, imageType); - text.setText(file.getAbsolutePath()); + imageLoader.save(file, imageType); + text.setText(file.toString()); Composite composite = new Composite(shell, SWT.NONE); RowData layoutData = new RowData(256, 256); @@ -75,11 +72,7 @@ static void initUI() throws Exception { canvas.setLayoutData(layoutData); canvas.addPaintListener(e -> { - ImageLoader canvasLoader = new ImageLoader(); - final ImageData[] cloadedImageData = canvasLoader.load(file.getAbsolutePath()); - final ImageData ctileImageData = cloadedImageData[0]; - canvasLoader.data = new ImageData[] { ctileImageData }; - + ImageData ctileImageData = ImageData.load(file); Image imageToDraw = new Image(display, ctileImageData); e.gc.drawImage(imageToDraw, 0, 0); imageToDraw.dispose(); @@ -93,6 +86,6 @@ static void initUI() throws Exception { display.sleep(); } } - file.delete(); + Files.delete(file); } } diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java index c1b481b9ce8..472ef7a54f3 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java @@ -28,7 +28,6 @@ import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; import java.util.function.Consumer; @@ -40,8 +39,8 @@ import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.ImageDataProvider; -import org.eclipse.swt.graphics.ImageFileNameProvider; import org.eclipse.swt.graphics.ImageGcDrawer; +import org.eclipse.swt.graphics.ImagePathProvider; import org.eclipse.swt.graphics.PaletteData; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.graphics.Rectangle; @@ -63,11 +62,11 @@ public class Test_org_eclipse_swt_graphics_Image { @ClassRule public static TemporaryFolder tempFolder = new TemporaryFolder(); - private static String getPath(String fileName) { - return SwtTestUtil.getPath(fileName, tempFolder).toString(); + private static Path getPath(String fileName) { + return SwtTestUtil.getPath(fileName, tempFolder); } - ImageFileNameProvider imageFileNameProvider = zoom -> { + ImagePathProvider imageFileNameProvider = zoom -> { String fileName = switch (zoom) { case 100 -> "collapseall.png"; case 150 -> "collapseall@1.5x.png"; @@ -83,11 +82,11 @@ private static String getPath(String fileName) { case 200 -> "collapseall@2x.png"; default -> null; }; - return fileName != null ? new ImageData(getPath(fileName)) : null; + return fileName != null ? ImageData.load(getPath(fileName)) : null; }; ImageDataProvider imageDataProvider1xOnly = zoom -> { if (zoom == 100) { - return new ImageData(getPath("collapseall.png")); + return ImageData.load(getPath("collapseall.png")); } return null; }; @@ -356,12 +355,12 @@ public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvid Exception e; // Null provider - ImageFileNameProvider provider1 = null; + ImagePathProvider provider1 = null; e = assertThrows(IllegalArgumentException.class, ()->new Image(display, provider1)); assertSWTProblem("Incorrect exception thrown for provider == null", SWT.ERROR_NULL_ARGUMENT, e); // Invalid provider - ImageFileNameProvider provider2 = zoom -> null; + ImagePathProvider provider2 = zoom -> null; e = assertThrows(IllegalArgumentException.class, ()->new Image(display, provider2)); assertSWTProblem("Incorrect exception thrown for provider == null", SWT.ERROR_INVALID_ARGUMENT, e); @@ -369,7 +368,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvid Image image = new Image(display, imageFileNameProvider); image.dispose(); // Corrupt Image provider - ImageFileNameProvider provider3 = zoom -> { + ImagePathProvider provider3 = zoom -> { String fileName = switch (zoom) { case 100, 150, 200 -> "corrupt.png"; default -> null; @@ -380,7 +379,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvid assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e); // Valid provider only 100% zoom - ImageFileNameProvider provider4 = zoom -> { + ImagePathProvider provider4 = zoom -> { if (zoom == 100) { return getPath("collapseall.png"); } @@ -408,7 +407,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider() // Corrupt Image provider ImageDataProvider provider3 = zoom -> { return switch (zoom) { - case 100, 150, 200 -> new ImageData(getPath("corrupt.png")); + case 100, 150, 200 -> ImageData.load(getPath("corrupt.png")); default -> null; }; }; @@ -417,7 +416,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider() // Valid provider only 100% zoom ImageDataProvider provider4 = zoom -> { if (zoom == 100) { - return new ImageData(getPath("collapseall.png")); + return ImageData.load(getPath("collapseall.png")); } return null; }; @@ -896,7 +895,7 @@ void getImageData1() { String fileName = SwtTestUtil.imageFilenames[0]; for (String format : SwtTestUtil.imageFormats) { try (InputStream stream = SwtTestUtil.class.getResourceAsStream(fileName + "." + format)) { - ImageData data1 = new ImageData(stream); + ImageData data1 = ImageData.load(stream); Image image = new Image(display, data1); ImageData data2 = image.getImageData(); image.dispose(); @@ -1024,10 +1023,8 @@ public void test_updateWidthHeightAfterDPIChange() { @Test public void test_imageDataIsCached() { assumeTrue("On-demand image creation only implemented for Windows", SwtTestUtil.isWindows); - String imagePath = getPath("collapseall.png"); - ImageFileNameProvider imageFileNameProvider = __ -> { - return imagePath; - }; + Path imagePath = getPath("collapseall.png"); + ImagePathProvider imageFileNameProvider = __ -> imagePath; Image fileNameProviderImage = new Image(display, imageFileNameProvider); assertSame(fileNameProviderImage.getImageData(100), fileNameProviderImage.getImageData(100)); } @@ -1035,17 +1032,9 @@ public void test_imageDataIsCached() { @Test public void test_imageDataSameViaDifferentProviders() { assumeFalse("Cocoa generates inconsistent image data", SwtTestUtil.isCocoa); - String imagePath = getPath("collapseall.png"); - ImageFileNameProvider imageFileNameProvider = __ -> { - return imagePath; - }; - ImageDataProvider dataProvider = __ -> { - try (InputStream imageStream = Files.newInputStream(Path.of(imagePath))) { - return new ImageData(imageStream); - } catch (IOException e) { - } - return null; - }; + Path imagePath = getPath("collapseall.png"); + ImagePathProvider imageFileNameProvider = __ -> imagePath; + ImageDataProvider dataProvider = __ -> ImageData.load(imagePath); Image fileNameProviderImage = new Image(display, imageFileNameProvider); Image dataProviderImage = new Image(display, dataProvider); ImageData dataFromFileNameProviderImage = fileNameProviderImage.getImageData(100); @@ -1059,17 +1048,9 @@ public void test_imageDataSameViaDifferentProviders() { @Test public void test_imageDataSameViaProviderAndSimpleData() { assumeFalse("Cocoa generates inconsistent image data", SwtTestUtil.isCocoa); - String imagePath = getPath("collapseall.png"); - ImageFileNameProvider imageFileNameProvider = __ -> { - return imagePath; - }; - ImageDataProvider dataProvider = __ -> { - try (InputStream imageStream = Files.newInputStream(Path.of(imagePath))) { - return new ImageData(imageStream); - } catch (IOException e) { - } - return null; - }; + Path imagePath = getPath("collapseall.png"); + ImagePathProvider imageFileNameProvider = __ -> imagePath; + ImageDataProvider dataProvider = __ -> ImageData.load(imagePath); Image fileNameProviderImage = new Image(display, imageFileNameProvider); Image dataImage = new Image(display, dataProvider.getImageData(100)); ImageData dataFromFileNameProviderImage = fileNameProviderImage.getImageData(100); @@ -1080,7 +1061,6 @@ public void test_imageDataSameViaProviderAndSimpleData() { dataImage.dispose(); } - private Comparator imageDataComparator() { return Comparator.comparingInt(d -> d.width) // .thenComparing(d -> d.height) // diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_ImageData.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_ImageData.java index 7f6519abfc6..d4f0851d385 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_ImageData.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_ImageData.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.InputStream; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -213,31 +214,32 @@ public void test_ConstructorIIILorg_eclipse_swt_graphics_PaletteData() { public void test_ConstructorLjava_io_InputStream() throws IOException { InputStream stream = null; assertThrows("No exception thrown for InputStream == null", IllegalArgumentException.class, - () -> new ImageData(stream)); + () -> ImageData.load(stream)); try (InputStream stream1 = SwtTestUtil.class.getResourceAsStream("empty.txt")){ - assertThrows("No exception thrown for invalid InputStream", SWTException.class, () ->new ImageData(stream1)); + assertThrows("No exception thrown for invalid InputStream", SWTException.class, + () -> ImageData.load(stream1)); } String fileName = SwtTestUtil.imageFilenames[0]; for (String format : SwtTestUtil.imageFormats) { try (InputStream stream2 = SwtTestUtil.class.getResourceAsStream(fileName + "." + format)) { - new ImageData(stream2); + ImageData.load(stream2); } } } @Test public void test_ConstructorLjava_lang_String() { - String filename = null; + Path filename = null; assertThrows("No exception thrown for filename == null", IllegalArgumentException.class, - () -> new ImageData(filename)); + () -> ImageData.load(filename)); } @Test public void test_clone() throws IOException { try (InputStream stream = SwtTestUtil.class.getResourceAsStream(SwtTestUtil.imageFilenames[0] + "." + SwtTestUtil.imageFormats[0])) { - ImageData data1 = new ImageData(stream); + ImageData data1 = ImageData.load(stream); ImageData data2 = (ImageData) data1.clone(); // imageData does not implement an equals(Object) method assertEquals(":a:", data1.alpha, data2.alpha); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_ImageLoader.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_ImageLoader.java index 38e03c99709..30e4960180e 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_ImageLoader.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_ImageLoader.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Path; import java.util.Arrays; import org.eclipse.swt.SWT; @@ -101,6 +102,7 @@ public void test_loadLjava_io_InputStream() throws IOException { } @Test +@SuppressWarnings("deprecation") public void test_loadLjava_lang_String() { ImageLoader loader = new ImageLoader(); String filename = null; @@ -108,6 +110,14 @@ public void test_loadLjava_lang_String() { "No exception thrown for load filename == null"); } +@Test +public void test_loadLjava_nio_file_Path() { + ImageLoader loader = new ImageLoader(); + Path filename = null; + assertThrows(IllegalArgumentException.class, () -> loader.load(filename), + "No exception thrown for load filename == null"); +} + @Test public void test_saveLjava_io_OutputStreamI() throws IOException { ImageLoader loader = new ImageLoader(); @@ -141,6 +151,7 @@ public void test_saveLjava_io_OutputStreamI() throws IOException { } @Test +@SuppressWarnings("deprecation") public void test_saveLjava_lang_StringI() { ImageLoader loader = new ImageLoader(); String filename = null; @@ -148,6 +159,14 @@ public void test_saveLjava_lang_StringI() { "No exception thrown for save filename == null"); } +@Test +public void test_saveLjava_nio_file_PathI() { + ImageLoader loader = new ImageLoader(); + Path file = null; + assertThrows(IllegalArgumentException.class, () -> loader.save(file, 0), + "No exception thrown for save filename == null"); +} + /** * Ensure that saving and loading an image with {@link ImageLoader} * does not result in different {@link ImageData#data} arrays. diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_internal_SVGRasterizer.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_internal_SVGRasterizer.java index cd4a598799b..21ed3e0b597 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_internal_SVGRasterizer.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_internal_SVGRasterizer.java @@ -15,12 +15,14 @@ import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.nio.file.Path; + import org.eclipse.swt.SWT; import org.eclipse.swt.SWTException; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.ImageDataProvider; -import org.eclipse.swt.graphics.ImageFileNameProvider; +import org.eclipse.swt.graphics.ImagePathProvider; import org.eclipse.swt.widgets.Display; import org.junit.ClassRule; import org.junit.Test; @@ -36,17 +38,17 @@ public class Test_org_eclipse_swt_internal_SVGRasterizer { @ClassRule public static TemporaryFolder tempFolder = new TemporaryFolder(); - private static String getPath(String fileName) { - return SwtTestUtil.getPath(fileName, tempFolder).toString(); + private static Path getPath(String fileName) { + return SwtTestUtil.getPath(fileName, tempFolder); } @Test public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider() { - ImageFileNameProvider validImageFileNameProvider = zoom -> getPath("collapseall.svg"); + ImagePathProvider validImageFileNameProvider = zoom -> getPath("collapseall.svg"); Image image = new Image(Display.getDefault(), validImageFileNameProvider); image.dispose(); - ImageFileNameProvider corruptImageFileNameProvider = zoom -> getPath("corrupt.svg"); + ImagePathProvider corruptImageFileNameProvider = zoom -> getPath("corrupt.svg"); SWTException e = assertThrows(SWTException.class, () -> new Image(Display.getDefault(), corruptImageFileNameProvider)); assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e); @@ -56,14 +58,14 @@ public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvid public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider() { ImageDataProvider validImageDataProvider = zoom -> { String fileName = "collapseall.svg"; - return new ImageData(getPath(fileName)); + return ImageData.load(getPath(fileName)); }; Image image = new Image(Display.getDefault(), validImageDataProvider); image.dispose(); ImageDataProvider corruptImageDataProvider = zoom -> { String fileName = "corrupt.svg"; - return new ImageData(getPath(fileName)); + return ImageData.load(getPath(fileName)); }; SWTException e = assertThrows(SWTException.class, () -> new Image(Display.getDefault(), corruptImageDataProvider)); diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug576334_32bpp_BMP_Colors.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug576334_32bpp_BMP_Colors.java index 4ed9716c177..3a5a9c24a45 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug576334_32bpp_BMP_Colors.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Bug576334_32bpp_BMP_Colors.java @@ -14,12 +14,15 @@ package org.eclipse.swt.tests.manual; -import org.eclipse.swt.graphics.*; -import org.eclipse.swt.layout.GridLayout; -import org.eclipse.swt.widgets.*; - import java.io.InputStream; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; + public class Bug576334_32bpp_BMP_Colors { public static void main (String[] args) { final Display display = new Display (); @@ -30,7 +33,7 @@ public static void main (String[] args) { hint.setText ("The image below shall be orange; not green"); InputStream imageStream = Bug576334_32bpp_BMP_Colors.class.getResourceAsStream ("/Bug576334_32bpp_BMP_Colors.bmp"); - ImageData imageData = new ImageData (imageStream); + ImageData imageData = ImageData.load(imageStream); Image image = new Image (display, imageData); Label imageLabel = new Label (shell, 0); diff --git a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0445_HiDPISmoothScaling.java b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0445_HiDPISmoothScaling.java index 88bdb17c031..68fb3085bd4 100644 --- a/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0445_HiDPISmoothScaling.java +++ b/tests/org.eclipse.swt.tests/ManualTests/org/eclipse/swt/tests/manual/Issue0445_HiDPISmoothScaling.java @@ -66,7 +66,7 @@ public static void main(String[] args) { try (InputStream in = new BufferedInputStream( Issue0445_HiDPISmoothScaling.class.getResourceAsStream("gear_icon1.png"))) { if (in != null) { - result = new ImageData(in); + result = ImageData.load(in); } } catch (IOException | SWTException e) { }