edu.tsinghua.lumaqq.ui.jobs.DialogJobExecutor.java Source code

Java tutorial

Introduction

Here is the source code for edu.tsinghua.lumaqq.ui.jobs.DialogJobExecutor.java

Source

/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.ui.jobs;

import static edu.tsinghua.lumaqq.resource.Messages.*;

import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;

import edu.tsinghua.lumaqq.ui.MainShell;
import edu.tsinghua.lumaqq.ui.helper.BusyFlag;

/**
 * ??????
 * ?modeless???????
 * ??
 * 
 * @author luma
 */
public class DialogJobExecutor extends AbstractExecutor {
    /**
     * 
     * 
     * @author luma
     */
    private class JobSequencer implements IRunnableWithProgress {
        private IProgressMonitor monitor;

        /* (non-Javadoc)
         * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
         */
        public void run(IProgressMonitor m) throws InvocationTargetException, InterruptedException {
            this.monitor = m;
            monitor.beginTask(taskName, getAllJobCount() * 100);
            for (IJob job : jobs)
                runJob(job);
            monitor.done();
        }

        /**
         * ?
         * 
         * @param job
         * @throws InvocationTargetException
         * @throws InterruptedException
         */
        private void runJob(IJob job) throws InvocationTargetException, InterruptedException {
            job.prepare(main);
            job.run(new SubProgressMonitor(monitor, 100));
            job.clear();
            followLink(job);
        }

        /**
         * ?
         * 
         * @param job
         * @throws InvocationTargetException
         * @throws InterruptedException
         */
        private void followLink(IJob job) throws InvocationTargetException, InterruptedException {
            if (job.isSuccess()) {
                // follow success link
                if (job.getSuccessLink() != null) {
                    IJob linkJob = job.getSuccessLink();
                    linkJob.setLinkArgument(job.getLinkArgument());
                    runJob(linkJob);
                }
            } else {
                // TODO add error to error list
                if (job.getErrorString() != null) {

                }

                // follow fail link
                if (job.getFailLink() != null) {
                    IJob linkJob = job.getFailLink();
                    linkJob.setLinkArgument(job.getLinkArgument());
                    runJob(linkJob);
                }
            }
        }
    }

    private String taskName;
    private boolean modeless;
    private boolean cancelable;

    /**
     * 
     * 
     * @param main
     *       MainShell
     */
    public DialogJobExecutor(MainShell main) {
        this(main, "");
    }

    /**
     * 
     * 
     * @param main
     *       MainShell
     * @param taskName
     *       ??
     */
    public DialogJobExecutor(MainShell main, String taskName) {
        super(main);
        this.taskName = taskName;
        modeless = false;
        cancelable = false;
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.ui.jobs.IExecutor#execute()
     */
    public void execute() {
        if (!BusyFlag.get()) {
            MessageDialog.openWarning(main.getShell(), message_box_common_warning_title, message_box_job_running);
            return;
        }

        main.getDisplay().syncExec(new Runnable() {
            public void run() {
                JobProgressMonitorDialog dialog = new JobProgressMonitorDialog(main.getShell(), modeless);
                try {
                    dialog.run(true, cancelable, new JobSequencer());
                } catch (Exception e) {
                }
            }
        });
        BusyFlag.release();

        for (IExecutorListener lis : listeners)
            lis.allCompleted();
    }

    /**
     * @return Returns the modeless.
     */
    public boolean isModeless() {
        return modeless;
    }

    /**
     * @param modeless The modeless to set.
     */
    public void setModeless(boolean modeless) {
        this.modeless = modeless;
    }

    /**
     * @return Returns the cancelable.
     */
    public boolean isCancelable() {
        return cancelable;
    }

    /**
     * @param cancelable The cancelable to set.
     */
    public void setCancelable(boolean cancelable) {
        this.cancelable = cancelable;
    }

    public boolean isBlocked() {
        return true;
    }
}