From 55d6bdc7c168455e4326c4f7c27ae2ba9a2d6f9a Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Tue, 16 Dec 2014 07:41:23 +0000
Subject: [PATCH] - Do not cache LabelValueMapping since {@link
 IDerivedAnnotation}s support that the {@link {EStructuralFeature} of their
 return value is changed at runtime refs 1841

---
 .../ComboBoxEditingSupport.java               | 42 +++++++++++--------
 1 file changed, 24 insertions(+), 18 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 fa2141722..bd9ffeaad 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
@@ -31,6 +31,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.IDerivedAnnotation;
 import org.fortiss.tooling.base.ui.annotation.AnnotationEntry;
 import org.fortiss.tooling.base.ui.annotation.valueprovider.EStructuralFeatureDescriptor;
 
@@ -51,12 +52,6 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 	/** Flag if this {@link ComboBoxEditingSupport} is editable. */
 	boolean isEditable;
 
-	/**
-	 * Translates between labels shown in this {@link ComboBoxEditingSupport}, and the underlying
-	 * actual values (e.g., required for {@link EReference}s.
-	 */
-	private LabelValueMapping comboBoxLabelValueMapping;
-
 	/**
 	 * Input-choice based on an {@link EStructuralFeature} (i.e., {@link EEnum} or
 	 * {@link EReference}).
@@ -114,6 +109,16 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 		this.stringInputChoice = stringInputChoice;
 	}
 
+	/**
+	 * Creates a {@link LabelValueMapping} for the given {@link AnnotationEntry}. Note: Since
+	 * {@link IDerivedAnnotation}s support that the {@link EStructuralFeature} of their return value
+	 * is changed at runtime, the resulting {@link LabelValueMapping} should not be cached.
+	 */
+	private LabelValueMapping getLabelValueMapping(AnnotationEntry entry) {
+		return new LabelValueMapping(eStructuralFeatureDescriptor,
+				entry.getSpecification(specClass), entry.getModelElement(), stringInputChoice);
+	}
+
 	/** {@inheritDoc} */
 	@Override
 	protected CellEditor getCellEditor(Object element) {
@@ -122,14 +127,7 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 			return null;
 		}
 
-		AnnotationEntry entry = (AnnotationEntry)element;
-
-		comboBoxLabelValueMapping =
-				new LabelValueMapping(eStructuralFeatureDescriptor,
-						entry.getSpecification(specClass), entry.getModelElement(),
-						stringInputChoice);
-
-		cellEditor.setInput(comboBoxLabelValueMapping.getLabels());
+		cellEditor.setInput(getLabelValueMapping((AnnotationEntry)element).getLabels());
 
 		return cellEditor;
 	}
@@ -137,6 +135,10 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 	/** {@inheritDoc} */
 	@Override
 	protected void setValue(Object element, Object label) {
+		if(!(element instanceof AnnotationEntry)) {
+			return;
+		}
+
 		if(isEditable && label == null) {
 			// New values, i.e values that have been entered into the text input field, and have not
 			// been selected from the combo box are indicated by a null value. Hence, fetch the text
@@ -145,7 +147,8 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 		}
 
 		if(label != null) {
-			super.setValue(element, comboBoxLabelValueMapping.getValueForLabel(label.toString()));
+			super.setValue(element, getLabelValueMapping((AnnotationEntry)element)
+					.getValueForLabel(label.toString()));
 		}
 	}
 
@@ -160,11 +163,14 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 	 * {@link AnnotationEntry}).
 	 */
 	public String getLabel(Object element) {
-		// super.getValue() checks if element is actually an AnnotationEntry
+		if(!(element instanceof AnnotationEntry)) {
+			return null;
+		}
+
 		Object value = super.getValue(element);
 
-		if(value != null && comboBoxLabelValueMapping != null) {
-			return comboBoxLabelValueMapping.getLabelForValue(value);
+		if(value != null) {
+			return getLabelValueMapping((AnnotationEntry)element).getLabelForValue(value);
 		}
 
 		return null;
-- 
GitLab