package org.reldb.dbrowser.ui;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageDataProvider;
import org.eclipse.swt.widgets.Display;
import org.eclipse.wb.swt.SWTResourceManager;
import org.reldb.dbrowser.ui.preferences.PreferencePageGeneral;
import org.reldb.dbrowser.ui.preferences.Preferences;

public class IconLoader {
	private final static String base = "/org/reldb/dbrowser/icons/";
	
	public static Image loadIcon(String name) {
		boolean largeIcons = Preferences.getPreferenceBoolean(PreferencePageGeneral.LARGE_ICONS);
		return (largeIcons) ? loadIconLarge(name) : loadIconNormal(name);
	}
	
	public static Image loadIconNormal(String name) {
		Image imgBigRaw = SWTResourceManager.getImageOrNull(base + name + "@2x.png");
		Image imgSmallRaw = SWTResourceManager.getImageOrNull(base + name + ".png");
		if (imgBigRaw == null && imgSmallRaw == null) {
			imgBigRaw = SWTResourceManager.getImage(base + "noimage@2x.png");
			imgSmallRaw = SWTResourceManager.getImage(base + "noimage.png");
		}
		Image imgLarge = (imgBigRaw == null) ? imgSmallRaw : imgBigRaw;
		Image imgSmall = imgSmallRaw;
		final ImageDataProvider imageDataProvider = zoom -> {
			switch (zoom) {
			case 200:
				return imgLarge.getImageData();
			default:
				return imgSmall.getImageData();
			}
		};
		// TODO - should cache image in SWTResourceManager here!
		return new Image(Display.getCurrent(), imageDataProvider);
	}
	
	public static Image loadIconLarge(String name) {
		Image imgBig = SWTResourceManager.getImageOrNull(base + name + "@2x.png");
		if (imgBig == null)
			return SWTResourceManager.getImage(base + name + ".png");
		return imgBig;
	}
}

@@ -64,16 +64,59 @@
 .Image;%0A
+import org.eclipse.swt.graphics.ImageData;%0A
 import o
@@ -672,96 +672,130 @@
 %0A%09%0A%09
-public static Image loadIconNormal(String name) %7B%0A%09%09Image imgBigRaw = SWTResourceMan
+static int scaleFactor;%0A%09%0A%09private static ImageData grabzoom(int zoom, Image image) %7B%0A%09%09scaleFactor = zoom;%0A%09%09return im
 age
-r
 .get
@@ -803,40 +803,57 @@
 mage
-OrNull(base + name + %22@2x.png%22);
+Data();%0A%09%7D%0A%09%0A%09public static int getDPIScaling() %7B
 %0A%09%09I
@@ -856,29 +856,24 @@
 %0A%09%09Image img
-Small
 Raw = SWTRes
@@ -889,38 +889,32 @@
 ger.getImage
-OrNull
 (base + 
 name + %22.png
@@ -897,32 +897,32 @@
 mage(base + 
-name + %22
+%22noimage
 .png%22);%0A%09%09if
@@ -923,64 +923,284 @@
 ;%0A%09%09
-if (imgBigRaw == null && imgSmallRaw == null) %7B%0A%09%09%09imgBi
+new Image(Display.getCurrent(), (ImageDataProvider)zoom -%3E grabzoom(zoom, imgRaw));%0A%09%09return scaleFactor;%0A%09%7D%0A%09%0A%09public static Image loadIconNormal(String name) %7B%0A%09%09return new Image(Display.getCurrent(), (ImageDataProvider)zoom -%3E %7B%0A%09%09%09switch (zoom) %7B%0A%09%09%09case 200:%0A%09%09%09%09Image im
 gRaw
@@ -1225,32 +1225,38 @@
 ger.getImage
+OrNull
 (base + 
 %22noimage@2x.
@@ -1243,24 +1243,24 @@
 (base + 
-%22noimage
+name + %22
 @2x.png%22
@@ -1265,24 +1265,47 @@
 g%22);%0A%09%09%09
-imgSmall
+%09if (imgRaw == null) %7B%0A%09%09%09%09%09img
 Raw = SW
@@ -1325,32 +1325,38 @@
 ger.getImage
+OrNull
 (base + 
 %22noimage.png
@@ -1339,32 +1339,32 @@
 Null(base + 
-%22noimage
+name + %22
 .png%22);%0A%09%09%7D%0A
@@ -1365,36 +1365,18 @@
 ;%0A%09%09
-%7D%0A%09%09Image imgLarge =
+%09%09%09if
  (img
-Big
 Raw 
@@ -1387,155 +1387,83 @@
 ull)
- ? imgSmallRaw : imgBigRaw;%0A%09%09Image imgSmall = imgSmallRaw;%0A%09%09final ImageDataProvider imageDataProvider = zoom -%3E %7B%0A%09%09%09switch (zoom) %7B%0A%09%09%09case 200:
+%0A%09%09%09%09%09%09imgRaw = SWTResourceManager.getImage(base + %22noimage@2x.png%22);%0A%09%09%09%09%7D
 %0A%09%09%09
@@ -1473,21 +1473,19 @@
 turn img
-Large
+Raw
 .getImag
@@ -1513,167 +1513,205 @@
 %09%09%09%09
-return imgSmall.getImageData();%0A%09%09%09%7D%0A%09%09%7D;%0A%09%09// TODO - should cache image in SWTResourceManager here!%0A%09%09return new Image(Display.g
+imgRaw = SWTResourceManager.getImageOrNull(base + name + %22.png%22);%0A%09%09%09%09if (imgRaw == null)%0A%09%09%09%09%09imgRaw = SWTResourceManager.getImage(base + %22noimage.png%22);%0A%09%09%09%09r
 et
-C
 ur
-rent(), imageDataProvider
+n imgRaw.getImageData();%0A%09%09%09%7D%0A%09%09%7D
 );%0A%09

Optimise loadIconNormal() and add getDPIScaling()