From a0ac5dbc6e3bce3edac320fa9bf18fc4ed65138f Mon Sep 17 00:00:00 2001
From: Florian Hoelzl <hoelzl@fortiss.org>
Date: Mon, 2 May 2016 12:31:50 +0000
Subject: [PATCH] Added prototype and navigator new menu whitelisting during
 tutorial runs. refs 2567

---
 .../ui/internal/views/NavigatorNewMenu.java   |  7 ++++++
 .../extension/ITutorialWhitelistProvider.java |  6 ++++-
 .../kernel/internal/PrototypeService.java     | 21 +++++++++++++----
 .../kernel/internal/TutorialService.java      | 23 +++++++++++++++----
 .../kernel/service/ITutorialService.java      |  7 +++++-
 5 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorNewMenu.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorNewMenu.java
index d123c9f3d..a1ddee8c8 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorNewMenu.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorNewMenu.java
@@ -27,6 +27,7 @@ import org.fortiss.tooling.kernel.extension.data.Prototype;
 import org.fortiss.tooling.kernel.service.ICommandStackService;
 import org.fortiss.tooling.kernel.service.IElementCompositorService;
 import org.fortiss.tooling.kernel.service.IPrototypeService;
+import org.fortiss.tooling.kernel.service.ITutorialService;
 import org.fortiss.tooling.kernel.ui.ToolingKernelUIActivator;
 import org.fortiss.tooling.kernel.ui.extension.IContextMenuContributor;
 import org.fortiss.tooling.kernel.ui.extension.IModelElementHandler;
@@ -134,6 +135,12 @@ public class NavigatorNewMenu implements IContextMenuContributor {
 		 * prototype can be added to it.
 		 */
 		public boolean prepare(EObject container, boolean expert) {
+			if(ITutorialService.INSTANCE.isTutorialActive()) {
+				if(!ITutorialService.INSTANCE.prototypeActive(prototype)) {
+					this.container = null;
+					return false;
+				}
+			}
 			IModelElementHandler<EObject> handler =
 					IModelElementHandlerService.INSTANCE.getModelElementHandler(prototype
 							.getPrototype());
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/ITutorialWhitelistProvider.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/ITutorialWhitelistProvider.java
index 5778975b9..86c03d00c 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/ITutorialWhitelistProvider.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/ITutorialWhitelistProvider.java
@@ -18,6 +18,7 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
 package org.fortiss.tooling.kernel.extension;
 
 import org.eclipse.emf.ecore.EObject;
+import org.fortiss.tooling.kernel.extension.data.Prototype;
 
 /**
  * Instances of this interface provide white lists for tool functions
@@ -30,7 +31,10 @@ import org.eclipse.emf.ecore.EObject;
  */
 public interface ITutorialWhitelistProvider {
 	/** Returns whether the given element should be displayed in the navigator tree. */
-	public boolean displayElementInNavigator(EObject element);
+	public boolean elementVisibleInNavigator(EObject element);
+
+	/** Returns whether the given prototype is active in this tutorial. */
+	public boolean prototypeActive(Prototype prototype);
 
 	// TODO: define other methods
 }
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/PrototypeService.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/PrototypeService.java
index 4908a4164..8e7ee92e8 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/PrototypeService.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/PrototypeService.java
@@ -18,6 +18,7 @@ $Id$
 package org.fortiss.tooling.kernel.internal;
 
 import static java.util.Collections.emptyList;
+import static org.conqat.lib.commons.collections.CollectionUtils.asUnmodifiable;
 import static org.fortiss.tooling.kernel.utils.ExtensionPointUtils.getBundle;
 import static org.fortiss.tooling.kernel.utils.ExtensionPointUtils.getConfigurationElements;
 import static org.fortiss.tooling.kernel.utils.ExtensionPointUtils.loadClass;
@@ -26,6 +27,7 @@ import static org.fortiss.tooling.kernel.utils.LoggingUtils.error;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import org.conqat.lib.commons.collections.CollectionUtils;
 import org.conqat.lib.commons.collections.UnmodifiableList;
@@ -42,6 +44,7 @@ import org.fortiss.tooling.kernel.introspection.KernelIntrospectionSystemService
 import org.fortiss.tooling.kernel.introspection.items.PrototypeServiceIntrospectionDetailsItem;
 import org.fortiss.tooling.kernel.service.IElementCompositorService;
 import org.fortiss.tooling.kernel.service.IPrototypeService;
+import org.fortiss.tooling.kernel.service.ITutorialService;
 import org.osgi.framework.Bundle;
 
 /**
@@ -106,17 +109,27 @@ public class PrototypeService implements IPrototypeService, IIntrospectiveKernel
 	/** {@inheritDoc} */
 	@Override
 	public UnmodifiableList<Prototype> getAllPrototypes() {
-		return getPrototypes(false);
+		if(ITutorialService.INSTANCE.isTutorialActive()) {
+			ITutorialService service = ITutorialService.INSTANCE;
+			return asUnmodifiable(getPrototypes(false).stream()
+					.filter(proto -> service.prototypeActive(proto)).collect(Collectors.toList()));
+		}
+		return asUnmodifiable(getPrototypes(false));
 	}
 
 	/** {@inheritDoc} */
 	@Override
 	public UnmodifiableList<Prototype> getPrimaryPrototypes() {
-		return getPrototypes(true);
+		if(ITutorialService.INSTANCE.isTutorialActive()) {
+			ITutorialService service = ITutorialService.INSTANCE;
+			return asUnmodifiable(getPrototypes(true).stream()
+					.filter(proto -> service.prototypeActive(proto)).collect(Collectors.toList()));
+		}
+		return asUnmodifiable(getPrototypes(true));
 	}
 
 	/** Returns prototypes including or excluding non-primaries. */
-	private UnmodifiableList<Prototype> getPrototypes(boolean primaryOnly) {
+	private List<Prototype> getPrototypes(boolean primaryOnly) {
 		List<Prototype> result = new ArrayList<Prototype>();
 		for(IPrototypeProvider provider : prototypeProviderList) {
 			for(Prototype p : provider.getPrototypes()) {
@@ -125,7 +138,7 @@ public class PrototypeService implements IPrototypeService, IIntrospectiveKernel
 				}
 			}
 		}
-		return CollectionUtils.asUnmodifiable(result);
+		return result;
 	}
 
 	/** {@inheritDoc} */
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/TutorialService.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/TutorialService.java
index e05d70180..75205aa61 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/TutorialService.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/TutorialService.java
@@ -24,6 +24,7 @@ import org.conqat.lib.commons.collections.UnmodifiableMap;
 import org.eclipse.emf.ecore.EObject;
 import org.fortiss.tooling.kernel.extension.ITutorialProvider;
 import org.fortiss.tooling.kernel.extension.ITutorialWhitelistProvider;
+import org.fortiss.tooling.kernel.extension.data.Prototype;
 import org.fortiss.tooling.kernel.extension.data.TutorialStep;
 import org.fortiss.tooling.kernel.service.IPersistencyService;
 import org.fortiss.tooling.kernel.service.ITutorialService;
@@ -36,7 +37,7 @@ import org.fortiss.tooling.kernel.service.ITutorialService;
  * @version $Rev: 18709 $
  * @ConQAT.Rating RED Hash:
  */
-public final class TutorialService implements ITutorialService, ITutorialWhitelistProvider {
+public final class TutorialService implements ITutorialService {
 
 	/** The collection of all defined tutorials. */
 	private Map<String, Class<? extends ITutorialProvider>> allTutorials = new HashMap<>();
@@ -104,15 +105,29 @@ public final class TutorialService implements ITutorialService, ITutorialWhiteli
 
 	/** {@inheritDoc} */
 	@Override
-	public boolean displayElementInNavigator(EObject element) {
+	public boolean elementVisibleInNavigator(EObject element) {
 		if(activeTutorial == null || activeStep == null) {
 			return false;
 		}
 		ITutorialWhitelistProvider global = activeTutorial.getGlobalWhitelistProvider();
-		if(global != null && global.displayElementInNavigator(element)) {
+		if(global != null && global.elementVisibleInNavigator(element)) {
 			return true;
 		}
 		ITutorialWhitelistProvider local = activeStep.getWhitelistProvider();
-		return local != null && local.displayElementInNavigator(element);
+		return local != null && local.elementVisibleInNavigator(element);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean prototypeActive(Prototype prototype) {
+		if(activeTutorial == null || activeStep == null) {
+			return false;
+		}
+		ITutorialWhitelistProvider global = activeTutorial.getGlobalWhitelistProvider();
+		if(global != null && global.prototypeActive(prototype)) {
+			return true;
+		}
+		ITutorialWhitelistProvider local = activeStep.getWhitelistProvider();
+		return local != null && local.prototypeActive(prototype);
 	}
 }
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/service/ITutorialService.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/service/ITutorialService.java
index 744f3a78b..52b5729c8 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/service/ITutorialService.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/service/ITutorialService.java
@@ -20,6 +20,7 @@ package org.fortiss.tooling.kernel.service;
 import java.util.Map;
 
 import org.fortiss.tooling.kernel.extension.ITutorialProvider;
+import org.fortiss.tooling.kernel.extension.ITutorialWhitelistProvider;
 import org.fortiss.tooling.kernel.extension.data.TutorialStep;
 import org.fortiss.tooling.kernel.internal.TutorialService;
 
@@ -27,13 +28,17 @@ import org.fortiss.tooling.kernel.internal.TutorialService;
  * The tutorial service provides pre-defined models with step-wise
  * example procedures for implementing educational tutorials to
  * the end-user.
+ * <P>
+ * This interface extends {@link ITutorialWhitelistProvider}, which delegates its method calls to
+ * the current {@link ITutorialProvider}'s global whitelist provider and the current
+ * {@link TutorialStep}'s local whitelist provider.
  * 
  * @author hoelzl
  * @author $Author: hoelzl $
  * @version $Rev: 18709 $
  * @ConQAT.Rating RED Hash:
  */
-public interface ITutorialService {
+public interface ITutorialService extends ITutorialWhitelistProvider {
 	/** Returns the singleton instance of the service. */
 	public static final ITutorialService INSTANCE = new TutorialService();
 
-- 
GitLab