Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Preferences - Properties in non UI plugin and UI plugin
Preferences - Properties in non UI plugin and UI plugin [message #1746276] Wed, 26 October 2016 11:22 Go to next message
Djak Mising name is currently offline Djak Mising nameFriend
Messages: 16
Registered: November 2010
Junior Member
Hi,
in an E3 RCP Application I need to store and retrieve properties from a file. We have a core standalone plugin and an RCP UI plugin. I'm looking for the best way to manage the properties in these two plugins.

At this moment we use java.utils.Properties in the non UI plugin and fill them from a file (eg.: "data.prefs"). The plugin have only read access on the properties.

In the RCP UI plugin we load the file "data.prefs" and fill the Preference service from it, eg. :

IPreferencesService service = Platform.getPreferencesService();
service.getRootNode().node(SCOPE).node(Application.PLUGIN_ID).put(key, value)

then use the service in some JFace preference pages.

(Sorry it's not my own code and I'm trying to explain what I understand about the existent code but it's approximately the scenario)

I'm looking for help to manage all these properties in a better way, using AbstractPreferenceInitializer, etc.
For example, I thought to use IPreferenceStore in the non UI plugin but it's in the jface bundle so it may be non sense to use it in a non UI plugin?

Do you have any advice about managing this? Which bundles/extensions would be the best to use in this case?

Later, I will also split the entire application into several plugins. Each functionality/command will have a non UI core plugin and an UI RCP plugin. So I'd like to make something clean by storing and initializing all properties of each core plugin in the plugins themselves but using only one preference file for the main Application (Host plugin), what do you think about that, would it be the "right" way to do that and do you have some advices to achieve that?

Thanks in advance for any help!
Sebastian

[Updated on: Wed, 26 October 2016 11:26]

Report message to a moderator

Re: Preferences - Properties in non UI plugin and UI plugin [message #1746428 is a reply to message #1746276] Fri, 28 October 2016 14:12 Go to previous messageGo to next message
Eclipse UserFriend
The preferences story in Eclipse is a complicated one. There are 5 preferences stories available to Eclipse plugins.

org.eclipse.core.runtime.Preferences is deprecated and should not be used.

JFace's org.eclipse.jface.preferences.IPreferenceStore/IPersistentPreferenceStore, and its many implementers, presents a flat key-value typed store. It is supported by many JFace and workbench components.

The JRE's java.util.prefs.Preferences presents a tree-based preference store. In fact, 2 tree-based stores: one is for user preferences, the other for system preferences. This model provides a tree-based store. Typically applications use their fully-qualified class names as key prefixes to avoid conflicts.

OSGi extends the JRE model with org.osgi.service.prefs.Preferences/PreferencesService where each preferences service maintains independent per-bundle user and system preference trees. So two bundles can store independent values for a key "com.example.Foo.bar". Which I certainly find a bit confusing.

org.eclipse.core.runtime.preferences.IEclipsePreferences is somewhat based on OSGi's, but instead maintains a set of independently scoped trees (represented by org.eclipse.core.runtime.preferences.IScopeContext). These scopes include:

  • per-project: represented by the ProjectScope class
  • per-workspace: represented by the InstanceScope class
  • per-installation: represented by the ConfigurationScope
  • defaults: a non-persisted scope intended to hold onto default values, represented by the DefaultScope

Eclipse provides a org.eclipse.core.runtime.preferences.IPreferencesService to look up preferences that attempts to resolve a key across different scopes (e.g., project, then instance, then configuration, finally the default scope, and if not found then returns the default-default value).

There is support for populating the DefaultScope when a bundle is started using the org.eclipse.equinox.preferences.preferences / org.eclipse.core.runtime.preferences extension point to point to an initializer class. This class will be called into on bundle load. You can actually initialize any scope, not just the DefaultScope.

There is a ScopedPreferenceStore in org.eclipse.ui.preferences that maps a scope (IScopeContext) to JFace's IPreferenceStore. This is pretty handy.



New plugins usually use the scopes approach. You could use the preference initializer to pull in from your external properties.

Using your bundle id as a node value is a common pattern, but you're now tying your preferences to your implementation structure. It may be better to use more meaningful names that remain invariant in the face of refactoring.

If you need to interact with other Java components, use the JRE java.util.prefs package.
Re: Preferences - Properties in non UI plugin and UI plugin [message #1746627 is a reply to message #1746428] Wed, 02 November 2016 08:53 Go to previous messageGo to next message
Djak Mising name is currently offline Djak Mising nameFriend
Messages: 16
Registered: November 2010
Junior Member
Hi,
thank you very much for those very detailed information (really!).
Here is what I tried before reading you and it seems to work as I wanted.
In the non UI plugins:
- add extension org.eclipse.core.runtime.preferences
- create a new initializer containing:

	@Override
	public void initializeDefaultPreferences() {
		
		Preferences defaultPreferences = DefaultScope.INSTANCE.getNode(PREFERENCE_NODE);
		defaultPreferences.put(MAX,"50");
		defaultPreferences.put(MIN, "2");
	}


Then in the UI plugin, in the Preference Page (and when calling the command linked to the plugin) I use:

new ScopedPreferenceStore(ConfigurationScope.INSTANCE, PREFERENCE_NODE)

to retrieve the current preferences values.
It works as expected:
- ConfigurationScope.INSTANCE is filled by the values stored in file if exists
- if not exists, in the preference page the values seem to be filled with the DefaultScope.INSTANCE values
- the "Reset to default value" of preference page works as expected
- I need to check again if when I create a new ScopedPreferenceStore(ConfigurationScope.INSTANCE, PREFERENCE_NODE) outside the preference page and the preferences file doesn't exist, it is well populated by the values of the DefaultScope.INSTANCE

You said org.eclipse.core.runtime.preferences is deprecated? I guess I can replace it by org.eclipse.equinox.preferences.preferences and get the same result? (I'll try right now)

I think I will use
Platform.getProduct().getId()
for the node name of the preferences shared by all plugins to get a global file (for non optional plugins) and maybe the bundle id for the optional plugins.

Thanks again for the help Smile
Re: Preferences - Properties in non UI plugin and UI plugin [message #1746655 is a reply to message #1746627] Wed, 02 November 2016 14:01 Go to previous messageGo to next message
Eclipse UserFriend
The class org.eclipse.core.runtime.Preferences is deprecated. The extension point org.eclipse.core.runtime.preferences is not.

One thing I forgot to mention, that you discovered, is that ScopedPreferenceStore implicitly consults the DefaultScope by default.

And one thing to be careful: Platform.getProduct() is your active product. You can have several products in one installation, and one is set as the default. Your use of 'Platform.getProduct().getId()' could bite you if you ever ship multiple products. For example, many of the Eclipse Packages include multiple products, such as the Eclipse IDE for RCP Developers which includes org.eclipse.platform.ide, org.eclipse.sdk.ide, and an org.eclipse.epp.package.rcp.product.

Brian.

Brian.
Re: Preferences - Properties in non UI plugin and UI plugin [message #1746903 is a reply to message #1746655] Mon, 07 November 2016 14:05 Go to previous messageGo to next message
Djak Mising name is currently offline Djak Mising nameFriend
Messages: 16
Registered: November 2010
Junior Member
Hi, thank you very much for these extra advices!
So I will keep using the extension org.eclipse.core.runtime.preferences. Our application uses only one product so it would work fine to use the id.
Sebastian
Re: Preferences - Properties in non UI plugin and UI plugin [message #1752150 is a reply to message #1746903] Thu, 19 January 2017 09:33 Go to previous messageGo to next message
Djak Mising name is currently offline Djak Mising nameFriend
Messages: 16
Registered: November 2010
Junior Member
Hi,
actually I still have trouble for using a global shared .prefs file for non optional/core plugins. I didn't realize that AbstractPreferenceInitializer.initializeDefaultPreferences() methods I implemented in the plugins are not called if I use another qualifier than the plugin Id.
In our RCP Application we use a string as node qualifier:

public static final String PLUGIN_ID = "org.txm.rcpapplication"; //$NON-NLS-1$

and it generates a file named "org.txm.rcpapplication.prefs", but I don't manage how to use this node and file to store the plugins preferences using the AbstractPreferenceInitializer.initializeDefaultPreferences() and org.eclipse.core.runtime.preferences mechanism, any advice are welcome Smile
Re: Preferences - Properties in non UI plugin and UI plugin [message #1752201 is a reply to message #1752150] Thu, 19 January 2017 20:39 Go to previous messageGo to next message
Eclipse UserFriend
You updated your `initializeDefaultPreferences()` to write to `DefaultScope` and not the `org.eclipse.core.runtime.Preferences` instance you showed initially, right? And you've changed the consumers of the preference to use ScopedPreferenceStore or the IPreferenceService, right?
Re: Preferences - Properties in non UI plugin and UI plugin [message #1752501 is a reply to message #1752201] Tue, 24 January 2017 14:28 Go to previous messageGo to next message
Djak Mising name is currently offline Djak Mising nameFriend
Messages: 16
Registered: November 2010
Junior Member
Hi,
yes, here is what I'm doing at this moment in a "non UI" core plugin that have ID "org.txm.tbx.partition":

public class PartitionDimensionsPreferences extends AbstractPreferenceInitializer {


	public static final String PREFERENCES_NODE = "org.txm.tbx.partition"; //$NON-NLS-1$
	
	public static final String PREFERENCES_PREFIX = "partition_"; //$NON-NLS-1$
	
	public static final String DIMENSIONS_DISPLAY_PARTS_COUNT_IN_TITLE = PREFERENCES_PREFIX + "dimensions_display_parts_count_in_title"; //$NON-NLS-1$
	public static final String DIMENSIONS_SORTED_BY_SIZE = PREFERENCES_PREFIX + "dimensions_sort_by_parts_size"; //$NON-NLS-1$
	
	
	
	public PartitionDimensionsPreferences() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void initializeDefaultPreferences() {
		Preferences defaultPreferences = DefaultScope.INSTANCE.getNode(PREFERENCES_NODE);
		defaultPreferences.putBoolean(DIMENSIONS_DISPLAY_PARTS_COUNT_IN_TITLE, true);
		defaultPreferences.putBoolean(DIMENSIONS_SORTED_BY_SIZE, false);
	}
	
}


And in another plugin contributing to the UI, for example in the linked preference page I'm creating a ScopedPreferenceStore:

new ScopedPreferenceStore(InstanceScope.INSTANCE, PartitionDimensionsPreferences.PREFERENCES_NODE);


It works very well for all, default preference, instance/file preference but the file used is "org.txm.tbx.partition.prefs" so we will have one separated file for each of our plugins. Actually this is maybe not a real problem but what I'd like to do was to use only one file for all the plugin preferences. However it's finally maybe not much important as I thought since the system of Eclipse permits to export/import all the plugin preferences in one file even if the original preferences are stored in multiple files.
What I wanted to do was to set the same PREFERENCES_NODE qualifier for most each plugin, for example "org.txm.rcpapplication" but since the plugin IDs are not "org.txm.rcpapplication" the initializers of each plugins are never called (not sure if I am very clear Smile )

[Updated on: Tue, 24 January 2017 14:32]

Report message to a moderator

Re: Preferences - Properties in non UI plugin and UI plugin [message #1752615 is a reply to message #1752501] Wed, 25 January 2017 15:33 Go to previous messageGo to next message
Eclipse UserFriend
The default initializers only work for preference nodes ids that match the bundle symbolic name. There's no other way for the workbench to know which initializers to call.

An alternative to using the default preference initializers is to use a bundle activator to configure the different DefaultScopes. But you must ensure you somehow trigger that bundle. Or perhaps re-implement something similar to how the default preference initializers are triggered (I don't recall the mechanism off-hand). Or you could use the org.eclipse.ui.startup extension point to provide org.eclipse.ui.IStartup listener to load them on workbench startup, and you could use an IWorkbenchListener to write out the preference values to a single value on exit.

Brian.
Re: Preferences - Properties in non UI plugin and UI plugin [message #1752708 is a reply to message #1752615] Thu, 26 January 2017 12:09 Go to previous messageGo to next message
Djak Mising name is currently offline Djak Mising nameFriend
Messages: 16
Registered: November 2010
Junior Member
Hi,
thank you again very much for these advices and informations. I also thought to use an Activator to do this, we need to decide if it's important to have multiple files or not but I think we will keep using this standard mechanism using one file by plug-in.
Sebastien
Re: Preferences - Properties in non UI plugin and UI plugin [message #1752798 is a reply to message #1752708] Fri, 27 January 2017 12:24 Go to previous message
Djak Mising name is currently offline Djak Mising nameFriend
Messages: 16
Registered: November 2010
Junior Member
Only for information, here is what I do now to define the preference node qualifier which seems pretty useful:

public static final String PREFERENCES_NODE = FrameworkUtil.getBundle(PartitionDimensionsPreferences.class).getSymbolicName();

Previous Topic:Inactivity timeout Login screen
Next Topic:java.lang.RuntimeException: Application "xxxxx" could not be found in the registry
Goto Forum:
  


Current Time: Thu Mar 28 18:58:04 GMT 2024

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

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

Back to the top