From d868d1b76380a410c0f073ae98a082df3272c636 Mon Sep 17 00:00:00 2001
From: Florian Hoelzl <hoelzl@fortiss.org>
Date: Tue, 8 Mar 2011 13:54:42 +0000
Subject: [PATCH] some extensions to type system

---
 .../trunk/META-INF/MANIFEST.MF                |  3 +-
 .../kernel/internal/CommandStackService.java  | 30 ++++-----
 .../kernel/util/ProjectRootElementUtils.java  | 63 +++++++++++++++++++
 3 files changed, 76 insertions(+), 20 deletions(-)
 create mode 100644 org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/util/ProjectRootElementUtils.java

diff --git a/org.fortiss.tooling.kernel/trunk/META-INF/MANIFEST.MF b/org.fortiss.tooling.kernel/trunk/META-INF/MANIFEST.MF
index 170081c08..95b10c8ff 100644
--- a/org.fortiss.tooling.kernel/trunk/META-INF/MANIFEST.MF
+++ b/org.fortiss.tooling.kernel/trunk/META-INF/MANIFEST.MF
@@ -23,4 +23,5 @@ Export-Package: org.fortiss.tooling.kernel,
  org.fortiss.tooling.kernel.model,
  org.fortiss.tooling.kernel.model.impl,
  org.fortiss.tooling.kernel.model.util,
- org.fortiss.tooling.kernel.services
+ org.fortiss.tooling.kernel.services,
+ org.fortiss.tooling.kernel.util
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/CommandStackService.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/CommandStackService.java
index f60a0e5cb..efb972e5c 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/CommandStackService.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/internal/CommandStackService.java
@@ -29,6 +29,7 @@ import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.edit.command.ChangeCommand;
 import org.fortiss.tooling.kernel.ToolingKernelActivator;
 import org.fortiss.tooling.kernel.services.ICommandStackService;
+import org.fortiss.tooling.kernel.util.ProjectRootElementUtils;
 import org.unicase.ecp.model.ECPWorkspaceManager;
 import org.unicase.ecp.model.NoWorkspaceException;
 import org.unicase.ecp.model.workSpaceModel.ECPProject;
@@ -85,31 +86,36 @@ public class CommandStackService implements ICommandStackService {
 				runner.run();
 			}
 		};
-		commandStackMap.get(getECPProject(target)).execute(chgCommand);
+		commandStackMap.get(ProjectRootElementUtils.getECPProject(target))
+				.execute(chgCommand);
 	}
 
 	/** {@inheritDoc} */
 	@Override
 	public boolean canUndo(EObject target) {
-		return commandStackMap.get(getECPProject(target)).canUndo();
+		return commandStackMap.get(
+				ProjectRootElementUtils.getECPProject(target)).canUndo();
 	}
 
 	/** {@inheritDoc} */
 	@Override
 	public boolean canRedo(EObject target) {
-		return commandStackMap.get(getECPProject(target)).canRedo();
+		return commandStackMap.get(
+				ProjectRootElementUtils.getECPProject(target)).canRedo();
 	}
 
 	/** {@inheritDoc} */
 	@Override
 	public void undo(EObject target) {
-		commandStackMap.get(getECPProject(target)).undo();
+		commandStackMap.get(ProjectRootElementUtils.getECPProject(target))
+				.undo();
 	}
 
 	/** {@inheritDoc} */
 	@Override
 	public void redo(EObject target) {
-		commandStackMap.get(getECPProject(target)).redo();
+		commandStackMap.get(ProjectRootElementUtils.getECPProject(target))
+				.redo();
 	}
 
 	/**
@@ -137,20 +143,6 @@ public class CommandStackService implements ICommandStackService {
 		};
 	}
 
-	/**
-	 * Returns the model element's ECP project or <code>null</code> .
-	 */
-	private ECPProject getECPProject(EObject target) {
-		try {
-			return ECPWorkspaceManager.getInstance().getWorkSpace()
-					.getProject(target);
-		} catch (Exception e) {
-			LoggingUtils.error(ToolingKernelActivator.getDefault(),
-					"Unable to find ECP project!", e);
-		}
-		return null;
-	}
-
 	/** Adds the command stack for the given project. */
 	private void addCommandStack(ECPProject project) {
 		BasicCommandStack cmdStack = (BasicCommandStack) project
diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/util/ProjectRootElementUtils.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/util/ProjectRootElementUtils.java
new file mode 100644
index 000000000..5356f04db
--- /dev/null
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/util/ProjectRootElementUtils.java
@@ -0,0 +1,63 @@
+/*--------------------------------------------------------------------------+
+$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.util;
+
+import org.conqat.ide.commons.ui.logging.LoggingUtils;
+import org.conqat.lib.commons.reflect.ReflectionUtils;
+import org.eclipse.emf.ecore.EObject;
+import org.fortiss.tooling.kernel.ToolingKernelActivator;
+import org.fortiss.tooling.kernel.model.IProjectRootElement;
+import org.unicase.ecp.model.ECPWorkspaceManager;
+import org.unicase.ecp.model.workSpaceModel.ECPProject;
+import org.unicase.workspace.ProjectSpace;
+
+/**
+ * Utility methods for accessing {@link IProjectRootElement}s.
+ * 
+ * @author hoelzlf
+ * @author $Author$
+ * @version $Rev$
+ * @levd.rating RED Rev:
+ */
+public final class ProjectRootElementUtils {
+
+	/**
+	 * Returns the model element's ECP project or <code>null</code> .
+	 */
+	public static ECPProject getECPProject(EObject element) {
+		try {
+			return ECPWorkspaceManager.getInstance().getWorkSpace()
+					.getProject(element);
+		} catch (Exception e) {
+			LoggingUtils.error(ToolingKernelActivator.getDefault(),
+					"Unable to find ECP project!", e);
+		}
+		return null;
+	}
+
+	/**
+	 * Returns the instance of a project root element corresponding to the given
+	 * class.
+	 */
+	public static <T extends IProjectRootElement> T getRootElement(
+			EObject element, Class<T> clazz) {
+		ECPProject project = getECPProject(element);
+		return ReflectionUtils.pickInstanceOf(clazz, ((ProjectSpace) project
+				.getRootObject()).getProject().getAllModelElements());
+	}
+}
-- 
GitLab