From 76e718f94e75edc8f080d62632d84a4e281fa8b8 Mon Sep 17 00:00:00 2001
From: David Trachtenherz <trachtenherz@fortiss.org>
Date: Tue, 3 Jan 2012 16:31:54 +0000
Subject: [PATCH] Base classes for ContextMenuContributor for sub-menus refs
 451

---
 .../ContextMenuSubMenuContributorBase.java    | 120 ++++++++++++++++++
 .../ui/extension/base/MenuActionBase.java     |  56 ++++++++
 2 files changed, 176 insertions(+)
 create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/ContextMenuSubMenuContributorBase.java
 create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/MenuActionBase.java

diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/ContextMenuSubMenuContributorBase.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/ContextMenuSubMenuContributorBase.java
new file mode 100644
index 000000000..60256d2e0
--- /dev/null
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/ContextMenuSubMenuContributorBase.java
@@ -0,0 +1,120 @@
+/*--------------------------------------------------------------------------+
+$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
+|                                                                          |
+| Copyright 2012 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.ui.extension.base;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.fortiss.tooling.kernel.model.INamedElement;
+import org.fortiss.tooling.kernel.ui.extension.IContextMenuContributor;
+import org.fortiss.tooling.kernel.ui.extension.base.MenuActionBase.ActionFactory;
+import org.fortiss.tooling.kernel.ui.extension.data.ContextMenuContextProvider;
+import org.fortiss.tooling.kernel.ui.service.IContextMenuService;
+
+/**
+ * Base class for context menu contributors creating a sub-menu
+ * {@link IMenuManager} with actions provided by actions factories
+ * {@link ActionFactory} returned by {@link #getActionFactories()}.
+ * 
+ * <p>
+ * 
+ * The generic type parameter {@code T} indicated the type of selection
+ * elements.
+ * 
+ * @author trachtenherz
+ * @author $Author: hoelzl $
+ * @version $Rev: 18709 $
+ * @ConQAT.Rating RED Hash:
+ */
+public abstract class ContextMenuSubMenuContributorBase implements
+		IContextMenuContributor {
+
+	/** Constructor. */
+	public ContextMenuSubMenuContributorBase() {
+		super();
+	}
+
+	/** The name of the sub-menu */
+	protected abstract String getMenuName();
+
+	/** Returns the action factories for the actions to be shown in the menu */
+	protected abstract Collection<ActionFactory<INamedElement>> getActionFactories();
+
+	/** Creates action instances for the given selection. */
+	protected Collection<Action> createActionsForElement(INamedElement selection) {
+		ArrayList<Action> list = new ArrayList<Action>();
+		for (ActionFactory<INamedElement> factory : getActionFactories()) {
+			list.add(factory.createAction(selection));
+		}
+		return list;
+	}
+
+	/** The image for the sub-menu */
+	protected ImageDescriptor getMenuIcon() {
+		return null;
+	}
+
+	/** The Id for the sub-menu */
+	protected String getMenuId() {
+		return null;
+	}
+
+	/**
+	 * Creates a sub-menu {@link IMenuManager} containing specified actions.
+	 */
+	protected IMenuManager createMenu(Collection<Action> actions) {
+		IMenuManager menu = new MenuManager(getMenuName(), getMenuIcon(),
+				getMenuId());
+		for (Action a : actions) {
+			menu.add(a);
+		}
+		return menu;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public final List<IContributionItem> getContributedItems(EObject selection,
+			ContextMenuContextProvider contextProvider) {
+		if (selection instanceof INamedElement
+				&& acceptSelection((INamedElement) selection, contextProvider)) {
+			List<IContributionItem> contributionItems = new ArrayList<IContributionItem>();
+			contributionItems
+					.add(createMenu(createActionsForElement((INamedElement) selection)));
+			return contributionItems;
+		}
+		return Collections.emptyList();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public String getMenuSectionID() {
+		return IContextMenuService.BOTTOM_MOST_MENU_SECTION_ID;
+	}
+
+	/** Returns {@code true} if the selected element is acceptable. */
+	protected abstract boolean acceptSelection(INamedElement selection,
+			ContextMenuContextProvider contextProvider);
+}
\ No newline at end of file
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/MenuActionBase.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/MenuActionBase.java
new file mode 100644
index 000000000..a8a5c0071
--- /dev/null
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/MenuActionBase.java
@@ -0,0 +1,56 @@
+/*--------------------------------------------------------------------------+
+$Id: EasyStartMenuBase.java 1907 2011-11-11 14:04:26Z hoelzl $
+|                                                                          |
+| Copyright 2011 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.ui.extension.base;
+
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.resource.ImageDescriptor;
+
+/**
+ * Base class for menu actions.
+ * 
+ * @author trachtenherz
+ * @author $Author: hoelzl $
+ * @version $Rev: 1907 $
+ * @ConQAT.Rating YELLOW Hash: A55559E6A76127EF46123531794FB17B
+ */
+public abstract class MenuActionBase<T> extends Action {
+
+	/** Factory for the action instances used for this menu */
+	public static interface ActionFactory<T> {
+		/** Creates and returns an action instance for the given target. */
+		MenuActionBase<T> createAction(T target);
+	}
+
+	/** The target to be processed. */
+	protected final T target;
+
+	/** Name of the action. */
+	protected abstract String getActionName();
+
+	/** Icon for the action. */
+	protected ImageDescriptor getActionIcon() {
+		return null;
+	}
+
+	/** Constructor. */
+	public MenuActionBase(T target) {
+		setText(getActionName());
+		setImageDescriptor(getActionIcon());
+		this.target = target;
+	}
+}
-- 
GitLab