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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.jvnet.lookout.core.adapter.SearchAdapter;
import org.jvnet.lookout.core.preference.Preferences;
import org.jvnet.lookout.core.util.Utilities;
import org.jvnet.lookout.ui.core.Plugin;

public class CommonPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
	private RadioGroupFieldEditor groupingModeEditor;
	private org.jvnet.lookout.core.Plugin coreSearchPlugin;
		
	public CommonPreferencePage() {
		super(GRID);
		setPreferenceStore(Plugin.getDefault().getPreferenceStore());
		coreSearchPlugin = org.jvnet.lookout.core.Plugin.getDefault();
		
		//CXB_EXTERNALIZE
		setDescription("Common Search Engine Preferences");
	}
	
	public void createFieldEditors() {
		
		IntegerFieldEditor numberOfResultsEditor = 
			new IntegerFieldEditor(
					Preferences.NumberOfResults,
					//CXB_EXTERNALIZE
					"&Number of Results to Display:", 
					getFieldEditorParent());
		
		addField(numberOfResultsEditor);
		
		groupingModeEditor =
			new RadioGroupFieldEditor(org.jvnet.lookout.ui.core.preference.Preferences.GroupingMode, 
					//CXB_EXTERNALIZE
					"Search Results Grouping Mode:", 1, 
					new String [][] {{"By &Search Engine:", org.jvnet.lookout.ui.core.preference.Preferences.SearchEngineMode},
					{"By &Resource Type:", org.jvnet.lookout.ui.core.preference.Preferences.ResourceTypeMode},
					{"&No grouping (alphabetical):", org.jvnet.lookout.ui.core.preference.Preferences.AlphabeticalMode}},
					getFieldEditorParent(),
					true); 
		addField(groupingModeEditor);	
				
		//ADAPTER STUFF
		//Create enable search adapters section...
		
		Label enableSearchEngineLabel = new Label(getFieldEditorParent(), SWT.FLAT);
		//CXB_EXTERNALIZE
		enableSearchEngineLabel.setText("Available Search Engines:");
		
		List<SearchAdapter> searchAdapters = coreSearchPlugin.getSearchAdapters();
		
		for (Iterator iter = searchAdapters.iterator(); iter.hasNext();) {
			SearchAdapter adapter = (SearchAdapter) iter.next();
			
			BooleanFieldEditor searchAdapterEditor = 
				new BooleanFieldEditor(
						coreSearchPlugin.getSearchAdapterId(adapter),
						coreSearchPlugin.getSearchAdapterName(adapter), 
						getFieldEditorParent());	
		
			addField(searchAdapterEditor);	
		}
	}

	public void init(IWorkbench workbench) {
	}	

	@Override
	public boolean performOk() {
		boolean result = super.performOk();
		
		if(result) {
			
			//Extract enabled search adapter info 		
			List<SearchAdapter> searchAdapters = coreSearchPlugin.getSearchAdapters();
			List<String> activeAdaptersIds = new ArrayList<String>(); 
			
			for (Iterator iter = searchAdapters.iterator(); iter.hasNext();) {
				SearchAdapter adapter = (SearchAdapter) iter.next();
				
				String adapterId = coreSearchPlugin.getSearchAdapterId(adapter);
				
				if(!getPreferenceStore().isDefault(adapterId)) {
					activeAdaptersIds.add(adapterId);
				}					
			}
			
			String stringList = Utilities.createString(activeAdaptersIds, ",");
			
			//Save the preference..
			coreSearchPlugin.getPluginPreferences().setValue
			(Preferences.SearchableSearchAdapters,stringList);
			
			Utilities.saveSearchCorePreferences();
		}
		
		//The preference store is automatically saved so no need to explicitly save it...
		return result;
	}	
}