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.
Add a
String greeting
field to theHello
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;
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);
Run the application, and the Hello label will show a
null
value for the greeting.To set a preference value, an
IEclipsePreferences...