Class FeatureLabels

java.lang.Object
com.amalgamasimulation.validation.FeatureLabels

public class FeatureLabels extends Object
Labels of model fields in the language of the user. Checks puts the label of a field into the text of a problem, for example "Field is not set (Speed)".

The class relies on a naming convention. In a subclass of PlatformMessages, the string with the label of a field has the same name as the constant of this field in the Literals interface of the EMF package. For example, the field DatamodelPackage.Literals.TRUCK_TYPE__SPEED has the label in the string MyMessages.TRUCK_TYPE__SPEED. Then putLiterals(Class, Object) finds the labels of all fields, and you do not have to set them one by one.

If there is no such string, or it has a different name, the field has no label. Then get(EStructuralFeature) returns the name of the field in the model, for example speed, and the user sees it without translation. Set such a label with put(EStructuralFeature, String).

Example:


 FeatureLabels labels = FeatureLabels.fromLiterals(DatamodelPackage.Literals.class, MyMessages.messages())
         .put(DatamodelPackage.Literals.MINE_ARC__LENGTH, MyMessages.messages().MINE_SEGMENT__LENGTH);
 

The class reads the labels once, when they are added.

  • Constructor Details

    • FeatureLabels

      public FeatureLabels()
  • Method Details

    • fromLiterals

      public static FeatureLabels fromLiterals(Class<?> literals, Object messages)
      Creates labels for all fields of an EMF package. The label of a field is the string in messages with the same name as the constant of the field.
      Parameters:
      literals - the Literals interface of the EMF package, for example DatamodelPackage.Literals.class
      messages - the object with the strings, usually a subclass of PlatformMessages
      Returns:
      the new labels
    • fromLiterals

      public static FeatureLabels fromLiterals(Class<?> literals, Object messages, UnaryOperator<String> messageName)
      Creates labels in the same way as fromLiterals(Class, Object), but gets the name of the string from the name of the constant with messageName. Use this method if the strings have a different naming rule.
      Parameters:
      literals - the Literals interface of the EMF package
      messages - the object with the strings
      messageName - the rule that converts the name of a constant to the name of a string, for example name -> "obj_" + name
      Returns:
      the new labels
    • putLiterals

      public FeatureLabels putLiterals(Class<?> literals, Object messages)
      Adds the labels of the fields of one more EMF package, for example the package of a derived model. The label of a field is the string in messages with the same name as the constant of the field. If the field already has a label, the new label replaces it. The method skips fields that have no string.
      Parameters:
      literals - the Literals interface of the EMF package
      messages - the object with the strings, usually a subclass of PlatformMessages
      Returns:
      this object, to continue the chain
    • putLiterals

      public FeatureLabels putLiterals(Class<?> literals, Object messages, UnaryOperator<String> messageName)
      Adds labels in the same way as putLiterals(Class, Object), but gets the name of the string from the name of the constant with messageName.
      Parameters:
      literals - the Literals interface of the EMF package
      messages - the object with the strings
      messageName - the rule that converts the name of a constant to the name of a string
      Returns:
      this object, to continue the chain
    • put

      public FeatureLabels put(org.eclipse.emf.ecore.EStructuralFeature feature, String label)
      Sets the label of one field. If the field already has a label, the new label replaces it. Use this method if the name of the string is different from the name of the constant of the field.
      Parameters:
      feature - the field of the model
      label - the label in the language of the user
      Returns:
      this object, to continue the chain
    • get

      public String get(org.eclipse.emf.ecore.EStructuralFeature feature)
      Returns the label of a field. If the field has no label, returns the name of the field in the model (feature.getName()).
      Parameters:
      feature - the field of the model
      Returns:
      the label of the field, or its name in the model