package org.jboss.as.console.client.domain.hosts;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import org.jboss.as.console.client.Console;
import org.jboss.as.console.client.domain.events.HostSelectionEvent;
import org.jboss.as.console.client.shared.state.CurrentHostSelection;
import org.jboss.ballroom.client.widgets.forms.ComboBox;

import java.util.List;

/**
 * @author Heiko Braun
 * @date 11/2/11
 */
public class HostSelector {

    private ComboBox hosts;

    public Widget asWidget() {

        HorizontalPanel layout = new HorizontalPanel();
        layout.setStyleName("fill-layout-width");

        layout.getElement().setAttribute("style","padding:4px;");
        hosts = new ComboBox();
        hosts.addValueChangeHandler(new ValueChangeHandler<String>() {
            @Override
            public void onValueChange(final ValueChangeEvent<String> event) {

                Scheduler.get().scheduleEntry(new Scheduler.ScheduledCommand() {
                    @Override
                    public void execute() {
                        Console.MODULES.getEventBus().fireEvent(new HostSelectionEvent(event.getValue()));
                    }
                });
            }
        });

        Label hostLabel = new Label("Host:");
        hostLabel.setStyleName("header-label");
        layout.add(hostLabel);
        Widget hWidget = hosts.asWidget();
        layout.add(hWidget);

        // combo box use all available space
        hWidget.getElement().getParentElement().setAttribute("width", "100%");

        return layout;
    }

    public void setHosts(List<String> hostNames)
    {
        hosts.setValues(hostNames);

        CurrentHostSelection hostSelection = Console.MODULES.getCurrentSelectedHost();
        if(hostSelection.isSet())
        {
            int i = 0;
            for(String name : hostNames)
            {
                if(name.equals(hostSelection.getName()))
                {
                    setItemSelected(i, true);
                    break;
                }
                i++;
            }
        }

    }

    public void setItemSelected(int item, boolean b) {
        hosts.setItemSelected(item, b);
    }
}

@@ -307,24 +307,76 @@
 t.ui.Label;%0A
+import com.google.gwt.user.client.ui.VerticalPanel;%0A
 import com.g
@@ -408,16 +408,16 @@
 Widget;%0A
-
 import o
@@ -819,24 +819,22 @@
         
-Horizont
+Vertic
 alPanel 
@@ -846,24 +846,22 @@
 t = new 
-Horizont
+Vertic
 alPanel(
@@ -1180,24 +1180,94 @@
 %3E event) %7B%0A%0A
+                if(!event.getValue().isEmpty())%0A                %7B%0A    
             
@@ -1347,32 +1347,36 @@
                 
+    
 @Override%0A      
@@ -1381,32 +1381,36 @@
                 
+   
+ 
 public void exec
@@ -1413,24 +1413,28 @@
 execute() %7B%0A
+    
             
@@ -1485,16 +1485,53 @@
 reEvent(
+%0A                                    
 new Host
@@ -1562,16 +1562,45 @@
 Value())
+%0A                            
 );%0A     
@@ -1606,32 +1606,36 @@
                 
+    
 %7D%0A              
@@ -1636,20 +1636,42 @@
         
+ 
+   
 %7D);%0A
+                %7D%0A
         
@@ -2093,24 +2093,58 @@
 ames)%0A    %7B%0A
+%0A%0A        hosts.clearSelection();%0A
         host
@@ -2472,16 +2472,22 @@
         
+hosts.
 setItemS
@@ -2598,71 +2598,68 @@
   %7D%0A
-%0A
     
-%7D%0A%0A
     
-public void setItemSelected(int item, boolean b) %7B%0A
+else %7B%0A            if(!hostNames.isEmpty())%0A        
     
@@ -2688,17 +2688,28 @@
 ted(
-item, b);
+0, true);%0A        %7D%0A
 %0A   

fix problems with host selector (related to recent ballroom changes)