From eee86f542b8b7ee80453458d9b8d06d294e23349 Mon Sep 17 00:00:00 2001
From: Florian Hoelzl <hoelzl@fortiss.org>
Date: Mon, 14 Mar 2011 12:32:26 +0000
Subject: [PATCH] added delete operation

---
 .../trunk/model/kernel.ecore                  | 44 ++++++++++++++--
 .../kernel/internal/ActionService.java        | 49 ++++++++++++++++++
 .../kernel/internal/ContextMenuService.java   | 16 ------
 .../internal/navigator/NavigatorViewPart.java |  4 +-
 .../model/impl/IRemovableStaticImpl.java      | 50 +++++++++++++++++++
 .../kernel/services/IActionService.java       |  4 ++
 .../kernel/services/IContextMenuService.java  |  4 --
 7 files changed, 146 insertions(+), 25 deletions(-)
 create mode 100644 org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/model/impl/IRemovableStaticImpl.java

diff --git a/org.fortiss.tooling.kernel/trunk/model/kernel.ecore b/org.fortiss.tooling.kernel/trunk/model/kernel.ecore
index ac5a93211..520d37b47 100644
--- a/org.fortiss.tooling.kernel/trunk/model/kernel.ecore
+++ b/org.fortiss.tooling.kernel/trunk/model/kernel.ecore
@@ -5,11 +5,25 @@
     nsURI="http://www.fortiss.org/tooling/kernel" nsPrefix="org-fortiss-tooling-kernel">
   <eClassifiers xsi:type="ecore:EClass" name="INamedElement" abstract="true" interface="true"
       eSuperTypes="#//IIdLabeled">
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+      <details key="documentation" value="Model elements implementing this interface have a unique id and a name."/>
+    </eAnnotations>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString">
+      <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+        <details key="documentation" value="The name attribute."/>
+      </eAnnotations>
+    </eStructuralFeatures>
   </eClassifiers>
   <eClassifiers xsi:type="ecore:EClass" name="INamedCommentedElement" abstract="true"
       interface="true" eSuperTypes="#//INamedElement">
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="comment" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
+    <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+      <details key="documentation" value="Model elements implementing this interface have a unique id, a name and a comment."/>
+    </eAnnotations>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="comment" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString">
+      <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+        <details key="documentation" value="The comment attribute."/>
+      </eAnnotations>
+    </eStructuralFeatures>
   </eClassifiers>
   <eClassifiers xsi:type="ecore:EClass" name="IProjectRootElement" abstract="true"
       interface="true" eSuperTypes="#//INamedCommentedElement">
@@ -18,6 +32,30 @@
     </eAnnotations>
   </eClassifiers>
   <eClassifiers xsi:type="ecore:EClass" name="IIdLabeled" abstract="true" interface="true">
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="id" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
+    <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+      <details key="documentation" value="Model elements implementing this interface have a unique identifier."/>
+    </eAnnotations>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="id" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt">
+      <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+        <details key="documentation" value="The unique integer id."/>
+      </eAnnotations>
+    </eStructuralFeatures>
+  </eClassifiers>
+  <eClassifiers xsi:type="ecore:EClass" name="IRemovable" abstract="true" interface="true">
+    <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+      <details key="documentation" value="Model elements implementing this interface can provide a custom remove operatoin."/>
+    </eAnnotations>
+    <eOperations name="canRemove" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean">
+      <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+        <details key="documentation" value="Returns whether the current model element can be removed."/>
+        <details key="body" value="return IRemovableStaticImpl.canRemove(this);"/>
+      </eAnnotations>
+    </eOperations>
+    <eOperations name="remove" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean">
+      <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+        <details key="documentation" value="Removes the current model element from its parent."/>
+        <details key="body" value="return IRemovableStaticImpl.remove(this);"/>
+      </eAnnotations>
+    </eOperations>
   </eClassifiers>
 </ecore:EPackage>
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/ActionService.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/ActionService.java
index 95c5fe5e1..757f5f490 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/ActionService.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/ActionService.java
@@ -18,14 +18,18 @@ $Id$
 package org.fortiss.tooling.kernel.internal;
 
 import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
 import org.eclipse.ui.IActionBars;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.ISharedImages;
 import org.eclipse.ui.IViewSite;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.actions.ActionFactory;
+import org.fortiss.tooling.kernel.model.IRemovable;
 import org.fortiss.tooling.kernel.services.IActionService;
 import org.fortiss.tooling.kernel.services.ICommandStackService;
 import org.fortiss.tooling.kernel.util.EObjectSelectionUtils;
@@ -40,6 +44,9 @@ import org.fortiss.tooling.kernel.util.EObjectSelectionUtils;
  */
 public class ActionService implements IActionService {
 
+	/** The group id of the global default action group. */
+	public static final String GROUP_GLOBAL_DEFAULTS = "globalDefaults";
+
 	/** The global undo action. */
 	public final IAction globalUndoAction = new Action("Undo", PlatformUI
 			.getWorkbench().getSharedImages()
@@ -63,6 +70,48 @@ public class ActionService implements IActionService {
 		}
 	};
 
+	/** The global delete action. */
+	public final IAction globalDeleteAction = new Action("Delete", PlatformUI
+			.getWorkbench().getSharedImages()
+			.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)) {
+		@Override
+		public void run() {
+			EObject selectedObject = EObjectSelectionUtils
+					.getCurrentSelectionFirstElement();
+			if (selectedObject != null) {
+				if (selectedObject instanceof IRemovable) {
+					((IRemovable) selectedObject).remove();
+				} else {
+					EcoreUtil.delete(selectedObject, true);
+				}
+			}
+		}
+
+		@Override
+		public boolean isEnabled() {
+			EObject selectedObject = EObjectSelectionUtils
+					.getCurrentSelectionFirstElement();
+			if (selectedObject != null) {
+				if (selectedObject instanceof IRemovable) {
+					return ((IRemovable) selectedObject).canRemove();
+				}
+				return selectedObject.eContainer() != null;
+			}
+			return false;
+		}
+	};
+
+	/** {@inheritDoc} */
+	@Override
+	public void addGlobalDefaultActionSectionToMenu(MenuManager menuManager) {
+		Separator globalDefaults = new Separator(GROUP_GLOBAL_DEFAULTS);
+		globalDefaults.setVisible(true);
+		menuManager.add(globalDefaults);
+
+		menuManager.appendToGroup(GROUP_GLOBAL_DEFAULTS, globalDeleteAction);
+		menuManager.appendToGroup(GROUP_GLOBAL_DEFAULTS, globalUndoAction);
+	}
+
 	/** {@inheritDoc} */
 	@Override
 	public void registerGlobalActions(IViewSite site) {
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/ContextMenuService.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/ContextMenuService.java
index b4a708132..d9945d0b0 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/ContextMenuService.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/ContextMenuService.java
@@ -17,9 +17,6 @@ $Id$
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.kernel.internal;
 
-import org.eclipse.jface.action.MenuManager;
-import org.eclipse.jface.action.Separator;
-import org.fortiss.tooling.kernel.services.IActionService;
 import org.fortiss.tooling.kernel.services.IContextMenuService;
 
 /**
@@ -32,17 +29,4 @@ import org.fortiss.tooling.kernel.services.IContextMenuService;
  */
 public class ContextMenuService implements IContextMenuService {
 
-	/** The group id of the global default action group. */
-	public static final String GROUP_GLOBAL_DEFAULTS = "globalDefaults";
-
-	/** {@inheritDoc} */
-	@Override
-	public void addGlobalDefaultActionSection(MenuManager menuManager) {
-		Separator globalDefaults = new Separator(GROUP_GLOBAL_DEFAULTS);
-		globalDefaults.setVisible(true);
-		menuManager.add(globalDefaults);
-
-		menuManager.appendToGroup(GROUP_GLOBAL_DEFAULTS,
-				((ActionService) IActionService.INSTANCE).globalUndoAction);
-	}
 }
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/navigator/NavigatorViewPart.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/navigator/NavigatorViewPart.java
index cc106d17c..945e85e29 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/navigator/NavigatorViewPart.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/navigator/NavigatorViewPart.java
@@ -48,7 +48,6 @@ import org.eclipse.ui.progress.UIJob;
 import org.fortiss.tooling.kernel.ToolingKernelActivator;
 import org.fortiss.tooling.kernel.internal.NavigatorService;
 import org.fortiss.tooling.kernel.services.IActionService;
-import org.fortiss.tooling.kernel.services.IContextMenuService;
 import org.fortiss.tooling.kernel.services.IEditorService;
 import org.fortiss.tooling.kernel.services.INavigatorService;
 import org.fortiss.tooling.kernel.util.EObjectSelectionUtils;
@@ -209,7 +208,8 @@ public final class NavigatorViewPart extends ViewPart implements
 		repositorySection.setVisible(true);
 		menuManager.add(repositorySection);
 
-		IContextMenuService.INSTANCE.addGlobalDefaultActionSection(menuManager);
+		IActionService.INSTANCE
+				.addGlobalDefaultActionSectionToMenu(menuManager);
 
 		Separator additions = new Separator(
 				IWorkbenchActionConstants.MB_ADDITIONS);
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/model/impl/IRemovableStaticImpl.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/model/impl/IRemovableStaticImpl.java
new file mode 100644
index 000000000..c9039610d
--- /dev/null
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/model/impl/IRemovableStaticImpl.java
@@ -0,0 +1,50 @@
+/*--------------------------------------------------------------------------+
+$Id$
+|                                                                          |
+| 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.model.impl;
+
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.fortiss.tooling.kernel.model.IRemovable;
+
+/**
+ * Static implementations for {@link IRemovable}.
+ * 
+ * @author hoelzlf
+ * @author $Author$
+ * @version $Rev$
+ * @levd.rating RED Rev:
+ */
+public final class IRemovableStaticImpl {
+	/** Constructor. */
+	private IRemovableStaticImpl() {
+		// class not intended to be instantiated.
+	}
+
+	/** Returns true, if a parent exists. */
+	public static boolean canRemove(IRemovable element) {
+		return element.eContainer() != null;
+	}
+
+	/**
+	 * Removes element from its parent container and deletes all references to
+	 * it and its children.
+	 */
+	public static boolean remove(IRemovable element) {
+		EcoreUtil.delete(element, true);
+		return true;
+	}
+}
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/services/IActionService.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/services/IActionService.java
index 90cf40fa9..a0c2c8196 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/services/IActionService.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/services/IActionService.java
@@ -17,6 +17,7 @@ $Id$
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.kernel.services;
 
+import org.eclipse.jface.action.MenuManager;
 import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.IViewSite;
 import org.fortiss.tooling.kernel.internal.ActionService;
@@ -41,6 +42,9 @@ public interface IActionService {
 	/** Registers global actions with an editor site. */
 	void registerGlobalActions(IEditorSite site);
 
+	/** Adds the global default actions like undo to the given context menu. */
+	void addGlobalDefaultActionSectionToMenu(MenuManager menuManager);
+
 	/** Updates the enabled state of the global actions. */
 	void update();
 }
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/services/IContextMenuService.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/services/IContextMenuService.java
index c4aff7d48..e17bd3f52 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/services/IContextMenuService.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/services/IContextMenuService.java
@@ -17,7 +17,6 @@ $Id$
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.kernel.services;
 
-import org.eclipse.jface.action.MenuManager;
 import org.fortiss.tooling.kernel.internal.ContextMenuService;
 
 /**
@@ -33,7 +32,4 @@ public interface IContextMenuService {
 
 	/** Returns the singleton instance of the service. */
 	public static final IContextMenuService INSTANCE = new ContextMenuService();
-
-	/** Adds the global default actions like undo to the given context menu. */
-	void addGlobalDefaultActionSection(MenuManager menuManager);
 }
-- 
GitLab