From 14127e8cca6cad0e0751ef4e5d15773f91928ec4 Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Tue, 3 Apr 2018 13:08:51 +0000
Subject: [PATCH] - Update available choices every time the corresponding combo
 box is opened - Move EditingSupportFactory.initializeInputChoice() to
 ValueProviderBase.updateInputChoice() refs 3247

---
 .../ui/annotation/editingsupport/.ratings     |  4 +-
 .../ComboBoxEditingSupport.java               | 31 ++++------
 .../editingsupport/EditingSupportFactory.java | 60 +------------------
 .../base/annotation/valueprovider/.ratings    |  2 +-
 .../valueprovider/ValueProviderBase.java      | 49 +++++++++++++++
 5 files changed, 67 insertions(+), 79 deletions(-)

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/.ratings b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/.ratings
index 34277d244..fd5794995 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/.ratings
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/.ratings
@@ -1,7 +1,7 @@
 AnnotationEditingSupportBase.java 7e542e9d1084e929b798cf19de5c6b0e995e5922 GREEN
 CheckBoxEditingSupport.java 5ef3594428071da0c8df68202a101fda0fbc516c GREEN
-ComboBoxEditingSupport.java 8f2dfe75250de3a764c62ab73e21e217ece44d53 GREEN
-EditingSupportFactory.java b174a19f502d46f7e2ebefdc50437c6bdc007a97 GREEN
+ComboBoxEditingSupport.java e8536d85ac787d9e14c79a800d8b6a1fc6053499 YELLOW
+EditingSupportFactory.java 1c4226e983ac7a7b63df273323ed9aec98d1b238 YELLOW
 ElementCommentEditingSupport.java b7cbec5084aab81efc1da6b48647b172206f6256 GREEN
 ElementEditingSupportBase.java f7e731a8720aa8c2a0f12b57c0ae11e21dad22cc GREEN
 ElementNameEditingSupport.java 6393bb52ad1f7ff4823a3f0dd30128124b0e3347 GREEN
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 57f901813..e95d10a7d 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
@@ -16,7 +16,6 @@
 package org.fortiss.tooling.base.ui.annotation.editingsupport;
 
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.List;
 
 import org.eclipse.emf.ecore.EClass;
@@ -33,6 +32,7 @@ import org.eclipse.swt.custom.CCombo;
 import org.eclipse.swt.widgets.Composite;
 import org.fortiss.tooling.base.annotation.AnnotationEntry;
 import org.fortiss.tooling.base.annotation.valueprovider.EStructuralFeatureDescriptor;
+import org.fortiss.tooling.base.annotation.valueprovider.ValueProviderBase;
 import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
 import org.fortiss.tooling.base.model.element.IDerivedAnnotation;
 
@@ -57,14 +57,11 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 	 */
 	EStructuralFeatureDescriptor eStructuralFeatureDescriptor;
 
-	/** {@link String}-based input choice */
-	Collection<String> stringInputChoice;
-
 	/**
 	 * Base constructor that sets up the {@link ComboBoxEditingSupport} (apart from the set of
 	 * available choices which is prepared on demand in {@link #getCellEditor(Object)}.
 	 */
-	private ComboBoxEditingSupport(ColumnViewer viewer,
+	public ComboBoxEditingSupport(ColumnViewer viewer,
 			Class<? extends IAnnotatedSpecification> clazz, boolean isEditable) {
 		super(viewer, clazz);
 
@@ -95,18 +92,6 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 		this.eStructuralFeatureDescriptor = eStructuralFeatureDescriptor;
 	}
 
-	/**
-	 * Creates a {@link ComboBoxEditingSupport} for a {@link String}-based (i.e., not
-	 * {@link EStructuralFeature}-based input-choice.
-	 */
-	public ComboBoxEditingSupport(ColumnViewer viewer,
-			Class<? extends IAnnotatedSpecification> clazz, Collection<String> stringInputChoice,
-			boolean isEditable) {
-
-		this(viewer, clazz, isEditable);
-		this.stringInputChoice = stringInputChoice;
-	}
-
 	/**
 	 * <p>
 	 * Creates a {@link LabelValueMapping} for the given {@link AnnotationEntry}.
@@ -117,9 +102,17 @@ public class ComboBoxEditingSupport extends AnnotationEditingSupportBase {
 	 * cached.
 	 * </p>
 	 */
-	private LabelValueMapping getLabelValueMapping(AnnotationEntry entry) {
+	@SuppressWarnings("unchecked")
+	private <T extends IAnnotatedSpecification> LabelValueMapping getLabelValueMapping(
+			AnnotationEntry entry) {
+		ValueProviderBase<T> valueProvider =
+				(ValueProviderBase<T>)entry.getAnnotationValueProvider(specClass);
+		valueProvider.updateInputChoice(getViewer().getInput(), (Class<T>)specClass);
+
 		return new LabelValueMapping(eStructuralFeatureDescriptor,
-				entry.getSpecification(specClass), entry.getModelElement(), stringInputChoice);
+				entry.getSpecification(specClass), entry.getModelElement(),
+				valueProvider.getCurrentInputChoice());
+
 	}
 
 	/** {@inheritDoc} */
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/EditingSupportFactory.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/EditingSupportFactory.java
index dd1ad7608..291d4d038 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/EditingSupportFactory.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/editingsupport/EditingSupportFactory.java
@@ -17,18 +17,13 @@ package org.fortiss.tooling.base.ui.annotation.editingsupport;
 
 import java.util.Collection;
 import java.util.List;
-import java.util.Set;
-import java.util.TreeSet;
 
-import org.eclipse.emf.common.util.EList;
 import org.eclipse.emf.ecore.EClass;
 import org.eclipse.emf.ecore.EClassifier;
 import org.eclipse.emf.ecore.EEnum;
-import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.EStructuralFeature;
 import org.eclipse.jface.viewers.ColumnViewer;
 import org.eclipse.jface.viewers.EditingSupport;
-import org.fortiss.tooling.base.annotation.AnnotationEntry;
 import org.fortiss.tooling.base.annotation.valueprovider.DerivedAnnotationValueProviderBase;
 import org.fortiss.tooling.base.annotation.valueprovider.EStructuralFeatureDescriptor;
 import org.fortiss.tooling.base.annotation.valueprovider.EStructuralFeatureDescriptor.EReferenceScope;
@@ -38,7 +33,6 @@ import org.fortiss.tooling.base.annotation.valueprovider.ValueProviderBase;
 import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
 import org.fortiss.tooling.base.model.element.IDerivedAnnotation;
 import org.fortiss.tooling.base.ui.annotation.view.generic.GenericAnnotationView;
-import org.fortiss.tooling.kernel.utils.EcoreUtils;
 
 /**
  * Factory to build {@link AnnotationEditingSupportBase}-based {@link EditingSupport} for the
@@ -125,54 +119,6 @@ public class EditingSupportFactory {
 				"For feature multiplicity > 1, EStructuralValueProvider has been implemented for EReferences and EEnumns, only .");
 	}
 
-	/**
-	 * Helper method for
-	 * {@link #createEditingSupport4ValueProviderBase(ValueProviderBase, ColumnViewer, Class, IAnnotatedSpecification)}
-	 * that initialized the current input map of the value provider.
-	 * 
-	 * @param valueProvider
-	 *            Annotation's {@link IAnnotationValueProvider}
-	 * @param viewer
-	 *            {@link ColumnViewer} used to display annotation
-	 * @param clazz
-	 *            Annotation type
-	 */
-	@SuppressWarnings("unchecked")
-	private static <T extends IAnnotatedSpecification, V extends ValueProviderBase<T>> void
-			initializeInputChoice(V valueProvider, ColumnViewer viewer,
-					Class<? extends IAnnotatedSpecification> clazz) throws Exception {
-		// Input choice is not set, or it is restricted to concrete set of values
-		// (use SingleEnumAttributeValueProviderBase for EEnum types!)s
-		List<String> fixedInputChoice = valueProvider.getFixedInputChoice();
-		if(fixedInputChoice == null || !fixedInputChoice.isEmpty()) {
-			valueProvider.setCurrentInputChoice(fixedInputChoice);
-		} else {
-			Object viewerInput = viewer.getInput();
-			if(!(viewerInput instanceof Set<?>) || ((Set<?>)viewerInput).isEmpty() ||
-					!(((Set<?>)viewerInput).iterator().next() instanceof AnnotationEntry)) {
-				valueProvider.setCurrentInputChoice(null);
-			} else {
-
-				AnnotationEntry annotationEntry =
-						((Set<AnnotationEntry>)viewerInput).iterator().next();
-
-				EObject root = annotationEntry.getModelElement();
-				while(root.eContainer() != null) {
-					root = root.eContainer();
-				}
-
-				Set<String> choices = new TreeSet<String>();
-				for(T specification : (EList<T>)EcoreUtils.getChildrenWithType(root, clazz)) {
-					Object annotationValue = valueProvider.getAnnotationValue(specification);
-					if(annotationValue != null) {
-						choices.add(annotationValue.toString());
-					}
-				}
-				valueProvider.setCurrentInputChoice(choices);
-			}
-		}
-	}
-
 	/**
 	 * Creates the {@link EditingSupport} for value providers that are directly based on
 	 * {@link ValueProviderBase}.
@@ -188,13 +134,14 @@ public class EditingSupportFactory {
 	 * @return {@link EditingSupport} for value providers that are directly based on
 	 *         {@link ValueProviderBase}.
 	 */
+	@SuppressWarnings("unchecked")
 	private static <T extends IAnnotatedSpecification, V extends ValueProviderBase<T>>
 			EditingSupport createEditingSupport4ValueProviderBase(V valueProvider,
 					ColumnViewer viewer, Class<? extends IAnnotatedSpecification> clazz,
 					T specification) throws Exception {
 
 		try {
-			initializeInputChoice(valueProvider, viewer, clazz);
+			valueProvider.updateInputChoice(viewer.getInput(), (Class<T>)clazz);
 		} catch(Exception e) {
 			// Use standard text editor as fall-back
 			valueProvider.setCurrentInputChoice(null);
@@ -214,8 +161,7 @@ public class EditingSupportFactory {
 		// Input is restricted to concrete set of values
 		// (use SingleEnumAttributeValueProviderBase for EEnum types!)
 		List<String> fixedInputChoice = valueProvider.getFixedInputChoice();
-		return new ComboBoxEditingSupport(viewer, clazz, currentInputChoice,
-				fixedInputChoice.isEmpty());
+		return new ComboBoxEditingSupport(viewer, clazz, fixedInputChoice.isEmpty());
 	}
 
 	/**
diff --git a/org.fortiss.tooling.base/trunk/src/org/fortiss/tooling/base/annotation/valueprovider/.ratings b/org.fortiss.tooling.base/trunk/src/org/fortiss/tooling/base/annotation/valueprovider/.ratings
index 6dd7a9315..cdd3b3915 100644
--- a/org.fortiss.tooling.base/trunk/src/org/fortiss/tooling/base/annotation/valueprovider/.ratings
+++ b/org.fortiss.tooling.base/trunk/src/org/fortiss/tooling/base/annotation/valueprovider/.ratings
@@ -3,4 +3,4 @@ DerivedAnnotationValueProviderBase.java ae12ab7e386cc53a6a66dfc331ba43549b864b93
 EStructuralFeatureDescriptor.java b4093c8997472d0ab63a69c1d025d9aeb9b4f294 GREEN
 EStructuralFeatureValueProviderBase.java 6c9ff62ffce5373fa102054d96fba468abfe95f3 GREEN
 IAnnotationValueProvider.java 2d7b8ceff3ed83e7b5d2973bfa0632b9ff501d92 GREEN
-ValueProviderBase.java e577cc1cfffbad8ef47681f2dcdb2622f6be209e GREEN
+ValueProviderBase.java f8c32846d7a544cb37adf44c6353dad101df5bf2 YELLOW
diff --git a/org.fortiss.tooling.base/trunk/src/org/fortiss/tooling/base/annotation/valueprovider/ValueProviderBase.java b/org.fortiss.tooling.base/trunk/src/org/fortiss/tooling/base/annotation/valueprovider/ValueProviderBase.java
index fbc884688..1980e8711 100644
--- a/org.fortiss.tooling.base/trunk/src/org/fortiss/tooling/base/annotation/valueprovider/ValueProviderBase.java
+++ b/org.fortiss.tooling.base/trunk/src/org/fortiss/tooling/base/annotation/valueprovider/ValueProviderBase.java
@@ -17,6 +17,7 @@ package org.fortiss.tooling.base.annotation.valueprovider;
 
 import static org.fortiss.tooling.base.annotation.valueprovider.IAnnotationValueProvider.AnnotationActionEntry.ActionScope.ALL_VISIBLE_ITEMS;
 import static org.fortiss.tooling.base.annotation.valueprovider.IAnnotationValueProvider.AnnotationActionEntry.ActionScope.SINGLE_ITEM;
+import static org.fortiss.tooling.kernel.utils.EcoreUtils.getChildrenWithType;
 import static org.fortiss.tooling.kernel.utils.EcoreUtils.pickFirstInstanceOf;
 import static org.fortiss.tooling.kernel.utils.LoggingUtils.error;
 
@@ -24,6 +25,8 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
 import java.util.function.BiConsumer;
 
 import org.eclipse.emf.common.util.BasicEList;
@@ -356,4 +359,50 @@ public abstract class ValueProviderBase<T extends IAnnotatedSpecification> imple
 		}
 		return false;
 	}
+
+	/**
+	 * Updates the current input map of the value provider.
+	 * 
+	 * @param viewerInput
+	 *            Input object (provides by GUI).
+	 * @param clazz
+	 *            Annotation type
+	 */
+	@SuppressWarnings("unchecked")
+	public void updateInputChoice(Object viewerInput, Class<T> clazz) {
+		// Input choice is not set, or it is restricted to concrete set of values
+		// (use SingleEnumAttributeValueProviderBase for EEnum types!)s
+		List<String> fixedInputChoice = getFixedInputChoice();
+		if(fixedInputChoice == null || !fixedInputChoice.isEmpty()) {
+			setCurrentInputChoice(fixedInputChoice);
+		} else {
+			if(!(viewerInput instanceof Set<?>) || ((Set<?>)viewerInput).isEmpty() ||
+					!(((Set<?>)viewerInput).iterator().next() instanceof AnnotationEntry)) {
+				setCurrentInputChoice(null);
+			} else {
+
+				AnnotationEntry annotationEntry =
+						((Set<AnnotationEntry>)viewerInput).iterator().next();
+
+				EObject root = annotationEntry.getModelElement();
+				while(root.eContainer() != null) {
+					root = root.eContainer();
+				}
+
+				Set<String> choices = new TreeSet<String>();
+				for(T specification : getChildrenWithType(root, clazz)) {
+					Object annotationValue;
+					try {
+						annotationValue = getAnnotationValue(specification);
+					} catch(Exception e) {
+						annotationValue = null;
+					}
+					if(annotationValue != null) {
+						choices.add(annotationValue.toString());
+					}
+				}
+				setCurrentInputChoice(choices);
+			}
+		}
+	}
 }
-- 
GitLab