From da02276b4d157ee9a654d2eb30f215f27df52423 Mon Sep 17 00:00:00 2001
From: Alexander Diewald <diewald@fortiss.org>
Date: Fri, 12 Dec 2014 08:50:37 +0000
Subject: [PATCH] Refactor the handling model element-dependent annotations,
 fixes the EReference handling. refs 2171

---
 .../ComboBoxEditingSupport.java               | 22 +++++--
 .../EStructuralFeatureValueProviderBase.java  | 57 +++++++++----------
 2 files changed, 44 insertions(+), 35 deletions(-)

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/ComboBoxEditingSupport.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/ComboBoxEditingSupport.java
index 4a2e771e9..517032f53 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/ComboBoxEditingSupport.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/ComboBoxEditingSupport.java
@@ -32,6 +32,7 @@ import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.swt.custom.CCombo;
 import org.eclipse.swt.widgets.Composite;
 import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
+import org.fortiss.tooling.base.model.element.IModelElement;
 import org.fortiss.tooling.base.ui.annotation.AnnotationEntry;
 import org.fortiss.tooling.base.ui.annotation.valueprovider.EStructuralFeatureDescriptor;
 
@@ -132,10 +133,12 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 	 */
 	public ComboBoxLabelValueMapping createComboBoxLabelValueMapping(EObject modelElement,
 			EStructuralFeature feature) {
-		// TODO: AD: Make the creation of the feature descriptor dependent on the need for supporting multiple features.
-		//			 Remove the cast.
-		return new ComboBoxLabelValueMapping(new EStructuralFeatureDescriptor((EAttribute)feature),
-				modelElement, stringInputChoice);
+		if(labelValueMappingMayChange(feature)) {
+			return new ComboBoxLabelValueMapping(new EStructuralFeatureDescriptor(
+					(EAttribute)feature), modelElement, stringInputChoice);
+		}
+		return new ComboBoxLabelValueMapping(eStructuralFeatureDescriptor, modelElement,
+				stringInputChoice);
 	}
 
 	/** {@inheritDoc} */
@@ -165,4 +168,15 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 		return null;
 	}
 
+	/**
+	 * Check, whether the {@link EStructuralFeatureDescriptor} may depend on the
+	 * {@link IModelElement}.
+	 */
+	private boolean labelValueMappingMayChange(EStructuralFeature feature) {
+		if(feature != null && feature instanceof EAttribute) {
+			return true;
+		}
+		return false;
+	}
+
 }
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/EStructuralFeatureValueProviderBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/EStructuralFeatureValueProviderBase.java
index 9d27d7505..0942e9bad 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/EStructuralFeatureValueProviderBase.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/EStructuralFeatureValueProviderBase.java
@@ -297,45 +297,40 @@ public abstract class EStructuralFeatureValueProviderBase<T extends IAnnotatedSp
 	public EditingSupport createEditingSupport(ColumnViewer viewer,
 			Class<? extends IAnnotatedSpecification> clazz, String instanceKey,
 			EStructuralFeature structuralFeature) throws Exception {
-		EStructuralFeature feature = structuralFeature;
-		if(feature == null) {
-			feature = getEStructuralFeature(instanceKey);
-		}
+		EStructuralFeatureDescriptor descriptor = null;
+		if(structuralFeature == null) {
+			structuralFeature = getEStructuralFeature(instanceKey);
 
-		if(feature.getUpperBound() > 1 ||
-				feature.getUpperBound() == ETypedElement.UNBOUNDED_MULTIPLICITY) {
-			throw new Exception(
-					"EStructuralValueProvider has not been implemented for structural feature multiplicity > 1.");
-		}
+			if(structuralFeature.getUpperBound() > 1 ||
+					structuralFeature.getUpperBound() == ETypedElement.UNBOUNDED_MULTIPLICITY) {
+				throw new Exception(
+						"EStructuralValueProvider has not been implemented for structural feature multiplicity > 1.");
+			}
 
-		EClassifier eType = null;
-		try {
-			eType = feature.getEType();
-		} catch(Exception e) {
-			// Ignore
-		}
+			EClassifier eType = null;
+			try {
+				eType = structuralFeature.getEType();
+			} catch(Exception e) {
+				// Ignore
+			}
 
-		if(((!(eType instanceof EEnum)) && (!(eType instanceof EClass))) ||
-				((eType instanceof EClass) && (structuralFeatureDescriptorMap.get(instanceKey)
-						.getEReferenceScope() == null))) {
+			if(((!(eType instanceof EEnum)) && (!(eType instanceof EClass))) ||
+					((eType instanceof EClass) && (structuralFeatureDescriptorMap.get(instanceKey)
+							.getEReferenceScope() == null))) {
 
-			return super.createEditingSupport(viewer, clazz, instanceKey, null);
+				return super.createEditingSupport(viewer, clazz, instanceKey, null);
+			}
+			return new ComboBoxEditingSupport(viewer, clazz, instanceKey,
+					structuralFeatureDescriptorMap.get(instanceKey));
 		}
-
 		// Create a new EStructuralFeatureDescriptor for specifications that return different
 		// features per model element.
-		EStructuralFeatureDescriptor descriptor = null;
-		if(structuralFeature != null) {
-			if(feature instanceof EAttribute) {
-				descriptor = new EStructuralFeatureDescriptor((EAttribute)feature);
-			} else {
-				throw new Exception(
-						"Creating EStructuralFeature-dependent EditingSupports is not implemented for features that are not of the tye EAttribute.");
-			}
-		} else {
-			descriptor = structuralFeatureDescriptorMap.get(instanceKey);
+		if(structuralFeature instanceof EAttribute) {
+			descriptor = new EStructuralFeatureDescriptor((EAttribute)structuralFeature);
+			return new ComboBoxEditingSupport(viewer, clazz, instanceKey, descriptor);
 		}
-		return new ComboBoxEditingSupport(viewer, clazz, instanceKey, descriptor);
+		throw new Exception("Creating an EditingSupport for EStructuralFeatures of the type " +
+				structuralFeature.getEType().toString() + " is not supported");
 	}
 
 	/**
-- 
GitLab