Question

I have a view in a perspective which got a Action in the toolbar. This action opens a PreferenceDialog. So the preferences in this Dialog are binded to the view which opens it. I Chose this way because the user can open serval numbers of this view with different content and each view has its preferences.

how can i Access this preferences in the ViewPart? I tried it like this

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

But Eclipse tells me this:

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

what i am doing in my PreferencePage class:

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

calling

setPreferenceStore(Activator.getDefault().getPreferenceStore());

in the constructor of my PreferencePage gives me a null pointer. I added this line to my manifest

Bundle-Activator: xxx.Activator

I also added the extension org.eclipse.core.runtime.preferences and connected it to this class:

 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);
}
}
Was it helpful?

Solution

You should be using the activator for your plugin not the preference plugin. If your activator extends AbstractUIPlugin it will have a getPreferenceStore() method.

Your preference page needs to call

setPreferenceStore(Activator.getDefault().getPreferenceStore());

during initialization (normally in the constructor).

You probably also want to use the org.eclipse.core.runtime.preferences extension point to define a preferences initializer to ensure the preferences always have a value.

Update: Example activator

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;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top