package org.safehaus.subutai.plugin.shark.ui.manager;


import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutorService;

import org.safehaus.subutai.common.protocol.Agent;
import org.safehaus.subutai.common.tracker.ProductOperationState;
import org.safehaus.subutai.common.tracker.ProductOperationView;
import org.safehaus.subutai.core.tracker.api.Tracker;
import org.safehaus.subutai.plugin.shark.api.Shark;
import org.safehaus.subutai.plugin.shark.api.SharkClusterConfig;

import com.vaadin.server.ThemeResource;
import com.vaadin.server.VaadinSession;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.Window;


public class AddNodeWindow extends Window
{

    private final TextArea outputTxtArea;
    private final Label indicator;
    private Button ok;
    private volatile boolean track = true;


    public AddNodeWindow( final Shark shark, final ExecutorService executorService, final Tracker tracker,
                          final SharkClusterConfig config, Set<Agent> nodes )
    {
        super( "Add New Node" );
        setModal( true );

        setWidth( 600, Unit.PIXELS );
        setHeight( 400, Unit.PIXELS );

        GridLayout content = new GridLayout( 1, 3 );
        content.setSizeFull();
        content.setMargin( true );
        content.setSpacing( true );

        HorizontalLayout topContent = new HorizontalLayout();
        topContent.setSpacing( true );

        content.addComponent( topContent );
        topContent.addComponent( new Label( "Nodes:" ) );

        final ComboBox cmbNodes = new ComboBox();
        cmbNodes.setImmediate( true );
        cmbNodes.setTextInputAllowed( false );
        cmbNodes.setNullSelectionAllowed( false );
        cmbNodes.setRequired( true );
        cmbNodes.setWidth( 80, Unit.PERCENTAGE );
        for ( Agent node : nodes )
        {
            cmbNodes.addItem( node );
            cmbNodes.setItemCaption( node, node.getHostname() );
        }
        cmbNodes.setValue( nodes.iterator().next() );

        topContent.addComponent( cmbNodes );

        final Button addNodeBtn = new Button( "Add" );
        addNodeBtn.addStyleName( "default" );
        topContent.addComponent( addNodeBtn );

        ok = new Button( "Ok" );
        ok.addStyleName( "default" );
        ok.addClickListener( new Button.ClickListener()
        {
            @Override
            public void buttonClick( Button.ClickEvent clickEvent )
            {
                track = false;
                close();
            }
        } );

        addNodeBtn.addClickListener( new Button.ClickListener()
        {
            @Override
            public void buttonClick( Button.ClickEvent clickEvent )
            {
                addNodeBtn.setEnabled( false );
                showProgress();
                Agent agent = ( Agent ) cmbNodes.getValue();
                final UUID trackID = shark.addNode( config.getClusterName(), agent.getHostname() );
                executorService.execute( new Runnable()
                {
                    @Override
                    public void run()
                    {
                        while ( track )
                        {
                            ProductOperationView po =
                                    tracker.getProductOperation( SharkClusterConfig.PRODUCT_KEY, trackID );
                            if ( po != null )
                            {
                                setOutput(
                                        po.getDescription() + "\nState: " + po.getState() + "\nLogs:\n" + po.getLog() );
                                if ( po.getState() != ProductOperationState.RUNNING )
                                {
                                    hideProgress();
                                    break;
                                }
                            }
                            else
                            {
                                setOutput( "Product operation not found. Check logs" );
                                break;
                            }
                            try
                            {
                                Thread.sleep( 1000 );
                            }
                            catch ( InterruptedException ex )
                            {
                                break;
                            }
                        }
                    }
                } );
            }
        } );

        outputTxtArea = new TextArea( "Operation output" );
        outputTxtArea.setRows( 13 );
        outputTxtArea.setColumns( 43 );
        outputTxtArea.setImmediate( true );
        outputTxtArea.setWordwrap( true );

        content.addComponent( outputTxtArea );

        indicator = new Label();
        indicator.setIcon( new ThemeResource( "img/spinner.gif" ) );
        indicator.setContentMode( ContentMode.HTML );
        indicator.setHeight( 11, Unit.PIXELS );
        indicator.setWidth( 50, Unit.PIXELS );
        indicator.setVisible( false );

        HorizontalLayout bottomContent = new HorizontalLayout();
        bottomContent.addComponent( indicator );
        bottomContent.setComponentAlignment( indicator, Alignment.MIDDLE_RIGHT );
        bottomContent.addComponent( ok );

        content.addComponent( bottomContent );
        content.setComponentAlignment( bottomContent, Alignment.MIDDLE_RIGHT );

        setContent( content );
    }


    private void showProgress()
    {
        indicator.setVisible( true );
        ok.setEnabled( false );
    }


    private void setOutput( String output )
    {
        try
        {
            VaadinSession.getCurrent().getLockInstance().lock();
            if ( !com.google.gwt.thirdparty.guava.common.base.Strings.isNullOrEmpty( output ) )
            {
                outputTxtArea.setValue( output );
                outputTxtArea.setCursorPosition( outputTxtArea.getValue().length() - 1 );
            }
        }
        finally
        {
            VaadinSession.getCurrent().getLockInstance().unlock();
        }
    }


    private void hideProgress()
    {
        indicator.setVisible( false );
        ok.setEnabled( true );
    }


    @Override
    public void close()
    {
        track = false;
        super.close();
    }
}


@@ -5857,99 +5857,8 @@
   %7B%0A
-        try%0A        %7B%0A            VaadinSession.getCurrent().getLockInstance().lock();%0A    
     
@@ -5865,16 +5865,17 @@
     if (
+ 
  !com.go
@@ -5950,32 +5950,26 @@
 ) )%0A        
-    %7B%0A  
+%7B%0A
             
@@ -5960,34 +5960,32 @@
   %7B%0A            
-  
 outputTxtArea.se
@@ -5998,28 +5998,24 @@
 ( output );%0A
-    
             
@@ -6092,125 +6092,8 @@
  );%0A
-            %7D%0A        %7D%0A        finally%0A        %7B%0A            VaadinSession.getCurrent().getLockInstance().unlock();%0A
     
@@ -6098,32 +6098,33 @@
       %7D%0A    %7D%0A%0A%0A
+%0A
     private void

undo changes in AddNodeWindow.java class