From 2361a484f0cce15dd3e0e213369cc258702780e4 Mon Sep 17 00:00:00 2001
From: Dongyue Mou <mou@fortiss.org>
Date: Fri, 25 Jan 2013 11:19:16 +0000
Subject: [PATCH] fixed npe bug if dummy object is removed from top element
 list.

---
 .../tooling/kernel/utils/ModelUtils.java      | 25 +++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/ModelUtils.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/ModelUtils.java
index 893fc9d1e..ec024f724 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/ModelUtils.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/ModelUtils.java
@@ -17,7 +17,10 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.kernel.utils;
 
+import org.eclipse.emf.common.util.TreeIterator;
 import org.eclipse.emf.ecore.EObject;
+import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
+import org.fortiss.tooling.kernel.model.IIdLabeled;
 import org.fortiss.tooling.kernel.service.ICommandStackService;
 import org.fortiss.tooling.kernel.service.IPersistencyService;
 
@@ -27,7 +30,7 @@ import org.fortiss.tooling.kernel.service.IPersistencyService;
  * @author doebber
  * @author $Author: hoelzl $
  * @version $Rev: 18709 $
- * @ConQAT.Rating GREEN Hash: 196FA181C009BB49CC3E878D79547091
+ * @ConQAT.Rating YELLOW Hash: 4E7A872801564F1C1CEDB77AA20BC8ED
  */
 public class ModelUtils {
 
@@ -58,6 +61,24 @@ public class ModelUtils {
 	 *             if the context is not contained in a persisted model.
 	 */
 	public static void prepareUniqueID(EObject element, EObject context) {
-		IPersistencyService.INSTANCE.getTopLevelElementFor(context).prepareIDs(element);
+		ITopLevelElement top = IPersistencyService.INSTANCE.getTopLevelElementFor(context);
+
+		if(top == null) {
+			EObject root = context;
+			while(root.eContainer() != null)
+				root = root.eContainer();
+
+			int id = Integer.MIN_VALUE;
+			for(TreeIterator<EObject> iter = root.eAllContents(); iter.hasNext();) {
+				EObject o = iter.next();
+				if(o instanceof IIdLabeled) {
+					id = Math.max(id, ((IIdLabeled)o).getId());
+				}
+			}
+
+			((IIdLabeled)element).setId(id + 1);
+		} else {
+			top.prepareIDs(element);
+		}
 	}
 }
-- 
GitLab