/*
 * This handles all the test execution activities.
 */
package net.sourceforge.javydreamercsw.validation.manager.web.execution;

import com.vaadin.ui.HorizontalLayout;
import com.validation.manager.core.server.core.TestCaseExecutionServer;
import de.steinwedel.messagebox.MessageBox;
import java.util.List;
import java.util.TreeMap;
import net.sourceforge.javydreamercsw.validation.manager.web.VMWindow;
import net.sourceforge.javydreamercsw.validation.manager.web.ValidationManagerUI;
import org.openide.util.Exceptions;
import org.vaadin.teemu.wizards.Wizard;
import org.vaadin.teemu.wizards.event.WizardCancelledEvent;
import org.vaadin.teemu.wizards.event.WizardCompletedEvent;
import org.vaadin.teemu.wizards.event.WizardProgressListener;
import org.vaadin.teemu.wizards.event.WizardStepActivationEvent;
import org.vaadin.teemu.wizards.event.WizardStepSetChangedEvent;

/**
 *
 * @author Javier A. Ortiz Bultron <javier.ortiz.78@gmail.com>
 */
public final class ExecutionWindow extends VMWindow {

    private final ValidationManagerUI ui;

    /**
     * Display all the executions one after another.
     *
     * @param ui ValidationManagerUI instance
     * @param executions Executions to display.
     */
    public ExecutionWindow(ValidationManagerUI ui,
            List<TestCaseExecutionServer> executions) {
        super();
        init(executions, -1);
        this.ui = ui;
    }

    /**
     * Display all the executions one after another for the specific test case.
     *
     * @param ui ValidationManagerUI instance
     * @param executions Executions to display.
     * @param tcID test case to show
     */
    public ExecutionWindow(ValidationManagerUI ui,
            List<TestCaseExecutionServer> executions, int tcID) {
        super();
        this.ui = ui;
        init(executions, tcID);
    }

    private void init(List<TestCaseExecutionServer> executions, int tcID) {
        HorizontalLayout layout = new HorizontalLayout();
        Wizard execution = new Wizard();
        TreeMap<Integer, TreeMap<Integer, ExecutionWizardStep>> sorted = new TreeMap<>();
        executions.forEach((tce) -> {
            tce.getExecutionStepList().forEach(es -> {
                if (tcID < 0
                        || es.getExecutionStepPK().getStepTestCaseId() == tcID) {
                    if (!sorted.containsKey(es.getExecutionStepPK().getStepTestCaseId())) {
                        sorted.put(es.getExecutionStepPK().getStepTestCaseId(),
                                new TreeMap<>());
                    }
                    sorted.get(es.getExecutionStepPK().getStepTestCaseId())
                            .put(es.getStep().getStepSequence(),
                                    new ExecutionWizardStep(execution, ui, es));
                }
            });
        });
        sorted.values().forEach(tm -> {
            tm.values().forEach(ew -> {
                execution.addStep(ew);
            });
        });
        execution.setDisplayedMaxTitles(3);
        execution.addListener(new WizardProgressListener() {
            @Override
            public void activeStepChanged(WizardStepActivationEvent event) {
                //Do nothing
            }

            @Override
            public void stepSetChanged(WizardStepSetChangedEvent event) {
                //Do nothing
            }

            @Override
            public void wizardCompleted(WizardCompletedEvent event) {
                //TODO: Add confirmation prior to locking the Test Case
                //See: https://vaadin.com/directory#!addon/messagebox
                MessageBox prompt = MessageBox.createQuestion()
                        .withCaption("Do you want to lock the test case?")
                        .withMessage("Locked test cases are commited and can no "
                                + "longer be modified.\nIt would be equivalent "
                                + "to documenting in paper.")
                        .withYesButton(() -> {
                    execution.getSteps().stream().map((step)
                            -> (ExecutionWizardStep) step).map((s)
                            -> s.getStep()).filter((ess) -> (!ess.isLocked()
                            && ess.getResultId() != null))
                            .forEachOrdered((ess) -> {
                                try {
                                    ess.setLocked(true);
                                    ess.write2DB();
                                    ui.updateScreen();
                                } catch (Exception ex) {
                                    Exceptions.printStackTrace(ex);
                                }
                            });
                        })
                        .withNoButton(() -> {
                            System.out.println("No button was pressed.");
                        });
                prompt.getWindow().setIcon(ValidationManagerUI.SMALL_APP_ICON);
                prompt.open();
                ui.removeWindow(ExecutionWindow.this);
            }

            @Override
            public void wizardCancelled(WizardCancelledEvent event) {
                ui.removeWindow(ExecutionWindow.this);
            }
        });
        layout.addComponent(execution);
        layout.setSizeFull();
        setContent(layout);
    }
}

@@ -130,16 +130,54 @@
 ion;%0D%0A%0D%0A
+import com.vaadin.icons.VaadinIcons;%0D%0A
 import c
@@ -277,24 +277,71 @@
 ionServer;%0D%0A
+import de.steinwedel.messagebox.ButtonOption;%0D%0A
 import de.st
@@ -3614,152 +3614,8 @@
  %7B%0D%0A
-                //TODO: Add confirmation prior to locking the Test Case%0D%0A                //See: https://vaadin.com/directory#!addon/messagebox%0D%0A
     
@@ -3671,24 +3671,24 @@
 Question()%0D%0A
+
             
@@ -4039,32 +4039,40 @@
                 
+        
 execution.getSte
@@ -4097,16 +4097,24 @@
 (step)%0D%0A
+        
         
@@ -4201,16 +4201,24 @@
         
+        
 -%3E s.get
@@ -4255,24 +4255,32 @@
 isLocked()%0D%0A
+        
             
@@ -4355,16 +4355,24 @@
         
+        
 .forEach
@@ -4383,32 +4383,40 @@
 red((ess) -%3E %7B%0D%0A
+        
                 
@@ -4470,24 +4470,32 @@
             
+        
 ess.setLocke
@@ -4536,24 +4536,32 @@
             
+       
+ 
 ess.write2DB
@@ -4561,24 +4561,32 @@
 rite2DB();%0D%0A
+        
             
@@ -4661,16 +4661,24 @@
         
+       
+ 
 %7D catch 
@@ -4691,24 +4691,32 @@
 tion ex) %7B%0D%0A
+        
             
@@ -4796,35 +4796,51 @@
                 
-%7D%0D%0A
+        %7D%0D%0A        
                 
@@ -4885,158 +4885,178 @@
    %7D
-)%0D%0A                        .withNoButton(() -%3E %7B%0D%0A                            System.out.println(%22No button was pressed.%22);%0D%0A                        %7D
+, ButtonOption.focus(),%0D%0A                                ButtonOption.icon(VaadinIcons.CHECK))%0D%0A                        .withNoButton(ButtonOption.icon(VaadinIcons.CLOSE)
 );%0D%0A

Use same icons to keep look and feel.