From db703c75063f7bb5f6513fa66ad4e3dae14d1df9 Mon Sep 17 00:00:00 2001
From: Andrei Serban Vlad <andreiasw1@gmail.com>
Date: Mon, 10 Mar 2014 15:00:21 +0000
Subject: [PATCH] refs 1882

---
 .../base/ui/editor/DiagramEditorBase.java     | 10 ++
 ...IContextMenuMultiSelectionContributor.java | 43 +++++++++
 .../base/MultiEObjectActionBase.java          | 94 +++++++++++++++++++
 .../data/ContextMenuContextProvider.java      |  7 ++
 .../ui/internal/ContextMenuService.java       | 51 +++++++---
 .../editor/ExtendableMultiPageEditor.java     |  8 +-
 .../ui/internal/views/NavigatorViewPart.java  |  9 ++
 .../kernel/ui/util/EObjectSelectionUtils.java |  7 +-
 8 files changed, 216 insertions(+), 13 deletions(-)
 create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/IContextMenuMultiSelectionContributor.java
 create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/MultiEObjectActionBase.java

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/DiagramEditorBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/DiagramEditorBase.java
index 76d51e455..e80bddeef 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/DiagramEditorBase.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/DiagramEditorBase.java
@@ -27,6 +27,7 @@ import static org.eclipse.gef.editparts.ZoomManager.FIT_WIDTH;
 import static org.fortiss.tooling.base.layout.DefaultLayoutConstants.DEFAULT_GRID_ORIGIN_X;
 import static org.fortiss.tooling.base.layout.DefaultLayoutConstants.DEFAULT_GRID_ORIGIN_Y;
 import static org.fortiss.tooling.base.layout.DefaultLayoutConstants.DEFAULT_GRID_SIZE;
+import static org.fortiss.tooling.kernel.ui.util.EObjectSelectionUtils.getEObjectElements;
 import static org.fortiss.tooling.kernel.ui.util.EObjectSelectionUtils.getFirstElement;
 
 import java.util.ArrayList;
@@ -176,6 +177,15 @@ public class DiagramEditorBase<T extends EObject> extends GEFEditorBase<T> imple
 		return null;
 	}
 
+	/** {@inheritDoc} */
+	@Override
+	public List<EObject> getSelectedModelElementList() {
+		if(getSite().getSelectionProvider() != null) {
+			return getEObjectElements(getSite().getSelectionProvider().getSelection());
+		}
+		return null;
+	}
+
 	/** Creates the context menu. */
 	private void createContextMenu() {
 		MenuManager menuManager = IContextMenuService.INSTANCE.createDefaultContextMenu(this);
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/IContextMenuMultiSelectionContributor.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/IContextMenuMultiSelectionContributor.java
new file mode 100644
index 000000000..a37666b37
--- /dev/null
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/IContextMenuMultiSelectionContributor.java
@@ -0,0 +1,43 @@
+/*--------------------------------------------------------------------------+
+$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
+|                                                                          |
+| Copyright 2014 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;
+
+import java.util.List;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.action.IContributionItem;
+import org.fortiss.tooling.kernel.ui.extension.data.ContextMenuContextProvider;
+
+/**
+ * 
+ * @author Andreiasw1
+ * @author $Author: hoelzl $
+ * @version $Rev: 18709 $
+ * @ConQAT.Rating RED Hash:
+ */
+public interface IContextMenuMultiSelectionContributor extends IContextMenuContributor {
+	/**
+	 * Returns the contributed items, i.e., actions or menus in the context of
+	 * the given provider and the given selected model element. The selection
+	 * may be null, but is always equal to
+	 * {@link ContextMenuContextProvider#getSelectedModelElement()}.
+	 */
+
+	List<IContributionItem> getContributedItems(List<EObject> selection,
+			ContextMenuContextProvider contextProvider);
+}
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/MultiEObjectActionBase.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/MultiEObjectActionBase.java
new file mode 100644
index 000000000..1cc25cd3e
--- /dev/null
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/MultiEObjectActionBase.java
@@ -0,0 +1,94 @@
+/*--------------------------------------------------------------------------+
+$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
+|                                                                          |
+| Copyright 2014 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.List;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.resource.ImageDescriptor;
+
+/**
+ * support for multi object selection
+ * 
+ * @author Andreiasw1
+ * @author $Author: hoelzl $
+ * @version $Rev: 18709 $
+ * @ConQAT.Rating RED Hash:
+ */
+public abstract class MultiEObjectActionBase<T extends List<? extends EObject>> extends Action {
+
+	/** Stores the target element. */
+	private T target;
+
+	/** Constructor. */
+	public MultiEObjectActionBase(String text, ImageDescriptor image) {
+		super(text, image);
+	}
+
+	/** Constructor. */
+	public MultiEObjectActionBase(T target, String text, ImageDescriptor image) {
+		this(text, image);
+		refresh(target);
+	}
+
+	/** Constructor. */
+	public MultiEObjectActionBase(T target, String text) {
+		this(target, text, null);
+	}
+
+	/** Sets the target object. */
+	public void setTarget(T target) {
+		this.target = target;
+	}
+
+	/** Returns the current target. */
+	public T getTarget() {
+		return target;
+	}
+
+	/** Refreshes the enabled state for the current action target. */
+	public final void refresh() {
+		if(getTarget() == null || getTarget().isEmpty()) {
+			setEnabled(false);
+			return;
+		}
+		setEnabled(computeEnabled());
+	}
+
+	/**
+	 * Refreshes the action's target and enabled state.
+	 * 
+	 * @see #setTarget(List)
+	 * @see #refresh()
+	 */
+	public final void refresh(T element) {
+		setTarget(element);
+		refresh();
+	}
+
+	/**
+	 * Computes the enabled state of the action for the current target, which is
+	 * not <code>null</code> when this method is called. The default
+	 * implementation returns <code>true</code>.
+	 */
+	protected boolean computeEnabled() {
+		return true;
+	}
+
+}
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/data/ContextMenuContextProvider.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/data/ContextMenuContextProvider.java
index 860ac01b2..32b915b03 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/data/ContextMenuContextProvider.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/data/ContextMenuContextProvider.java
@@ -17,6 +17,8 @@ $Id$
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.kernel.ui.extension.data;
 
+import java.util.List;
+
 import org.eclipse.emf.ecore.EObject;
 import org.fortiss.tooling.kernel.ui.extension.IContextMenuContributor;
 import org.fortiss.tooling.kernel.ui.internal.ContextMenuService;
@@ -42,4 +44,9 @@ public interface ContextMenuContextProvider {
 	 * creation or update.
 	 */
 	EObject getSelectedModelElement();
+
+	/**
+	 * Returns a list containing the selected Eobjects
+	 */
+	List<EObject> getSelectedModelElementList();
 }
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 f106ed5c4..9249f3dba 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
@@ -30,6 +30,7 @@ import org.eclipse.jface.action.Separator;
 import org.eclipse.ui.IWorkbenchActionConstants;
 import org.fortiss.tooling.kernel.ToolingKernelActivator;
 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;
 import org.fortiss.tooling.kernel.ui.service.IActionService;
 import org.fortiss.tooling.kernel.ui.service.IContextMenuService;
@@ -85,9 +86,11 @@ public class ContextMenuService implements IContextMenuService {
 				Class<?> handlerClass =
 						ExtensionPointUtils.loadClass(
 								ce.getAttribute(HANDLER_CLASS_ATTRIBUTE_NAME), bundle);
+
 				IContextMenuContributor provider =
 						(IContextMenuContributor)handlerClass.getConstructor().newInstance();
 				providerList.add(provider);
+
 			} catch(Exception ex) {
 				LoggingUtils.error(ToolingKernelActivator.getDefault(), ex.getMessage(), ex);
 			}
@@ -127,18 +130,44 @@ public class ContextMenuService implements IContextMenuService {
 
 	/** Adds registered contributions to the given menu. */
 	private void addContributions(IMenuManager menu, ContextMenuContextProvider contextProvider) {
-		EObject selection = contextProvider.getSelectedModelElement();
-		for(IContextMenuContributor contributor : providerList) {
-			String menuSectionID = contributor.getMenuSectionID();
-			if(menuSectionID == null) {
-				menuSectionID = IWorkbenchActionConstants.MB_ADDITIONS;
-			}
-			List<IContributionItem> l = contributor.getContributedItems(selection, contextProvider);
-			if(l == null) {
-				continue;
+
+		List<EObject> selection = contextProvider.getSelectedModelElementList();
+		if(selection == null) {
+			// do nothing
+		} else if(selection.size() == 1) {
+			EObject selectionElem = contextProvider.getSelectedModelElement();
+			for(IContextMenuContributor contributor : providerList) {
+				String menuSectionID = contributor.getMenuSectionID();
+				if(menuSectionID == null) {
+					menuSectionID = IWorkbenchActionConstants.MB_ADDITIONS;
+				}
+				List<IContributionItem> l =
+						contributor.getContributedItems(selectionElem, contextProvider);
+				if(l == null) {
+					continue;
+				}
+				for(IContributionItem item : l) {
+					menu.appendToGroup(menuSectionID, item);
+				}
 			}
-			for(IContributionItem item : l) {
-				menu.appendToGroup(menuSectionID, item);
+		} 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);
+					}
+				}
 			}
 		}
 	}
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/editor/ExtendableMultiPageEditor.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/editor/ExtendableMultiPageEditor.java
index 048c1e6e4..573127b2d 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/editor/ExtendableMultiPageEditor.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/editor/ExtendableMultiPageEditor.java
@@ -256,7 +256,13 @@ public class ExtendableMultiPageEditor extends MultiPageEditorPart implements
 	/** {@inheritDoc} */
 	@Override
 	public void commandStackChanged(EventObject event) {
-		firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
+		getEditorSite().getWorkbenchWindow().getShell().getDisplay().syncExec(new Runnable() {
+			/** {@inheritDoc} */
+			@Override
+			public void run() {
+				firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
+			}
+		});
 	}
 
 	/**
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java
index e3c45f0eb..6db6fb9bd 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorViewPart.java
@@ -17,6 +17,8 @@ $Id$
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.kernel.ui.internal.views;
 
+import java.util.List;
+
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -346,6 +348,13 @@ public final class NavigatorViewPart extends ViewPart implements ISelectionListe
 				.getSelection());
 	}
 
+	/** {@inheritDoc} */
+	@Override
+	public List<EObject> getSelectedModelElementList() {
+		return EObjectSelectionUtils.getEObjectElements(getSite().getSelectionProvider()
+				.getSelection());
+	}
+
 	/** Returns whether expert view is active. */
 	public boolean isExpertViewActive() {
 		return ((NavigatorTreeContentProvider)viewer.getContentProvider()).isExpertViewActive();
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/EObjectSelectionUtils.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/EObjectSelectionUtils.java
index 08887dd58..2895aa2b8 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/EObjectSelectionUtils.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/EObjectSelectionUtils.java
@@ -118,11 +118,16 @@ public final class EObjectSelectionUtils {
 	 */
 	public static List<EObject> getEObjectElements(ISelection selection) {
 		List<EObject> result = new ArrayList<EObject>();
-		if(selection instanceof StructuredSelection) {
+		if(selection instanceof StructuredSelection && !selection.isEmpty()) {
 			for(Object o : ((StructuredSelection)selection).toList()) {
 				EObject eo = EObjectOfObject(o);
 				if(eo != null) {
 					result.add(eo);
+				} else if(o instanceof IAdaptable) {
+					EObject eo2 = (EObject)((IAdaptable)o).getAdapter(EObject.class);
+					if(eo2 != null) {
+						result.add(eo2);
+					}
 				}
 			}
 		}
-- 
GitLab