 
 use namespace mx_internal;  //ListBase and List share selection properties that are mx_internal
 
-//--------------------------------------
-//  Other metadata
-//--------------------------------------
+[IconFile("ButtonBar.png")]
 
 [AccessibilityClass(implementation="spark.accessibility.ButtonBarAccImpl")]
-
-[IconFile("ButtonBar.png")]
 
 /**
  *  The ButtonBar control defines a horizontal group of 

    override public function set dataProvider(value:IList):void
    {
        if (dataProvider)
        {
            dataProvider.removeEventListener(CollectionEvent.COLLECTION_CHANGE, resetCollectionChangeHandler);
            if (dataProvider is ISelectableList)
                dataProvider.removeEventListener(Event.CHANGE, navigationChangeHandler);
        }
    
        // not really a default handler, we just want it to run after the datagroup
        if (value)
        {
            value.addEventListener(CollectionEvent.COLLECTION_CHANGE, resetCollectionChangeHandler, false, EventPriority.DEFAULT_HANDLER);
            if (value is ISelectableList)
                value.addEventListener(Event.CHANGE, navigationChangeHandler);
        }

        super.dataProvider = value;

        if (value is ISelectableList)
            if (ISelectableList(dataProvider).selectedIndex != selectedIndex)
                selectedIndex = ISelectableList(dataProvider).selectedIndex;
    }

    //----------------------------------
    //  requireSelection
    //---------------------------------- 
    
    /**
     *  @private
     */
    private var requireSelectionChanging:Boolean;
    
    /**
     *  @private
     */
    override public function set requireSelection(value:Boolean):void
    {
        super.requireSelection = value;
        requireSelectionChanging = true;
    }

    //--------------------------------------------------------------------------
    //
    //  Overridden methods
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     *  Called by the initialize() method of UIComponent
     *  to hook in the accessibility code.
     */
    override protected function initializeAccessibility():void
    {
        if (createAccessibilityImplementation != null)
            createAccessibilityImplementation(this);
    }

    /**
     *  @private
     */
    override protected function commitProperties():void
    {
        super.commitProperties();

        if (requireSelectionChanging && dataProvider)
        {
            requireSelectionChanging = false;
            var n:int = dataProvider.length;
            for (var i:int = 0; i < n; i++)
            {
                var renderer:ButtonBarButton = 
                    dataGroup.getElementAt(i) as ButtonBarButton;
                if (renderer)
                    renderer.allowDeselection = !requireSelection;
            }
        }
    }
    
    /**
     *  @private
     */
    override protected function partAdded(partName:String, instance:Object):void
    {
        super.partAdded(partName, instance);
        if (instance == dataGroup)
        {
            dataGroup.addEventListener(
                RendererExistenceEvent.RENDERER_ADD, dataGroup_rendererAddHandler);
            dataGroup.addEventListener(
                RendererExistenceEvent.RENDERER_REMOVE, dataGroup_rendererRemoveHandler);
        }
    }

    /**
     *  @private
     */
    override protected function partRemoved(partName:String, instance:Object):void
    {
        if (instance == dataGroup)
        {
            dataGroup.removeEventListener(
                RendererExistenceEvent.RENDERER_ADD, dataGroup_rendererAddHandler);
            dataGroup.removeEventListener(
                RendererExistenceEvent.RENDERER_REMOVE, dataGroup_rendererRemoveHandler);
        }
        
        super.partRemoved(partName, instance);
    }

    /**
     *  @private
     */
    override public function drawFocus(isFocused:Boolean):void
    {
        adjustLayering(caretIndex);
        drawButtonFocus(caretIndex, isFocused);
    }


    /**
     *  @private
     */
    override protected function itemSelected(index:int, selected:Boolean):void
    {
        super.itemSelected(index, selected);
        
        var renderer:IItemRenderer = 
            dataGroup.getElementAt(index) as IItemRenderer;
        
        if (renderer)
        {
            setCurrentCaretIndex(index);
            renderer.selected = selected;
        }
        if (dataProvider is ISelectableList && selected)
            ISelectableList(dataProvider).selectedIndex = index;
    }
        
    /**
     *  @private
     *  button bar always keeps something under the caret so don't let it
     *  become -1
     */
    override mx_internal function setCurrentCaretIndex(value:Number):void
    {
        if (value == -1)
            return;

        super.setCurrentCaretIndex(value);
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    /**
	 *  @private
	 */
	private function defaultButtonBarItemRendererFunction(data:Object):IFactory
    {
        var i:int = dataProvider.getItemIndex(data);
        if (i == 0)
            return firstButton ? firstButton : middleButton;

        var n:int = dataProvider.length - 1;
        if (i == n)
            return lastButton ? lastButton : middleButton;

        return middleButton;
    }


    /**
     *  @private
     */
    private function drawButtonFocus(index:int, focused:Boolean):void
    {
        var n:int = dataProvider ? dataProvider.length : 0;
        if (n > 0 && index < n)
        {
            var renderer:IItemRenderer = 
                dataGroup.getElementAt(index) as IItemRenderer;
            if (renderer)
                renderer.showsCaret = focused;
        }
    }

    /**
     *  @private
     *  Attempt to lift the focused button above the others
     *  so that the focus ring can show.
     */
    private function adjustLayering(caretIndex:int):void
    {
        var n:int = dataProvider ? dataProvider.length : 0;
        for (var i:int = 0; i < n; i++)
        {
            var renderer:IVisualElement = IVisualElement(dataGroup.getElementAt(i));
            // renderer may not exist in commitProps
            // should get called again when we get focus
            if (renderer)
            {
                if (i == caretIndex)
                    renderer.depth = 1;
                else
                    renderer.depth = 0;
            }
        }
    }

    //--------------------------------------------------------------------------
    //
    //  Event handlers
    //
    //--------------------------------------------------------------------------
    
    /**
     *  @private
     */
    private function navigationChangeHandler(event:Event):void
    {
        if (ISelectableList(dataProvider).selectedIndex != selectedIndex)
            selectedIndex = ISelectableList(dataProvider).selectedIndex;
    }

    /**

    public function ButtonBar()
    {
        super();
        
        itemRendererFunction = defaultButtonBarItemRendererFunction;
    }
    
    //--------------------------------------------------------------------------
    //
    //  Properties
