Search code examples
eclipseeclipse-kepler

Eclipse e4 Kepler: IEclipsePreferences read/write


I am using DI (Dependency Injection) to access the IEclipsePreferences from my handlers. But I need to access the same IEclipsePreferences from other code parts to save/load these settings.

This is the example how I get the preferences and show em in a dialog:

import java.lang.reflect.InvocationTargetException;
import javax.inject.Inject;
import javax.inject.Named;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.core.di.extensions.Preference;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.osgi.service.prefs.BackingStoreException;

public class PojoShowPersistencePreferencesHandler {
@Inject
@Preference
IEclipsePreferences preferences;

@Inject
MApplication application;

@Execute
public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell
) throws InvocationTargetException, InterruptedException {
    System.out.println(getClass().getSimpleName() + ".execute()");
    System.out.println("preferences: " + preferences);
    System.out.println("application: " + application);
    try {

        String msg = "Driver:\t"    + preferences.get("jdbc_driver", "404") + "\n" + //
                "URL:\t" + preferences.get("jdbc_url", "404") + "\n" + //
                "User:\t" + preferences.get("jdbc_user", "404") + "\n" + //
                "Password:\t" + preferences.get("jdbc_pwd", "404");
        preferences.flush();
        MessageDialog.openInformation(shell,
                "Info :: Save Persistence Preferences",msg //
        );
        System.out.println(msg);
    } catch (BackingStoreException e) {
        e.printStackTrace();
    }
}

}

a) How can I do the same without using an Handler? b) I need to SET these values by code without DI or as a Handler with Parameters, which can be called Programmatically

I've been trying this (Vogella) article but somehow I cannot find the values stored in these (Instance, Configuration) scopes, but they are stored, as they get shown with the Handler!

NOTE: i am using 3.x Plugin style, so using old style is not a problem.


Solution

  • Try this:

    @Preference(nodePath = "com.example.e4.rcp.todo")

    This may be the reason why you do not find your preferences.