From 2f0df7b470ce5daae44b68d429216158fa70d14f Mon Sep 17 00:00:00 2001
From: Florian Hoelzl <hoelzl@fortiss.org>
Date: Tue, 18 Feb 2020 12:23:55 +0100
Subject: [PATCH] Kernel: JavaFX tree viewer drag detection implemented.

Issue-Ref: 3921
Issue-Url: https://af3-developer.fortiss.org/issues/3907
Signed-off-by: Florian Hoelzl <hoelzl@fortiss.org>
---
 .../ui/javafx/control/treetableview/.ratings  |  4 +--
 .../DynamicTreeUIProviderBase.java            |  8 ++---
 .../treetableview/DynamicTreeViewer.java      | 33 ++++++++++++++++++-
 .../tooling/kernel/ui/internal/views/.ratings |  2 +-
 .../internal/views/ModelElementsViewFX.java   |  9 +++--
 .../fortiss/tooling/kernel/extension/.ratings |  3 +-
 .../kernel/extension/IPrototypeProvider.java  | 13 +++++++-
 .../tooling/kernel/extension/base/.ratings    |  4 +--
 .../extension/base/PrototypeProviderBase.java | 29 ++++++++++++++++
 .../fortiss/tooling/kernel/internal/.ratings  |  2 +-
 .../kernel/internal/PrototypeService.java     | 24 ++++++++++++++
 .../fortiss/tooling/kernel/service/.ratings   |  3 +-
 .../kernel/service/IPrototypeService.java     |  9 +++++
 13 files changed, 122 insertions(+), 21 deletions(-)

diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings
index e06b4549d..d56df62c9 100644
--- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings
+++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/.ratings
@@ -2,7 +2,7 @@ DynamicTreeContentProviderBase.java e801da995a1b6e5a1b757247c1638bafb6073e6d GRE
 DynamicTreeItem.java 75dc5534b119ffdb3c10a65810c2a0f330b7955e GREEN
 DynamicTreeTableUIProviderBase.java 7bfc1395283d3dc10026aff5e2e65df88d25f3a7 GREEN
 DynamicTreeTableViewer.java 43757359b3071192ae79710bcbc0e9577bb6f62d GREEN
-DynamicTreeUIProviderBase.java 3f4a9f0e861ce1c8c122feb487388317464752a1 YELLOW
-DynamicTreeViewer.java 933021a9f4cfad90f85cabe3a1be278391851166 YELLOW
+DynamicTreeUIProviderBase.java 13b5f54f5a9c19a6c53a5856824fa64bf7460605 YELLOW
+DynamicTreeViewer.java 59da052c7c34ad526d3f81db9939676fd6050964 YELLOW
 DynamicTreeViewerBase.java 47124c847de322a0ae26eb7a114f85ce4bd02d7e GREEN
 IDoubleClickHandler.java 447f7769dead9a106b3ea3139ef0da51eb0b9a89 GREEN
diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeUIProviderBase.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeUIProviderBase.java
index 3f4a9f0e8..13b5f54f5 100644
--- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeUIProviderBase.java
+++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeUIProviderBase.java
@@ -15,7 +15,6 @@ package org.fortiss.tooling.common.ui.javafx.control.treetableview;
 
 import javafx.scene.Node;
 import javafx.scene.control.ContextMenu;
-import javafx.scene.input.MouseEvent;
 
 /**
  * This UI provider is responsible to return the label, the icon, and the context menu for each cell
@@ -50,12 +49,11 @@ public abstract class DynamicTreeUIProviderBase<T> {
 	}
 
 	/**
-	 * @param event
-	 *            the mouse event, which caused the drag detection
 	 * @param element
 	 *            the tree element, which was dragged
+	 * @return the content of the clipboard or {@code null} if the drag should be ignored.
 	 */
-	public void dragDetected(MouseEvent event, T element) {
-		// ignored
+	public Object getDragClipboardContent(T element) {
+		return null;
 	}
 }
diff --git a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeViewer.java b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeViewer.java
index 933021a9f..59da052c7 100644
--- a/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeViewer.java
+++ b/org.fortiss.tooling.common.ui/src/org/fortiss/tooling/common/ui/javafx/control/treetableview/DynamicTreeViewer.java
@@ -23,8 +23,13 @@ import javafx.scene.control.ContextMenu;
 import javafx.scene.control.TreeCell;
 import javafx.scene.control.TreeItem;
 import javafx.scene.control.TreeView;
+import javafx.scene.input.ClipboardContent;
+import javafx.scene.input.DataFormat;
+import javafx.scene.input.DragEvent;
+import javafx.scene.input.Dragboard;
 import javafx.scene.input.MouseButton;
 import javafx.scene.input.MouseEvent;
+import javafx.scene.input.TransferMode;
 
 /**
  * A JavaFX {@link TreeView} based on a {@link DynamicTreeContentProviderBase} and a
@@ -139,7 +144,13 @@ public final class DynamicTreeViewer<T> extends DynamicTreeViewerBase<T> {
 						this.setText(uiProvider.getLabel(item));
 						this.setGraphic(uiProvider.getIconNode(item));
 						this.setOnDragDetected(evt -> {
-							uiProvider.dragDetected(evt, item);
+							dragDetected(evt, this, item);
+						});
+						this.setOnDragOver(evt -> {
+							dragOver(evt, item);
+						});
+						this.setOnDragDropped(evt -> {
+							dragDropped(evt, item);
 						});
 					} else {
 						this.setText(null);
@@ -152,6 +163,26 @@ public final class DynamicTreeViewer<T> extends DynamicTreeViewerBase<T> {
 		});
 	}
 
+	/** Called when an item in the tree is dragged. */
+	private void dragDetected(MouseEvent evt, TreeCell<T> cell, T item) {
+		Object cbContent = uiProvider.getDragClipboardContent(item);
+		if(cbContent != null) {
+			Dragboard db = cell.startDragAndDrop(TransferMode.MOVE);
+			ClipboardContent content = new ClipboardContent();
+			content.put(new DataFormat("JAVA_OBJECT"), cbContent);
+			db.setContent(content);
+			evt.consume();
+		}
+	}
+
+	private void dragOver(DragEvent evt, T item) {
+
+	}
+
+	private void dragDropped(DragEvent evt, T item) {
+
+	}
+
 	/** Expands the tree to the given item. */
 	public void expandItem(TreeItem<T> item) {
 		while(item.getParent() != null) {
diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/views/.ratings b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/views/.ratings
index 5aba20d5b..98884b1f9 100644
--- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/views/.ratings
+++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/views/.ratings
@@ -5,7 +5,7 @@ LibraryViewDragSourceAdapter.java 56ef61b214ef5d6cb5b751791a92158bda0391ec GREEN
 LinkWithEditorPartListener.java c5ab74424378e7b158a805c4dd14fc03c8abeded GREEN
 MarkerViewContentProvider.java 4cb1192baebe21bca951c439c163d0c171512515 GREEN
 MarkerViewPart.java cbb650271b6877af205421b7cb11f930440a7ef9 GREEN
-ModelElementsViewFX.java f08b6ddb8aab338c3f2a53b899a8189e4e005d63 RED
+ModelElementsViewFX.java 77b4a234b4d2bd37f532373265ee7618f7dbafaa YELLOW
 NavigatorNewMenu.java a35e391960d1dacbe7f77982e53e1891e9382d5a GREEN
 NavigatorTreeContentComparator.java d9f1354cfdff78b104b28887d2397e5ca0e9755b GREEN
 NavigatorTreeContentProvider.java 1fbe97bebf3805cc1af190cecd784fc1cfd12306 GREEN
diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/views/ModelElementsViewFX.java b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/views/ModelElementsViewFX.java
index f08b6ddb8..77b4a234b 100644
--- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/views/ModelElementsViewFX.java
+++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/views/ModelElementsViewFX.java
@@ -47,7 +47,6 @@ import org.fortiss.tooling.kernel.ui.service.IModelElementHandlerService;
 import javafx.scene.Node;
 import javafx.scene.Scene;
 import javafx.scene.control.TextField;
-import javafx.scene.input.MouseEvent;
 import javafx.scene.layout.BorderPane;
 
 /**
@@ -242,8 +241,12 @@ public final class ModelElementsViewFX extends FXViewPart {
 
 		/** {@inheritDoc} */
 		@Override
-		public void dragDetected(MouseEvent event, Object element) {
-			System.out.println("Detected drag on " + element.toString());
+		public Object getDragClipboardContent(Object element) {
+			if(element instanceof Prototype) {
+				Prototype proto = (Prototype)element;
+				return IPrototypeService.getInstance().getUniquePrototypeID(proto);
+			}
+			return null;
 		}
 	}
 
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/.ratings b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/.ratings
index b2f23c1f2..cf8eca638 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/.ratings
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/.ratings
@@ -1,5 +1,4 @@
 IConnectionCompositor.java 82188750593a08df75a5f21fd91d4b41f72593fd GREEN
-IConstraint.java e5e95efa8b06b30d6feffd9a2033e5caa3e710e9 GREEN
 IConstraintChecker.java a6d76e332ece919adb990397dd5ef6aaa542ea7d GREEN
 IEclipseResourcePostLoadProvider.java e842bb7485ef27917092ffc60af8a57e475d01d6 GREEN
 IEclipseResourceStorageLocationProvider.java 0ab7f304d52a9d86f01f66e308e9a7ca420497ba GREEN
@@ -7,7 +6,7 @@ IElementCompositor.java 5b0ab1732f71b3f8467e0276c844f0dd549e191f GREEN
 ILibraryElementHandler.java 00ef5b25c63b8570006e6f6748aed0da1f33a5f1 GREEN
 ILogMessageHandler.java 9ab53e836a095ef00fd84ecc0375167edf593b46 GREEN
 IMigrationProvider.java 241bfd8594dfb86ce0f89dc95b43662f52d9e450 GREEN
-IPrototypeProvider.java d5e3dbae19b5654caf28b81da6b1609d3c12be12 GREEN
+IPrototypeProvider.java 5a135f31c5b65a9ebd3432f6d65fd790992b724b YELLOW
 IStorageProvider.java d9b14cdd254d0c956dc5715c1c4d4d955a705dd5 GREEN
 ITransformationProvider.java a4ee2ea08720bb2fce29806062eb01499bb5071e GREEN
 ITutorialProvider.java 0f649c7856fc0e13c5dd71e116ee8bf4ba372216 GREEN
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/IPrototypeProvider.java b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/IPrototypeProvider.java
index d5e3dbae1..5a135f31c 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/IPrototypeProvider.java
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/IPrototypeProvider.java
@@ -33,10 +33,21 @@ import org.fortiss.tooling.kernel.service.IPrototypeService;
  * @author hoelzl
  */
 public interface IPrototypeProvider {
-
 	/** Returns the prototypes provided by this provider. */
 	List<Prototype> getPrototypes();
 
 	/** Returns the categories of prototypes. */
 	List<PrototypeCategory> getCategories();
+
+	/**
+	 * Returns a unique ID for the given {@link Prototype} or {@code null} if the prototype does not
+	 * stem from this provider.
+	 */
+	String getUniqueID(Prototype prototype);
+
+	/**
+	 * Returns the {@link Prototype} for the given unique ID or {@code null} if the unique ID does
+	 * not correspond to any prototype of this provider.
+	 */
+	Prototype getPrototypeByID(String id);
 }
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/base/.ratings b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/base/.ratings
index 05ded098f..9e887f8ff 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/base/.ratings
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/base/.ratings
@@ -1,10 +1,8 @@
-ConstraintBases.java 93b4743cc1f5e2677635e644663ba934ef7f7f4e GREEN
 ConstraintCheckerBase.java 92de33f1a3071dc6d66a55d8c10f5d5cc967a4fc GREEN
 ConstraintViolationBase.java ec66973ab2183623f0cd4a85c59c886dddad6cf6 GREEN
 DialogMessage.java 8420640e999e4fb15fa644333e5d71e1d16c2559 GREEN
 ElementCompositorBase.java 7a445e5adde11878fe0515baca8b915287149b28 GREEN
 MultiViolationConstraintCheckerBase.java 30886a94c99cf8948f64401b1db821abe06e1e6c GREEN
-PrototypeProviderBase.java ebcd1794c3798b9899a620b01fd5aa0402129423 GREEN
-RemoveDeprecatedArtifactsMigrationProviderBase.java 4a1b676223b7ec53259a26d1a671b11ff2f911e2 GREEN
+PrototypeProviderBase.java 44545d964c2fdbd05ee79ce941205ed1b8fc5864 YELLOW
 TransformationContextChainBase.java 1ef37880ab275778c563928e80ba378fec964cb6 GREEN
 TransformationProviderBase.java 9e91100cc1f2c8fbd8d41af55aedfea34e02ff71 GREEN
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/base/PrototypeProviderBase.java b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/base/PrototypeProviderBase.java
index ebcd1794c..44545d964 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/base/PrototypeProviderBase.java
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/extension/base/PrototypeProviderBase.java
@@ -111,4 +111,33 @@ public abstract class PrototypeProviderBase implements IPrototypeProvider {
 		result.addAll(categories.values());
 		return result;
 	}
+
+	/** {@inheritDoc} */
+	@Override
+	public final String getUniqueID(Prototype prototype) {
+		if(!prototypes.contains(prototype)) {
+			return null;
+		}
+		return this.getClass().getName() + '/' + prototype.getName();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public final Prototype getPrototypeByID(String id) {
+		String cname = this.getClass().getName();
+		if(!id.startsWith(cname)) {
+			return null;
+		}
+		id = id.substring(cname.length());
+		if(!id.startsWith("/")) {
+			return null;
+		}
+		id = id.substring(1);
+		for(Prototype p : prototypes) {
+			if(p.getName().equals(id)) {
+				return p;
+			}
+		}
+		return null;
+	}
 }
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/.ratings b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/.ratings
index 95a442ad4..d3403fdc2 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/.ratings
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/.ratings
@@ -8,7 +8,7 @@ LibraryService.java d22671ba820466062852c15873698adf28960d94 GREEN
 LoggingService.java da784259f7b456b54bf75c41ec268f64919ce78d GREEN
 MigrationService.java 2f800eac9793aa736089a802bbfc2c4c1c09770d GREEN
 PersistencyService.java 103eef642c038ef63fa49b743d803aaa3fea2724 GREEN
-PrototypeService.java 18c3db05ab11f189a9711bf241c3c7f35c954a9e GREEN
+PrototypeService.java cf8e6fa96ba9c2f65b24400054ed68e93238a975 YELLOW
 ToolingKernelInternal.java f6e7114825748683c7f1d040b41ab854a6c4d79b GREEN
 TransformationService.java 3cdb86fe920158f93cd9466c6ef9697b2dd8ca7f GREEN
 TutorialService.java 675d3f365ce062869f86baa3779d50687674bda0 GREEN
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/PrototypeService.java b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/PrototypeService.java
index 18c3db05a..cf8e6fa96 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/PrototypeService.java
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/internal/PrototypeService.java
@@ -162,6 +162,30 @@ public class PrototypeService implements IPrototypeService, IIntrospectiveKernel
 		return result;
 	}
 
+	/** {@inheritDoc} */
+	@Override
+	public String getUniquePrototypeID(Prototype prototype) {
+		for(IPrototypeProvider prov : prototypeProviderList) {
+			String uid = prov.getUniqueID(prototype);
+			if(uid != null) {
+				return uid;
+			}
+		}
+		return null;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public Prototype getPrototypeByUniqueID(String id) {
+		for(IPrototypeProvider prov : prototypeProviderList) {
+			Prototype proto = prov.getPrototypeByID(id);
+			if(proto != null) {
+				return proto;
+			}
+		}
+		return null;
+	}
+
 	/** {@inheritDoc} */
 	@Override
 	public List<Prototype> getComposablePrototypes(Class<? extends EObject> modelElementType) {
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/service/.ratings b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/service/.ratings
index 6c4b249d4..66f5be3ce 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/service/.ratings
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/service/.ratings
@@ -1,7 +1,6 @@
 ICommandStackService.java 678dcd1a6ab435ed0870fa2a9ec48ce47f25a187 GREEN
 IConnectionCompositorService.java 0cdf4568b2cd3e95ea195df90a84699eff36442b GREEN
 IConstraintCheckerService.java 291e53297aaea213e07e78f63350938ee2c7b155 GREEN
-IConstraintService.java d55c3e25b8cef7e2f7c555adec7ff31e28449370 GREEN
 IEclipseResourceStorageService.java b1155ca15cd9474d4d533d6cb2725e8a22040ec9 GREEN
 IElementCompositorService.java acd462ec15f3bcc247b544b46ceebee971fe1408 GREEN
 IKernelIntrospectionSystemService.java 7005c3acb4c6f978729d93279c595765e94e38eb GREEN
@@ -9,6 +8,6 @@ ILibraryService.java e1e2ec72b1db892478ed20c7fbb7dcf94472a1cd GREEN
 ILoggingService.java 1ee9723af5a79299249e8db345e8419f814ff6d1 GREEN
 IMigrationService.java 7cfa6268b97f0c38c838905791e065655c6d6a04 GREEN
 IPersistencyService.java 2b2eeb329e3040e75f4352d9c374855583e27538 GREEN
-IPrototypeService.java 47f5ab07c17d5da704860378a49493fa83b3e20b GREEN
+IPrototypeService.java 3f11fc887f729ab739736081af9e663d67d3131f YELLOW
 ITransformationService.java 71f175e94d7257713cb14c8148de5a309b03788a GREEN
 ITutorialService.java 22a490516e38536203b1edd32711b615b77a4728 GREEN
diff --git a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/service/IPrototypeService.java b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/service/IPrototypeService.java
index 47f5ab07c..3f11fc887 100644
--- a/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/service/IPrototypeService.java
+++ b/org.fortiss.tooling.kernel/src/org/fortiss/tooling/kernel/service/IPrototypeService.java
@@ -51,6 +51,15 @@ public interface IPrototypeService {
 	/** Returns the list of primary prototypes registered any provider. */
 	UnmodifiableList<Prototype> getPrimaryPrototypes();
 
+	/** Returns a unique ID for the given {@link Prototype}. */
+	String getUniquePrototypeID(Prototype prototype);
+
+	/**
+	 * Returns the {@link Prototype} for the given unique ID or {@code null} if the unique ID does
+	 * not correspond to any prototype of any provider.
+	 */
+	Prototype getPrototypeByUniqueID(String id);
+
 	/**
 	 * Returns the list of all prototypes composable with the given model
 	 * element class.
-- 
GitLab