Pages

Showing posts with label flex tree component. Show all posts
Showing posts with label flex tree component. Show all posts

Tuesday, December 22, 2009

Flex Tree Component Problem and Solution

Problem:
When we set horizontalScrollPolicy to auto, the scroll bar does appear at time where you expecting to appear. Let say for example, when text width within the nodes of the tree is more than the width of the tree component, then horizontal scroll bar should appear but it doesn't. It appears only when we set maxHorizontalScrollPosition to some value and it will get scrolled up to that value which is static but not dynamic.

Solution:
We will create custom tree component which will extends to Tree and to override the method such as updateDisplayList, getter and setter of maxHorizontalScrollPosition and it will work dynamically same as vertical scroll bar of the tree component.

package{
    import flash.events.Event;   
   
    import mx.controls.Tree;
    import mx.core.mx_internal;
    import mx.core.ScrollPolicy;
    import mx.events.TreeEvent;
   
    /**
     * ...
     * @author
     */
    public class CustomTreeComponent extends Tree
    {
       
        public function CustomTreeComponent()
        {
            super();           
        }       

        /**
         * It is used for setting the max horizontal scroll position
         * of the horizontal scroll bar.
         * It has been override to get actual value of the horizontal
          * distance i.e. without indentation of the width.
         *
         */
        override public function get maxHorizontalScrollPosition():Number {
            if (isNaN(mx_internal::_maxHorizontalScrollPosition)) {
                return 0;
            }
            return mx_internal::_maxHorizontalScrollPosition;        
        }           
       
        /**
         * @private
         */
        override public function set maxHorizontalScrollPosition(value:Number):void
        {
            mx_internal::_maxHorizontalScrollPosition = value;
            dispatchEvent(new Event("maxHorizontalScrollPositionChanged"));
            scrollAreaChanged = true;
            invalidateDisplayList();
        }
       
        /**
         * It is used for setting the horizontal scroll bar on or off depending
         * on the tree structure.
         * @param    unscaledWidth
         * @param    unscaledHeight
         */
        override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void {
            var diffWidth:Number = measureWidthOfItems(0, 0) - (unscaledWidth - viewMetrics.left - viewMetrics.right);
           
            if (diffWidth <= 0) {
                maxHorizontalScrollPosition = NaN;
            }
            else {
                maxHorizontalScrollPosition = diffWidth;
            }
           
            super.updateDisplayList(unscaledWidth,unscaledHeight);
        }
       
    }
   
}

In the code above,  you will notice that 'mx_internal' is been used. Flex framework uses internal namespace as 'mx_internal' for most of its internal data and it is available for developers to use mx_internal namespace.