Eclipse插件访问ViewPart中的首选项

时间:2014-05-09 06:58:35

标签: view eclipse-plugin preferences

我在透视图中有一个视图,它在工具栏中有一个Action。此操作将打开PreferenceDialog。因此,此对话框中的首选项绑定到打开它的视图。我选择这种方式是因为用户可以使用不同的内容打开此视图的serval编号,并且每个视图都有其首选项。

如何在ViewPart中访问此首选项? 我试过这样的

IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();

但Eclipse告诉我:

Multiple markers at this line
- The method getDefault() is undefined for the type Activator
- Discouraged access: The type Activator is not accessible due to restriction on required library 
 C:\Users\xxx\eclipse\plugins\org.eclipse.equinox.preferences_3.5.100.v20130422-1538.jar

我在PreferencePage类中正在做什么:

public class PaketFilterPreferencePage extends PreferencePage {

public static final int ALL_PACKETS_SELECTED = 1;
public static final int FILTER_DECODER = 2;
public static final int ALL_DECODER = 0;
public static final String PKFILTER_PREF = "PaketFilterDecoderPref";
public static final String PKFILTER_NODE = "PaketFilterDecoderNode";
...
private void initButtons(){
    IPreferenceStore preferenceStore = getPreferenceStore();
    int pref = preferenceStore.getInt(PKFILTER_PREF);
    if(pref==ALL_PACKETS_SELECTED){
        selectAllPacketsButton.setSelection(true);
        ctv.getTable().setEnabled(false);
        if(selectAllDecoderButton!=null){
            selectAllDecoderButton.setEnabled(false);
            unselectAllButton.setEnabled(false);
        }
    }
    else
        selectFilterButton.setSelection(true);
}

@Override
protected void performDefaults() {
    IPreferenceStore preferenceStore = getPreferenceStore();
    preferenceStore.setValue(PKFILTER_PREF, ALL_DECODER);
    initButtons();

}

@Override
public boolean performOk() {
    IPreferenceStore preferenceStore = getPreferenceStore();
    if(selectAllPacketsButton.getSelection())
        preferenceStore.setValue(PKFILTER_PREF, ALL_PACKETS_SELECTED);
    else if(decoderNamesCurrentCopy.equals(decoderNamesAll))
        preferenceStore.setValue(PKFILTER_PREF, ALL_DECODER);
    else
        preferenceStore.setValue(PKFILTER_PREF, FILTER_DECODER);
    return true;
}
}

致电

setPreferenceStore(Activator.getDefault().getPreferenceStore());
在我的PreferencePage的构造函数中的

给了我一个空指针。我将此行添加到我的清单

Bundle-Activator: xxx.Activator

我还添加了扩展名org.eclipse.core.runtime.preferences并将其连接到此类:

 public class AbstractPreferenceInit extends
    AbstractPreferenceInitializer {

public AbstractPreferenceInit() {
    // TODO Auto-generated constructor stub
}

@Override
public void initializeDefaultPreferences() {
    IEclipsePreferences node = DefaultScope.INSTANCE.getNode(PaketFilterPreferencePage.PKFILTER_NODE);
    node.putBoolean(PaketFilterPreferencePage.PKFILTER_ALL_PACKETS_RADIO, false);
    node.putInt(PaketFilterPreferencePage.PKFILTER_STATUS, PaketFilterPreferencePage.ALL_DECODER);
}
}

1 个答案:

答案 0 :(得分:0)

您应该使用激活器作为插件而不是首选插件。如果您的激活器延伸AbstractUIPlugin,它将采用getPreferenceStore()方法。

您的偏好设置页面需要致电

setPreferenceStore(Activator.getDefault().getPreferenceStore());
初始化期间

(通常在构造函数中)。

您可能还希望使用org.eclipse.core.runtime.preferences扩展点来定义首选项初始值设定项,以确保首选项始终具有值。

更新: 示例激活器

public class Activator extends AbstractUIPlugin {
private static Activator plugin;

public Activator() {
}

@Override
public void start(BundleContext context) throws Exception {
    super.start(context);
    plugin = this;
}

@Override
public void stop(BundleContext context) throws Exception {
    plugin = null;
    super.stop(context);
}

public static Activator getDefault() {
    return plugin;
}
}