Class BaseMessageRegistry<M>
- Type Parameters:
M
- the message class type
When updating the dependencies from Java 7 to Java 8, this class can be replaced by a more modern variant that makes use of functional interfaces and method references as shown in the above linked blog post.
To use the registry you need to implement a subclass of
BaseMessageRegistry
that is typed to the messages class that it
is related to. The main thing to do is to override
updateMessages(M)
while getting the messages instance injected.
@Creatable public class ExampleMessageRegistry extends BaseMessageRegistry<ExampleMessages> { @Override @Inject public void updateMessages(@Translation ExampleMessages messages) { super.updateMessages(messages); } }
Note that the registry instance is annotated with @Creatable so it is created per requestor and is making use of DI.
- Since:
- 2.0
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected MessageConsumer
createConsumer
(Object control, String method) protected MessageSupplier
createSupplier
(String messageKey) void
Binds a method of an object to a message.void
register
(MessageConsumer consumer, MessageFunction<M> function) Register a consumer and a function that is acting as the supplier of the translation value.void
register
(MessageConsumer consumer, MessageSupplier supplier) Register a binding for the given consumer and supplier.void
registerProperty
(Object control, String property, String messageKey) Binds the setter of a property of an object to a message.void
updateMessages
(M messages) This method performs the localization update for all bound objects.
-
Constructor Details
-
BaseMessageRegistry
public BaseMessageRegistry()
-
-
Method Details
-
register
Register a consumer and a function that is acting as the supplier of the translation value.This method allows to register a binding using method references and lambdas if used in an environment that already uses Java 8.
@Inject ExampleMessageRegistry registry; Label myFirstLabel = new Label(parent, SWT.WRAP); registry.register(myFirstLabel::setText, (m) -> m.firstLabelMessage);
- Parameters:
consumer
- The consumer of the message.function
- The function that supplies the message.
-
register
Register a binding for the given consumer and supplier.Unless you don't want to anonymously implement the consumer and supplier interfaces yourself, use the register methods that take the Control instance and String(s) as parameters.
- Parameters:
consumer
- The consumer of the message.supplier
- The supplier of the message.- See Also:
-
register
Binds a method of an object to a message. Doing this the specified method will be called on the instance with the message String as parameter that is retrieved via message key out of the local Messages instance.- Parameters:
control
- The control for which a message binding should be createdmethod
- The method that should be bound. Methods that can be bound need to accept one String parameter.messageKey
- The key of the message property that should be bound- See Also:
-
registerProperty
Binds the setter of a property of an object to a message. Doing this the setter of the given property will be called on the instance with the message String as parameter that is retrieved via message key out of the local Messages instance.- Parameters:
control
- The control for which a message binding should be createdproperty
- The property of the control which should be boundmessageKey
- The key of the message property that should be bound- See Also:
-
updateMessages
This method performs the localization update for all bound objects.Typically this method is overriden by a concrete implementation where the Messages instance is injected via @Inject and @Translation.
- Parameters:
messages
- The new Messages instance that should be used to update the localization.
-
createConsumer
- Parameters:
control
- The control on which the created consumer should operatemethod
- The method the created consumer should call to set the new value- Returns:
- A MessageConsumer that sets a value to the property of the given
control, or
null
in case of any exception
-
createSupplier
- Parameters:
messageKey
- The name of the field that should be accessed- Returns:
- A MessageSupplier that returns the message value for the given message key
-