From 0a80ea3df3626ef19d7680802e14344a829f4c99 Mon Sep 17 00:00:00 2001
From: Alexander Diewald <diewald@fortiss.org>
Date: Wed, 11 Dec 2019 15:15:35 +0100
Subject: [PATCH] Editors: Allow editors consuming the model type on
 construction

Issue-Ref: 3883
Issue-Url: https://af3-developer.fortiss.org/issues/3883
Signed-off-by: Alexander Diewald <diewald@fortiss.org>
---
 .../kernel/ui/internal/editor/.ratings        |  2 +-
 .../editor/ExtendableMultiPageEditor.java     | 21 ++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/editor/.ratings b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/editor/.ratings
index 7b672cb40..c25fd1ce6 100644
--- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/editor/.ratings
+++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/editor/.ratings
@@ -1,5 +1,5 @@
 ActionBarContributor.java 18d9db3744c5381cca8b6823b5f7bc18183a1cfa GREEN
-ExtendableMultiPageEditor.java f8eb6fdc347098fb03e776f23fab61109aa55d6e GREEN
+ExtendableMultiPageEditor.java e06a7746f2bdcd2dfa279ae87e0578e0ee5046b2 YELLOW
 IActionContributingEditor.java 4aa7496d67822de919a8cf0af0ddaafc61bf2919 GREEN
 ModelElementEditorInput.java e269eff5d992d375a646e54d048f1f0efc6144dd GREEN
 TutorialStepUIEditor.java 9eadc96c302b5131ff4cc3715777718fa06ec7e8 GREEN
diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/editor/ExtendableMultiPageEditor.java b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/editor/ExtendableMultiPageEditor.java
index f8eb6fdc3..e06a7746f 100644
--- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/editor/ExtendableMultiPageEditor.java
+++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/internal/editor/ExtendableMultiPageEditor.java
@@ -18,6 +18,7 @@ package org.fortiss.tooling.kernel.ui.internal.editor;
 import static java.util.Collections.sort;
 import static org.conqat.ide.commons.ui.logging.LoggingUtils.error;
 
+import java.lang.reflect.Constructor;
 import java.util.Collection;
 import java.util.Comparator;
 import java.util.EventObject;
@@ -196,7 +197,8 @@ public class ExtendableMultiPageEditor extends MultiPageEditorPart
 				Class<? extends IEditorPart> editorClass =
 						editorBinding.getEditorClass(editedObject);
 				if(editorClass != null) {
-					IEditorPart editorPart = editorClass.newInstance();
+					Class<? extends EObject> inputType = editedObject.getClass();
+					IEditorPart editorPart = constructEditorPart(editorClass, inputType);
 					addPage(editorPart, getEditorInput());
 					setPageText(pageIndex++, editorBinding.getLabel(editedObject));
 				}
@@ -206,6 +208,23 @@ public class ExtendableMultiPageEditor extends MultiPageEditorPart
 		}
 	}
 
+	/**
+	 * Constructs an {@link IEditorPart} instance of the given {@code editorClass}.
+	 * {@link IEditorPart}s that take the input model element type as a parameter are preferred over
+	 * no-arg constructors.
+	 */
+	protected IEditorPart constructEditorPart(Class<? extends IEditorPart> editorClass,
+			Class<? extends EObject> inputType) throws Exception {
+		try {
+			Constructor<? extends IEditorPart> ctor = editorClass.getConstructor(Class.class);
+			return ctor.newInstance(inputType);
+		} catch(NoSuchMethodException | SecurityException e) {
+			// Fallback for no-arg constructors.
+			Constructor<? extends IEditorPart> ctor = editorClass.getConstructor();
+			return ctor.newInstance();
+		}
+	}
+
 	/** {@inheritDoc} */
 	@Override
 	public void doSave(IProgressMonitor monitor) {
-- 
GitLab