blob: d8cfd33c3bf4781d9feb9127c2258b3855c2f937 [file] [log] [blame]
<?xml version="1.0" encoding="utf-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--- 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">
<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>