wiki:PreferenceUpdateForUI

Updating UI after Preference changes

IPreferenceUpdateListener

All the classes that have UI component and need to update UI on preference changes should implement this class. The concrete class would implement the public void preferenceUpdated(List<String> preferenceIds) method and based on the preferenceIds, it can do its required changes to UI.

  • notifies the class implementing this interface when some of the preference values are changed
    • preferenceIds are the list of ids of the preference element that were changed. For example
      • "org.grits.toolbox.entry.sample.preference.AnalytePreferenceVariables.numberOfComponents" is the id of the preference "Number of Components" in the Analyte Editor.
    • Each "id" refers to the name of the preference element in the ".preference.xml" that was changed.
public interface IPreferenceUpdateListener
{
	/**
	 * notifies the class implementing this interface when some of the preference values are changed
	 * @param preferenceIds are the list of ids of the preference element that were changed
	 * e.g.  "org.grits.toolbox.entry.sample.preference.AnalytePreferenceVariables.numberOfComponents" is the 
	 * id of the preference "Number of Components" in the Analyte Editor.
	 * Each "id" refers to the name of the preference element in ".preference.xml" that was changed.
	 */
	public void preferenceUpdated(List<String> preferenceIds);
}

An example code from ProjectEntryEditor class that implements this interface is shown below:

	@Override
	public void preferenceUpdated(List<String> preferenceIds)
	{
		if(preferenceIds.contains(CollaboratorTablePreference.VISIBLE_COLUMNS_ELEMENT))
		{
			logger.debug("Resetting columns for collaborators table");

			collaboratorsTableViewer.getTable().removeControlListener(controlListeners[0]);
			removeColumns(collaboratorsTableViewer);
			int[] columnRatios = getColumnRatios(collaboratorsTableViewer, 
					CollaboratorTableColumn.COLUMNS, CollaboratorTablePreference.VISIBLE_COLUMNS);
			collaboratorsTableViewer.getTable().addControlListener(controlListeners[0] = 
					new MaintainTableColumnRatioListener(TABLE_MIN_WIDTH, columnRatios));
			collaboratorsTableViewer.setLabelProvider(
					new CollaboratorsLabelProvider(collaboratorsTableViewer));
			resizeTable(collaboratorsTableViewer, controlListeners[0]);
			addViewerComparator(collaboratorsTableViewer);
		}
	}

UtilityPreference class

This class has 3 helper methods which updates all opened editors and views when preference is updated.

  • public static void updateUIs(List<String> preferenceIds)
    • updates both editors and views
  • public static void updateEditors(List<String> preferenceIds)
    • updates all editors
  • public static void updateViews(List<String> preferenceIds)
    • updates all views

These methods are generally called from the Preference Page when preference values are saved in the .preference.xml file. Here is an example code from TaskPreference class that stores and displays task preference for projects:

	private void save()
	{
		// some stuff done here		

		...

		saveValues();
		UtilityPreference.updateUIs(
				Arrays.asList(ALL_TASKS_ELEMENT));
	}

Possible Integration after Preference Versioning is complete

After versioning one possible way to integrate the helper method would be to call this updateUIs in the PreferenceWriter class itself while saving preference. This would free plugins from calling this method individually. The method in the PreferenceWriter class would look like:

	public static void savePreference(PreferenceEntity preferenceEntity)
	{
		// saves preference entity into the file
		//  creates the element if it is not already there


		// update the UIs after changing and saving the preference
		UtilityPreference.updateUIs(Arrays.asList(preferenceEntity.getName()));
	}

Last modified 6 years ago Last modified on 09/22/2015 12:00:36 AM