Bug 563950 - [Robotics] Provide text-based editors for data-types and communication objects

- Add unparser in separate file
- Fix "null" comment, if description does not exist

Change-Id: Ifd2d1bd9d2dc59de66b56bb0ffa5eeed7a667b9f
Signed-off-by: Ansgar Radermacher <ansgar.radermacher@cea.fr>
diff --git a/plugins/customization/textedit/org.eclipse.papyrus.robotics.xtext.compdef.ui/src/org/eclipse/papyrus/robotics/xtext/compdef/ui/contribution/CompDefEditorConfigurationContribution.java b/plugins/customization/textedit/org.eclipse.papyrus.robotics.xtext.compdef.ui/src/org/eclipse/papyrus/robotics/xtext/compdef/ui/contribution/CompDefEditorConfigurationContribution.java
index 7e1f625..038a4a3 100644
--- a/plugins/customization/textedit/org.eclipse.papyrus.robotics.xtext.compdef.ui/src/org/eclipse/papyrus/robotics/xtext/compdef/ui/contribution/CompDefEditorConfigurationContribution.java
+++ b/plugins/customization/textedit/org.eclipse.papyrus.robotics.xtext.compdef.ui/src/org/eclipse/papyrus/robotics/xtext/compdef/ui/contribution/CompDefEditorConfigurationContribution.java
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2010 CEA LIST.
+ * Copyright (c) 2020 CEA LIST.
  *
  *
  * All rights reserved. This program and the accompanying materials
@@ -13,6 +13,7 @@
  *  CEA LIST - Initial API and implementation
  *
  *****************************************************************************/
+
 package org.eclipse.papyrus.robotics.xtext.compdef.ui.contribution;
 
 import java.util.ArrayList;
@@ -31,7 +32,6 @@
 import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
 import org.eclipse.papyrus.designer.languages.common.base.ElementUtils;
 import org.eclipse.papyrus.infra.gmfdiag.extensionpoints.editors.configuration.ICustomDirectEditorConfiguration;
-import org.eclipse.papyrus.robotics.bpc.profile.bpc.Entity;
 import org.eclipse.papyrus.robotics.core.utils.ParameterUtils;
 import org.eclipse.papyrus.robotics.profile.robotics.components.Activity;
 import org.eclipse.papyrus.robotics.profile.robotics.components.ActivityInstance;
@@ -58,8 +58,6 @@
 import org.eclipse.uml2.uml.StructuredClassifier;
 import org.eclipse.uml2.uml.Type;
 import org.eclipse.uml2.uml.UMLPackage;
-import org.eclipse.uml2.uml.ValueSpecification;
-import org.eclipse.uml2.uml.util.UMLUtil;
 
 import com.google.inject.Injector;
 
@@ -153,60 +151,7 @@
 		return SWT.MULTI | SWT.WRAP;
 	}
 
-	String getProvided(Port port) {
-		EList<Interface> provideds = port.getProvideds();
-		if (provideds.size() > 0) {
-			Interface provided = provideds.get(0);
-			if (provided != null && provided.getName() != null) {
-				return "provides " + provided.getQualifiedName();
-			}
-		}
-		return "";
-	}
-
-	String getRequired(Port port) {
-		EList<Interface> requireds = port.getRequireds();
-		if (requireds.size() > 0) {
-			Interface required = requireds.get(0);
-			if (required != null && required.getName() != null) {
-				// add space, if provided is non null
-				String space = getProvided(port).length() > 0 ? " " : "";
-				return space + "requires " + required.getQualifiedName();
-			}
-		}
-		return "";
-	}
-
-	public String getDefaultValue(Property attribute) {
-		ValueSpecification value = attribute.getDefaultValue();
-		if (value != null) {
-			String strVal = value.stringValue();
-			if (strVal != null) {
-				// quote all non numeric values.
-				if (!(strVal.charAt(0) >= '0' && strVal.charAt(0) <= '9')) {
-					strVal = "\"" + strVal + "\"";
-				}
-				return " = " + strVal;
-			}
-		}
-		return "";
-	}
-
-	public String getType(Property attribute) {
-		if (attribute.getType() != null) {
-			return ": " + attribute.getType().getQualifiedName();
-		}
-		return "";
-	}
-
-
-	public String getComment(Property parameter) {
-		Entity entity = UMLUtil.getStereotypeApplication(parameter, Entity.class);
-		if (entity != null) {
-			return "	// " + entity.getDescription();
-		}
-		else return "";
-	}
+	
 
 	/*
 	 * (non-Javadoc)
@@ -221,32 +166,7 @@
 			if (invalidStr != null) {
 				return invalidStr;
 			}
-			String textToEdit = String.format("component %s\n", clazz.getName());
-			textToEdit += "{\n";
-			// Triggers
-			for (Port port : clazz.getOwnedPorts()) {
-				textToEdit += String.format("\tport %s %s%s\n", port.getName(),
-						getProvided(port), getRequired(port));
-			}
-			for (Property attribute : clazz.getOwnedAttributes()) {
-				Type type = attribute.getType();
-				if (type != null && StereotypeUtil.isApplied(type, Activity.class)) {
-					textToEdit += String.format("\tactivity %s%s\n", type.getName(),
-							getDefaultValue(attribute));
-				}
-			}
-			StructuredClassifier paramClass = ParameterUtils.getParameterClass(clazz);
-			if (paramClass != null) {
-				for (Property parameter : paramClass.getAttributes()) {
-					// Type type = attribute.getType();
-					// if (StereotypeUtil.isApplied(type, ParameterEntry.class)) {
-					textToEdit += String.format("\tparameter %s%s%s%s\n", parameter.getName(),
-							getType(parameter), getDefaultValue(parameter), getComment(parameter));
-				}
-			}
-
-			textToEdit += "}\n";
-			return textToEdit;
+			return UnparseCompDef.getCompDefText(clazz).toString();
 		}
 
 		return "not a component";
diff --git a/plugins/customization/textedit/org.eclipse.papyrus.robotics.xtext.compdef.ui/src/org/eclipse/papyrus/robotics/xtext/compdef/ui/contribution/UnparseCompDef.xtend b/plugins/customization/textedit/org.eclipse.papyrus.robotics.xtext.compdef.ui/src/org/eclipse/papyrus/robotics/xtext/compdef/ui/contribution/UnparseCompDef.xtend
new file mode 100644
index 0000000..6366c52
--- /dev/null
+++ b/plugins/customization/textedit/org.eclipse.papyrus.robotics.xtext.compdef.ui/src/org/eclipse/papyrus/robotics/xtext/compdef/ui/contribution/UnparseCompDef.xtend
@@ -0,0 +1,104 @@
+/*****************************************************************************
+ * Copyright (c) 2020 CEA LIST.
+ *
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ *  CEA LIST - Initial API and implementation
+ *
+ *****************************************************************************/
+ 
+ package org.eclipse.papyrus.robotics.xtext.compdef.ui.contribution
+
+import org.eclipse.uml2.uml.Class
+import org.eclipse.uml2.uml.Port
+import org.eclipse.uml2.uml.Property
+import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil
+import org.eclipse.papyrus.robotics.profile.robotics.components.Activity
+import org.eclipse.papyrus.robotics.core.utils.ParameterUtils
+import org.eclipse.papyrus.robotics.bpc.profile.bpc.Entity
+import org.eclipse.uml2.uml.util.UMLUtil
+
+class UnparseCompDef {
+	
+	def static getCompDefText(Class clazz) '''
+		component «clazz.name» {
+			«FOR port : clazz.ownedPorts»
+				port «port.name» «port.provided»«port.required»
+			«ENDFOR»
+			«FOR attribute : clazz.ownedAttributes»
+				«val  type = attribute.type»
+				«IF type !== null && StereotypeUtil.isApplied(type, Activity)»
+					activity «type.name»«attribute.defaultValue»
+				«ENDIF»
+			«ENDFOR»
+			«val paramClass = ParameterUtils.getParameterClass(clazz)»
+			«IF paramClass !== null»
+				«FOR parameter : paramClass.attributes»
+					parameter «parameter.name»«parameter.typeStr»«parameter.defaultValueStr»«parameter.getComment»
+				«ENDFOR»
+			«ENDIF»
+		}
+	'''
+	
+	def static String getProvided(Port port) {
+		val provideds = port.getProvideds()
+		if (provideds.size() > 0) {
+			val provided = provideds.get(0)
+			if (provided !== null && provided.getName() !== null) {
+				return '''provides «provided.qualifiedName»'''
+			}
+		}
+		return ""
+	}
+
+	def static String getRequired(Port port) {
+		val requireds = port.getRequireds()
+		if (requireds.size() > 0) {
+			val required = requireds.get(0)
+			if (required !== null && required.getName() !== null) {
+				// add space, if provided is non empty
+				return '''«IF port.provided.length > 0» «ENDIF»requires «required.qualifiedName»'''
+			}
+		}
+		return ""
+	}
+
+	def static getDefaultValueStr(Property attribute) {
+		val value = attribute.getDefaultValue()
+		if (value !== null) {
+			var strVal = value.stringValue()
+			if (strVal !== null) {
+				// quote all non numeric values.
+				if (!(strVal.charAt(0) >= '0' && strVal.charAt(0) <= '9')) {
+					strVal = "\"" + strVal + "\""
+				}
+				return " = " + strVal
+			}
+		}
+		return ""
+	}
+
+	def static getTypeStr(Property attribute) {
+		if (attribute.type !== null) {
+			return ": " + attribute.type.qualifiedName
+		}
+		return ""
+	}
+
+
+	def static getComment(Property parameter) {
+		val entity = UMLUtil.getStereotypeApplication(parameter, Entity)
+		if (entity !== null && entity.getDescription !== null) {
+			return "	// " + entity.getDescription()
+		}
+		else return ""
+	}
+		
+}
\ No newline at end of file