Class BaseMessageRegistry<M>

java.lang.Object
org.eclipse.e4.core.services.nls.BaseMessageRegistry<M>
Type Parameters:
M - the message class type

public class BaseMessageRegistry<M> extends Object
Using this MessageRegistry allows to register controls for attributes in a Messages class. These controls will automatically get updated in case of Locale changes.

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 Details

    • BaseMessageRegistry

      public BaseMessageRegistry()
  • Method Details

    • register

      public void register(MessageConsumer consumer, MessageFunction<M> function)
      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

      public void register(MessageConsumer consumer, MessageSupplier supplier)
      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

      public void register(Object control, String method, String messageKey)
      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 created
      method - 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

      public void registerProperty(Object control, String property, String messageKey)
      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 created
      property - The property of the control which should be bound
      messageKey - The key of the message property that should be bound
      See Also:
    • updateMessages

      public void updateMessages(M messages)
      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

      protected MessageConsumer createConsumer(Object control, String method)
      Parameters:
      control - The control on which the created consumer should operate
      method - 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

      protected MessageSupplier createSupplier(String messageKey)
      Parameters:
      messageKey - The name of the field that should be accessed
      Returns:
      A MessageSupplier that returns the message value for the given message key