I use the @Preference annotation on method level to get the current preference value injected:
@Inject
@Optional
public void updatePrefValue(@Preference(value = PREFERENCE_NAME) String prefValue) {
System.out.println("updatePrefValue with '" + prefValue + "'.");
this.prefValue = prefValue;
if (lblPrefValue != null && !lblPrefValue.isDisposed()) {
lblPrefValue.setText(prefValue);
}
}
On several places (e.g vogella and in the book Eclipse 4 by Marc Teufel and Dr. Jonas Helming) it has been said, that this method gets called again when the preference value changes.
So after pressing a button that sets a new preferences value
IEclipsePreferences node = ConfigurationScope.INSTANCE.getNode(PREFERENCES_NODE);
node.put(PREFERENCE_NAME, txtNewPrefValue.getText());
try {
node.flush();
} catch (BackingStoreException e1) {
e1.printStackTrace();
}
I would assume that the method gets called again. This is true but only if I do not change the ConfigurationScope but the InstanceScope instead.
InstanceScope.INSTANCE.getNode(PREFERENCES_NODE).put(PREFERENCE_NAME, txtNewPrefValue.getText());
The full source code of the example can be seen on github.
Is it possible to do this? Or is it a bug?
Kind regards, tobbaumann
Update: If I specify the nodePath including the scope (/configuration/...) of the annotation
@Inject
@Optional
public void updatePrefValue(@Preference(nodePath = "/configuration/" + PREFERENCES_NODE, value = PREFERENCE_NAME) int intPrefValue) {
System.out.println("updatePrefValue with '" + intPrefValue + "'.");
this.intPrefValue = intPrefValue;
if (lblPrefValue != null && !lblPrefValue.isDisposed()) {
lblPrefValue.setText(String.valueOf(intPrefValue));
}
}
then the method gets called again if the preference value changes in the ConfigurationScope. Still this cannot be used reasonably, because the first time, this method gets called, the argument is null (if I want to set a string value) or 0 (if I want to set an integer value). I assume this happens because the value cannot be found (yet) in the ConfigurationScope. The value you like to have here, is the value from the DefaultScope (which was injected before when I do not use /configuration as nodePath prefix).
Any ideas?