Class FeatureLabels
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic FeatureLabelsfromLiterals(Class<?> literals, Object messages) Creates labels for all fields of an EMF package.static FeatureLabelsfromLiterals(Class<?> literals, Object messages, UnaryOperator<String> messageName) Creates labels in the same way asfromLiterals(Class, Object), but gets the name of the string from the name of the constant withmessageName.get(org.eclipse.emf.ecore.EStructuralFeature feature) Returns the label of a field.Sets the label of one field.putLiterals(Class<?> literals, Object messages) Adds the labels of the fields of one more EMF package, for example the package of a derived model.putLiterals(Class<?> literals, Object messages, UnaryOperator<String> messageName) Adds labels in the same way asputLiterals(Class, Object), but gets the name of the string from the name of the constant withmessageName.
-
Constructor Details
-
FeatureLabels
public FeatureLabels()
-
-
Method Details
-
fromLiterals
Creates labels for all fields of an EMF package. The label of a field is the string inmessageswith the same name as the constant of the field.- Parameters:
literals- theLiteralsinterface of the EMF package, for exampleDatamodelPackage.Literals.classmessages- the object with the strings, usually a subclass ofPlatformMessages- Returns:
- the new labels
-
fromLiterals
public static FeatureLabels fromLiterals(Class<?> literals, Object messages, UnaryOperator<String> messageName) Creates labels in the same way asfromLiterals(Class, Object), but gets the name of the string from the name of the constant withmessageName. Use this method if the strings have a different naming rule.- Parameters:
literals- theLiteralsinterface of the EMF packagemessages- the object with the stringsmessageName- the rule that converts the name of a constant to the name of a string, for examplename -> "obj_" + name- Returns:
- the new labels
-
putLiterals
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 inmessageswith 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- theLiteralsinterface of the EMF packagemessages- the object with the strings, usually a subclass ofPlatformMessages- Returns:
- this object, to continue the chain
-
putLiterals
public FeatureLabels putLiterals(Class<?> literals, Object messages, UnaryOperator<String> messageName) Adds labels in the same way asputLiterals(Class, Object), but gets the name of the string from the name of the constant withmessageName.- Parameters:
literals- theLiteralsinterface of the EMF packagemessages- the object with the stringsmessageName- the rule that converts the name of a constant to the name of a string- Returns:
- this object, to continue the chain
-
put
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 modellabel- the label in the language of the user- Returns:
- this object, to continue the chain
-
get
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
-