Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
<copyright> Copyright (c) 2011-2012 Springsite BV (The Netherlands) and others All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Martin Taal - Initial API and implementation </copyright> $Id: EntityManagerProvider.java,v 1.7 2011/09/26 19:48:10 mtaal Exp $
 
 package org.eclipse.emf.texo.provider;
 
 import java.util.List;
 import java.util.Map;
 
 import  org.eclipse.emf.ecore.EAnnotation;
 import  org.eclipse.emf.ecore.EAttribute;
 import  org.eclipse.emf.ecore.EClass;
 import  org.eclipse.emf.ecore.EDataType;
 import  org.eclipse.emf.ecore.EObject;
 import  org.eclipse.emf.ecore.EReference;
 import  org.eclipse.emf.ecore.EStructuralFeature;
 import  org.eclipse.emf.ecore.util.FeatureMapUtil;
 import  org.eclipse.emf.ecore.xml.type.XMLTypePackage;
Provides the title of an object, can be used to fill comboboxes and such. The title of an object can be defined in 2 ways:
  • By annotating one or more EStructuralFeature of an EClass with an EAnnotation with source ModelConstants.TITLE_ANNOTATION_SOURCE.
  • By annotating an EClass with an EAnnotation with source ModelConstants.TITLE_ANNOTATION_SOURCE. This annotation is considered to be an expression.
When creating a title of an object the system will handle the above cases in different ways:
  • EStructuralFeature annotation: the values of the title EStructuralFeature annotations are concatenated with a ModelConstants.TITLE_SEPARATOR. The separaotor can be customized by extending this class and overriding getTitleSeparator().
  • EClass annotation: the value of the EClass annotation is considered to be an expression which contains references to EStructuralFeatures, for example: ${lastName}, ${firstName}
Note that an EReference can be part of a title, the title generation process will step one level deep to generate a title. This to prevent infinite cycles. This means that a title which consists of an EReference and the referenced type has again a title with an EReference that for that second title the Object.toString() method is called to generate a title when generating a title for the first EReference. Future functionality:
  • make it possible to create/define a short title
  • support the dot notation in the title expression

Author(s):
Martin Taal
 
 public class TitleProvider implements TexoComponentTexoStaticSingleton {
 
   private static TitleProvider instance = ComponentProvider.getInstance().newInstance(TitleProvider.class);
 
   private static final EDataType STRING_EDATATYPE = XMLTypePackage.eINSTANCE.getString();
 
   public static TitleProvider getInstance() {
     return ;
   }
 
   public static void setInstance(TitleProvider instance) {
     . = instance;
   }
 
   private final Map<EClass, EClassTitleProvidertitleProviders = new ConcurrentHashMap<EClass, EClassTitleProvider>();

  
Determine the title of an object.

Parameters:
object must be an object which can be converted to a ModelObject using the ModelResolver.getModelObject(Object).
Returns:
the title of an object.
 
   public String getTitle(Object object) {
    if (object instanceof EObject) {
      final EObject eObject = (EObject) object;
      return getCreateTitleProvider(eObject.eClass()).getTitle(eObject);
    }
    final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(object);
    return getCreateTitleProvider(modelObject.eClass()).getTitle(modelObject);
  }

  
Will determine the title of an object, the nextStep parameter defines if title EStructuralFeatures which are EReferences should be traversed. For the first step in the title generation process this is done, for subsequent steps this is not done to prevent infinite cycles.

Parameters:
object must be an object which can be converted to a ModelObject using the ModelResolver.getModelObject(Object).
nextStep if true then EReferences are traversed to generate the title, if false then EReferences are not traversed.
Returns:
the title of an object
  protected String getTitle(Object objectboolean nextStep) {
    final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(object);
    return getCreateTitleProvider(modelObject.eClass()).getTitle(modelObjectnextStep);
  }
  protected String getTitleSeparator() {
  }
  protected EClassTitleProvider getCreateTitleProvider(EClass eClass) {
    EClassTitleProvider titleProvider = .get(eClass);
    if (titleProvider != null) {
      return titleProvider;
    }
    // determine what type of titleprovider should be used
    String expression;
    if ((expression = ModelUtils.getEAnnotation(eClass.)) != null) {
      final ExpressionTitleProvider expressionTitleProvider = ComponentProvider.getInstance().newInstance(
          ExpressionTitleProvider.class);
      expressionTitleProvider.setEClass(eClass);
      expressionTitleProvider.setExpression(expression);
      titleProvider = expressionTitleProvider;
    } else {
      // find efeatures with the title annotations
      final List<EStructuralFeature> titleEFeatures = new ArrayList<EStructuralFeature>();
      for (EStructuralFeature eFeature : eClass.getEAllStructuralFeatures()) {
        if (ModelUtils.hasEAnnotation(eFeature.)) {
          titleEFeatures.add(eFeature);
        }
      }
      if (titleEFeatures.isEmpty()) {
        // take the first String eAttribute
        for (EAttribute eAttribute : eClass.getEAllAttributes()) {
          // check if a string, note TITLE_SEPARATOR is used but any string
          // could be used for this check
          if (eAttribute.getEAttributeType() == 
              || eAttribute.getEAttributeType().isInstance(.)) {
            titleEFeatures.add(eAttribute);
            break;
          }
        }
      }
      // take the first eattribute
      if (titleEFeatures.isEmpty() && eClass.getEAllAttributes().size() > 0) {
        titleEFeatures.add(eClass.getEAllAttributes().get(0));
      }
      // take the first estructuralfeature
      if (titleEFeatures.isEmpty() && eClass.getEAllStructuralFeatures().size() > 0) {
        titleEFeatures.add(eClass.getEAllStructuralFeatures().get(0));
      }
      final EFeatureTitleProvider eFeatureTitleProvider = new EFeatureTitleProvider();
      eFeatureTitleProvider.setEFeatures(titleEFeatures);
      titleProvider = eFeatureTitleProvider;
    }
    .put(eClasstitleProvider);
    return titleProvider;
  }
  public abstract static class EClassTitleProvider {
    protected abstract String getTitle(ModelObject<?> modelObjectboolean nextStep);
    protected abstract String getTitle(EObject modelObjectboolean nextStep);
    public String getTitle(EObject modelObject) {
      return getTitle(modelObjecttrue);
    }
    public String getTitle(ModelObject<?> modelObject) {
      return getTitle(modelObjecttrue);
    }
  }
  public static class EFeatureTitleProvider extends EClassTitleProvider {
    private List<EStructuralFeature> eFeatures = new ArrayList<EStructuralFeature>();
    @Override
    protected String getTitle(EObject modelObjectboolean nextStep) {
      if (.isEmpty()) {
        // only return the type name
        return modelObject.eClass().getName();
      }
      final StringBuilder sb = new StringBuilder();
      for (EStructuralFeature eFeature : ) {
        if (FeatureMapUtil.isFeatureMap(eFeature)) {
          continue;
        }
        if (sb.length() > 0) {
          sb.append(.);
        }
        Object value = modelObject.eGet(eFeature);
        if (value == null) {
          continue;
        }
        if (eFeature instanceof EAttribute) {
          if (value instanceof Collection<?>) {
            if (((Collection<?>) value).isEmpty()) {
              continue;
            }
            value = ((Collection<?>) value).iterator().next();
            if (value == null) {
              continue;
            }
          }
          final EDataType eDataType = ((EAttribute) eFeature).getEAttributeType();
          sb.append(eDataType.getEPackage().getEFactoryInstance().convertToString(eDataTypevalue));
        } else if (nextStep) {
          if (eFeature.isMany()) {
            for (Object o : (Collection<?>) value) {
              if (o instanceof EObject) {
                sb.append(TitleProvider.getInstance().getTitle(ofalse));
              }
            }
          } else {
            if (value instanceof EObject) {
              sb.append(TitleProvider.getInstance().getTitle(valuefalse));
            }
          }
        }
      }
      return sb.toString();
    }
    @Override
    protected String getTitle(ModelObject<?> modelObjectboolean nextStep) {
      if (.isEmpty()) {
        // just return the type name
        return modelObject.eClass().getName();
      }
      final StringBuilder sb = new StringBuilder();
      for (EStructuralFeature eFeature : ) {
        if (sb.length() > 0) {
          sb.append(.);
        }
        final Object value = modelObject.eGet(eFeature);
        if (value == null) {
          continue;
        }
        if (eFeature instanceof EAttribute) {
          final EDataType eDataType = ((EAttribute) eFeature).getEAttributeType();
          if (eFeature.isMany()) {
            for (Object o : (Collection<?>) value) {
              sb.append(eDataType.getEPackage().getEFactoryInstance().convertToString(eDataTypeo));
            }
          } else {
            sb.append(eDataType.getEPackage().getEFactoryInstance().convertToString(eDataTypevalue));
          }
        } else if (nextStep) {
          if (eFeature.isMany()) {
            for (Object o : (Collection<?>) value) {
              if (ModelResolver.getInstance().isModelEnabled(o)) {
                sb.append(TitleProvider.getInstance().getTitle(ofalse));
              }
            }
          } else {
            if (ModelResolver.getInstance().isModelEnabled(value)) {
              sb.append(TitleProvider.getInstance().getTitle(valuefalse));
            }
          }
        }
      }
      return sb.toString();
    }
    public List<EStructuralFeature> getEFeatures() {
      return ;
    }
    public void setEFeatures(List<EStructuralFeature> eFeatures) {
      this. = eFeatures;
    }
  }
  public static class ExpressionTitleProvider extends EClassTitleProvider implements TexoComponent {
    private static final String EXPRESSION_START = "${"//$NON-NLS-1$
    private static final int EXPRESSION_START_LENGTH = .length();
    private static final String EXPRESSION_END = "}"//$NON-NLS-1$
    private String expression = null;
    private List<ObjectexpressionList = new ArrayList<Object>();
    private EClass eClass;
    @Override
    protected String getTitle(EObject modelObjectboolean nextStep) {
      final StringBuilder sb = new StringBuilder();
      for (Object element : ) {
        if (element instanceof String) {
          sb.append((Stringelement);
        } else {
          final Object value = modelObject.eGet((EStructuralFeature) element);
          if (element instanceof EReference) {
            if (nextStep) {
              sb.append(TitleProvider.getInstance().getTitle(valuefalse));
            }
          } else if (element instanceof EAttribute) {
            final EDataType eDataType = ((EAttribute) element).getEAttributeType();
            sb.append(eDataType.getEPackage().getEFactoryInstance().convertToString(eDataTypevalue));
          } else {
            throw new IllegalStateException("Type " + element.getClass().getName() //$NON-NLS-1$
                + " not supported, expression " + ); //$NON-NLS-1$
          }
        }
      }
      return sb.toString();
    }
    @Override
    protected String getTitle(ModelObject<?> modelObjectboolean nextStep) {
      final StringBuilder sb = new StringBuilder();
      for (Object element : ) {
        if (element instanceof String) {
          sb.append((Stringelement);
        } else {
          final Object value = modelObject.eGet((EStructuralFeature) element);
          if (element instanceof EReference) {
            if (nextStep) {
              sb.append(TitleProvider.getInstance().getTitle(valuefalse));
            }
          } else if (element instanceof EAttribute) {
            final EDataType eDataType = ((EAttribute) element).getEAttributeType();
            sb.append(eDataType.getEPackage().getEFactoryInstance().convertToString(eDataTypevalue));
          } else {
            throw new IllegalStateException("Type " + element.getClass().getName() //$NON-NLS-1$
                + " not supported, expression " + ); //$NON-NLS-1$
          }
        }
      }
      return sb.toString();
    }
    public void setExpression(String expression) {
      this. = expression;
      int previousIndex = 0;
      do {
        int index = expression.indexOf(previousIndex);
        if (index != -1) {
          int nextIndex = expression.indexOf(index);
          if (nextIndex == -1) {
            break;
          }
          if (index > previousIndex) {
            .add(expression.substring(previousIndexindex));
          }
          final String featureName = expression.substring(index + nextIndex);
          final EStructuralFeature eFeature = .getEStructuralFeature(featureName);
          if (eFeature == null) {
            .add("FEATURE NOT FOUND: " + featureName); //$NON-NLS-1$
          } else {
            .add(eFeature);
          }
          previousIndex = nextIndex + 1;
        } else {
          if (previousIndex < expression.length()) {
            .add(expression.substring(previousIndex));
          }
          break;
        }
      } while (true);
    }
    public EClass getEClass() {
      return ;
    }
    public void setEClass(EClass eClass) {
      this. = eClass;
    }
  }
New to GrepCode? Check out our FAQ X