TaskExecutor.java :  » XML-UI » SwingML » org » swingml » task » Java Open Source

Java Open Source » XML UI » SwingML 
SwingML » org » swingml » task » TaskExecutor.java
package org.swingml.task;

import java.util.*;

import org.swingml.task.monitoring.*;


/**
 * @author CrossLogic
 */
public class TaskExecutor {

    private static TaskExecutor instance;
    
    public static TaskExecutor getInstance () {
        if (instance == null) {
            instance = new TaskExecutor();
        }

        return instance;
    }

    private TaskExecutor () {
        super();
    }

    /**
     * This method is typically called from Swing, and will put a task into the
     * queue of tasks to be processed by the long running operation thread. This
     * method always queues the task to run without a modal dialog window. To
     * use a modal dialog window, see queueTask(ITask, boolean).
     * 
     * @param task
     * @param showModalDialog
     */
    public Object runTask (ITask task) {
        return runTask(task, false);
    }

    /**
     * This method is typically called from Swing, and will put a task into the
     * queue of tasks to be processed by the long running operation thread. If
     * the showModalDialog parameter is true, a modal window will open with a
     * progress bar for the task. The window will close when the task completes,
     * or if the user clicks the "hide" button.
     * 
     * @param task
     * @param showModalDialog
     */
    public Object runTask (ITask task, boolean showModalDialog) {
        Object result = null;
        if (task != null) {
            // Find pre and post execute tasks and enqueue them all
            List tasks = new ArrayList();

            // Add pre execute tasks
            AbstractTask yellowTask = (AbstractTask) task;
            List preExecutes = yellowTask.getPreExecuteTasks();
            if (preExecutes != null && preExecutes.size() > 0) {
                tasks.addAll(preExecutes);
            }

            // Add the task itself
            tasks.add(yellowTask);

            // Add post execute tasks
            List postExecutes = yellowTask.getPostExecuteTasks();
            if (postExecutes != null && postExecutes.size() > 0) {
                tasks.addAll(postExecutes);
            }

            // Queue them all to run
            result = runTasks(tasks, showModalDialog);
        }
        return result;
    }

    /**
     * Queue the given tasks to be run.
     * 
     * @param someTasks
     * @param showModalDialog
     */
    private Object runTasks (List someTasks, boolean showModalDialog) {
        Object finalResult = null;
        if (someTasks != null && someTasks.size() > 0) {

            Iterator schmiterator = someTasks.iterator();
            while (schmiterator.hasNext()) {
                Object aTask = schmiterator.next();
                if (aTask instanceof ITask) {
                    final ITask task = (ITask) aTask;

                    // Show the dialog?
                    if (showModalDialog) {
                        TaskMonitorDialog.getInstance().prepare(task);
                    }
                    Object executeResult = task.execute();
                    finalResult = task.onComplete(executeResult);
                }
            }
        }
        return finalResult;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.