Skip to content
Snippets Groups Projects
Commit 2d34ff62 authored by Alexander Diewald's avatar Alexander Diewald
Browse files

Adds the possibility to filter the list of available choices for an enum-type...

Adds the possibility to filter the list of available choices for an enum-type annotation via the annotation's value provider.
Therefore, a map containing the class of the to be filtered model elements and the allowed enum literal must be given in the constructor of the value provider.
parent 9a97bd76
No related branches found
No related tags found
No related merge requests found
......@@ -76,13 +76,7 @@ public class LabelValueMapping {
EClassifier eType = eStructuralFeatureDescriptor.getEType(specification);
if(eType instanceof EEnum) {
labelToValueMap = new TreeMap<String, Object>();
Set<String> enumValues = new TreeSet<String>();
for(EEnumLiteral e : ((EEnum)eType).getELiterals()) {
enumValues.add(e.getName());
addLabelValuePair(e.getName(), e.getInstance());
}
createEnumLabels((EEnum)eType, eStructuralFeatureDescriptor, modelElement);
} else if(eType instanceof EClass) {
EObject root = null;
......@@ -141,6 +135,25 @@ public class LabelValueMapping {
}
}
/**
* Creates the enum labels based on the literals that are given by the
* {@link EStructuralFeatureDescriptor}.
*/
private void createEnumLabels(EEnum eType,
EStructuralFeatureDescriptor eStructuralFeatureDescriptor, EObject modelElement) {
labelToValueMap = new TreeMap<String, Object>();
Set<String> enumValues = new TreeSet<String>();
EList<EEnumLiteral> literalsToShow =
eStructuralFeatureDescriptor.getEnumLiterals(modelElement);
if(eStructuralFeatureDescriptor.getEnumLiterals(modelElement) == null) {
literalsToShow = eType.getELiterals();
}
for(EEnumLiteral e : literalsToShow) {
enumValues.add(e.getName());
addLabelValuePair(e.getName(), e.getInstance());
}
}
/** Returns the set of labels managed by this {@link LabelValueMapping}. */
public Collection<String> getLabels() {
if(labelToValueMap == null) {
......
......@@ -17,8 +17,14 @@ $Id$
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.base.annotation.valueprovider;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
......@@ -72,12 +78,30 @@ public class EStructuralFeatureDescriptor {
*/
private EReferenceScope eReferenceScope;
/**
* Filter map that allows to define the valid set of {@link EEnumLiteral}s for a particular
* class. Only those {@link EEnumLiteral}s that are defined for the given class are available
* through the {@link IAnnotationValueProvider}.
*/
private Map<Class<?>, EList<EEnumLiteral>> enumLiteralFilter;
/** Constructs a {@link EStructuralFeatureDescriptor} for an {@link EAttribute}. */
public EStructuralFeatureDescriptor(EAttribute eAttribute) {
this.eStructuralFeature = eAttribute;
this.eReferenceScope = null;
}
/** Constructs a {@link EStructuralFeatureDescriptor} for an {@link EAttribute}. */
public EStructuralFeatureDescriptor(EAttribute eAttribute,
Map<Class<?>, EList<EEnumLiteral>> enumLiterals) {
this(eAttribute);
this.enumLiteralFilter = new HashMap<Class<?>, EList<EEnumLiteral>>();
if(enumLiterals != null && !enumLiterals.isEmpty()) {
this.enumLiteralFilter.putAll(enumLiterals);
}
}
/** Constructs a {@link EStructuralFeatureDescriptor} for an {@link EReference}. */
public EStructuralFeatureDescriptor(EReference eReference, EReferenceScope eReferenceScope) {
this.eStructuralFeature = eReference;
......@@ -144,4 +168,27 @@ public class EStructuralFeatureDescriptor {
}
return eType;
}
/**
* Returns the {@link EList} of literals that shall be available for given {@code modelElement}
* as defined in the {@link IAnnotationValueProvider}.
*
* @return <li>The list of allowed {@link EEnumLiteral}s if defined.</li> <li>{@code null} if
* there are no restrictions for the given {@code modelElement}, or none are defined for
* the {@link IAnnotationValueProvider}.</li>
*
*/
public EList<EEnumLiteral> getEnumLiterals(EObject modelElement) {
if(enumLiteralFilter != null) {
Class<?> filterForClass = null;
Iterator<Class<?>> classFilterIterator = enumLiteralFilter.keySet().iterator();
do {
filterForClass = classFilterIterator.next();
} while(classFilterIterator.hasNext() &&
filterForClass.isAssignableFrom(modelElement.getClass()));
return enumLiteralFilter.get(filterForClass);
}
return null;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment