Bug 528694 - [SysML 1.4] NPE in ConstraintBlockParameterCreationCommand

 - write a warning when the node isn't created 
 (in most case it's because of a missing expansion model)
 
Change-Id: If9ea34078689bc2314276821afaa48e729bf814e
Signed-off-by: Benoit Maggi <benoit.maggi@cea.fr>
diff --git a/diagram/org.eclipse.papyrus.sysml14.diagram.parametric/src/org/eclipse/papyrus/sysml14/diagram/parametric/Activator.java b/diagram/org.eclipse.papyrus.sysml14.diagram.parametric/src/org/eclipse/papyrus/sysml14/diagram/parametric/Activator.java
index c4a1d49..9fd9089 100644
--- a/diagram/org.eclipse.papyrus.sysml14.diagram.parametric/src/org/eclipse/papyrus/sysml14/diagram/parametric/Activator.java
+++ b/diagram/org.eclipse.papyrus.sysml14.diagram.parametric/src/org/eclipse/papyrus/sysml14/diagram/parametric/Activator.java
@@ -12,6 +12,8 @@
  *****************************************************************************/
 package org.eclipse.papyrus.sysml14.diagram.parametric;
 
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
 import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.jface.resource.ImageDescriptor;
@@ -26,6 +28,9 @@
 	// The plug-in ID
 	public static final String PLUGIN_ID = "org.eclipse.papyrus.sysml14.diagram.parametric"; //$NON-NLS-1$
 
+	// The shared instance
+	private static Activator plugin;
+
 	/**
 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
 	 *
@@ -35,10 +40,24 @@
 	@Override //FIXME : remove the code after Papyrus Core Oxygen.2 (See Bug 522295)
 	public void start(BundleContext context) throws Exception {
 		super.start(context);
+		plugin = this;
 		IEclipsePreferences node = InstanceScope.INSTANCE.getNode("org.eclipse.papyrus.infra.gmfdiag.dnd"); //$NON-NLS-1$
 		node.put("org.eclipse.papyrus.infra.gmfdiag.dnd.expansiondropsteategy.isActive", "false"); //$NON-NLS-1$ //$NON-NLS-2$
 	}
 
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see
+	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+	 */
+	@Override
+	public void stop(BundleContext context) throws Exception {
+		plugin = null;
+		super.stop(context);
+	}	
+	
+	
 	/**
 	 * Returns an image descriptor for the image file at the given
 	 * plug-in relative path
@@ -50,4 +69,27 @@
 	public static ImageDescriptor getImageDescriptor(String path) {
 		return imageDescriptorFromPlugin(PLUGIN_ID, path);
 	}
+	
+	public static Activator getDefault() {
+		return plugin;
+	}
+
+	/**
+	 * Log the status
+	 * @param status
+	 */
+	public static void log(IStatus status) {
+		Activator.getDefault().getLog().log(status);
+	}
+
+	/**
+	 * Log the message
+	 * @param severity
+	 * @param message
+	 */
+	public static void log(int severity, String message) {
+		log(new Status(severity, PLUGIN_ID, message));
+	}	
+		
+	
 }
diff --git a/diagram/org.eclipse.papyrus.sysml14.diagram.parametric/src/org/eclipse/papyrus/sysml14/diagram/parametric/command/ConstraintBlockParameterCreationCommand.java b/diagram/org.eclipse.papyrus.sysml14.diagram.parametric/src/org/eclipse/papyrus/sysml14/diagram/parametric/command/ConstraintBlockParameterCreationCommand.java
index 70eb653..affd996 100644
--- a/diagram/org.eclipse.papyrus.sysml14.diagram.parametric/src/org/eclipse/papyrus/sysml14/diagram/parametric/command/ConstraintBlockParameterCreationCommand.java
+++ b/diagram/org.eclipse.papyrus.sysml14.diagram.parametric/src/org/eclipse/papyrus/sysml14/diagram/parametric/command/ConstraintBlockParameterCreationCommand.java
@@ -8,7 +8,7 @@
  *
  * Contributors:
  *   Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Initial API and implementation
- *   
+ *   Benoit Maggi (CEA LIST) benoit.maggi@cea.fr- Bug 528694
  *****************************************************************************/
 
 package org.eclipse.papyrus.sysml14.diagram.parametric.command;
@@ -16,6 +16,7 @@
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.draw2d.IFigure;
 import org.eclipse.draw2d.geometry.Point;
 import org.eclipse.emf.ecore.EObject;
@@ -33,6 +34,7 @@
 import org.eclipse.gmf.runtime.notation.Node;
 import org.eclipse.gmf.runtime.notation.View;
 import org.eclipse.papyrus.sysml14.diagram.common.util.GraphicalSysMLServiceTypeUtil;
+import org.eclipse.papyrus.sysml14.diagram.parametric.Activator;
 import org.eclipse.papyrus.uml.service.types.element.UMLElementTypes;
 import org.eclipse.uml2.uml.UMLPackage;
 
@@ -101,7 +103,7 @@
 		propertyCreationCommand.execute(progressMonitor, info);
 
 		CommandResult commandResult = propertyCreationCommand.getCommandResult();
-		if (null != commandResult && commandResult.getStatus().isOK()) {
+		if (commandResult != null  && commandResult.getStatus().isOK()) {
 			// Set the parameter type with ConstraintBlock
 			final EObject createdProperty = propertyCreationCommand.getNewElement();
 			createdProperty.eSet(UMLPackage.eINSTANCE.getTypedElement_Type(), typeElement);
@@ -110,20 +112,23 @@
 
 			// Create the node corresponding to the parameter
 			final View notationView = targetEditPart.getNotationView();
-			Node node = ViewService.createNode(notationView, createdProperty, ((IHintedType) type).getSemanticHint(), ((GraphicalEditPart) targetEditPart).getDiagramPreferencesHint());
-
-			if (null != point && node.getLayoutConstraint() instanceof Bounds) {
-				IFigure parentFigure = targetEditPart.getFigure().getParent();
-				while (null != parentFigure) {
-					point.translate(-(parentFigure.getBounds().x + parentFigure.getInsets().left), -(parentFigure.getBounds().y + parentFigure.getInsets().top));
-					parentFigure = parentFigure.getParent();
-				}
-
-				((Bounds) node.getLayoutConstraint()).setX(point.x);
-				((Bounds) node.getLayoutConstraint()).setY(point.y);
+			String semanticHint = ((IHintedType) type).getSemanticHint();
+			Node node = ViewService.createNode(notationView, createdProperty, semanticHint, ((GraphicalEditPart) targetEditPart).getDiagramPreferencesHint());
+			
+			if (node == null) {
+				Activator.log(IStatus.WARNING,"Impossible to create the view "+semanticHint);//$NON-NLS-1$
+			} else {
+				if (point != null  && node.getLayoutConstraint() instanceof Bounds) {
+					IFigure parentFigure = targetEditPart.getFigure().getParent();
+					while (null != parentFigure) {
+						point.translate(-(parentFigure.getBounds().x + parentFigure.getInsets().left), -(parentFigure.getBounds().y + parentFigure.getInsets().top));
+						parentFigure = parentFigure.getParent();
+					}
+					((Bounds) node.getLayoutConstraint()).setX(point.x);
+					((Bounds) node.getLayoutConstraint()).setY(point.y);
+				}				
 			}
 		}
-
 		return commandResult;
 	}