/***
 * author : Van de Casteele Arnaud
 * Date : 2009/04/29
 * Purpose : Grid with GML Features
 * Version : 0.2
 * -> 1/05/2009 : Modify the code with 
 *  the new GeoExt Class. Better interaction with GeoExt.data.FeatureStore Class
***/


Ext.namespace('GeoExt', 'GeoExt.grid');

/**
 * Class: GeoExt.data.GridPanelFeature
 *
 * Typical usage in a store:
 * (start code)
 *        var featureStore = new GeoExt.data.FeatureStore({	
 *			fields: [
 *				{name: 'ID', type: 'int'}
 *				,{name: 'NAME', type: 'string'}
 *				,{name: 'REGION', type: 'string'}	
 *				,{name:'idFeature', type:'int'}
 *				,{name: 'Y_2004', type: 'int'}
 *			],
 *			layer : Disaster
 *		});
 *			
 *		var olGrid = new GeoExt.grid.GridPanelFeature({
 *			store: featureStore,
 *			width:300,
 *			columns: [
 *				{
 *				 header: "Pays",
 *				 width:100,
 *				 dataIndex: "NAME"
 *				},{
 *	 			 header: "Population touchée",
 *				 width:170,
 *				 dataIndex: "Y_2004"
 *				}			
 *			],//EOF columns
 *			bbar: new Ext.StatusBar({
 *				defaultText: '',
 *				id: 'basic-statusbar'						
 *			})
 *			});
 * (end)
 *
 * Inherits from:
 *  - {Ext.data.Store}
	 */

/**
 * Constructor: GeoExt.grid.GridPanelFeature
 * Create a grid wich interact with the data on the map. Requieres that the store 
 * is like featureStore.
 *
 * Parameters:
 * config {Object} The config object.
 *
 * Returns:
 * {<GeoExt.grid.GridPanelFeature>} The Grid.
 */
GeoExt.grid.GridPanelFeature = function(config) {
	Ext.apply(this, config);
    GeoExt.grid.GridPanelFeature.superclass.constructor.call(this);
};

Ext.extend(GeoExt.grid.GridPanelFeature, Ext.grid.GridPanel, {

	selectedFeature : new Array(),	




	initComponent: function(){
		//Add new options
		this.on("rowclick",this.getRowsSelected);		
		this.selectControl = this.selectControl();
		GeoExt.grid.GridPanelFeature.superclass.initComponent.call(this);
	},

	/**
	 * Method: selectControl
	 * Return OpenLayers.Control.Selectfeature	
	 */
	selectControl : function() {
		//We must do this because this
		//in OpenLayers.Control.SelectFeature
		//return is own object
		var grid = this;
		return new OpenLayers.Control.SelectFeature(grid.getStore().layer, {
	    	hover: false, clickout: true
		    ,multiple : true, hover: false
		    ,toggleKey : "ctrlKey" // ctrl key removes from selection
            ,multipleKey : "shiftKey" // shift key adds to selection
            ,box: false
			//Better than onSelect and onUnselect method 
			//because of loop between the grid and the control
		    ,callbacks : {"click": grid.getMapFeatureSelected, "clickout":grid.unSelectFeatures}
			//onSelect : grid.getMapFeatureSelected, //onUnselect : grid.unSelectFeatures,			
			,grid : grid
		});
	},

	/**
	 * Method: getRowsSelected
	 * get the selected row and after it 
	 * call the selectFeature method
	 */
	getRowsSelected : function(grid, index, evt) {	
		//We must before clear the old selection
		if(this.selectedFeature.length>0){
			this.unSelectFeatures({data: this.selectedFeature, type: "grid"});
		}
		var featRecord = this.getSelectionModel().getSelections();		
		var opt = {data: featRecord, type: "grid"};
		this.selectFeatures(opt);
		this.selectedFeature = featRecord;
	},

	/**
	 * Method: selectMapControl
	 * Create a OpenLayers Select Feature control
	 * when a feature has been clicked on the map 
	 * the record on the grid is selectionned
	 */
	getMapFeatureSelected : function(feat) {	
		var opt = {data: feat, type: "map"};
		this.grid.selectFeatures(opt);
	},

	/**
	 * Method: selectFeature
	 * Select the map Feature or the grid feature
     * This method shall be call by getMapFeatureSelected or getRowsSelected
     * the parameter "grid" or "map" is used to know what is the action to do
	 */
	selectFeatures : function(features){
		var grid = this;	
		if(features.type=="grid"){					
			Ext.each(features.data, function(oItem, i, len){
				//We must get the feature OL Object and then call the OL clickFeature Method
				var feat = grid.getStore().layer.getFeatureById(features.data[i].id);	
				grid.selectControl.select(feat);
			});
		}else{ //type == "Map"
			grid.selectControl.select(features.data);
			var featIndex = new Array();			
			Ext.each(features.data.layer.selectedFeatures, function(feat, i, len){					
				featIndex.push(grid.getStore().indexOfId(feat.id));	
			});					
			grid.getSelectionModel().selectRows(featIndex);
			this.selectedFeature = this.getSelectionModel().getSelections();	
		}
	},

	/**
	 * Method: unSelectedFeature
	 * Unselect the feature 
	 */
	unSelectFeatures : function(features){
		var grid = this;
		if(features.type=="grid"){			
			Ext.each(features.data, function(feat, i, len){
				//We must get the feature OL Object and then call the OL clickFeature Method
				var feat = grid.getStore().layer.getFeatureById(feat.id);		
				grid.selectControl.unselect(feat);
			});			
		}else{
			this.unselectAll();
			this.grid.getSelectionModel().clearSelections();
		}	
	}  
});
