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

import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.jvnet.lookout.ui.core.util.Utilities;

abstract public class SynchronizedPreferenceInitializer extends AbstractPreferenceInitializer implements IPropertyChangeListener, Cleanable {

	private IPreferenceStore uiPreferenceStore;
	private Preferences sourcePreferences;
	
	public SynchronizedPreferenceInitializer() {
		
		uiPreferenceStore = obtainPreferenceStore();
		sourcePreferences = obtainSourcePreference();
		
		connectListener();
	}
	
	public void initializeDefaultPreferences() {
		
		disconnectListener();
		
		actuallyInitializeDefaultPreferences();
		
		connectListener();		
	}
	
	final protected IPreferenceStore getPreferenceStore() {
		return uiPreferenceStore;
	}
	final protected Preferences getSourcePreference() {
		return sourcePreferences;
	}
	
	protected void connectListener() {
		uiPreferenceStore.addPropertyChangeListener(this);
	}
	
	protected void disconnectListener() {
		uiPreferenceStore.removePropertyChangeListener(this);
	}
	
	abstract protected IPreferenceStore obtainPreferenceStore();	
	abstract protected Preferences obtainSourcePreference();

	abstract public void actuallyInitializeDefaultPreferences();
	
	protected void synchPropertyWithSource(String preference, Class type) {
		
		if(String.class == type) {
			
			//Transfer default value...
			String defaultValue = getSourcePreference().getDefaultString(preference);
			getPreferenceStore().setDefault(preference, defaultValue);
			
			//Transfer current value
			String value = getSourcePreference().getString(preference);
			getPreferenceStore().setValue(preference, value);
					
		} else if (Boolean.class == type) {

			//Transfer default value...
			Boolean defaultValue = getSourcePreference().getDefaultBoolean(preference);
			getPreferenceStore().setDefault(preference,defaultValue);
			
			//Transfer current value
			Boolean value = getSourcePreference().getBoolean(preference);
			getPreferenceStore().setValue(preference, value);
		} else if (Integer.class == type) {

			//Transfer default value...
			Integer defaultValue = getSourcePreference().getDefaultInt(preference);
			getPreferenceStore().setDefault(preference,defaultValue);
			
			//Transfer current value
			Integer value = getSourcePreference().getInt(preference);
			getPreferenceStore().setValue(preference, value);
			
		} else if (Double.class == type) {

			//Transfer default value...
			Double defaultValue = getSourcePreference().getDefaultDouble(preference);
			getPreferenceStore().setDefault(preference,defaultValue);
			
			//Transfer current value
			Double value = getSourcePreference().getDouble(preference);
			getPreferenceStore().setValue(preference, value);
			
		}else {
			Utilities.showWarningMessage("Unaccounted synch for preference: " +preference);
		}
	}
	
	protected void synchPropertyWithStore(String preference, Class type) {
		
		if(String.class == type) {
			
			//Transfer current value
			String value = getPreferenceStore().getString(preference);
			getSourcePreference().setValue(preference, value);
					
		} else if (Boolean.class == type) {

			//Transfer current value
			Boolean value = getPreferenceStore().getBoolean(preference);
			getSourcePreference().setValue(preference, value);
		} else if (Integer.class == type) {

			//Transfer current value
			Integer value = getPreferenceStore().getInt(preference);
			getSourcePreference().setValue(preference, value);
			
		} else if (Double.class == type) {

			//Transfer current value
			Double value = getPreferenceStore().getDouble(preference);
			getSourcePreference().setValue(preference, value);
		} else {
			Utilities.showWarningMessage("Unaccounted synch for preference: " +preference);
		}		
	}
}
