package org.bdgp.MMSlide;

import java.awt.Dialog;
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", Dialog.ModalityType.APPLICATION_MODAL);
    	final WorkflowRunnerDialog self = this;
        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");
        
        final JButton btnStop = new JButton("Stop");
        btnStop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                workflowRunner.stop();
            }
        });
        getContentPane().add(btnStop, "flowx,cell 1 2,alignx trailing");
        
        final 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
        final List<Task> tasks = workflowRunner.getTaskStatus().select();
        progressBar.setIndeterminate(false);
        progressBar.setMaximum(tasks.size());
        
        final JButton btnClose = new JButton("Close");
        btnClose.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		self.dispose();
        	}
        });
        btnClose.setEnabled(false);
        getContentPane().add(btnClose, "cell 1 2");
        workflowRunner.addTaskListener(new TaskListener() {
        	int completedTasks = 0;
        	boolean stopped = false;
            @Override public void notifyTask(Task task) {
            	if (stopped == false) {
                    completedTasks++;
                    progressBar.setValue(completedTasks);
                    if (completedTasks == tasks.size()) {
                        btnStop.setEnabled(false);
                        btnKill.setEnabled(false);
                        btnClose.setEnabled(true);
                    }
            	}
            }
			@Override public void stopped() {
                progressBar.setValue(0);
                stopped = true;
                btnStop.setEnabled(false);
                btnKill.setEnabled(false);
                btnClose.setEnabled(true);
			}
			@Override public void killed() {
                progressBar.setValue(0);
                stopped = true;
                btnStop.setEnabled(false);
                btnKill.setEnabled(false);
                btnClose.setEnabled(true);
			}});
        
        if (!resume) {
            workflowRunner.deleteTaskRecords();
            workflowRunner.createTaskRecords();
        }
        workflowRunner.run(startModuleId);
    }

}

@@ -277,24 +277,56 @@
 .swing.J
-TextPane
+ScrollPane;%0Aimport javax.swing.JTextArea
 ;%0Aimport
@@ -1317,24 +1317,101 @@
  final JText
+Area text = new JTextArea();%0A        text.setEditable(false);%0A        JScroll
 Pane textPan
@@ -1403,24 +1403,30 @@
 ollPane text
+Scroll
 Pane = new J
@@ -1429,17 +1429,113 @@
 ew J
-TextPane(
+ScrollPane(text);%0A        textScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
 );%0A 
@@ -1545,24 +1545,30 @@
     text
+Scroll
 Pane.set
 Editable
@@ -1559,33 +1559,57 @@
 Pane.set
-Editable(false
+PreferredSize(new Dimension(800, 600)
 );%0A
+%0A
         
@@ -1629,24 +1629,30 @@
 e().add(text
+Scroll
 Pane, %22cell 
@@ -2684,20 +2684,16 @@
     text
-Pane
 .setText
@@ -2754,20 +2754,16 @@
     text
-Pane
 .getText

add scroll pane to log output text area