From 619ae7a619b114d5888db485f3190eed99cc29a3 Mon Sep 17 00:00:00 2001
From: Dongyue Mou <mou@fortiss.org>
Date: Tue, 23 Oct 2012 12:00:54 +0000
Subject: [PATCH] added protection code to avoid crash of whole menu

---
 .../ui/internal/views/NavigatorNewMenu.java   | 33 +++++++++++++++----
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorNewMenu.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorNewMenu.java
index a6f64e38a..df69054e5 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorNewMenu.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/views/NavigatorNewMenu.java
@@ -22,16 +22,20 @@ import org.eclipse.emf.ecore.EObject;
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.ActionContributionItem;
 import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.resource.ImageDescriptor;
 import org.fortiss.tooling.kernel.extension.data.Prototype;
 import org.fortiss.tooling.kernel.service.ICommandStackService;
 import org.fortiss.tooling.kernel.service.IElementCompositorService;
 import org.fortiss.tooling.kernel.service.IPrototypeService;
+import org.fortiss.tooling.kernel.ui.ToolingKernelUIActivator;
 import org.fortiss.tooling.kernel.ui.extension.IContextMenuContributor;
+import org.fortiss.tooling.kernel.ui.extension.IModelElementHandler;
 import org.fortiss.tooling.kernel.ui.extension.data.ContextMenuContextProvider;
 import org.fortiss.tooling.kernel.ui.service.IContextMenuService;
 import org.fortiss.tooling.kernel.ui.service.IModelEditorBindingService;
 import org.fortiss.tooling.kernel.ui.service.IModelElementHandlerService;
 import org.fortiss.tooling.kernel.ui.service.INavigatorService;
+import org.fortiss.tooling.kernel.utils.LoggingUtils;
 
 /**
  * The contributor for the dynamic new menu of the navigator view based on model
@@ -50,7 +54,12 @@ public class NavigatorNewMenu implements IContextMenuContributor {
 	/** Constructor. */
 	public NavigatorNewMenu() {
 		for(Prototype prototype : IPrototypeService.INSTANCE.getPrimaryPrototypes()) {
-			actions.add(new AddPrototypeAction(prototype));
+			try {
+				actions.add(new AddPrototypeAction(prototype));
+			} catch(Exception e) {
+				LoggingUtils.error(ToolingKernelUIActivator.getDefault(),
+						"Unexpected exception while adding prototypes", e);
+			}
 		}
 	}
 
@@ -89,11 +98,22 @@ public class NavigatorNewMenu implements IContextMenuContributor {
 
 		/** Constructor. */
 		public AddPrototypeAction(Prototype prototype) {
-			super(prototype.getName(), IModelElementHandlerService.INSTANCE.getModelElementHandler(
-					prototype.getPrototype()).getIconImageDescriptor());
+			super(prototype.getName(), getIcon(prototype));
 			this.prototype = prototype;
 		}
 
+		/** Get the icon of the prototype */
+		public static ImageDescriptor getIcon(Prototype prototype) {
+			IModelElementHandler<EObject> handler =
+					IModelElementHandlerService.INSTANCE.getModelElementHandler(prototype
+							.getPrototype());
+
+			if(handler == null)
+				return null;
+
+			return handler.getIconImageDescriptor();
+		}
+
 		/** {@inheritDoc} */
 		@Override
 		public void run() {
@@ -114,9 +134,10 @@ public class NavigatorNewMenu implements IContextMenuContributor {
 		 * prototype can be added to it.
 		 */
 		public boolean prepare(EObject container, boolean expert) {
-			if(!expert &&
-					IModelElementHandlerService.INSTANCE.getModelElementHandler(
-							prototype.getPrototype()).hiddenInNonExpertView()) {
+			IModelElementHandler<EObject> handler =
+					IModelElementHandlerService.INSTANCE.getModelElementHandler(prototype
+							.getPrototype());
+			if(!expert && handler != null && handler.hiddenInNonExpertView()) {
 				this.container = null;
 				return false;
 			}
-- 
GitLab