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

import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.jvnet.lookout.google.desktop.preference.Preferences;
import org.jvnet.lookout.ui.google.desktop.Plugin;

public class DesktopPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage, IPropertyChangeListener{

	private RadioGroupFieldEditor resourceTypeEditor;
	private StringFieldEditor fileTypeToSearchEditor;
	
	public DesktopPreferencePage() {
		super(GRID);
				
		setPreferenceStore(Plugin.getDefault().getPreferenceStore());
		
		//CXB_EXTERNALIZE
		setDescription("Google Desktop Search Engine Preferences");
	}
	
	public void createFieldEditors() {

		resourceTypeEditor =
			new RadioGroupFieldEditor(Preferences.ResourceTypeToSearch, 
					//CXB_EXTERNALIZE
					"Resource type to search:", 1, 
					new String [][] {
					{"Search &All Resources", Preferences.SearchAll},
					{"Search &Emails", Preferences.SearchEmails},
					{"Search Web &History", Preferences.SearchWebHistory},
					{"Search &Files", Preferences.SearchFiles}
					},
					getFieldEditorParent(),
					true); 
		addField(resourceTypeEditor);	
		
		//Need to set this so that the next call actually works...
		resourceTypeEditor.setPreferenceStore(getPreferenceStore());
		
		//Ensure that we test on valid data...
		resourceTypeEditor.load();
		
		resourceTypeEditor.setPropertyChangeListener(this);
		
		getPreferenceStore().addPropertyChangeListener(this);
		
		fileTypeToSearchEditor 
		= new StringFieldEditor(
				Preferences.FileTypeToSearch,
				//CXB_EXTERNALIZE
				"&File Type To Search (only 1 extension):", 
				getFieldEditorParent());
		addField(fileTypeToSearchEditor);
		
		//It's state is dependent on the state of 
		//the resource type to be searched 
		updateStateOfFileTypeField();
		
		StringFieldEditor serverURLEditor 
		= new StringFieldEditor(
				Preferences.ServerUrl,
				//CXB_EXTERNALIZE
				"&Server URL", 
				getFieldEditorParent());
		addField(serverURLEditor);
		
		StringFieldEditor securityTokenEditor 
		= new StringFieldEditor(
				Preferences.ClientSecurityToken,
				//CXB_EXTERNALIZE
				"Security &Token", 
				getFieldEditorParent());
		addField(securityTokenEditor);		
	}
	
	public void init(IWorkbench workbench) {
	}
	
	@Override
	protected void performDefaults() {
		super.performDefaults();
		
		updateStateOfFileTypeField();		
	}
	
	@Override
	public boolean performOk() {
		
		boolean result = super.performOk();
		
		if(result) {
			
			updateStateOfFileTypeField();
			
			//Save the plugin properties..
			org.jvnet.lookout.google.desktop.Plugin.getDefault().savePluginPreferences();
		}
		
		return result;
	}	
	
	public void propertyChange(PropertyChangeEvent event) {
		String property = event.getProperty();
		Object source = event.getSource();
		
		if(FieldEditor.VALUE.equals(property)) {
			
			if(resourceTypeEditor == source) {				
				updateStateOfFileTypeField();
			} 
		}
	}
	
	private void updateStateOfFileTypeField() {
		
		Button button 
		= (Button)resourceTypeEditor.getRadioBoxControl(getFieldEditorParent()).getChildren()[3];
		
		fileTypeToSearchEditor.setEnabled(button.getSelection(), getFieldEditorParent());					
	}
}