|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AsyncTask.java | - | 16.7% | 16.7% | 16.7% |
|
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 | /** | |
40 | * The AsyncTask base class is a framework that facilitates execution of | |
41 | * operations asynchronously in order to free up the event-handling thread. This | |
42 | * task is passed to an implementation of the AsyncTaskLauncher to be run. | |
43 | * <p> | |
44 | * Any code that should be performed in asynchronously with the UI should be put | |
45 | * in the <code>runAsync</code> method. Any code that is run in this method | |
46 | * should NOT modify any Swing components at all. All modifications to Swing | |
47 | * components must either be enqueued on the event-handling thread or performed | |
48 | * in the <code>complete</code> method. If there is any data that must be | |
49 | * passed from the asynchronous step to the completion step, it should be | |
50 | * returned by the <code>runAsync</code> method. The same data returned by the | |
51 | * <code>runAsync</code> method will be given to the <code>complete</code> | |
52 | * method. In short, implementations of an AsyncTask need not manage the | |
53 | * information passing between the task thread and the UI thread. | |
54 | * <p> | |
55 | * The <code>runAsync</code> method is given a progress monitor in order for | |
56 | * the task to provide feedback to the user as to the progress of the task. The | |
57 | * min and max values for the progress are specified by the | |
58 | * <code>getMinProgress</code> and <code>getMaxProgress</code> methods in | |
59 | * the task. | |
60 | * | |
61 | * @author jlugo | |
62 | */ | |
63 | public abstract class AsyncTask<ParamType, ResType> { | |
64 | ||
65 | private String _name; | |
66 | ||
67 | /** Default Constructor */ | |
68 | 0 | public AsyncTask() { this("Untitled"); } |
69 | ||
70 | /** Creates a task that has the given name | |
71 | * @param name The name of the task. | |
72 | */ | |
73 | 8 | public AsyncTask(String name) { _name = name; } |
74 | ||
75 | /** This is the method of the task that is run on the separate thread. Any | |
76 | * implementation of this method should not make any changes to GUI components | |
77 | * unless those calls are made explicitly thread safe by the developer. Any | |
78 | * code that modifies swing GUI components in any way should be located in the | |
79 | * <code>complete</code> method. | |
80 | * | |
81 | * @param param Any parameter that should be passed to the task when it is executed | |
82 | * @param monitor An object that controls the flow of information about task progress both to and from the runAsync | |
83 | * method. This also offers a means of passing a result from the async step to the completion step. | |
84 | * @throws RuntimeException | |
85 | */ | |
86 | public abstract ResType runAsync(ParamType param, IAsyncProgress monitor) throws Exception; | |
87 | ||
88 | /** Performs te completion step where modifications to swing components are made. This method runs in the event thread | |
89 | * so changes made to swing components are safe. | |
90 | */ | |
91 | public abstract void complete(AsyncCompletionArgs<ResType> args); | |
92 | ||
93 | /** Sets the description of the task that should be displayed in the progress | |
94 | * monitor that the user sees. While the task is in progress, a separate note | |
95 | * can be set in order to display specific information about the progress of | |
96 | * the task. This can be set by calling <code>ProgressMonitor.setNote</code> | |
97 | * | |
98 | * @return A brief description of the task being performed | |
99 | */ | |
100 | public abstract String getDiscriptionMessage(); | |
101 | ||
102 | /** Returns the name of this specific type of task. If this is not overridden | |
103 | * @return the name of the task | |
104 | */ | |
105 | 0 | public String getName() { return _name; } |
106 | ||
107 | /** Returns the minimum value of the progress monitor | |
108 | * @return The minimum value (0.0%) of the progress monitor | |
109 | */ | |
110 | 0 | public int getMinProgress() { return 0; } |
111 | ||
112 | /** Reutrns the minimum value of the progress monitor | |
113 | * @return The minimum value (100.0%) of the progress monitor | |
114 | */ | |
115 | 0 | public int getMaxProgress() { return 100; } |
116 | ||
117 | 0 | public String toString() { |
118 | 0 | return getClass().getName() + ": " + getName() + " (@" + System.identityHashCode(this) + ")"; |
119 | } | |
120 | } |
|