<?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 DefaultGridItemEditor class defines the default item renderer used 
      by the Spark grid controls, such as DataGrid and Grid.
      The DefaultGridItemEditor  displays a text area control.
      Enter the new value for the cell in the text editor.
      
      <p>Instead of using the DefaultGridItemEditor, you can create 
      a custom item editor. The item editor must implement the 
      spark.components.gridClasses.IGridItemEditor interface. 
      Typically, you create an item editor as a subclass of the
      spark.components.gridClasses.GridItemEditor class.</p>
      
      @see spark.components.DataGrid
      @see spark.components.Grid
      @see spark.components.gridClasses.IGridItemEditor
      @see spark.components.gridClasses.GridItemEditor
      @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 spark.components.gridClasses.IGridItemRenderer;
            import mx.core.IUITextField;
            import mx.core.IVisualElement;
            
            import spark.core.IEditableText;
            import spark.primitives.Rect;
            
            //--------------------------------------------------------------------------
            //
            //  Overridden properties
            //
            //--------------------------------------------------------------------------

            /**
             *  @private
             */ 
            override public function get value():Object
            {
                return textArea.text;            
            }
            
            /**
             *  @private
             */ 
            override public function set value(newValue:Object):void
            {
                textArea.text = newValue != null ? newValue.toString() : "";
            }
            
            //--------------------------------------------------------------------------
            //
            //  Overidden methods
            //
            //--------------------------------------------------------------------------

            /**
             *  @private
             */ 
            override public function prepare():void
            {
                super.prepare();
             
                var cellBounds:Rectangle = column.grid.getCellBounds(rowIndex, columnIndex);
                var textDisplay:IEditableText = textArea.textDisplay;
                var itemRenderer:IGridItemRenderer = dataGrid.grid.getItemRendererAt(rowIndex, columnIndex);
                
                if (itemRenderer is IUITextField)
                {
                    textDisplay.multiline = IUITextField(itemRenderer).multiline;
                    textDisplay.lineBreak = IUITextField(itemRenderer).wordWrap ? "toFit" : "explicit";
                }
                else if ("multiline" in itemRenderer)
                {
                    textDisplay.multiline = itemRenderer["multiline"];                    
                }
                else
                {
                    textDisplay.multiline = false;                    
                }
                    
                if (textDisplay.multiline == false &&
                    textDisplay.lineBreak == "explicit" &&
                    isTextAlignStart())
                {
                    // Single line control. Set the initial width to be the width of the cell.
                    textArea.width = NaN;
                    textArea.widthInChars = NaN;
                    
                    // Set a max width of six inches for the text or the visible edge of
                    // the application.
                    const MAX_WIDTH_INCHES:uint = 6;
                    var gridRect:Rectangle = dataGrid.grid.getVisibleRect();
                    var globalCellTopLeft:Point = dataGrid.grid.localToGlobal(cellBounds.topLeft);

                    // Bug in text width: the width is allowed to grow larger than maxWidth so
                    // subtract 2 pixels to stay within the max.
                    textArea.maxWidth = Math.min(gridRect.right - globalCellTopLeft.x - 1, 
                                                 MAX_WIDTH_INCHES * Capabilities.screenDPI) - 2;
                }
                else
                {
                    textArea.width = cellBounds.width + 1;
                }
                
                // Extend width of the TextArea so its borders overlay the cell's borders.
                textArea.minWidth = cellBounds.width + 1;
                textArea.height = cellBounds.height + 1;
                
                // Adjust the padding so the text appears in the same position
                // in the editor as the renderer. 
                // TODO (dloverin): This code is trying to match the default 
                // item renderer's padding. Ideally we should just be able to 
                // use the same values of padding on the item renderer and be done.
                var paddingTop:int = textArea.getStyle("paddingTop");
                var paddingLeft:int = textArea.getStyle("paddingLeft");
                var paddingBottom:int = textArea.getStyle("paddingBottom");
                var paddingRight:int = textArea.getStyle("paddingRight");
                textArea.setStyle("paddingTop", paddingTop + 3);
                textArea.setStyle("paddingLeft", paddingLeft + 3);
                textArea.setStyle("paddingBottom", paddingBottom + 3);
                textArea.setStyle("paddingRight", paddingRight + 4);
            }

            /**
             *  @private
             */ 
            override public function setFocus():void
            {
                textArea.setFocus();
                
                // If textArea is multiline, it will not select all of the
                // text by default. So select all of the text here.
                if (textArea.textDisplay.multiline)
                    textArea.selectAll();
            }
            
            /**
             *  @private
             * 
             *   Test if text is aligned at the beginning of the control.
             * 
             *   @return true if the text is aligned that the beginning of the
             *   control, false otherwise.
             */ 
            private function isTextAlignStart():Boolean
            {
                var textAlign:String = textArea.getStyle("textAlign")
                if ( textAlign == "start")
                    return true;
                
                if (textAlign == "end")
                    return false;
                
                var direction:String = textArea.getStyle("direction");
                if (direction == "ltr" && textAlign == "left" ||
                    direction == "rtl" && textAlign == "right")
                {
                    return true;
                }
                
                return false;
            }
        ]]>
    </fx:Script>

    <!--- The editor's TextArea component. 
    
          @langversion 3.0
          @playerversion Flash 10
          @playerversion AIR 2.5
          @productversion Flex 4.5
    -->
    <s:TextArea id="textArea" horizontalScrollPolicy="off" verticalScrollPolicy="off" />
    
</s:GridItemEditor>
