From 215a358dc1ed155257ae6777bdd904a19883e9e1 Mon Sep 17 00:00:00 2001
From: Florian Hoelzl <hoelzl@fortiss.org>
Date: Mon, 2 May 2016 14:12:08 +0000
Subject: [PATCH] Extended and implemented first filtering functions in kernel
 services. refs 2567

---
 .../ui/internal/ContextMenuService.java       | 102 +++++++++++++-----
 .../extension/ITutorialWhitelistProvider.java |  17 ++-
 .../base/TutorialBlacklistAllProvider.java    |  65 +++++++++++
 .../base/TutorialWhitelistAllProvider.java    |  65 +++++++++++
 .../kernel/internal/TutorialService.java      |  49 +++++----
 .../kernel/service/ITutorialService.java      |   3 +-
 6 files changed, 253 insertions(+), 48 deletions(-)
 create mode 100644 org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/base/TutorialBlacklistAllProvider.java
 create mode 100644 org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/base/TutorialWhitelistAllProvider.java

diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/ContextMenuService.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/ContextMenuService.java
index c83e90b5f..fbae3d7e1 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/ContextMenuService.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/ContextMenuService.java
@@ -21,6 +21,7 @@ import static java.util.Collections.emptyList;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import org.eclipse.core.runtime.IConfigurationElement;
 import org.eclipse.emf.ecore.EObject;
@@ -35,6 +36,7 @@ import org.fortiss.tooling.kernel.introspection.IIntrospectionDetailsItem;
 import org.fortiss.tooling.kernel.introspection.IIntrospectionItem;
 import org.fortiss.tooling.kernel.introspection.IIntrospectiveKernelService;
 import org.fortiss.tooling.kernel.introspection.KernelIntrospectionSystemService;
+import org.fortiss.tooling.kernel.service.ITutorialService;
 import org.fortiss.tooling.kernel.ui.extension.IContextMenuContributor;
 import org.fortiss.tooling.kernel.ui.extension.IContextMenuMultiSelectionContributor;
 import org.fortiss.tooling.kernel.ui.extension.data.ContextMenuContextProvider;
@@ -142,49 +144,95 @@ public class ContextMenuService implements IContextMenuService, IIntrospectiveKe
 
 			addContributions(menu, contextProvider);
 
-			IActionService.INSTANCE.addGlobalDefaultActionSectionToMenu(menu);
+			if(!ITutorialService.INSTANCE.isTutorialActive() ||
+					ITutorialService.INSTANCE.globalDefaultActionsVisible()) {
+				IActionService.INSTANCE.addGlobalDefaultActionSectionToMenu(menu);
+			}
 		}
 	}
 
 	/** Adds registered contributions to the given menu. */
 	private void addContributions(IMenuManager menu, ContextMenuContextProvider contextProvider) {
-
 		List<EObject> selection = contextProvider.getSelectedModelElementList();
 		if(selection == null) {
 			// do nothing
 		} else if(selection.size() == 1) {
-			EObject selectionElem = contextProvider.getSelectedModelElement();
-			for(IContextMenuContributor contributor : providerList) {
+			singleSelectionElementContributions(menu, contextProvider);
+		} else if(selection.size() > 1) {
+			addMultiSelectionContributions(menu, contextProvider, selection);
+		}
+	}
+
+	/** Populates the menu with contributions to a single model element selection. */
+	private void singleSelectionElementContributions(IMenuManager menu,
+			ContextMenuContextProvider contextProvider) {
+		EObject selectionElem = contextProvider.getSelectedModelElement();
+		for(IContextMenuContributor contributor : providerList) {
+			String menuSectionID = contributor.getMenuSectionID();
+			if(menuSectionID == null) {
+				menuSectionID = IWorkbenchActionConstants.MB_ADDITIONS;
+			}
+
+			// get contributions from contributor
+			List<IContributionItem> items =
+					contributor.getContributedItems(selectionElem, contextProvider);
+			if(items == null) {
+				continue;
+			}
+
+			// active tutorial may filter contributions
+			ITutorialService service = ITutorialService.INSTANCE;
+			if(service.isTutorialActive()) {
+				items =
+						items.stream()
+								.filter(i -> service.contextMenuContributionVisible(selectionElem,
+										i)).collect(Collectors.toList());
+			}
+			if(items == null) {
+				continue;
+			}
+
+			// populate menu
+			for(IContributionItem item : items) {
+				menu.appendToGroup(menuSectionID, item);
+			}
+		}
+	}
+
+	/** Populates the menu with contributions to multiple model element selection. */
+	private void addMultiSelectionContributions(IMenuManager menu,
+			ContextMenuContextProvider contextProvider, List<EObject> selection) {
+		for(IContextMenuContributor contributor : providerList) {
+			if(contributor instanceof IContextMenuMultiSelectionContributor) {
+				IContextMenuMultiSelectionContributor multicontributor =
+						(IContextMenuMultiSelectionContributor)contributor;
 				String menuSectionID = contributor.getMenuSectionID();
 				if(menuSectionID == null) {
 					menuSectionID = IWorkbenchActionConstants.MB_ADDITIONS;
 				}
-				List<IContributionItem> l =
-						contributor.getContributedItems(selectionElem, contextProvider);
-				if(l == null) {
+
+				// get contributions from contributor
+				List<IContributionItem> items =
+						multicontributor.getContributedItems(selection, contextProvider);
+				if(items == null) {
 					continue;
 				}
-				for(IContributionItem item : l) {
-					menu.appendToGroup(menuSectionID, item);
+
+				// active tutorial may filter contributions
+				ITutorialService service = ITutorialService.INSTANCE;
+				if(service.isTutorialActive()) {
+					items =
+							items.stream()
+									.filter(i -> service.contextMenuContributionVisible(selection,
+											i)).collect(Collectors.toList());
 				}
-			}
-		} else if(selection.size() > 1) {
-			for(IContextMenuContributor contributor : providerList) {
-				if(contributor instanceof IContextMenuMultiSelectionContributor) {
-					IContextMenuMultiSelectionContributor multicontributor =
-							(IContextMenuMultiSelectionContributor)contributor;
-					String menuSectionID = contributor.getMenuSectionID();
-					if(menuSectionID == null) {
-						menuSectionID = IWorkbenchActionConstants.MB_ADDITIONS;
-					}
-					List<IContributionItem> l =
-							multicontributor.getContributedItems(selection, contextProvider);
-					if(l == null) {
-						continue;
-					}
-					for(IContributionItem item : l) {
-						menu.appendToGroup(menuSectionID, item);
-					}
+				if(items == null) {
+					continue;
+				}
+
+				// populate menu
+				for(IContributionItem item : items) {
+					menu.appendToGroup(menuSectionID, item);
 				}
 			}
 		}
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 86c03d00c..3f8b1c908 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
@@ -17,6 +17,8 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.kernel.extension;
 
+import java.util.List;
+
 import org.eclipse.emf.ecore.EObject;
 import org.fortiss.tooling.kernel.extension.data.Prototype;
 
@@ -30,11 +32,24 @@ import org.fortiss.tooling.kernel.extension.data.Prototype;
  * @ConQAT.Rating RED Hash:
  */
 public interface ITutorialWhitelistProvider {
+	// FIXME: not used yet
 	/** Returns whether the given element should be displayed in the navigator tree. */
 	public boolean elementVisibleInNavigator(EObject element);
 
-	/** Returns whether the given prototype is active in this tutorial. */
+	/**
+	 * Returns whether the given prototype is active in this tutorial. Note that active
+	 * prototypes may still be filtered by other methods of this whitelist provider, e.g.,
+	 * {@link #contextMenuContributionVisible(EObject, Object)}.
+	 */
 	public boolean prototypeActive(Prototype prototype);
 
+	/** Returns whether the global default actions are visible in the context menu. */
+	public boolean globalDefaultActionsVisible();
+
+	/** Returns whether the given context menu contribution is visible. */
+	public boolean contextMenuContributionVisible(EObject element, Object contribution);
+
+	/** Returns whether the given context menu contribution is visible. */
+	public boolean contextMenuContributionVisible(List<EObject> element, Object contribution);
 	// TODO: define other methods
 }
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/base/TutorialBlacklistAllProvider.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/base/TutorialBlacklistAllProvider.java
new file mode 100644
index 000000000..c235e3691
--- /dev/null
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/base/TutorialBlacklistAllProvider.java
@@ -0,0 +1,65 @@
+/*--------------------------------------------------------------------------+
+$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
+|                                                                          |
+| Copyright 2016 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.kernel.extension.base;
+
+import java.util.List;
+
+import org.eclipse.emf.ecore.EObject;
+import org.fortiss.tooling.kernel.extension.ITutorialWhitelistProvider;
+import org.fortiss.tooling.kernel.extension.data.Prototype;
+
+/**
+ * An {@link ITutorialWhitelistProvider} that blacklists everything.
+ * 
+ * @author hoelzl
+ * @author $Author: hoelzl $
+ * @version $Rev: 18709 $
+ * @ConQAT.Rating RED Hash:
+ */
+public class TutorialBlacklistAllProvider implements ITutorialWhitelistProvider {
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean elementVisibleInNavigator(EObject element) {
+		return false;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean prototypeActive(Prototype prototype) {
+		return false;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean globalDefaultActionsVisible() {
+		return false;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean contextMenuContributionVisible(EObject element, Object contribution) {
+		return false;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean contextMenuContributionVisible(List<EObject> element, Object contribution) {
+		return false;
+	}
+}
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/base/TutorialWhitelistAllProvider.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/base/TutorialWhitelistAllProvider.java
new file mode 100644
index 000000000..c2d696c74
--- /dev/null
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/extension/base/TutorialWhitelistAllProvider.java
@@ -0,0 +1,65 @@
+/*--------------------------------------------------------------------------+
+$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
+|                                                                          |
+| Copyright 2016 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.kernel.extension.base;
+
+import java.util.List;
+
+import org.eclipse.emf.ecore.EObject;
+import org.fortiss.tooling.kernel.extension.ITutorialWhitelistProvider;
+import org.fortiss.tooling.kernel.extension.data.Prototype;
+
+/**
+ * An {@link ITutorialWhitelistProvider} that whitelists everything.
+ * 
+ * @author hoelzl
+ * @author $Author: hoelzl $
+ * @version $Rev: 18709 $
+ * @ConQAT.Rating RED Hash:
+ */
+public class TutorialWhitelistAllProvider implements ITutorialWhitelistProvider {
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean elementVisibleInNavigator(EObject element) {
+		return true;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean prototypeActive(Prototype prototype) {
+		return true;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean globalDefaultActionsVisible() {
+		return true;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean contextMenuContributionVisible(EObject element, Object contribution) {
+		return true;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean contextMenuContributionVisible(List<EObject> element, Object contribution) {
+		return true;
+	}
+}
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 75205aa61..b5ac2cb61 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
@@ -18,12 +18,12 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
 package org.fortiss.tooling.kernel.internal;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 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;
@@ -106,28 +106,39 @@ public final class TutorialService implements ITutorialService {
 	/** {@inheritDoc} */
 	@Override
 	public boolean elementVisibleInNavigator(EObject element) {
-		if(activeTutorial == null || activeStep == null) {
-			return false;
-		}
-		ITutorialWhitelistProvider global = activeTutorial.getGlobalWhitelistProvider();
-		if(global != null && global.elementVisibleInNavigator(element)) {
-			return true;
-		}
-		ITutorialWhitelistProvider local = activeStep.getWhitelistProvider();
-		return local != null && local.elementVisibleInNavigator(element);
+		return activeTutorial.getGlobalWhitelistProvider().elementVisibleInNavigator(element) ||
+				activeStep.getWhitelistProvider().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);
+		return activeTutorial.getGlobalWhitelistProvider().prototypeActive(prototype) ||
+				activeStep.getWhitelistProvider().prototypeActive(prototype);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean globalDefaultActionsVisible() {
+		return activeTutorial.getGlobalWhitelistProvider().globalDefaultActionsVisible() ||
+				activeStep.getWhitelistProvider().globalDefaultActionsVisible();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean contextMenuContributionVisible(EObject element, Object contribution) {
+		return activeTutorial.getGlobalWhitelistProvider().contextMenuContributionVisible(element,
+				contribution) ||
+				activeStep.getWhitelistProvider().contextMenuContributionVisible(element,
+						contribution);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public boolean contextMenuContributionVisible(List<EObject> element, Object contribution) {
+		return activeTutorial.getGlobalWhitelistProvider().contextMenuContributionVisible(element,
+				contribution) ||
+				activeStep.getWhitelistProvider().contextMenuContributionVisible(element,
+						contribution);
 	}
 }
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 52b5729c8..16f4eaa65 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
@@ -31,7 +31,8 @@ import org.fortiss.tooling.kernel.internal.TutorialService;
  * <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.
+ * {@link TutorialStep}'s local whitelist provider. Note that those methods <B>MUST NOT</B> be
+ * called unless {@link #isTutorialActive()} returns <code>true</code>.
  * 
  * @author hoelzl
  * @author $Author: hoelzl $
-- 
GitLab