package org.bdgp.MMSlide;

import java.awt.Dimension;
import java.util.Date;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

import javax.swing.JDialog;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.JProgressBar;
import javax.swing.JLabel;

import org.bdgp.MMSlide.DB.Task;
import org.bdgp.MMSlide.Modules.Interfaces.TaskListener;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class WorkflowRunnerDialog extends JDialog {
    private WorkflowRunner workflowRunner;
    
    public WorkflowRunnerDialog(WorkflowDialog workflowDialog, 
            WorkflowRunner runner,
            String startModuleId,
            boolean resume) 
    {
        super(workflowDialog, "Workflow Runner");
        this.workflowRunner = runner;
        getContentPane().setLayout(new MigLayout("", "[][grow]", "[grow][][]"));
        setPreferredSize(new Dimension(800,600));
        
        JLabel lblLogOutput = new JLabel("Log Output");
        getContentPane().add(lblLogOutput, "cell 0 0");
        
        final JTextPane textPane = new JTextPane();
        textPane.setEditable(false);
        getContentPane().add(textPane, "cell 1 0,grow");
        
        JLabel lblProgress = new JLabel("Progress");
        getContentPane().add(lblProgress, "cell 0 1");
        
        final JProgressBar progressBar = new JProgressBar();
        getContentPane().add(progressBar, "cell 1 1,growx");
        
        JButton btnNewButton = new JButton("Stop");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                workflowRunner.stop();
            }
        });
        getContentPane().add(btnNewButton, "flowx,cell 1 2,alignx trailing");
        
        JButton btnKill = new JButton("Kill");
        btnKill.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                workflowRunner.kill();
            }
        });
        getContentPane().add(btnKill, "cell 1 2");
        
        // logging output
        workflowRunner.addLogHandler(new Handler() {
            @Override public void publish(LogRecord record) {
        	    textPane.setText(String.format("%s[%s:%s:%s] %s%n", 
        	            textPane.getText(), 
        	            record.getLoggerName(),
        	            new Date(record.getMillis()), 
        	            record.getLevel(), 
        	            record.getMessage()));
            }
            @Override public void flush() {}
            @Override public void close() throws SecurityException { }});
        
        // progress bar
        List<Task> tasks = workflowRunner.getTaskStatus().select();
        progressBar.setIndeterminate(false);
        progressBar.setMaximum(tasks.size());
        workflowRunner.addTaskListener(new TaskListener() {
        	int completedTasks = 0;
            @Override public void notifyTask(Task task) {
                completedTasks++;
                progressBar.setValue(completedTasks);
            }});
        
        if (!resume) {
            workflowRunner.deleteTaskRecords();
            workflowRunner.createTaskRecords();
        }
        workflowRunner.run(startModuleId);
    }

}

@@ -857,24 +857,69 @@
 w Runner%22);%0A
+    %09final WorkflowRunnerDialog self = this;%0A
         this
@@ -1608,32 +1608,38 @@
        %0A        
+final 
 JButton btnNewBu
@@ -1633,25 +1633,20 @@
 tton btn
-NewButton
+Stop
  = new J
@@ -1672,25 +1672,20 @@
      btn
-NewButton
+Stop
 .addActi
@@ -1876,17 +1876,12 @@
 (btn
-NewButton
+Stop
 , %22f
@@ -1930,16 +1930,22 @@
         
+final 
 JButton 
@@ -2819,16 +2819,22 @@
         
+final 
 List%3CTas
@@ -2972,24 +2972,337 @@
 ks.size());%0A
+        %0A        final JButton btnClose = new JButton(%22Close%22);%0A        btnClose.addActionListener(new ActionListener() %7B%0A        %09public void actionPerformed(ActionEvent e) %7B%0A        %09%09self.dispose();%0A        %09%7D%0A        %7D);%0A        btnClose.setEnabled(false);%0A        getContentPane().add(btnClose, %22cell 1 2%22);%0A
         work
@@ -3528,16 +3528,220 @@
 Tasks);%0A
+                if (completedTasks == tasks.size()) %7B%0A                %09btnStop.setEnabled(false);%0A                %09btnKill.setEnabled(false);%0A                %09btnClose.setEnabled(true);%0A                %7D%0A
         

add Close button to workflow runner dialog