////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////
package mx.controls.fileSystemClasses
{
import flash.display.DisplayObject;
import flash.filesystem.File;
import mx.controls.FileSystemComboBox;
import mx.controls.FileSystemDataGrid;
import mx.controls.dataGridClasses.DataGridListData;
import mx.controls.listClasses.BaseListData;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.controls.listClasses.IListItemRenderer;
import mx.controls.listClasses.ListBase;
import mx.controls.listClasses.ListItemRenderer;
import mx.core.IDataRenderer;
import mx.core.IFlexDisplayObject;
import mx.core.IUITextField;
import mx.core.UIComponent;
import mx.core.UITextField;
import mx.core.mx_internal;
import mx.events.FlexEvent;
use namespace mx_internal;
[ExcludeClass]
/**
* @private
* This helper class implements the renderer for the Name column,
* which displays an icon and a name for a File.
* We need a custom renderer because DataGridItemRenderer
* doesn't support an icon.
*/
public class FileSystemDataGridNameColumnRenderer extends UIComponent
implements IDataRenderer, IDropInListItemRenderer, IListItemRenderer
{
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
/**
* Constructor.
*
* @langversion 3.0
* @playerversion AIR 1.1
* @productversion Flex 3
*/
public function FileSystemDataGridNameColumnRenderer()
{
super();
}
//--------------------------------------------------------------------------
//
// Variables
//
//--------------------------------------------------------------------------
/**
* @private
*/
private var listOwner:ListBase;
//--------------------------------------------------------------------------
//
// Overridden properties: UIComponent
//
//--------------------------------------------------------------------------
//----------------------------------
// baselinePosition
//----------------------------------
/**
* @private
* The baselinePosition of a NameColumnRenderer is calculated
* for its label.
*/
override public function get baselinePosition():Number
{
if (!validateBaselinePosition())
return NaN;
return label.y + label.baselinePosition;
}
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
//----------------------------------
// data
//----------------------------------
/**
* @private
* Storage for the data property.
*/
private var _data:Object;
[Bindable("dataChange")]
/**
* The implementation of the data
property
* as defined by the IDataRenderer interface.
* When set, it stores the value and invalidates the component
* to trigger a relayout of the component.
*
* @see mx.core.IDataRenderer
*
* @langversion 3.0
* @playerversion AIR 1.1
* @productversion Flex 3
*/
public function get data():Object
{
return _data;
}
/**
* @private
*/
public function set data(value:Object):void
{
_data = value;
invalidateProperties();
dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
//----------------------------------
// icon
//----------------------------------
/**
* The internal IFlexDisplayObject that displays the icon in this renderer.
*
* @langversion 3.0
* @playerversion AIR 1.1
* @productversion Flex 3
*/
protected var icon:IFlexDisplayObject;
//----------------------------------
// label
//----------------------------------
/**
* The internal IUITextField that displays the text in this renderer.
*
* @langversion 3.0
* @playerversion AIR 1.1
* @productversion Flex 3
*/
protected var label:IUITextField;
//----------------------------------
// listData
//----------------------------------
/**
* @private
* Storage for the listData property.
*/
private var _listData:DataGridListData;
[Bindable("dataChange")]
/**
* The implementation of the listData
property
* as defined by the IDropInListItemRenderer interface.
*
* @see mx.controls.listClasses.IDropInListItemRenderer
*
* @langversion 3.0
* @playerversion AIR 1.1
* @productversion Flex 3
*/
public function get listData():BaseListData
{
return _listData;
}
/**
* @private
*/
public function set listData(value:BaseListData):void
{
_listData = DataGridListData(value);
invalidateProperties();
}
//--------------------------------------------------------------------------
//
// Overridden methods: UIComponent
//
//--------------------------------------------------------------------------
/**
* @private
*/
override protected function createChildren():void
{
super.createChildren();
if (!label)
{
label = IUITextField(createInFontContext(UITextField));
label.styleName = this;
addChild(DisplayObject(label));
}
}
/**
* @private
* Apply the data and listData.
* Create an instance of the icon if specified,
* and set the text into the text field.
*/
override protected function commitProperties():void
{
super.commitProperties();
if (icon)
{
removeChild(DisplayObject(icon));
icon = null;
}
if (_data != null)
{
listOwner = ListBase(_listData.owner);
if (FileSystemDataGrid(listOwner).showIcons)
{
var iconClass:Class =
listOwner.getStyle(File(_data).isDirectory ?
"directoryIcon" :
"fileIcon");
icon = new iconClass();
addChild(DisplayObject(icon));
}
label.text = _listData.label ? _listData.label : " ";
label.multiline = listOwner.variableRowHeight;
label.wordWrap = listOwner.wordWrap;
if (listOwner.showDataTips)
{
if (label.textWidth > label.width ||
listOwner.dataTipFunction != null)
{
toolTip = listOwner.itemToDataTip(_data);
}
else
{
toolTip = null;
}
}
else
{
toolTip = null;
}
}
else
{
label.text = " ";
toolTip = null;
}
}
/**
* @private
*/
override protected function measure():void
{
super.measure();
var w:Number = 0;
if (icon)
w = icon.measuredWidth;
// Guarantee that label width isn't zero
// because it messes up ability to measure.
if (label.width < 4 || label.height < 4)
{
label.width = 4;
label.height = 16;
}
if (isNaN(explicitWidth))
{
w += label.getExplicitOrMeasuredWidth();
measuredWidth = w;
measuredHeight = label.getExplicitOrMeasuredHeight();
}
else
{
measuredWidth = explicitWidth;
label.setActualSize(Math.max(explicitWidth - w, 4), label.height);
measuredHeight = label.getExplicitOrMeasuredHeight();
if (icon && icon.measuredHeight > measuredHeight)
measuredHeight = icon.measuredHeight;
}
}
/**
* @private
*/
override protected function updateDisplayList(unscaledWidth:Number,
unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
var startX:Number = 0;
if (icon)
{
icon.x = startX;
startX = icon.x + icon.measuredWidth;
icon.setActualSize(icon.measuredWidth, icon.measuredHeight);
}
label.x = startX;
label.setActualSize(unscaledWidth - startX, measuredHeight);
var verticalAlign:String = getStyle("verticalAlign");
if (verticalAlign == "top")
{
label.y = 0;
if (icon)
icon.y = 0;
}
else if (verticalAlign == "bottom")
{
label.y = unscaledHeight - label.height + 2; // 2 for gutter
if (icon)
icon.y = unscaledHeight - icon.height;
}
else
{
label.y = (unscaledHeight - label.height) / 2;
if (icon)
icon.y = (unscaledHeight - icon.height) / 2;
}
var labelColor:Number;
if (data && parent)
{
if (!enabled)
labelColor = getStyle("disabledColor");
else if (listOwner.isItemHighlighted(listData.uid))
labelColor = getStyle("textRollOverColor");
else if (listOwner.isItemSelected(listData.uid))
labelColor = getStyle("textSelectedColor");
else
labelColor = getStyle("color");
label.setColor(labelColor);
}
}
}
}