0

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

Elias S
  • 118
  • 10

2 Answers2

1

You must still use the org.eclipse.core.runtime.preferences extension point to define the preferences initializer.

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

In the initializer use:

@Override
public void initializeDefaultPreferences()
{
  Preferences defaults = DefaultScope.INSTANCE.getNode(Activator.ID);

  // Set defaults using things like:
  defaults.putInt("pref id", 0);
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Thanks for the comment, I´m using the extension point (the code just got snipped out, I re-edited my post ;) – Elias S Nov 25 '15 at 08:43
  • 1
    How are you referencing the preferences? – greg-449 Nov 25 '15 at 09:01
  • I reference them in my initializer via IPreferenceStore store = InspectIT.getDefault().getPreferenceStore(); Map initializationEntries = PreferenceSupplierDUMMY.getInitializationEntries(); for(Map.Entry entry : initializationEntries.entrySet()) { store.setDefault(entry.getKey(), entry.getValue()); } store.setDefault... and so on, like i read in this: https://openchrom.wordpress.com/2014/01/11/how-to-handle-preferences-consistently/ But I don´t even get this code invoked – Elias S Nov 25 '15 at 09:12
  • Not sure if that will work. I have added the code I use for the initializer which does work. – greg-449 Nov 25 '15 at 09:35
  • I tried that, but the initializer still doesn´t get a call. It´s like Eclipse is just skipping the whole class. Not the constructor nor the init method gets invoked. – Elias S Nov 25 '15 at 09:44
  • The initializer is only called when you actually do something that references the preferences. What are you doing to access the preferences? – greg-449 Nov 25 '15 at 09:46
  • Oh, this could be the problem. Actually I was in the migration phasis. In the E3-approach of the app for instance, I try to get a Pref via String value = preferenceStore.getString(preferenceKey); and the Preference Key is a String with a value on another class. – Elias S Nov 25 '15 at 09:52
  • I edited my Post to show how I tried to access the default preferences. but there still is no call on the initializer. This kind of is, how I should access my values, right ? Via Debug: In the RootPreferences the getNode method, it return "" because of an EndIndex of -1 (what does this mean?) – Elias S Nov 26 '15 at 09:44
  • Should be OK. Note: The initializer must be declared in the plugin.xml for the same plugin as the Activator – greg-449 Nov 26 '15 at 10:11
  • Good tip, my product was based on the standard org.eclipse.e4.ui.workbench.swt.E4Application. Now I got an own Application (via extension) and a Application-class, doing nothing but starting the app. But still no invokation – Elias S Nov 26 '15 at 12:07
  • Do I might need a seperate Life Cycle implementation apart from my Activator? – Elias S Nov 27 '15 at 09:41
1

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

Elias S
  • 118
  • 10