Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Use PreferenceStore and IScopeContext together(Extend a org.eclipse.jface.preference.PreferenceStore which uses org.osgi.service.prefs.Preferences)
Use PreferenceStore and IScopeContext together [message #1748120] Fri, 18 November 2016 15:31 Go to next message
Paolo M. is currently offline Paolo M.Friend
Messages: 8
Registered: November 2016
Junior Member
I'm trying to implement a PreferenceDialog with a PreferenceStore, which is synchronized with an osgi Preferences, what do you think about the below solution?
There are drawbacks?

Thank you

public class MyPreferenceStore extends PreferenceStore {
	
	private Preferences preferences;
	
	public MyPreferenceStore() {
		super();
		this.preferences = InstanceScope.INSTANCE.getNode(FrameworkUtil.getBundle(getClass()).getSymbolicName());
		setDefault(MY_PREFERENCE_1, "Default_1");
		setDefault(MY_PREFERENCE_2, "Default_2");
	}
	
	@Override
	public void load() throws IOException {
		
		StringBuffer propEntries = new StringBuffer();
		propEntries.append(createPropertyEntry(MY_PREFERENCE_1, "Default_1"));
		propEntries.append(createPropertyEntry(MY_PREFERENCE_2, "Default_2"));

		InputStream stream = new ByteArrayInputStream(propEntries.toString().getBytes(StandardCharsets.UTF_8));
		load(stream);
		stream.close();
	}
	
	private String createPropertyEntry(String key, String defaultValue){
		String value = preferences.get(key, defaultValue);
		return key + "=" + value + System.getProperty("line.separator");
	}
	
	@Override
	public void save() throws IOException {
		
		savePreference(MY_PREFERENCE_1);
		savePreference(MY_PREFERENCE_2);
		
		// To set dirty = false;
		load();
		
		try {
			preferences.flush();
		} catch (BackingStoreException e) {
			e.printStackTrace();
		}
		
	}
	
	private void savePreference(String key){
		preferences.put(key, getString(key));
	}
}
Re: Use PreferenceStore and IScopeContext together [message #1748189 is a reply to message #1748120] Mon, 21 November 2016 06:32 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Not sure what your goal is. You are just wrapping the instance scope PreferenceStore of a bundle.

I have created a mechanism and published via GitHub that lets you create a PreferenceDialog via OSGi DS .. so you can contribute PreferenceNodeContributions via DS.

https://github.com/fipro78/e4-preferences

Not sure if this is something you are trying to achieve.
Previous Topic:Eclipse plugin life cycle
Next Topic:extension org.eclipse.ui.intro in E4
Goto Forum:
  


Current Time: Thu Apr 25 07:32:36 GMT 2024

Powered by FUDForum. Page generated in 0.02609 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top