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

📄 taskmanager.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
字号:
/*
 * Copyright (C) 2006-2007 Funambol
 *
 * 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
 *
 * Created on 5 dicembre 2006, 17.02
 *
 */

package com.funambol.mailclient.mm;

import com.funambol.mailclient.AccountListener;
import com.funambol.mailclient.loc.Localization;
import com.funambol.util.Log;
import com.funambol.util.Observable;
import com.funambol.util.Observer;


/**
 * Class that implements AccountListener to recieve informations from the
 * messagemanager.
 * This informations are elaborated and sent to the Observers at UI level
 * (Messagelistupdater and progrssbar)
 *
 */
abstract public class TaskManager implements Observable, AccountListener{
    
    protected int max=1;
    protected int progress=0;
    protected String userInfoMessage="";
    protected Observer[] observer;
    protected int observerIndex=0;
    protected int numObservers=1;
    protected boolean finished=false;
    
    /** Creates a new instance of TaskManager */
    public TaskManager() {
        this(1);
    }
    
    /**
     * create a taskmanager with a max of numObservers observer
     *@param numObservers max number of observers this taskmanager can handle
     */
    public TaskManager(int numObservers) {
        this.numObservers=numObservers;
        observer=new Observer[numObservers];
        
    }
    
    /**
     * adds an observer to this taskmanager and immediatly update it<br>
     *
     * @param o the observer
     *
     * @return false if observable
     * does not support more observers 
     *
     */
    public boolean addObserver(Observer o) {
        if (observerIndex <numObservers) {
            this.observer[observerIndex]=o;
            observerIndex++;
            return true;
        } else {
            return false;
        }
    }
    
    
    /**
     * @return a long like 32.87% that represents the progress
     */
    public long getProgressPercent() {
        //we need a cast to long
        if (max==0) return 0;
        return (long)(( (int) (progress*(long)1000/max)) /(long)10) ;
    }
    
    /**
     * @return progress of the task (value between 0 and max)
     */
    public int getProgress() {
        return progress;
    }
    
    /**
     * @return max progress
     */
    public int getMax() {
        return max;
    }
    
    /**
     * @return a string that describes the current operation.
     */
    public String getMessage() {
        return userInfoMessage;
    }
    
    /**
     * update all observers passing o as param
     *
     *@param o the object to be given to abservers
     */
    public void update(Object o) {
        for (int i=0; i<observer.length; i++)
            if (observer[i]!=null)
                this.observer[i].update(o);
    }
    
    /**
     * remove specified observer from observer list
     */
    public void removeObserver(Observer o) {
        for (int i=0; i<observer.length; i++)
            if (o==this.observer[i]) {
            for (int j=i;j<observer.length-1;j++)
                observer[j]=observer[j+1];
            observerIndex--;
            }
        
        
    }
    
    /**
     * @return an error string based on the error status received
     */
    public String getErrorMessage(int status) {
        Log.debug("Getting Error Message from error status: [" + status + "]");
        String message;
        switch (status) {
            case AccountListener.STATUS_CONNECTION_ERROR:
                message=Localization.getMessages().ACCOUNT_LISTENER_ERROR_CONNECTIONG;
                break;
            case AccountListener.STATUS_RECV_ERROR:
                message=Localization.getMessages().ACCOUNT_LISTENER_ERROR_RECEIVING;
                break;
            case AccountListener.STATUS_SEND_ERROR:
                message=Localization.getMessages().ACCOUNT_LISTENER_ERROR_SENDING;
                break;
            case AccountListener.STATUS_SERVER_ERROR:
                message=Localization.getMessages().ACCOUNT_LISTENER_ERROR_FROM_SERVER;
                break;
            default:
                message=Localization.getMessages().ACCOUNT_LISTENER_ERROR_DEFAULT;
                break;
                
        }
        return message;
    }
    
    
    public void reset() { //FIXME
        max=1;
        progress=0;
        userInfoMessage="";
        observerIndex=0;
        numObservers=1;
        finished=false;
        observer = null;
    }
    
}

⌨️ 快捷键说明

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