如何从 PropertyChangeEvent 更改新值
how to change new value from PropertyChangeEvent
我在 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));
}
}
}
}
我找到了解决方案,我要做的是,创建一个 StringFieldEditor,然后在侦听器中使用它来获取新值,使用我自己的加密算法处理这个值,然后将这个值加密设置为 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);
}
}
});
}
}
我在 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));
}
}
}
}
我找到了解决方案,我要做的是,创建一个 StringFieldEditor,然后在侦听器中使用它来获取新值,使用我自己的加密算法处理这个值,然后将这个值加密设置为 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);
}
}
});
}
}