Eclipse项目属性/首选项页面:如何在项目属性窗口(Eclipse)中查找打开首选项页面的事件

时间:2013-10-23 13:59:35

标签: java eclipse eclipse-plugin swt

我想在页面打开时在首选项页面上初始化一些字段值 我希望每次在Project属性窗口(Eclipse)中打开页面时都能使用它。

2 个答案:

答案 0 :(得分:1)

尝试实现属性更改侦听器。

这可能会有所帮助 -

public class MyFieldEditorPreferencePage extends PropertyPage {
private Text text;

public void initialize() {
    // TODO Auto-generated constructor stub
    IProject project = (IProject) getElement().getAdapter(IProject.class);

    final IPreferenceStore store = Activator
            .getImpaktPreferenceStore(project);

    store.addPropertyChangeListener(new IPropertyChangeListener() {

        @Override
        public void propertyChange(
                org.eclipse.jface.util.PropertyChangeEvent event) {
            // TODO Auto-generated method stub

            // TODO Auto-generated method stub

            if (text != null && !text.isDisposed())
                text.setText(store.getString("key"));

        }
    });

    setPreferenceStore(store);

}

@Override
protected Control createContents(Composite parent) {
    // TODO Auto-generated method stub

    initialize();
    GridLayout layout = new GridLayout(1, false);
    parent.setLayout(layout);
    GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);

    Composite container = new Composite(parent, 0);
    container.setLayoutData(gridData);

    GridLayout containerLayout = new GridLayout(2, false);
    container.setLayout(containerLayout);
    Label lbl = new Label(container, 0);
    lbl.setText("Locale");

    text = new Text(container, SWT.BORDER);
    text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    text.setText(getPreferenceStore().getString("impakt.properties.locale"));

    return parent;
}

}

答案 1 :(得分:0)

org.eclipse.jface.preference.PreferencePage有一个setPreferenceStore方法,您应该在首选项页面构造函数中调用它。指定的首选项存储通常来自插件激活器,如比较插件首选项中的此示例:

setPreferenceStore(CompareUIPlugin.getDefault().getPreferenceStore());

有一种getPreferenceStore()方法可以调用来访问此商店。

如果您希望每次页面可见时收到通知,请覆盖public void setVisible(boolean visible)