⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jobexecutor.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
字号:
/*
* 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 java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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.LumaQQ;
import edu.tsinghua.lumaqq.ui.MainShell;
import edu.tsinghua.lumaqq.ui.tool.BusyFlag;

/**
 * 用来执行一系列的长时间任务,缺省打开一个模态对话框,用户在任务执行期间不能执行其他操作,
 * 可以通过设置modeless属性显示一个非模态对话框,在非模态下,用户可以进行其他操作,但是
 * 依然不能进行其他长时间任务
 * 
 * @author luma
 */
public class JobExecutor {
    /**
     * 执行所有任务
     * 
     * @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 monitor)
                throws InvocationTargetException, InterruptedException {
            this.monitor = monitor;
            monitor.beginTask(taskName, getAllJobCount() * 100);
            Iterator i = jobs.iterator();
            while(i.hasNext()) {
                IJob job = (IJob)i.next();
                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)
                    runJob(job.getSuccessLink());
            } else {
                // TODO add error to error list
                if(job.getErrorString() != null) {
                    
                }
                
                // follow fail link
                if(job.getFailLink() != null)
                    runJob(job.getFailLink());
            }
        }
    }
    
    private MainShell main;
    private List jobs;
    private String taskName;
    private boolean modeless;
    private boolean cancelable;
    
    /**
     * 创建一个任务执行器
     * 
     * @param main
     * 		MainShell对象
     */
    public JobExecutor(MainShell main) {
        this(main, "");
    }
    
    /**
     * 得到一个Job链的所有job个数
     * 
     * @param job
     * 	 	链头部Job
     * @return
     * 		job个数
     */
    private int getJobCount(IJob job) {
        int i = 0;
        if(job.getSuccessLink() != null)
            i += getJobCount(job.getSuccessLink()) + 1;
        if(job.getFailLink() != null)
            i += getJobCount(job.getFailLink()) + 1;
        return i;
    }
    
    /**
     * @return
     * 		所有Job的个数
     */
    private int getAllJobCount() {
        int size = jobs.size();
        int total = 0;
        for(int i = 0; i < size; i++)
            total += getJobCount((IJob)jobs.get(i)) + 1;
        return total;
    }
    
    /**
     * 创建一个任务执行器
     * 
     * @param main
     * 		MainShell对象
     * @param taskName
     * 		总任务名
     */
    public JobExecutor(MainShell main, String taskName) {
        this.main = main;
        this.taskName = taskName;
        modeless = false;
        cancelable = false;
        jobs = new ArrayList();
    }
    
    /**
     * 运行所有任务
     */
    public void run() {
        if(!BusyFlag.get()) {
            MessageDialog.openWarning(main.getShell(), LumaQQ.getString("message.box.common.warning.title"), LumaQQ.getString("message.box.job.running"));
            return;
        }
        
        JobProgressMonitorDialog dialog = new JobProgressMonitorDialog(main.getShell(), modeless);
        try {
            dialog.run(true, cancelable, new JobSequencer());
        } catch (Exception e) {
            
        } 
        
        BusyFlag.release();
    }
    
    /**
     * 添加一个任务
     * 
     * @param job
     * 		IJob对象
     */
    public void addJob(IJob job) {
        jobs.add(job);
    }
    
    /**
     * @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;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -