/**
 * $id:  $
 */
package org.jvnet.lookout.ui.google.desktop.preference;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.jvnet.lookout.google.desktop.preference.Preferences;
import org.jvnet.lookout.ui.core.preference.SynchronizedPreferenceInitializer;
import org.jvnet.lookout.ui.google.desktop.Plugin;

public class DesktopPreferenceInitializer extends SynchronizedPreferenceInitializer {

	public DesktopPreferenceInitializer() {
		super();		
		
		//Register for cleaning...
		Plugin.getDefault().addCleanable(this);
	}
	
	protected IPreferenceStore obtainPreferenceStore() {
		return Plugin.getDefault().getPreferenceStore();
	}
	
	protected org.eclipse.core.runtime.Preferences obtainSourcePreference() {
		return org.jvnet.lookout.google.desktop.Plugin.getDefault().getPluginPreferences();
	}
	
	public void actuallyInitializeDefaultPreferences() {
		
		//Client's Server URL....
		synchPropertyWithSource(Preferences.ServerUrl, String.class);
		
		//Client Security Token....
		synchPropertyWithSource(Preferences.ClientSecurityToken, String.class);

		//File type to search....
		synchPropertyWithSource(Preferences.FileTypeToSearch, String.class);

		//Resource type to search....
		synchPropertyWithSource(Preferences.ResourceTypeToSearch, String.class);		
	}	
	
	public void propertyChange(PropertyChangeEvent event) {
		String property = event.getProperty();
		if(
				Preferences.ClientSecurityToken.equals(property) ||
				Preferences.ServerUrl.equals(property)||
				Preferences.FileTypeToSearch.equals(property)||
				Preferences.ResourceTypeToSearch.equals(property)) {
			
			//These are strings...
			synchPropertyWithStore(property, String.class);			
		}
	}		

	public void clean() {
		
		//Need to remove these preferences as they are used only to enable the 
		//use of PreferenceStores to create the UI...
		getPreferenceStore().setToDefault(Preferences.ServerUrl);
    	getPreferenceStore().setToDefault(Preferences.ClientSecurityToken);
    	getPreferenceStore().setToDefault(Preferences.FileTypeToSearch);
    	getPreferenceStore().setToDefault(Preferences.ResourceTypeToSearch);
    }
}
