From 8a970de9e301a8594e5109bf0b69242ac591b3df Mon Sep 17 00:00:00 2001
From: Tiziano Munaro <munaro@fortiss.org>
Date: Mon, 2 Dec 2019 11:30:13 +0100
Subject: [PATCH] [UI] Create utility class for handling graphics in
 JavaFX-based views

Issue-Ref: 3874
Issue-Url: https://af3-developer.fortiss.org/issues/3874

Signed-off-by: Tiziano Munaro <munaro@fortiss.org>
---
 .../tooling/common/ui/javafx/util/.ratings    |  1 +
 .../common/ui/javafx/util/GraphicUtils.java   | 67 +++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/GraphicUtils.java

diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/.ratings b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/.ratings
index cd9b77b00..f200d0155 100644
--- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/.ratings
+++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/.ratings
@@ -1 +1,2 @@
+GraphicUtils.java 4d471a310a52bda1c090f956dcdbe90775b12cb8 YELLOW
 SceneGraphUtils.java f54304c2eb604934de9afdf9d2a8ca88a762398a GREEN
diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/GraphicUtils.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/GraphicUtils.java
new file mode 100644
index 000000000..4d471a310
--- /dev/null
+++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/util/GraphicUtils.java
@@ -0,0 +1,67 @@
+/*-------------------------------------------------------------------------+
+| Copyright 2019 fortiss GmbH                                              |
+|                                                                          |
+| Licensed under the Apache License, Version 2.0 (the "License");          |
+| you may not use this file except in compliance with the License.         |
+| You may obtain a copy of the License at                                  |
+|                                                                          |
+|    http://www.apache.org/licenses/LICENSE-2.0                            |
+|                                                                          |
+| Unless required by applicable law or agreed to in writing, software      |
+| distributed under the License is distributed on an "AS IS" BASIS,        |
+| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
+| See the License for the specific language governing permissions and      |
+| limitations under the License.                                           |
++--------------------------------------------------------------------------*/
+package org.fortiss.tooling.common.ui.javafx.util;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+
+import javafx.scene.image.Image;
+import javafx.scene.paint.Color;
+
+/**
+ * Utility methods for handling graphics in JavaFX-based views.
+ * 
+ * @author munaro
+ */
+public class GraphicUtils {
+
+	/** The image cache of loaded images. */
+	private static final HashMap<String, Image> imageCache = new HashMap<>();
+
+	/** Returns the plugin-local URI string for the given resource. */
+	public static String getURIString(String pluginId, String localPath) {
+		return "platform:/plugin/" + pluginId + localPath;
+	}
+
+	/** Returns the Java FX Image load from the local path. */
+	public static Image getFXImage(String pluginId, String localPath) {
+		String uri = getURIString(pluginId, localPath);
+		if(uri == null) {
+			return null;
+		}
+		Image cacheImage = imageCache.get(uri);
+		if(cacheImage == null) {
+			InputStream in = null;
+			try {
+				in = new URL(uri).openStream();
+			} catch(Exception e) {
+				e.printStackTrace();
+			}
+			if(in != null) {
+				cacheImage = new Image(in);
+			}
+			imageCache.put(uri, cacheImage);
+		}
+		return cacheImage;
+	}
+
+	/** Converts the given SWT color to Java FX color. */
+	public static Color convertColor(org.eclipse.swt.graphics.Color swtColor) {
+		return Color.rgb(swtColor.getRed(), swtColor.getGreen(), swtColor.getBlue(),
+				swtColor.getAlpha() / 255.0);
+	}
+}
-- 
GitLab