Search code examples
eclipsepreferencesrcpe4initializer

E4 Preference Initializer won´t get called


I´m trying to migrate my e3-rcp-app to a e4-rcp-app.

Therefore I need to define my default Preferences. (Not the Pref.Pages)

And by doing and trying so, I just can´t get my Initializer called. Here Is my initializer-class:

public class MyPreferenceInitializer extends AbstractPreferenceInitializer {

public MyPreferenceInitializer (){}

@Override
public void initializeDefaultPreferences() {

Preferences defaults = DefaultScope.INSTANCE.getNode(InspectIT.ID);
          // Set defaults using things like:
          defaults.put("DUMMY", "DUMMYCONTENT");
          try {
            defaults.flush();
        } catch (BackingStoreException e) {
            e.printStackTrace();
        }

          //And this other approach to make sure that one of them works
          IPreferenceStore store = InspectIT.getDefault().getPreferenceStore();
          store.setDefault("DUMMY", "DUMMYCONTENT");         
          try {
            ((Preferences) store).flush();
        } catch (BackingStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //Dummy impl        
       default Preferences....,
    }

}

I also got an Activator class with the following structure: (Just posting the relevant methods(?))

 public class Activator implements BundleActivator {

        private static BundleContext context;

        static BundleContext getContext() {
            return context;
        }


        private static Activator plugin;

        private volatile ScopedPreferenceStore  preferenceStore;

        public void start(BundleContext context) throws Exception {
            plugin = this;
            Activator.context = context;
            locateRuntimeDir();     
            logListener = new LogListener();
            Platform.addLogListener(logListener);

//access to my initializor
        String text = getPreferenceStore().getDefaultString("DUMMY");
        String text2 = getPreferenceStore().getString("DUMMY");

        }

        public void stop(BundleContext context) throws Exception {

        Activator.context = null;
        plugin = null; 
    }

    public static <E> E getService(Class<E> clazz) {
        ServiceReference<E> reference = context.getServiceReference(clazz);
        if (null != reference) {  

            return context.getService(reference);
        }
        throw new RuntimeException("Requested service of the class " + clazz.getName() + " is not registered in the bundle.");
    }

    public ScopedPreferenceStore getPreferenceStore() {

        if (null == preferenceStore) {
            synchronized (this) {
                if (null == preferenceStore) { 


                    preferenceStore = new  ScopedPreferenceStore(ConfigurationScope.INSTANCE, ID); 
                } 
            } 
        } 
        return preferenceStore;
    }
}

The ScopedPreferenceStore I´m using is the one available at: https://github.com/opcoach/e4Preferences/tree/master/com.opcoach.e4.preferences

As well, I declared the plugin.xml Extension like this (I do need this, right?)

...
 <extension
         point="org.eclipse.core.runtime.preferences">
      <initializer            class="MyApplication.rcp.preferences.MyPreferenceInitializer ">
      </initializer>
   </extension>
...

I´m using Eclipse 4.5.1 on a win7 x64 I googled a lot and found a lot of Threads concerning this, but I just can´t find my mistake =/. Anyone got a suggestion for why my default preferences initializer won´t get called?

Thanks in advance


Solution

  • Finally I found a solution for this issue. Accidentally got over this problem again and the mistake was in the Activator. I wrongly set the ID onto a wrong name. I reset it to my projects name and now it is working!

    public ScopedPreferenceStore getPreferenceStore() {
    
        if (null == preferenceStore) {      
            synchronized (this) {
                if (null == preferenceStore) 
                preferenceStore = new ScopedPreferenceStore(ConfigurationScope.INSTANCE, ID); 
            }                   
        } 
        return preferenceStore;
    }
    

    ID = Project-Name