Time for action – injecting individual preferences

Although it is possible to inject an entire preference node, sometimes it is more convenient to inject just a single preference value. This can reduce the amount of code needed to extract and use a preference value. In addition, this can remove the runtime dependency on IEclipsePreferences, which can make the code more portable.

  1. Replace the injected IEclipsePreferences node with an int launchCount field, and append value = "launchCount" to the @Preferences annotation:
    @Preference(nodePath = "com.packtpub.e4.clock.ui",
     value = "launchCount")
    @Inject
    // IEclipsePreferences preferences;
    int launchCount;
  2. Update the print message to use the launchCount value directly:
    System.out.println("Launch count is: " + launchCount);
  3. Run the target Eclipse instance, and open the Time Zone Tree View. In the Console view, there should be a Launch count is: message with the same value as before.

What just happened?

Instead of injecting the entire preferences node, the single preference value is injected. If the preference value doesn't exist, then it will be assigned a default value (0 for numeric values, false for boolean, and null for object values). By removing the IEclipsePreferences and Preferences interfaces, the code is less tied to the Eclipse framework (the runtime visible annotation @Preference is still present, but this isn't required at runtime by the application outside of an Eclipse environment).

Note

Note that the preference is injected once—when the view is created. If the preference value is updated, then it will not be re-injected into the application.

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

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