Time for action – using preferences

In addition to injecting in specific elements from the context, it is also possible to acquire preferences from the Eclipse preference store. Recall that preferences are stored in a hierarchical node structure, with each node having an identifier (conventionally the plug-in name) and a number of key/value pairs. An annotation @Preference allows these to be accessed easily.

  1. Add a String greeting field to the Hello part. To obtain a preference, annotate it with @Inject and @Preference as follows:
    @Inject
    @Preference(nodePath="com.packtpub.e4.application", value="greeting")
    private String greeting;
  2. Modify the create() method to use this greeting value as the initial value for the Hello label.
    @PostConstruct
    public void create(Composite parent) {
      label = new Label(parent, SWT.NONE);
      label.setText(greeting+" "+window.getLabel()+" "+random);
  3. Run the application, and the Hello label will show a null value for the greeting.
  4. To set a preference value, an IEclipsePreferences object needs to be injected into the Hello part. Add a new field called prefs which is used to interact with the preferences store:
    @Inject
    @Preference(nodePath="com.packtpub.e4.application")
    private IEclipsePreferences prefs;

    Note

    If the preference's nodePath is not specified, a runtime error occurs.

  5. Modify the receiveEvent() method created previously and set the value of the color as the greeting:
    @Inject
    @Optional
    public void receiveEvent(
     @UIEventTopic("rainbow/colour") String data) {
      label.setText(data);
      prefs.put("greeting", "I like " + data);
      prefs.sync();
    }
  6. Now run the application, go to the Rainbow tab and select a color. Switch back to the Hello tab and verify that the event was received. Now, close down the application and re-open the application; the persisted greeting should be visible (provided that the workspace is not being cleaned upon each launch).
  7. Changes to the preference value gets injected dynamically into the part, but as it stands there is no notification when an injected field value has been changed. To be notified when a new value is set, the preference can be injected as an annotated @Preference parameter on an @Optional method:
    @Inject
    @Optional
    void setText(@Preference(nodePath="com.packtpub.e4.application",
      value="greeting") String text) {
      if (text != null && label != null && !label.isDisposed()) {
        // NB Run in UI thread!
        label.setText(text);
      }
    }
  8. Now, if the preference is set, the label's text will be updated automatically. However, the preference invocation is not necessarily on the UI thread, so the call should be delegated appropriately. It may also be the case that this method is called during startup or as the runtime is closing down, in which case, the setting of any UI components need to be guarded against either being null or being disposed.

What just happened?

Acquiring preferences with E4 injection makes it trivial to obtain and set preference values. Using a single preference value is the easiest way to get individual values; however, if the preferences need to be mutated then a reference to the IEclipsePreferences store is required.

If the user interface needs to react to changes in the preference, the preference value should be injected via a setter. When the preference value changes, the setter is invoked and it can update the UI.

Note that the preference value setter may be invoked on any thread; if the UI needs to be updated then this should be done via the UI thread. How to do this is covered in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset