<commit_before>package org.safehaus.subutai.core.env.ui.forms;


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.safehaus.subutai.core.env.api.Environment;
import org.safehaus.subutai.core.env.api.EnvironmentManager;
import org.safehaus.subutai.core.env.api.exception.EnvironmentDestructionException;
import org.safehaus.subutai.core.env.api.exception.EnvironmentNotFoundException;

import com.vaadin.server.ClientConnector;
import com.vaadin.ui.Button;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;


public class EnvironmentForm
{
    private final EnvironmentManager environmentManager;
    private static final String CONTAINERS = "Containers";
    private static final String NAME = "Name";
    private static final String STATUS = "Status";
    private static final String DESTROY = "Destroy";
    private static final String VIEW_ENVIRONMENTS = "View environments";

    private final VerticalLayout contentRoot;
    private Table environmentsTable;
    private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();


    public EnvironmentForm( final EnvironmentManager environmentManager )
    {
        this.environmentManager = environmentManager;

        contentRoot = new VerticalLayout();

        contentRoot.setSpacing( true );
        contentRoot.setMargin( true );

        Button viewEnvironmentsButton = new Button( VIEW_ENVIRONMENTS );
        viewEnvironmentsButton.setId( "viewEnvironmentsButton" );

        viewEnvironmentsButton.addClickListener( new Button.ClickListener()
        {
            @Override
            public void buttonClick( final Button.ClickEvent event )
            {
                updateEnvironmentsTable();
            }
        } );

        environmentsTable = createEnvironmentsTable( "Environments" );
        environmentsTable.setId( "Environments" );

        //        contentRoot.addComponent( viewEnvironmentsButton );
        contentRoot.addComponent( environmentsTable );

        contentRoot.addDetachListener( new ClientConnector.DetachListener()
        {
            @Override
            public void detach( final ClientConnector.DetachEvent event )
            {
                executor.shutdown();
            }
        } );

        startTableUpdateThread();
    }


    private void startTableUpdateThread()
    {
        executor.scheduleWithFixedDelay( new Runnable()
        {
            @Override
            public void run()
            {

                environmentsTable.getUI().access( new Runnable()
                {
                    @Override
                    public void run()
                    {
                        updateEnvironmentsTable();
                    }
                } );
            }
        }, 3, 5, TimeUnit.SECONDS );
    }


    private void updateEnvironmentsTable()
    {
        environmentsTable.removeAllItems();
        for ( final Environment environment : environmentManager.getEnvironments() )
        {
            final Button containersBtn = new Button( CONTAINERS );
            containersBtn.addClickListener( new Button.ClickListener()
            {
                @Override
                public void buttonClick( final Button.ClickEvent event )
                {
                    contentRoot.getUI().addWindow( new ContainersWindow( environment ) );
                }
            } );

            final Button destroyBtn = new Button( DESTROY );
            destroyBtn.setId( environment.getName() + "-destroy" );
            destroyBtn.addClickListener( new Button.ClickListener()
            {
                @Override
                public void buttonClick( final Button.ClickEvent clickEvent )
                {
                    destroyBtn.setEnabled( false );
                    destroyEnvironment( environment );
                }
            } );

            environmentsTable.addItem( new Object[] {
                    environment.getName(), environment.getStatus().name(), containersBtn, destroyBtn
            }, null );
        }
        environmentsTable.refreshRowCache();
    }


    private void destroyEnvironment( final Environment environment )
    {
        try
        {
            environmentManager.destroyEnvironment( environment.getId(), true, false );

            Notification.show( "Environment destruction started" );
        }
        catch ( EnvironmentDestructionException | EnvironmentNotFoundException e )
        {
            Notification.show( "Error destroying environment", e.getMessage(), Notification.Type.ERROR_MESSAGE );
        }
    }


    private Table createEnvironmentsTable( String caption )
    {
        Table table = new Table( caption );
        table.addContainerProperty( NAME, String.class, null );
        table.addContainerProperty( STATUS, String.class, null );
        table.addContainerProperty( CONTAINERS, Button.class, null );
        table.addContainerProperty( DESTROY, Button.class, null );<commit_msg>Env Mgr in progress<commit_after>package org.safehaus.subutai.core.env.ui.forms;


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.safehaus.subutai.core.env.api.Environment;
import org.safehaus.subutai.core.env.api.EnvironmentManager;
import org.safehaus.subutai.core.env.api.EnvironmentStatus;
import org.safehaus.subutai.core.env.api.exception.EnvironmentDestructionException;
import org.safehaus.subutai.core.env.api.exception.EnvironmentNotFoundException;

import com.vaadin.server.ClientConnector;
import com.vaadin.ui.Button;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;


public class EnvironmentForm
{
    private final EnvironmentManager environmentManager;
    private static final String CONTAINERS = "Containers";
    private static final String NAME = "Name";
    private static final String STATUS = "Status";
    private static final String DESTROY = "Destroy";
    private static final String VIEW_ENVIRONMENTS = "View environments";

    private final VerticalLayout contentRoot;
    private Table environmentsTable;
    private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();


    public EnvironmentForm( final EnvironmentManager environmentManager )
    {
        this.environmentManager = environmentManager;

        contentRoot = new VerticalLayout();

        contentRoot.setSpacing( true );
        contentRoot.setMargin( true );

        Button viewEnvironmentsButton = new Button( VIEW_ENVIRONMENTS );
        viewEnvironmentsButton.setId( "viewEnvironmentsButton" );

        viewEnvironmentsButton.addClickListener( new Button.ClickListener()
        {
            @Override
            public void buttonClick( final Button.ClickEvent event )
            {
                updateEnvironmentsTable();
            }
        } );

        environmentsTable = createEnvironmentsTable( "Environments" );
        environmentsTable.setId( "Environments" );

        //        contentRoot.addComponent( viewEnvironmentsButton );
        contentRoot.addComponent( environmentsTable );

        contentRoot.addDetachListener( new ClientConnector.DetachListener()
        {
            @Override
            public void detach( final ClientConnector.DetachEvent event )
            {
                executor.shutdown();
            }
        } );

        startTableUpdateThread();
    }


    private void startTableUpdateThread()
    {
        executor.scheduleWithFixedDelay( new Runnable()
        {
            @Override
            public void run()
            {

                environmentsTable.getUI().access( new Runnable()
                {
                    @Override
                    public void run()
                    {
                        updateEnvironmentsTable();
                    }
                } );
            }
        }, 3, 5, TimeUnit.SECONDS );
    }


    private void updateEnvironmentsTable()
    {
        environmentsTable.removeAllItems();
        for ( final Environment environment : environmentManager.getEnvironments() )
        {
            final Button containersBtn = new Button( CONTAINERS );
            containersBtn.addClickListener( new Button.ClickListener()
            {
                @Override
                public void buttonClick( final Button.ClickEvent event )
                {
                    contentRoot.getUI().addWindow( new ContainersWindow( environment ) );
                }
            } );

            final Button destroyBtn = new Button( DESTROY );
            destroyBtn.setId( environment.getName() + "-destroy" );
            destroyBtn.addClickListener( new Button.ClickListener()
            {
                @Override
                public void buttonClick( final Button.ClickEvent clickEvent )
                {
                    destroyBtn.setEnabled( false );
                    destroyEnvironment( environment );
                }
            } );

            destroyBtn.setEnabled( !environment.getStatus().equals( EnvironmentStatus.UNDER_MODIFICATION ) );

            environmentsTable.addItem( new Object[] {
                    environment.getName(), environment.getStatus().name(), containersBtn, destroyBtn
            }, null );
        }
        environmentsTable.refreshRowCache();
    }


    private void destroyEnvironment( final Environment environment )
    {
        try
        {
            environmentManager.destroyEnvironment( environment.getId(), true, false );

            Notification.show( "Environment destruction started" );
        }
        catch ( EnvironmentDestructionException | EnvironmentNotFoundException e )
        {
            Notification.show( "Error destroying environment", e.getMessage(), Notification.Type.ERROR_MESSAGE );
        }
    }


    private Table createEnvironmentsTable( String caption )
    {
        Table table = new Table( caption );
        table.addContainerProperty( NAME, String.class, null );
        table.addContainerProperty( STATUS, String.class, null );
        table.addContainerProperty( CONTAINERS, Button.class, null );
        table.addContainerProperty( DESTROY, Button.class, null );
{'fraction_non_alphanumeric': 0.08125301204819277, 'fraction_numerical': 0.0003855421686746988, 'mean_word_length': 2.22636815920398, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7596063', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2379, 'n_words': 530}