 import flash.events.MouseEvent;
 import flash.ui.Keyboard;
 
-import spark.components.supportClasses.ListBase;
+import spark.components.supportClasses.ButtonBarBase;
 import spark.events.IndexChangeEvent;
 import spark.events.RendererExistenceEvent;
 


    /**
     *  @private
     *  Called when an item has been added to this component.
     */
    private function dataGroup_rendererAddHandler(event:RendererExistenceEvent):void
    {
        var renderer:IVisualElement = event.renderer; 
        var index:int = event.index;
        
        if (renderer)
        {
            renderer.addEventListener(MouseEvent.CLICK, item_clickHandler);
            if (renderer is IFocusManagerComponent)
                IFocusManagerComponent(renderer).focusEnabled = false;
            if (renderer is ButtonBarButton)
                ButtonBarButton(renderer).allowDeselection = !requireSelection;
        }
    }
    
    /**
     *  @private
     *  Called when an item has been removed from this component.
     */
    private function dataGroup_rendererRemoveHandler(event:RendererExistenceEvent):void
    {        
        var renderer:IVisualElement = event.renderer;
        
        if (renderer)
            renderer.removeEventListener(MouseEvent.CLICK, item_clickHandler);
    }
    
    /**
     *  @private
     *  Called when an item is clicked.
     */
    private function item_clickHandler(event:MouseEvent):void
    {
        var newIndex:int
        if (event.currentTarget is IItemRenderer)
            newIndex = IItemRenderer(event.currentTarget).itemIndex;
        else
            newIndex = dataGroup.getElementIndex(event.currentTarget as IVisualElement);

        var currentRenderer:IItemRenderer;
        if (caretIndex >= 0 && !inKeyUpHandler) // don't remove caret when keybd nav
        {
            currentRenderer = dataGroup.getElementAt(caretIndex) as IItemRenderer;
            currentRenderer.showsCaret = false;
        }

        if (newIndex == selectedIndex)
        {
            if (!requireSelection)
                selectedIndex = NO_SELECTION;
        }
        else
        {
            selectedIndex = newIndex;
        }

    }
    
    /**
     *  @private
     */
    private function caretChangeHandler(event:Event):void
    {
        adjustLayering(caretIndex);
    }

    /**
     *  @private
     */
    override protected function keyDownHandler(event:KeyboardEvent):void
    {
        var currentRenderer:IItemRenderer;
        var renderer:IItemRenderer;
        
        if (event.eventPhase == EventPhase.BUBBLING_PHASE)
            return;

        if (!enabled || !dataProvider || event.isDefaultPrevented())
            return;
        
        super.keyDownHandler(event);

        var oldCaretIndex:Number = caretIndex; 
        var e:IndexChangeEvent;
        var length:int = dataProvider.length;
        switch (event.keyCode)
        {
            case Keyboard.UP:
            case Keyboard.LEFT:
            {
                currentRenderer = dataGroup.getElementAt(caretIndex) as IItemRenderer;
                if (caretIndex > 0 || arrowKeysWrapFocus)
                {
                    if (currentRenderer)
                        currentRenderer.showsCaret = false;
                    setCurrentCaretIndex((caretIndex - 1 + length) % length);
                    adjustLayering(caretIndex);
                    renderer = dataGroup.getElementAt(caretIndex) as IItemRenderer;
                    if (renderer)
                        renderer.showsCaret = true;
                    e = new IndexChangeEvent(IndexChangeEvent.CARET_CHANGE); 
                    e.oldIndex = oldCaretIndex; 
                    e.newIndex = caretIndex; 
                    dispatchEvent(e);    
                }

                event.preventDefault();
                break;
            }
            case Keyboard.DOWN:
            case Keyboard.RIGHT:
            {
                currentRenderer = dataGroup.getElementAt(caretIndex) as IItemRenderer;
                if (caretIndex < dataProvider.length - 1 || arrowKeysWrapFocus)
                {
                    if (currentRenderer)
                        currentRenderer.showsCaret = false;
                    setCurrentCaretIndex((caretIndex + 1) % length);
                    adjustLayering(caretIndex);
                    renderer = dataGroup.getElementAt(caretIndex) as IItemRenderer;
                    if (renderer)
                        renderer.showsCaret = true;
                    e = new IndexChangeEvent(IndexChangeEvent.CARET_CHANGE); 
                    e.oldIndex = oldCaretIndex; 
                    e.newIndex = caretIndex; 
                    dispatchEvent(e);    
                }

                event.preventDefault();
                break;
            }            
            case Keyboard.SPACE:
            {
                currentRenderer = dataGroup.getElementAt(caretIndex) as IItemRenderer;
                if (!currentRenderer || (currentRenderer.selected && requireSelection))
                    return;
                currentRenderer.dispatchEvent(event);
                break;
            }            
        }
    }
  
    /**
     *  @private
     */
    override protected function keyUpHandler(event:KeyboardEvent):void
    {
        var currentRenderer:IItemRenderer;
        var renderer:IItemRenderer;

        if (event.eventPhase == EventPhase.BUBBLING_PHASE)
            return;

        if (!enabled)
            return;

        inKeyUpHandler = true;

        super.keyUpHandler(event);

        switch (event.keyCode)
        {
            case Keyboard.SPACE:
            {
                  currentRenderer = dataGroup.getElementAt(caretIndex) as IItemRenderer;
                if (!currentRenderer || (currentRenderer.selected && requireSelection))
                    return;
                currentRenderer.dispatchEvent(event);
                break;
            }            
        }

        inKeyUpHandler = false;
    }
}



use namespace mx_internal;  //ListBase and List share selection properties that are mx_internal

[IconFile("ButtonBar.png")]

[AccessibilityClass(implementation="spark.accessibility.ButtonBarAccImpl")]

/**
 *  The ButtonBar control defines a horizontal group of 
