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

📄 backgroundtask.java

📁 手机邮箱撒的方式方式方式的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*MujMail - Simple mail client for J2MECopyright (C) 2008 David Hauzar <david.hauzar.mujmail@gmail.com>This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */package mujmail.tasks;import java.util.Vector;import javax.microedition.lcdui.Alert;import javax.microedition.lcdui.AlertType;import javax.microedition.lcdui.Displayable;import mujmail.Lang;import mujmail.MujMail;import mujmail.util.Functions;import mujmail.util.StartupModes;import mujmail.util.Observable;/** * Represents the task that runs in background thread and that's progress can * be displayed to the user. *  * New task is created by subclassing this class and defining method doWork(). * In this method business logic of task should be defined. The task is started * by calling method <code>start()</code> of instance of such subclass. * * By default progress bar is automatically showed when the task is started. * User can than minimalize this progress bar. The progress bar have to be * updated by the implementation of the task using methods of Progress. * * If method <code>disableDisplayingProgress</code> is called before starting * the task, progress is not displayed. * * The class is Observable. Registered Observers will receive events when * the state of the task is changed. Types of events that can be received * are defined in class {@link TaskEvents}. *  * @author David Hauzar */public abstract class BackgroundTask extends Observable implements Progress {    /** Flag signals if we want to print debug prints */    protected static final boolean DEBUG = false;    private static final Object enableStartingTasksNotifier = new Object();    private static final Object enableTerminatingTasksNotifier = new Object();    private static boolean enableStartingTasks = true;    private static boolean enableTerminatingTasks = true;    private boolean checkBeforeStartingTasksDisabled = false;    // TODO: hashmap would be better?    private static Vector allTasks = new Vector();    /**     * @return the allTasks     */    static Vector getRunningTasks() {        return allTasks;    }    private Thread thread;    private String alertText = null;    private boolean isRunning = false;        ScreenContainer nextScreen = new ScreenContainer();    private final ProgressManager progressManager;    private String taskName;    /** true if automatically display progress when starting the task. */    private boolean automaticallyDisplayProgress = true;    /** true when the progress was already displayed. */    boolean progressDisplayed = false;    /** The user interface for canceling staring of the task. */    ConditionalActionRunnerUI conditionalActionRunnerUI = null;     /**     * Creates the instance of Background task.     * @param taskName the name of background task that will be displayed to     *  users in tasks manager user interface.     */    public BackgroundTask(String taskName) {        this.taskName = taskName;        progressManager = (ProgressManager) createProgressManager();        setTitle(taskName);  // TODO: Teporary change ... remove in final .. to easyly found source task    }    public String toString() {        return taskName +                 "; actual state: " + getTitle() +                "; object identifier" + super.toString();    }    /**     * Enables starting tasks.     */    static void enableStartingTasks() {        synchronized (enableStartingTasksNotifier) {            enableStartingTasks = true;            enableStartingTasksNotifier.notifyAll();        }    }    /**     * Disables starting tasks.     */    static void disableStartingTasks() {        enableStartingTasks = false;    }    /**     * Enables terminating tasks.     */    static void enableTerminatingTasks() {        synchronized (enableTerminatingTasksNotifier) {            enableTerminatingTasks = true;            enableTerminatingTasksNotifier.notifyAll();        }    }    /**     * Disables terminating tasks.     */    static void disableTerminatingTasks() {        enableTerminatingTasks = false;    }    /**     * Disables displaying progress after start of the task.     */    public void disableDisplayingProgress() {        automaticallyDisplayProgress = false;    }    /**     * Disables displaying user interface to conditional action runner when     * the action cannot be started immadiately.     */    public void disableDisplayingUserActionRunnerUI() {        conditionalActionRunnerUI = ConditionalActionRunnerUI.NOT_DISPLAY_ACTION_RUNNER_UI;    }    /**     * Disables checking whether there are less tasks of the same class than     * given limit started whe starting the task.     * This means that the will be started immediately after executing     * <code>start</code> method.     */    public void disableCheckBeforeStarting() {        checkBeforeStartingTasksDisabled = true;    }    /**     * Gets the name of this task.     * @return the name of this task.     */    public String getTaskName() {        return taskName;    }        /**     * Creates progress manager for this background task. Descendants can     * redefine this method to return another progress manager.     * Note, that returned object has to be of type ProgressManager so the     * descendant redefining this method must be in the same package as      * BackgroundTask.     *      * @return the ProgressManager instance.     */    protected Object createProgressManager() {        return new ProgressManager(this);    }    public boolean isDisplayed() {        return getProgressManager().isDisplayed();    }    public int getActual() {        return progressManager.getActual();    }    public String getTitle() {        return progressManager.getTitle();    }    public int getTotal() {        return progressManager.getTotal();    }                /**     * Starts the background task in new thread and displays progress manager.     *      * @param nextScreen the screen that will be displayed when the task will     *  finish the work.     * @param prevScreen the screen that will be displayed when the task will     *  be minimized.     */    public void start(Displayable nextScreen, Displayable prevScreen) {        this.setNextScreen(nextScreen);        getProgressManager().setPrevScreen(prevScreen);        conditionallyStartTask();    }    /**     * If this task is waiting to start, cancels starting this task.     * If this task is not waiting to start, des nothing.     */    public void cancelStartingTask() {        conditionalActionRunnerUI.actionRunner.cancelAction();    }    /**     * Runs the task if all conditions when the task can be runned holds.     * If it do not hold, runs it later.     * Does not block.     */    private void conditionallyStartTask() {        if (checkBeforeStartingTasksDisabled) {            immediatelyStart();            return;        }        ConditionalActionRunner startTaskActionRunner = new ConditionalActionRunner.ConditionalTaskRunner(this);        if (conditionalActionRunnerUI == null) {            conditionalActionRunnerUI = new ConditionalActionRunnerUI(                    startTaskActionRunner,                    "There is big number of tasks running",                    "The task " + getTaskName() + " will be started automatically when some task will terminate.");        }        startTaskActionRunner.startAction(                conditionalActionRunnerUI);    }    /**     * Immediately starts the task. Does not check whether there is bigger than     * allowed tasks running.     *     * Does not set prev or next screen.     */    void immediatelyStart() {        registeringTaskWorkBeforeStartingThread();        thread = new TaskThread(this);        thread.start();    }    /**     * Starts the background task in new thread and displays progress manager.     *      * When the task will be minimized, it will be displayed the screen that is     * current in the time of starting progress manager. When     * the task will finished, it will be also displayed also this screen.     */    public void start() {        conditionallyStartTask();    }        /**     * Starts the background task in new thread and displays progress manager.     *      * When the task will be minimized, it will be displayed the screen that is     * current in the time of starting progress manager.     *     * @param nextScreen the screen that will be displayed when the task will     *  finish the work.     */    public void start(Displayable nextScreen) {        this.setNextScreen(nextScreen);        conditionallyStartTask();    }

⌨️ 快捷键说明

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