diff options
| author | Niraj Modi | 2015-03-31 18:08:40 +0000 |
|---|---|---|
| committer | Niraj Modi | 2015-03-31 18:17:19 +0000 |
| commit | 2f244e084ac5cca7ad4b8b06fd1e59e2a64926f4 (patch) | |
| tree | b23ffd0e7802ca79d3008c230d51211a6d37f743 | |
| parent | 15545d017cfc49844170a7161c0e82dbcff0ad24 (diff) | |
| download | eclipse.platform.swt-2f244e084ac5cca7ad4b8b06fd1e59e2a64926f4.tar.gz eclipse.platform.swt-2f244e084ac5cca7ad4b8b06fd1e59e2a64926f4.tar.xz eclipse.platform.swt-2f244e084ac5cca7ad4b8b06fd1e59e2a64926f4.zip | |
Bug 462569 - [api]
ImageFileNameProvider/ImageDataProvider#getImageData(int) can't
determine size of image
- Refactored [Win32/GTK]Image & DPIUtil classes to eliminate use of
Maps.
Change-Id: I4031672f5692c994cbb5e992ed6647259f90450a
Signed-off-by: Niraj Modi <niraj.modi@in.ibm.com>
3 files changed, 39 insertions, 96 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DPIUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DPIUtil.java index 6b16051329..9cdcbdfe96 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DPIUtil.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DPIUtil.java @@ -10,7 +10,7 @@ *******************************************************************************/ package org.eclipse.swt.graphics; -import java.util.*; +import org.eclipse.swt.*; /** * This class hold common constants and utility functions w.r.t. to SWT high DPI @@ -42,28 +42,32 @@ class DPIUtil { } /** - * Gets Map of image file path at specified zoom level and boolean flag - * as TRUE if expected image at zoom was found. + * Gets Image file path at specified zoom level, if image is missing then + * fall-back to 100% image. If provider or fall-back image is not available, + * throw error. */ - @SuppressWarnings("unchecked") - static Map<String, Boolean> getImagePathAtZoom (ImageFileNameProvider provider, int zoom) { - if (provider == null) return Collections.EMPTY_MAP; + static String validateAndGetImagePathAtZoom (ImageFileNameProvider provider, int zoom, boolean[] found) { + if (provider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); String filename = provider.getImagePath (zoom); + found [0] = (filename != null); /* If image is null when (zoom != 100%), fall-back to image at 100% zoom */ - if (zoom != 100 && filename == null) return Collections.singletonMap (provider.getImagePath (100), Boolean.FALSE); - return Collections.singletonMap (filename, Boolean.TRUE); + if (zoom != 100 && !found [0]) filename = provider.getImagePath (100); + if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + return filename; } /** - * Gets Map of Image data at specified zoom level and boolean flag - * as TRUE if expected image at zoom was found. + * Gets Image data at specified zoom level, if image is missing then + * fall-back to 100% image. If provider or fall-back image is not available, + * throw error. */ - @SuppressWarnings("unchecked") - static Map<ImageData, Boolean> getImageDataAtZoom (ImageDataProvider provider, int zoom) { - if (provider == null) return Collections.EMPTY_MAP; + static ImageData validateAndGetImageDataAtZoom (ImageDataProvider provider, int zoom, boolean[] found) { + if (provider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); ImageData data = provider.getImageData (zoom); + found [0] = (data != null); /* If image is null when (zoom != 100%), fall-back to image at 100% zoom */ - if (zoom != 100 && data == null) return Collections.singletonMap (provider.getImageData (100), Boolean.FALSE); - return Collections.singletonMap (data, Boolean.TRUE); + if (zoom != 100 && !found [0]) data = provider.getImageData (100); + if (data == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + return data; } } 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 fb75c223a2..2b6c2bef40 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 @@ -715,18 +715,9 @@ public Image(Device device, String filename) { */ public Image(Device device, ImageFileNameProvider imageFileNameProvider) { super(device); - if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.imageFileNameProvider = imageFileNameProvider; currentDeviceZoom = getDeviceZoom (); - String filename = null; - Map <String, Boolean> result = DPIUtil.getImagePathAtZoom (imageFileNameProvider, currentDeviceZoom); - if (result != null && !result.isEmpty ()) { - for (String str : result.keySet()) { - filename = str; - break; - } - } - if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + String filename = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, currentDeviceZoom, new boolean[1]); initNative (filename); if (this.pixmap == 0 && this.surface == 0) init(new ImageData(filename)); init (); @@ -763,18 +754,9 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) { */ public Image(Device device, ImageDataProvider imageDataProvider) { super(device); - if (imageDataProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.imageDataProvider = imageDataProvider; currentDeviceZoom = getDeviceZoom (); - ImageData data = null; - Map <ImageData, Boolean> result = DPIUtil.getImageDataAtZoom (imageDataProvider, currentDeviceZoom); - if (result != null && !result.isEmpty ()) { - for (ImageData imageData : result.keySet()) { - data = imageData; - break; - } - } - if (data == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + ImageData data = DPIUtil.validateAndGetImageDataAtZoom(imageDataProvider, currentDeviceZoom, new boolean[1]); init (data); init (); } @@ -793,16 +775,10 @@ boolean refreshImageForZoom () { boolean refreshed = false; if (deviceZoomLevel != currentDeviceZoom) { if (imageFileNameProvider != null) { - String filename = null; - Map <String, Boolean> result = DPIUtil.getImagePathAtZoom (imageFileNameProvider, deviceZoomLevel); - if (result != null && !result.isEmpty ()) { - for (String str : result.keySet()) { - filename = str; - break; - } - } - if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); - if (result.get (filename) || currentDeviceZoom != 100) { + boolean[] found = new boolean[1]; + String filename = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, deviceZoomLevel, found); + /* Avoid re-creating the fall-back image, when current zoom is already 100% */ + if (found[0] || currentDeviceZoom != 100) { /* Release current native resources */ destroy (); initNative(filename); @@ -811,16 +787,10 @@ boolean refreshImageForZoom () { refreshed = true; } } else if (imageDataProvider != null) { - ImageData data = null; - Map <ImageData, Boolean> result = DPIUtil.getImageDataAtZoom (imageDataProvider, deviceZoomLevel); - if (result != null && !result.isEmpty ()) { - for (ImageData imageData : result.keySet()) { - data = imageData; - break; - } - } - if (data == null) SWT.error(SWT.ERROR_INVALID_IMAGE); - if (result.get (data) || currentDeviceZoom != 100) { + boolean[] found = new boolean[1]; + ImageData data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, deviceZoomLevel, found); + /* Avoid re-creating the fall-back image, when current zoom is already 100% */ + if (found[0] || currentDeviceZoom != 100) { /* Release current native resources */ destroy (); init(data); 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 7db0ec874a..eedf18d734 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 @@ -12,7 +12,6 @@ package org.eclipse.swt.graphics; import java.io.*; -import java.util.*; import org.eclipse.swt.*; import org.eclipse.swt.internal.gdip.*; @@ -662,18 +661,9 @@ public Image (Device device, String filename) { */ public Image(Device device, ImageFileNameProvider imageFileNameProvider) { super(device); - if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.imageFileNameProvider = imageFileNameProvider; currentDeviceZoom = getDeviceZoom (); - String fileName = null; - Map <String, Boolean> result = DPIUtil.getImagePathAtZoom (imageFileNameProvider, currentDeviceZoom); - if (result != null && !result.isEmpty ()) { - for (String str : result.keySet()) { - fileName = str; - break; - } - } - if (fileName == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + String fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, currentDeviceZoom, new boolean[1]); initNative (fileName); if (this.handle == 0) init(new ImageData (fileName)); init(); @@ -710,18 +700,9 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) { */ public Image(Device device, ImageDataProvider imageDataProvider) { super(device); - if (imageDataProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.imageDataProvider = imageDataProvider; currentDeviceZoom = getDeviceZoom (); - ImageData data = null; - Map <ImageData, Boolean> result = DPIUtil.getImageDataAtZoom (imageDataProvider, currentDeviceZoom); - if (result != null && !result.isEmpty ()) { - for (ImageData imageData : result.keySet()) { - data = imageData; - break; - } - } - if (data == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); + ImageData data = DPIUtil.validateAndGetImageDataAtZoom(imageDataProvider, currentDeviceZoom, new boolean[1]); init(data); init(); } @@ -740,16 +721,10 @@ boolean refreshImageForZoom () { boolean refreshed = false; if (deviceZoomLevel != currentDeviceZoom) { if (imageFileNameProvider != null) { - String filename = null; - Map <String, Boolean> result = DPIUtil.getImagePathAtZoom (imageFileNameProvider, deviceZoomLevel); - if (result != null && !result.isEmpty ()) { - for (String str : result.keySet()) { - filename = str; - break; - } - } - if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); - if (result.get (filename) || currentDeviceZoom != 100) { + boolean[] found = new boolean[1]; + String filename = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, deviceZoomLevel, found); + /* Avoid re-creating the fall-back image, when current zoom is already 100% */ + if (found[0] || currentDeviceZoom != 100) { /* Release current native resources */ destroy (); initNative(filename); @@ -758,16 +733,10 @@ boolean refreshImageForZoom () { refreshed = true; } } else if (imageDataProvider != null) { - ImageData data = null; - Map <ImageData, Boolean> result = DPIUtil.getImageDataAtZoom (imageDataProvider, deviceZoomLevel); - if (result != null && !result.isEmpty ()) { - for (ImageData imageData : result.keySet()) { - data = imageData; - break; - } - } - if (data == null) SWT.error(SWT.ERROR_INVALID_IMAGE); - if (result.get (data) || currentDeviceZoom != 100) { + boolean[] found = new boolean[1]; + ImageData data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, deviceZoomLevel, found); + /* Avoid re-creating the fall-back image, when current zoom is already 100% */ + if (found[0] || currentDeviceZoom != 100) { /* Release current native resources */ destroy (); init(data); |
