001: package org.swingml.task;
002:
003: import java.util.*;
004:
005: import org.swingml.task.monitoring.*;
006:
007: /**
008: * @author CrossLogic
009: */
010: public abstract class AbstractTask implements ITask {
011:
012: private List postExecuteTasks = new ArrayList();
013: private List preExecuteTasks = new ArrayList();
014: private int status = TaskStatus.NOT_STARTED;
015: private List taskMonitors = new ArrayList();
016:
017: public AbstractTask(ITaskMonitor aTaskMonitor) {
018: addTaskMonitor(aTaskMonitor);
019: }
020:
021: public AbstractTask(ITaskMonitor[] taskMonitors) {
022: if (getTaskMonitors() != null && getTaskMonitors().size() > 0) {
023: for (int x = 0; x < taskMonitors.length; x++) {
024: addTaskMonitor(taskMonitors[x]);
025: }
026: }
027: }
028:
029: public void addPostExecuteTask(ITask task) {
030: if (task != null) {
031: getPostExecuteTasks().add(task);
032: }
033: }
034:
035: public void addPreExecuteTask(ITask task) {
036: if (task != null) {
037: getPreExecuteTasks().add(task);
038: }
039: }
040:
041: public void addTaskMonitor(ITaskMonitor aTaskMonitor) {
042: if (aTaskMonitor != null
043: && !getTaskMonitors().contains(aTaskMonitor)) {
044: getTaskMonitors().add(aTaskMonitor);
045: }
046: }
047:
048: protected void beginTask() {
049: setStatus(TaskStatus.RUNNING);
050: Iterator schmiterator = getTaskMonitors().iterator();
051: ITaskMonitor taskMonitor;
052: while (schmiterator.hasNext()) {
053: taskMonitor = (ITaskMonitor) schmiterator.next();
054: taskMonitor.beginTask(this );
055: }
056: }
057:
058: protected void endTask() {
059: setStatus(TaskStatus.COMPLETE);
060: Iterator schmiterator = getTaskMonitors().iterator();
061: ITaskMonitor taskMonitor;
062: while (schmiterator.hasNext()) {
063: taskMonitor = (ITaskMonitor) schmiterator.next();
064: taskMonitor.endTask(this );
065: }
066: }
067:
068: protected List getPostExecuteTasks() {
069: return postExecuteTasks;
070: }
071:
072: protected List getPreExecuteTasks() {
073: return preExecuteTasks;
074: }
075:
076: public int getStatus() {
077: return this .status;
078: }
079:
080: public int getSteps() {
081: return 0;
082: }
083:
084: private List getTaskMonitors() {
085: return this .taskMonitors;
086: }
087:
088: public int getTotalStepCount() {
089: // add up all steps
090: int result = 0;
091: ITask task;
092: Iterator schmiterator = getPreExecuteTasks().iterator();
093: while (schmiterator.hasNext()) {
094: task = (ITask) schmiterator.next();
095: result += task.getSteps();
096: }
097:
098: result += getSteps();
099:
100: schmiterator = getPostExecuteTasks().iterator();
101: while (schmiterator.hasNext()) {
102: task = (ITask) schmiterator.next();
103: result += task.getSteps();
104: }
105:
106: return result;
107: }
108:
109: private void notifyMonitors(int ticks) {
110: Iterator schmiterator = getTaskMonitors().iterator();
111: ITaskMonitor taskMonitor;
112: while (schmiterator.hasNext()) {
113: taskMonitor = (ITaskMonitor) schmiterator.next();
114: taskMonitor.progressMade(this , ticks);
115: }
116: }
117:
118: private void notifyMonitors(int ticks, String message) {
119: Iterator schmiterator = getTaskMonitors().iterator();
120: ITaskMonitor taskMonitor;
121: while (schmiterator.hasNext()) {
122: taskMonitor = (ITaskMonitor) schmiterator.next();
123: taskMonitor.progressMade(this , ticks, message);
124: }
125: }
126:
127: private void notifyMonitors(String message) {
128: Iterator schmiterator = getTaskMonitors().iterator();
129: ITaskMonitor taskMonitor;
130: while (schmiterator.hasNext()) {
131: taskMonitor = (ITaskMonitor) schmiterator.next();
132: taskMonitor.progressMade(this , message);
133: }
134: }
135:
136: public void progressMade() {
137: progressMade(1);
138: }
139:
140: public void progressMade(int ticks) {
141: notifyMonitors(ticks);
142: }
143:
144: public void progressMade(int ticks, String message) {
145: notifyMonitors(ticks, message);
146: }
147:
148: public void progressMade(String message) {
149: notifyMonitors(message);
150: }
151:
152: public void removeTaskMonitor(ITaskMonitor aTaskMonitor) {
153: if (aTaskMonitor != null
154: && getTaskMonitors().contains(aTaskMonitor)) {
155: getTaskMonitors().remove(aTaskMonitor);
156: }
157: }
158:
159: public void setStatus(int status) {
160: this.status = status;
161: }
162: }
|