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

<!--

ADOBE SYSTEMS INCORPORATED
Copyright 2008 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 default header renderer for s:DataGrid columns.

@see spark.components.DataGrid

@langversion 3.0
@playerversion Flash 10
@playerversion AIR 1.5
@productversion Flex 4
-->

<s:GridItemRenderer minWidth="21" minHeight="21"
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx">
    
    <fx:Declarations>
        <!--- The default value of the <code>sortIndicator</code> property.
        It must be an IFactory for an IVisualElement.        
        
        <p>This value is specified in a <code>fx:Declaration</code> block and can be overridden
        by a declaration with <code>id="defaultSortIndicator"</code>
        in an MXML subclass.</p>
        
        @langversion 3.0
        @playerversion Flash 10
        @playerversion AIR 2.0
        @productversion Flex 4.5
        -->
        <fx:Component id="defaultSortIndicator">
            <s:Path data="M 4 7 L 7.5 0 0 0 Z" height="5" width="7" >
                <s:fill>
                    <s:SolidColor color="#666666" />
                </s:fill>
            </s:Path>
        </fx:Component>
        
        <s:Label id="labelDisplay" 
                 verticalCenter="1" left="0" right="0" top="0" bottom="0"
                 textAlign="start"
                 fontWeight="bold"
                 verticalAlign="middle"
                 maxDisplayedLines="1"
                 showTruncationTip="true" />
    </fx:Declarations>
    
    <fx:Script>
        <![CDATA[
            import mx.core.IVisualElement;
            
            import spark.components.DataGrid;
            import spark.components.GridColumnHeaderGroup;
            import spark.components.gridClasses.GridColumn;
            
            /**
             *  @private
             */
            private function dispatchChangeEvent(type:String):void
            {
                if (hasEventListener(type))
                    dispatchEvent(new Event(type));
            }            
            
            //----------------------------------
            //  maxDisplayedLines
            //----------------------------------
            
            private var _maxDisplayedLines:int = 1;
            
            [Bindable("maxDisplayedLinesChanged")]
            [Inspectable(minValue="-1")]
            
            /**
             *  @copy spark.components.supportClasses.TextBase#maxDisplayedLines   
             *
             *  @default 1
             * 
             *  @langversion 3.0
             *  @playerversion Flash 10
             *  @playerversion AIR 1.5
             *  @productversion Flex 4.5
             */
            public function get maxDisplayedLines():int
            {
                return _maxDisplayedLines;
            }
            
            /**
             *  @private
             */
            public function set maxDisplayedLines(value:int):void
            {
                if (value == _maxDisplayedLines)
                    return;
                
                _maxDisplayedLines = value;
                if (labelDisplay)
                    labelDisplay.maxDisplayedLines = value;
                
                invalidateSize();
                invalidateDisplayList();
                
                dispatchChangeEvent("maxDisplayedLinesChanged");
            }
            
            //----------------------------------
            //  sortIndicator
            //----------------------------------
            
            private var _sortIndicator:IFactory;
            private var sortIndicatorInstance:IVisualElement;
            
            [Bindable("sortIndicatorChanged")]
            
            /**
             *  An IVisualElement that's displayed when the column is sorted.
             * 
             *  @default null
             * 
             *  @langversion 3.0
             *  @playerversion Flash 10
             *  @playerversion AIR 1.5
             *  @productversion Flex 4.5
             */
            public function get sortIndicator():IFactory
            {
                return (_sortIndicator) ? _sortIndicator : defaultSortIndicator;
            }
            
            /**
             *  @private
             */
            public function set sortIndicator(value:IFactory):void
            {
                if (_sortIndicator == value)
                    return;
                
                _sortIndicator = value;
                if (sortIndicatorInstance)
                {
                    sortIndicatorGroup.includeInLayout = false;
                    sortIndicatorGroup.removeElement(sortIndicatorInstance);
                    sortIndicatorInstance = null;
                }
                
                invalidateDisplayList();
                dispatchChangeEvent("sortIndicatorChanged");
            }
            
            /**
             *  @private
             *  Use prepare method to create and put the sortIndicator into the
             *  sortIndicatorGroup and the labelDisplay into the labelDisplayGroup.
             */
            override public function prepare(hasBeenRecycled:Boolean):void
            {
                super.prepare(hasBeenRecycled);
                
                if (labelDisplay && labelDisplayGroup && (labelDisplay.parent != labelDisplayGroup))
                    labelDisplayGroup.addElement(labelDisplay);
                
                const column:GridColumn = this.column;
                if (sortIndicator && column && column.grid && column.grid.dataGrid && column.grid.dataGrid.columnHeaderGroup)
                {
                    const dataGrid:DataGrid = column.grid.dataGrid;
                    const columnHeaderGroup:GridColumnHeaderGroup = dataGrid.columnHeaderGroup;
                    
                    if (columnHeaderGroup.isSortIndicatorVisible(column.columnIndex))
                    {
                        if (!sortIndicatorInstance)
                        {
                            sortIndicatorInstance = sortIndicator.newInstance();
                            sortIndicatorGroup.addElement(sortIndicatorInstance);
                        }
                        sortIndicatorInstance.visible = true;
                        sortIndicatorGroup.includeInLayout = true;
                        sortIndicatorGroup.scaleY = (column.sortDescending) ? 1 : -1;
                    }
                    else
                    {
                        if (sortIndicatorInstance)
                        {
                            sortIndicatorGroup.removeElement(sortIndicatorInstance);
                            sortIndicatorGroup.includeInLayout = false;
                            sortIndicatorInstance = null;
                        }
                    }
                }
            }
        ]]>
    </fx:Script>
    
    <s:states>
        <s:State name="normal" />
        <s:State name="hovered" />
        <s:State name="down" />
    </s:states>
    
    <!-- background -->
    <s:Rect left="0" right="0" top="0" bottom="0">
        <s:fill>
            <s:SolidColor color="#BABABA" color.normal="#EEEEEE" color.hovered="#DDDDDD" color.down="#DDDDDD" />
        </s:fill>
    </s:Rect>
    
    <!-- shadow (down state only) -->
    <s:Rect includeIn="down" left="0" right="0" y="0" height="4">
        <s:fill>
            <s:LinearGradient rotation="90">
                <s:GradientEntry alpha="0.23" color="#000000" ratio="0"/>
                <s:GradientEntry alpha="0.20" color="#000000" ratio="0.4"/>
                <s:GradientEntry alpha="0.055" color="#000000" ratio="0.55"/>
                <s:GradientEntry alpha="0.0" color="#000000" ratio="0.8"/>
            </s:LinearGradient>
        </s:fill>
    </s:Rect>
    <s:Rect includeIn="down" x="0" width="4" top="0" bottom="0">
        <s:fill>
            <s:LinearGradient rotation="0">
                <s:GradientEntry alpha="0.13" color="#000000" ratio="0"/>
                <s:GradientEntry alpha="0.12" color="#000000" ratio="0.2"/>
                <s:GradientEntry alpha="0.0" color="#000000" ratio="0.55"/>
            </s:LinearGradient>
        </s:fill>
    </s:Rect>
    
    <s:HGroup left="7" right="7" top="5" bottom="5" gap="2" verticalAlign="middle">
       
        <!--- Container for labelDisplay:TextBase.  -->
        <s:Group id="labelDisplayGroup" width="100%" />
        
        <!--- Container for sortIndicator:IVisualElement.  -->
        <s:Group id="sortIndicatorGroup" includeInLayout="false" />
    
    </s:HGroup>
    
</s:GridItemRenderer>
