|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AsyncTaskLauncher.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.util.swing; | |
| 38 | ||
| 39 | /** The base class of the component that launches an AsyncTask. It manages the multi-threading and ensures that the | |
| 40 | * correct methods of the task are performed on the correct thread. | |
| 41 | * @author jlugo | |
| 42 | */ | |
| 43 | public abstract class AsyncTaskLauncher { | |
| 44 | ||
| 45 | /** Returns whether the launcher should call <code>setParentContainerEnabled</code> both to disable and to re-enable | |
| 46 | * the parent. This facility gives launchers more control over the view in case dissabling or re-enabling the view | |
| 47 | * produces inconsistent behavior. <p> | |
| 48 | * In some cases, this method should always return true, e.g., when for each call to lock the UI, you must call the | |
| 49 | * unlock method an equal number of times to actually unlock the frame. If this were dissabling a normal swing | |
| 50 | * component, where can only be on/off, you wouldn't want to re-enable the component if it was already disabled. | |
| 51 | * @return whether the launcher should call <code>setParentContainerEnabled</code> | |
| 52 | */ | |
| 53 | protected abstract boolean shouldSetEnabled(); | |
| 54 | ||
| 55 | /** Sets the enabled state of the parent component. When the parent component is dissabled, the user cannot invoke any | |
| 56 | * operations via mouse clicks or key strokes. <i><b>Note:</b> this method only runs in the event thread.</i> | |
| 57 | * @param enabled Whether the parent container should be enabled | |
| 58 | */ | |
| 59 | protected abstract void setParentContainerEnabled(boolean enabled); | |
| 60 | ||
| 61 | /** Creates a progress monitor that can be used to provide feedback to the user during the asynchronous task. This | |
| 62 | * progress monitor can also be used to allow the user to request the task to be canceled. <p> | |
| 63 | * <i>This method only executes in the event-handling thread.</i> | |
| 64 | * @return The progress monitor used to provide feedback. | |
| 65 | */ | |
| 66 | protected abstract IAsyncProgress createProgressMonitor(String description, int min, int max); | |
| 67 | ||
| 68 | /** Executes the AsyncTask in its own thread after performing any needed steps to prepare the UI for its execution. | |
| 69 | * | |
| 70 | * @param <R> The type of result to pass from <code>runAsync</code> to <code>complete</code> | |
| 71 | * @param task The task to execute on its own worker thread | |
| 72 | * @param showProgress Whether the progress monitor should be displayed to the user. If it is false, the user will | |
| 73 | * not be able to make any cancelation requests to the task. | |
| 74 | * @param lockUI Whether the user should be able to interact with the rest of the UI while the task is in progress. | |
| 75 | */ | |
| 76 | 0 | public <P, R> void executeTask(final AsyncTask<P, R> task, final P param, final boolean showProgress, |
| 77 | final boolean lockUI) { | |
| 78 | 0 | Runnable uiInit = new Runnable() { |
| 79 | 0 | public void run() { |
| 80 | 0 | final boolean shouldUnlockUI = shouldSetEnabled() && lockUI; |
| 81 | 0 | final IAsyncProgress monitor = |
| 82 | createProgressMonitor(task.getDiscriptionMessage(), task.getMinProgress(), task.getMaxProgress()); | |
| 83 | 0 | if (shouldSetEnabled() && lockUI) setParentContainerEnabled(false); |
| 84 | ||
| 85 | 0 | Thread taskThread = new Thread(new Runnable() { |
| 86 | 0 | public void run() { |
| 87 | 0 | R result = null; |
| 88 | 0 | Exception caughtException = null; |
| 89 | 0 | try { |
| 90 | 0 | result = task.runAsync(param, monitor); |
| 91 | } catch (Exception e) { | |
| 92 | 0 | caughtException = e; |
| 93 | } | |
| 94 | ||
| 95 | 0 | final AsyncCompletionArgs<R> args = |
| 96 | new AsyncCompletionArgs<R>(result, caughtException, monitor.isCanceled()); | |
| 97 | ||
| 98 | 0 | Runnable cleanup = new Runnable() { |
| 99 | 0 | public void run() { |
| 100 | 0 | task.complete(args); |
| 101 | ||
| 102 | 0 | if (shouldUnlockUI) setParentContainerEnabled(true); |
| 103 | } | |
| 104 | }; | |
| 105 | ||
| 106 | 0 | Utilities.invokeLater(cleanup); |
| 107 | } | |
| 108 | }, "Task Thread - " + task.getName()); | |
| 109 | ||
| 110 | 0 | taskThread.start(); |
| 111 | } | |
| 112 | }; | |
| 113 | ||
| 114 | 0 | Utilities.invokeLater(uiInit); |
| 115 | } | |
| 116 | } |
|
||||||||||