package jalview.ws2.helpers;
import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
import jalview.ws2.actions.api.JobI;
import jalview.ws2.actions.api.TaskEventListener;
public class TaskEventSupport<T>
{
- TaskI<T> source;
- TaskEventListener<T> handler;
-
- public TaskEventSupport(TaskI<T> source, TaskEventListener<T> handler)
+ private TaskI<T> source;
+
+ private List<TaskEventListener<T>> listeners = new CopyOnWriteArrayList<>();
+
+ public TaskEventSupport(TaskI<T> source)
{
this.source = source;
- this.handler = handler;
+ }
+
+ public TaskEventSupport(TaskI<T> source, TaskEventListener<T> handler)
+ {
+ this(source);
+ addListener(handler);
+ }
+
+ public void addListener(TaskEventListener<T> listener)
+ {
+ listeners.add(listener);
}
+ public void removeListener(TaskEventListener<T> listener)
+ {
+ listeners.remove(listener);
+ }
+
public void fireTaskStarted(List<? extends JobI> subJobs)
{
- handler.taskStarted(source, subJobs);
+ for (var listener : listeners)
+ listener.taskStarted(source, subJobs);
}
-
+
public void fireTaskStatusChanged(JobStatus status)
{
- handler.taskStatusChanged(source, status);
+ for (var listener : listeners)
+ listener.taskStatusChanged(source, status);
}
-
+
public void fireTaskCompleted(T result)
{
- handler.taskCompleted(source, result);
+ for (var listener : listeners)
+ listener.taskCompleted(source, result);
}
-
+
public void fireTaskException(Exception e)
{
- handler.taskException(source, e);
- }
-
- public void fireTaskRestarted()
- {
- handler.taskRestarted(source);
+ for (var listener : listeners)
+ listener.taskException(source, e);
}
-
+
public void fireSubJobStatusChanged(JobI job, JobStatus status)
{
- handler.subJobStatusChanged(source, job, status);
+ for (var listener : listeners)
+ listener.subJobStatusChanged(source, job, status);
}
-
+
public void fireSubJobLogChanged(JobI job, String log)
{
- handler.subJobLogChanged(source, job, log);
+ for (var listener : listeners)
+ listener.subJobLogChanged(source, job, log);
}
-
+
public void fireSubJobErrorLogChanged(JobI job, String log)
{
- handler.subJobErrorLogChanged(source, job, log);
+ for (var listener : listeners)
+ listener.subJobErrorLogChanged(source, job, log);
}
}