package org.jboss.as.console.client.widgets.tabs;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Heiko Braun
 * @date 1/31/12
 */
public class DefaultTabLayoutPanel extends TabLayoutPanel {

    private final static boolean isIE = Window.Navigator.getUserAgent().contains("MSIE");

    public DefaultTabLayoutPanel(double barHeight, Style.Unit barUnit) {
        super(barHeight, barUnit);
        addStyleName("default-tabpanel");
    }


    @Override
    protected void onAttach() {
        super.onAttach();

        if(isIE)
        {
            Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
                @Override
                public void execute() {
                    forceLayout();
                }
            });
        }
    }

    public void insert(Widget child, Widget tab, final int beforeIndex) {

        String text = ((HasText) tab).getText();

        HTML html = new HTML(text) {
            {
                this.sinkEvents(Event.ONKEYDOWN);
                this.sinkEvents(Event.ONMOUSEDOWN);
            }

            @Override
            public void onBrowserEvent(Event event) {

                int type = DOM.eventGetType(event);
                switch (type) {
                    case Event.ONKEYDOWN:
                        if(event.getKeyCode()== KeyCodes.KEY_ENTER)
                        {
                            DefaultTabLayoutPanel.this.selectTab(beforeIndex);
                            event.stopPropagation();
                        }
                        break;
                    case Event.ONMOUSEDOWN:
                        DefaultTabLayoutPanel.this.selectTab(beforeIndex);
                        event.stopPropagation();
                        break;
                    default:
                        return;

                }
            }
        };



        html.getElement().setTabIndex(0);

        super.insert(child, html, beforeIndex);
    }
}

@@ -686,16 +686,56 @@
 %22MSIE%22);
+%0A    private int prevSelectedIndex = -1;
 %0A%0A    pu
@@ -874,24 +874,78 @@
 tabpanel%22);%0A
+        getElement().setAttribute(%22role%22, %22tablist%22);%0A
     %7D%0A%0A%0A    
@@ -2404,56 +2404,684 @@
 0);%0A
-%0A        super.insert(child, html, beforeIndex);
+        html.getElement().setAttribute(%22role%22, %22tab%22);%0A        super.insert(child, html, beforeIndex);%0A    %7D%0A%0A    @Override%0A    public void selectTab(int index) %7B%0A%0A        flagSelected(prevSelectedIndex, false);%0A        super.selectTab(index);%0A        flagSelected(getSelectedIndex(), true);%0A%0A        prevSelectedIndex = getSelectedIndex();%0A%0A    %7D%0A%0A    private void flagSelected(int index, boolean isSelected)%0A    %7B%0A        if(index==-1) return;%0A%0A        if(isSelected)%7B%0A            getTabWidget(index).getElement().setAttribute(%22aria-selected%22, %22true%22);%0A        %7D%0A        else%0A        %7B%0A            getTabWidget(index).getElement().removeAttribute(%22aria-selected%22);%0A        %7D
 %0A   

set aria tab list role on DefaultTabLayoutPanel