<?xml version="1.0" encoding="utf-8"?>
<!--

ADOBE SYSTEMS INCORPORATED
Copyright 2010 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: Adobe permits you to use, modify, and distribute this file
in accordance with the terms of the license agreement accompanying it.

-->
<!--- The ComboBoxGridItemEditor class defines an item renderer for use 
      with the Spark grid controls, such as DataGrid and Grid.
      The ComboBoxGridItemEditor  displays a drop-down list of cell values.
      Select a value to set the new value of the cell.
      
      <p>The following example shows a GridColumn using the ComboBoxGridItemEditor
      as its item editor:</p>
      
      <pre>
        &lt;s:GridColumn dataField="quant" 
            headerText="Qty"&gt;
                &lt;s:itemEditor&gt;
                    &lt;fx:Component&gt;
                        &lt;s:ComboBoxGridItemEditor&gt;
                            &lt;s:dataProvider&gt;
                                &lt;s:ArrayList&gt;
                                    &lt;fx:int&gt;0&lt;/fx:int&gt;
                                    &lt;fx:int&gt;1&lt;/fx:int&gt;
                                    &lt;fx:int&gt;2&lt;/fx:int&gt;
                                    &lt;fx:int&gt;3&lt;/fx:int&gt;
                                &lt;/s:ArrayList&gt;
                            &lt;/s:dataProvider&gt;
                        &lt;/s:ComboBoxGridItemEditor&gt;
                    &lt;/fx:Component&gt;
                &lt;/s:itemEditor&gt;
        &lt;/s:GridColumn&gt;
      </pre>
      
      @see spark.components.DataGrid
      @see spark.components.Grid
      @see spark.components.gridClasses.GridColumn
   
      @langversion 3.0
      @playerversion Flash 10
      @playerversion AIR 2.5
      @productversion Flex 4.5
      
-->
<s:GridItemEditor xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                  xmlns:s="library://ns.adobe.com/flex/spark" 
                                  xmlns:mx="library://ns.adobe.com/flex/mx" 
                                  xmlns:gridEditorClasses="spark.components.gridEditorClasses.*">
    
    <fx:Script>
        <![CDATA[
            import mx.collections.IList;
            import mx.collections.ArrayList;
            import mx.core.IVisualElement;

            import spark.events.DropDownEvent;

            //--------------------------------------------------------------------------
            //
            //  Overriden properties
            //
            //--------------------------------------------------------------------------
            
            /**
             *  @private
             * 
             *  returns the selected item in the comboBox.
             */ 
            override public function get value():Object
            {
                return comboBox.selectedItem;            
            }
            
            /**
             *  @private
             * 
             *  Set the selected item of the combo box with the data from the
             *  item. 
             */ 
            override public function set value(newValue:Object):void
            {
                comboBox.selectedItem = newValue;
            }
            
            //--------------------------------------------------------------------------
            //
            //  Properties
            //
            //--------------------------------------------------------------------------
            
            //----------------------------------
            //  dataProvider
            //----------------------------------
            
            private var _dataProvider:IList;
            
            [Bindable("comboBoxGridItemEditorDataProviderChanged")]
            
            /**
             *  The data provider for the combox box in the item editor.
             *
             *  @langversion 3.0
             *  @playerversion Flash 10
             *  @playerversion AIR 2.5
             *  @productversion Flex 4.5
             */ 
            public function get dataProvider():IList
            {
                return _dataProvider;
            }
            
            /**
             *  @private
             */ 
            public function set dataProvider(value:IList):void
            {
                if (_dataProvider == value)
                    return;
                
                _dataProvider = value;
                dispatchEvent(new Event("comboBoxGridItemEditorDataProviderChanged"));
            }
            
            //--------------------------------------------------------------------------
            //
            //  Overridden methods
            //
            //--------------------------------------------------------------------------
            
            /**
             *  @private
             */ 
            override public function prepare():void
            {
                super.prepare();

                comboBox.addEventListener(DropDownEvent.OPEN, dropDown_openHandler);
                comboBox.openDropDown();
            }
            
            
            /**
             *  @private
             */ 
            override public function setFocus():void
            {
                // delegate focus to the combox box.
                comboBox.setFocus();
            }
            
            /**
             *  @private
             */ 
            private function dropDown_openHandler(event:DropDownEvent):void
            {
                // Add a mouse down handler for the dropDown part. When 
                // the user clicks in the drop down we want to end the editing 
                // session. We listen to the dataGroup here instead of the dropDown
                // part to avoid closing the editor when the dropDown's 
                // scrollbar is clicked.
                if (comboBox.dataGroup)
                    comboBox.dataGroup.addEventListener(MouseEvent.MOUSE_DOWN, dataGroup_mouseClickHandler, false, 0, true);
            }
            
            /**
             *  @private
             */ 
            private function dataGroup_mouseClickHandler(event:MouseEvent):void
            {
                // When the drop down list is clicked, end the editting session
                // to allow the edit to be completed with just one click.
                // Use callLater to let the click and change handlers finish 
                // before we close the editor.
                callLater(dataGrid.endItemEditorSession);
            }
            
        ]]>
    </fx:Script>

    <!--- @private
    
          Background to cover the cell that the ComboBox control does not. 
        
          @langversion 3.0
          @playerversion Flash 10
          @playerversion AIR 2.5
          @productversion Flex 4.5
    -->
    <s:Rect id="bgRect" top="1" left="1" bottom="1" right="1">
        <s:fill>
            <!--- @private
                Defines the color of the background. 
                @default 0xFFFFFF
        
                @langversion 3.0
                @playerversion Flash 10
                @playerversion AIR 2.5
                @productversion Flex 4.5
            -->
            <s:SolidColor id="bgFill" color="0xFFFFFF" />
        </s:fill>
    </s:Rect>

    <!--- The item editor's ComboBox control. 
        
          @langversion 3.0
          @playerversion Flash 10
          @playerversion AIR 2.5
          @productversion Flex 4.5
    -->
    <s:ComboBox id="comboBox" width="100%" dataProvider="{dataProvider}" />
    
</s:GridItemEditor>
