如何从PropertyChangeEvent

时间:2016-08-04 19:36:18

标签: java listener propertychangelistener

我在eclipse(IDE)中有一个首选项页面,每次用户从属性更改值并单击“确定”时,这些值都会保存在首选项文件中。我正在使用一个监听器来获取新值。但我需要做的是获得这个新值 - >过程 - >并保存此新值处理并存储在首选项文件中。我的实现是:

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));                                      
            }
}
}
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我必须做的是,创建一个StringFieldEditor,然后在侦听器中使用它来获取新值,使用我自己的加密算法处理此值,然后将此值加密为StringFieldEditor。 / p>

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);
            }
        }
    });      
  }
}