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

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

public class WebPreferenceInitializer extends SynchronizedPreferenceInitializer {

	public WebPreferenceInitializer() {
		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.web.Plugin.getDefault().getPluginPreferences();
	}
	
	public void actuallyInitializeDefaultPreferences() {
		
		//Web API Key....
		synchPropertyWithSource(Preferences.Key, String.class);
		
		//Part to search
		synchPropertyWithSource(Preferences.PartOfResourceToSearch, String.class);

		//Site...
		synchPropertyWithSource(Preferences.SiteToSearch, String.class);

		//Search date
		synchPropertyWithSource(Preferences.DateToSearchFrom, Integer.class);
		
		//Files to Search....
		synchPropertyWithSource(Preferences.SearchableFileTypes, String.class);

		//Search all files....
		synchPropertyWithSource(Preferences.SearchAllFiles, Boolean.class);

	}
	
	public void propertyChange(PropertyChangeEvent event) {
		String property = event.getProperty();
		if(
				Preferences.Key.equals(property) ||
				Preferences.PartOfResourceToSearch.equals(property) ||
				Preferences.SiteToSearch.equals(property) ||
				Preferences.SearchableFileTypes.equals(property)
		) {
			
			//These are strings...
			synchPropertyWithStore(property, String.class);			
		} else if (Preferences.DateToSearchFrom.equals(property)) {
			
			//These are doubles...
			synchPropertyWithStore(property, Integer.class);
		} else if(Preferences.SearchAllFiles.equals(property)) {
			
			//These are boolean...
			synchPropertyWithStore(property, Boolean.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.Key);
		getPreferenceStore().setToDefault(Preferences.PartOfResourceToSearch);
		getPreferenceStore().setToDefault(Preferences.SiteToSearch);
		getPreferenceStore().setToDefault(Preferences.DateToSearchFrom);
		getPreferenceStore().setToDefault(Preferences.SearchableFileTypes);
		getPreferenceStore().setToDefault(Preferences.SearchAllFiles);
		
	}
}
