package com.gyosh.ui;

import com.gyosh.worker.TaskRunner;
import com.gyosh.worker.task.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

public class Main {
    private static final int TIMER_INTERVAL = 100;

    private JList taskList;
    private JButton addTask;
    private JButton removeTask;
    private JButton previousTask;
    private JButton nextTask;
    private JButton start;
    private JProgressBar progressBar;
    private JPanel mainPanel;
    private JLabel progressLog;
    private JTextField inputFilePath;
    private JButton browseInputFile;
    private JTextField outputFilePath;
    private JButton browseOutputFile;

    private File inputFile;
    private File outputFile;
    private TaskSelector taskSelector;
    private TaskRunner taskRunner;
    private Timer progressTicker;

    private DefaultListModel taskListModel;

    public Main() {
        browseInputFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                final JFileChooser fileChooser = new JFileChooser();

                int returnVal = fileChooser.showOpenDialog(mainPanel);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    inputFile = fileChooser.getSelectedFile();
                    inputFilePath.setText(inputFile.getAbsolutePath());
                }
            }
        });

        browseOutputFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                final JFileChooser fileChooser = new JFileChooser();
                int returnVal = fileChooser.showSaveDialog(mainPanel);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    outputFile = fileChooser.getSelectedFile();
                    outputFilePath.setText(outputFile.getAbsolutePath());
                }
            }
        });

        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                taskRunner = new TaskRunner(inputFilePath.getText(), outputFilePath.getText());
                for (int i = 0; i < taskListModel.getSize(); i++) {
                    taskRunner.addTask((Task)taskListModel.get(i));
                }

                progressBar.setMinimum(0);
                progressBar.setMaximum(100);
                progressTicker = new Timer(TIMER_INTERVAL, new ActionListener() {
                    public void actionPerformed(ActionEvent actionEvent) {
                        updateProgress();
                    }
                });

                Runnable taskRunnerWrapper = new Runnable() {
                    public void run() {
                        progressTicker.start();

                        taskRunner.run();

                        updateProgress();
                        progressTicker.stop();
                    }
                };
                new Thread(taskRunnerWrapper).start();
            }
        });

        addTask.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                taskSelector = new TaskSelector();
                taskSelector.pack();
                taskSelector.setVisible(true);

                taskListModel.addElement(taskSelector.getCreatedTask());
                taskList.updateUI();
            }
        });

        removeTask.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                int selectedIndex = taskList.getSelectedIndex();

                if ((0 <= selectedIndex) && (selectedIndex < taskListModel.getSize())) {
                    taskListModel.remove(selectedIndex);
                    taskList.setSelectedIndex(Math.min(selectedIndex, taskListModel.getSize() - 1));
                    taskList.updateUI();
                }
            }
        });

        previousTask.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                int selectedIndex = taskList.getSelectedIndex();

                if (selectedIndex - 1 >= 0) {
                    swapTask(selectedIndex - 1, selectedIndex);
                    taskList.setSelectedIndex(selectedIndex - 1);
                    taskList.updateUI();
                }
            }
        });

        nextTask.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                int selectedIndex = taskList.getSelectedIndex();

                if (selectedIndex + 1 < taskListModel.getSize()) {
                    swapTask(selectedIndex, selectedIndex + 1);
                    taskList.setSelectedIndex(selectedIndex + 1);
                    taskList.updateUI();
                }
            }
        });
    }

    private void createUIComponents() {
        taskListModel = new DefaultListModel();
        taskList = new JList(taskListModel);
    }

    private void swapTask(int index1, int index2) {
        Task temp = (Task)taskListModel.get(index1);
        taskListModel.set(index1, taskListModel.get(index2));
        taskListModel.set(index2, temp);
    }

    private void updateProgress() {
        progressBar.setValue(taskRunner.getProgressPercentage());
        progressBar.updateUI();

        progressLog.setText(taskRunner.getActivity());
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Kemangi");

        Main main = new Main();
        frame.setContentPane(main.mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

@@ -48,24 +48,57 @@
 TaskRunner;%0A
+import com.gyosh.worker.Utility;%0A
 import com.g
@@ -117,16 +117,16 @@
 task.*;%0A
-
 %0Aimport 
@@ -234,16 +234,44 @@
 o.File;%0A
+import java.util.ArrayList;%0A
 import j
@@ -306,16 +306,16 @@
  Main %7B%0A
-
     priv
@@ -356,16 +356,239 @@
 L = 100;
+%0A    private static final String NO_INPUT = %22Please specify input file!%22;%0A    private static final String NO_TASK = %22Please add at least one task!%22;%0A    private static final String NO_OUTPUT = %22Please specify output file!%22;
 %0A%0A    pr
@@ -2396,32 +2396,275 @@
  actionEvent) %7B%0A
+                List%3CString%3E inputErrors = validateInput();%0A                if (inputErrors.size() %3E 0) %7B%0A                    JOptionPane.showMessageDialog(null, Utility.join(inputErrors, %22%5Cn%22));%0A                    return;%0A                %7D%0A%0A
                 
@@ -6042,24 +6042,429 @@
 ());%0A    %7D%0A%0A
+    private List%3CString%3E validateInput() %7B%0A        List%3CString%3E errors = new ArrayList%3CString%3E();%0A%0A        if (inputFilePath.getText().isEmpty()) %7B%0A            errors.add(NO_INPUT);%0A        %7D%0A        if (taskListModel.getSize() == 0) %7B%0A            errors.add(NO_TASK);%0A        %7D%0A        if (outputFilePath.getText().isEmpty()) %7B%0A            errors.add(NO_OUTPUT);%0A        %7D%0A%0A        return errors;%0A    %7D%0A%0A
     public s

Add input validation on main form