wiki:Grits_PreferenceInE4

Grits Preference Handling In E4

Preference Page Extension point

Contributing to Preference Dialog

  • Extension point org.eclipse.ui.preferencePages was replaced with org.grits.toolbox.core.preferencePages in core plugin
  • In plugin.xml replace extension point "org.eclipse.ui.preferencePages" with "org.grits.toolbox.core.preferencePages"

Preference Store

Previously GRITS used PreferenceReader class to read a preference from ".preference.xml" file. The object returned was a PreferenceEntity object as documented here

In e4 PreferenceHandler, PreferenceReader and PreferenceWriter have been replaced by IGritsPreferenceStore for reading preference values from ".preference.xml" file. The three classes are still available but internally uses IGritsPreferenceStore. This class is injected in the application context. It still uses the PreferenceEntity object as the generic object to serialize xml values from ".preference.xml" file.


Example Code :

	@Inject private static IGritsPreferenceStore gritsPreferenceStore;

	String getPreference(String preferenceName)
	{
		String preferenceValue = null;
		try
		{
			preferenceValue = gritsPreferenceStore.getPreferenceEntity(preferenceName).getValue();
		} catch (UnsupportedVersionException e)
		{
			Element preferenceElement = gritsPreferenceStore.getPreferenceElement(preferenceName);
			if(preferenceElement != null)
			{
				preferenceValue =  preferenceElement.getAttribute("value") == null 
						? null : preferenceElement.getAttributeValue("value");

				// remove older version preference
				gritsPreferenceStore.removePreference(preferenceName);

				// save this as current version preference
				PreferenceEntity preferenceEntity = new PreferenceEntity(preferenceName);
				preferenceEntity.setValue(preferenceValue);
				gritsPreferenceStore.savePreference(preferenceEntity);
			}
		} catch (Exception e)
		{
			logger.fatal(e.getMessage(), e);
		}
		return preferenceValue;
	}

Notification System

When one calls the method gritsPreferenceStore.savePreference(preferenceEntity) the IGritsPreferenceStore posts a notification about preference change on the event bus with the topic IGritsPreferenceStore.EVENT_TOPIC_PREF_VALUE_CHANGED. One can subscribe to this topic if one would refresh some view or editor. For more on this read the javadoc.

Example code :

	
	@Optional @Inject
	public void resetSomething(@UIEventTopic(IGritsPreferenceStore.EVENT_TOPIC_PREF_VALUE_CHANGED)
	 					String preferenceName)
	{
	 	if(MY_PREFERENCE_NAME.equals(preferenceName)
	 	{
		 // reset this
		}
	}

Local Preference Store

It is recommended to create a local preference store for particular type of preferences (or per plugin). In the local preference store, one can cache the preferences as IGritsPreferenceStore current implementation reads preferences each time from the file which is expensive. Grits core plugin also defines a preference store for project, about which you can read here.

Issues

PreferenceManager for JFace Preference Dialog

Loading Preference Page for PreferenceNode

  • Calling constructor with Preference Page instance as parameter
    • JFace Preference Dialog disposes the preference page upon closing. close() method is internally called in many places. In close method preferenceNode's disposeResources() is called, which sets page to null. This means if pages are not lazily created using class uri and instead page instance is set in the preference node then the preference manager is to be loaded each time dialog is opened.
  • Calling constructor with class uri of page
    • The second alternative would be to set the class uri, but jface Preference node class uses Class.forName(String) method to load class which does not work for eclipse plugins
Last modified 6 years ago Last modified on 06/01/2016 10:16:58 PM

Attachments (7)

Download all attachments as: .zip