From 369088f24f519f0ff718a1fd78355550efcd3e4e Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Mon, 16 Mar 2020 00:27:52 +0100
Subject: [PATCH] ModelElementHanderBase: Unify path handling for SWT and FX
 icon

* Add getPluginId() and getIconPath()
* ResourceUtils::getResourceURI: accept paths with and without leading /

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

Signed-off-by: Simon Barner <barner@fortiss.org>
---
 .../tooling/base/ui/extension/base/.ratings   |  2 +-
 .../base/HierarchicElementHandlerBase.java    |  9 +---
 .../tooling/kernel/ui/extension/base/.ratings |  2 +-
 .../base/ModelElementHandlerBase.java         | 45 +++++++++----------
 .../org/fortiss/tooling/kernel/utils/.ratings |  2 +-
 .../tooling/kernel/utils/ResourceUtils.java   | 13 +++---
 6 files changed, 32 insertions(+), 41 deletions(-)

diff --git a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/extension/base/.ratings b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/extension/base/.ratings
index 6b4246af4..0a4cceb08 100644
--- a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/extension/base/.ratings
+++ b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/extension/base/.ratings
@@ -1 +1 @@
-HierarchicElementHandlerBase.java 5207a32e345912f16d753c466e9b603bb72bd2ef GREEN
+HierarchicElementHandlerBase.java eef567d577422c9cdfb8bff9a5238ebd2d08e3bd YELLOW
diff --git a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/extension/base/HierarchicElementHandlerBase.java b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/extension/base/HierarchicElementHandlerBase.java
index 5207a32e3..eef567d57 100644
--- a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/extension/base/HierarchicElementHandlerBase.java
+++ b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/extension/base/HierarchicElementHandlerBase.java
@@ -18,7 +18,6 @@ package org.fortiss.tooling.base.ui.extension.base;
 import java.util.List;
 
 import org.eclipse.emf.ecore.EObject;
-import org.eclipse.jface.resource.ImageDescriptor;
 import org.fortiss.tooling.base.model.base.HierarchicElementBase;
 import org.fortiss.tooling.base.model.element.IHierarchicElement;
 import org.fortiss.tooling.kernel.ui.extension.IModelElementHandler;
@@ -30,7 +29,7 @@ import org.fortiss.tooling.kernel.ui.extension.base.NamedCommentedModelElementHa
  * @author mou
  */
 @SuppressWarnings({"rawtypes", "unchecked"})
-public class HierarchicElementHandlerBase<T extends HierarchicElementBase>
+public abstract class HierarchicElementHandlerBase<T extends HierarchicElementBase>
 		extends NamedCommentedModelElementHandlerBase<T> {
 
 	/** {@inheritDoc} */
@@ -50,10 +49,4 @@ public class HierarchicElementHandlerBase<T extends HierarchicElementBase>
 	public List<EObject> getSpecifications(T element) {
 		return (List)element.getSpecifications();
 	}
-
-	/** {@inheritDoc} */
-	@Override
-	public ImageDescriptor getIconImageDescriptor() {
-		return null;
-	}
 }
diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/.ratings b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/.ratings
index 4dfe4762a..dd22c408d 100644
--- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/.ratings
+++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/.ratings
@@ -8,7 +8,7 @@ FXEditorBase.java 40caf638c7b4c02da5aece0d9d58883bce630e76 GREEN
 IListPropertySection.java 8bb00fe7959583e794ff9437b7a77404c9a9e70f GREEN
 LWFXEFEditorBase.java f6b160b700a0287021402b5702beb2bfdce3dc2e GREEN
 ModelEditorBindingBase.java b9b1a1c5a48a6e677d1f57ad55a6126d9703c4b5 GREEN
-ModelElementHandlerBase.java d08583ffdf78938a14caeb46124eda4ce5cac3e4 GREEN
+ModelElementHandlerBase.java d97a0762c39792721b7002fd2d7fb2f06bcecdcd YELLOW
 MultiEObjectActionBase.java 9e237d8ea640c4194e4877af4a9cfce88698e543 GREEN
 NamedCommentedModelElementHandlerBase.java 681b98b50b362f01abb7a36f108f4f11b9e51829 GREEN
 PropertySectionBase.java 20fb1daea544123ea941743aafeb9ac59daf5356 GREEN
diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/ModelElementHandlerBase.java b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/ModelElementHandlerBase.java
index d08583ffd..d97a0762c 100644
--- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/ModelElementHandlerBase.java
+++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/ModelElementHandlerBase.java
@@ -16,7 +16,9 @@
 package org.fortiss.tooling.kernel.ui.extension.base;
 
 import static java.util.Collections.emptyList;
+import static org.eclipse.jface.resource.ResourceLocator.imageDescriptorFromBundle;
 import static org.fortiss.tooling.common.ui.javafx.util.GraphicUtils.getFXImageFromURI;
+import static org.fortiss.tooling.kernel.utils.ResourceUtils.getResourceURI;
 
 import java.util.HashMap;
 import java.util.List;
@@ -35,8 +37,8 @@ import javafx.scene.image.ImageView;
 /**
  * Base implementation for {@link IModelElementHandler}s.
  * 
- * Sub-classes must implement {@link #getIconImageDescriptor()} and may override
- * the other non-final methods of {@link IModelElementHandler}. By default the base
+ * Sub-classes must implement {@link #getPluginId()} and {@link #getIconPath(EObject)} and may
+ * override the other non-final methods of {@link IModelElementHandler}. By default the base
  * implementation returns empty lists for the respective methods. Open editor
  * requests are handled by returning the model element itself.
  * 
@@ -53,9 +55,24 @@ public abstract class ModelElementHandlerBase<T extends EObject>
 		return "";
 	}
 
+	/**
+	 * Returns the ID of the plugin used by this model element handler to provide icons for model
+	 * elements.
+	 */
+	protected abstract String getPluginId();
+
+	/** Returns the plugin-relative path of the icon to be displayed for the given element. */
+	protected abstract String getIconPath(T element);
+
+	/** The default implementation forwards the request by ignoring the model element. */
+	@Override
+	public ImageDescriptor getIconImageDescriptor(T element) {
+		return imageDescriptorFromBundle(getPluginId(), getIconPath(element)).orElse(null);
+	}
+
 	/** {@inheritDoc} */
 	@Override
-	public final Image getIcon(T element) {
+	public Image getIcon(T element) {
 		ImageDescriptor descr = getIconImageDescriptor(element);
 		if(descr == null) {
 			return null;
@@ -79,21 +96,10 @@ public abstract class ModelElementHandlerBase<T extends EObject>
 		return iconImage;
 	}
 
-	/**
-	 * Returns the URL to the JavaFX icon image.
-	 * 
-	 * @param element
-	 *            the model element
-	 * @return the URL of the icon
-	 */
-	protected String getFXIconURL(T element) {
-		return "platform:/plugin/org.fortiss.tooling.kernel.ui/icons/fix.png";
-	}
-
 	/** {@inheritDoc} */
 	@Override
 	public Node getFXIcon(T element) {
-		String uri = getFXIconURL(element);
+		String uri = getResourceURI(getPluginId(), getIconPath(element));
 		if(uri == null) {
 			return null;
 		}
@@ -101,15 +107,6 @@ public abstract class ModelElementHandlerBase<T extends EObject>
 		return new ImageView(img);
 	}
 
-	/** The default implementation forwards the request by ignoring the model element. */
-	@Override
-	public ImageDescriptor getIconImageDescriptor(T element) {
-		return getIconImageDescriptor();
-	}
-
-	/** Get image descriptor by ignoring the model element. */
-	public abstract ImageDescriptor getIconImageDescriptor();
-
 	/** {@inheritDoc} */
 	@Override
 	public List<EObject> getSubnodes(T element) {
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/.ratings b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/.ratings
index 87159f543..ade7805db 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/.ratings
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/.ratings
@@ -10,6 +10,6 @@ KernelModelElementUtils.java fded09befe7e543fc04ea5184ffc1c8a309d7a66 GREEN
 KernelServiceBasedModelListenerBase.java 8d916509ae75f7118ce72564ee715c46556fb709 GREEN
 LoggingUtils.java 0e0aa5d466d80ea29cfc7e91178b23a5cdd4ddf7 GREEN
 PrototypesUtils.java ec75bed75cfc5103f1f38e3a29df86f729428775 GREEN
-ResourceUtils.java 7f4941a83115dfbed75eb58b79c0a372fe1cbb94 GREEN
+ResourceUtils.java 1388e642ceeead4c8852e7ae39c6d57a362d4845 YELLOW
 TransformationUtils.java 552d3a9d56d34450be781af828efe0b8aa5d359e GREEN
 UniqueIDUtils.java 665955b1790c1bd1c2087e23114da920bfec2265 GREEN
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/ResourceUtils.java b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/ResourceUtils.java
index 7f4941a83..1388e642c 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/ResourceUtils.java
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/utils/ResourceUtils.java
@@ -240,22 +240,23 @@ public final class ResourceUtils {
 	 * 
 	 * @param pluginId
 	 *            The plugin ID
-	 * @param absPath
+	 * @param path
 	 *            The absolute path in the plugin
 	 * @return a resource URI
 	 */
-	public static String getResourceURI(String pluginId, String absPath) {
+	public static String getResourceURI(String pluginId, String path) {
 		if(pluginId == null) {
 			throw new NullPointerException("The plugin ID must not be null!");
 		}
-		if(absPath == null) {
+		if(path == null) {
 			throw new NullPointerException("The path must not be null!");
 		}
-		if(!absPath.startsWith("/")) {
-			throw new IllegalArgumentException("The path is not a absolute path.");
+
+		if(!pluginId.endsWith("/") && !path.startsWith("/")) {
+			path = "/" + path;
 		}
 
-		return "platform:/plugin/" + pluginId + absPath;
+		return "platform:/plugin/" + pluginId + path;
 	}
 
 	/**
-- 
GitLab