I have a preferences page in eclipse (IDE) and every time user change value from a property and click ok, those values are saved in a preferences files. I'm using a listener for getting new values. But what I need to do is get this new value -> process -> and save this new value processed and store in preference file. The implementation that I have is this:
public class PreferenceUtil extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
private StringFieldEditor stringUser;
@Override
public void init(IWorkbench arg0) {
setPreferenceStore(Activator.getDefault().getPreferenceStore());
}
@Override
protected void createFieldEditors() {
stringUser = new StringFieldEditor("user", "&User:\t\t", getFieldEditorParent());
addField(stringUser);
Activator.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getProperty() == "user") {
user = event.getNewValue().toString();
//ConnectionValues.convert(user) is an algorithm that process a string
stringUser.setStringValue(ConnectionValues.convert(user));
}
}
}
}
I found the solution, what I had to do is, create a StringFieldEditor and then use it in a listener for get the new value, process this value using my own algorithm for encrypt, then set this value encrypted to StringFieldEditor.
public class PreferenceUtil extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
StringFieldEditor secretSFE;
@Override
public void init(IWorkbench arg0) {
setPreferenceStore(Activator.getDefault().getPreferenceStore());
}
@Override
protected void createFieldEditors() {
secretSFE = new StringFieldEditor("secret", "&Secret:\t\t",getFieldEditorParent()){
@Override
protected void doFillIntoGrid(Composite parent, int numColumns) {
super.doFillIntoGrid(parent, numColumns);
getTextControl().setEchoChar('*');
}
};
addField(secretSFE);
Activator.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if("secret".equalsIgnoreCase(event.getProperty())){
String valueToEncrypt = (String)event.getNewValue();
valueToEncrypt = ConnectionValues.convert(valueToEncrypt);
secretFFE.setStringValue(valueToEncrypt);
}
}
});
}
}