From fc4694b5aae5364b5677cc034e23601246fa5b2f Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Tue, 4 Jul 2023 10:15:11 +0200 Subject: [PATCH] JAL-4199 Allow multiple listeners in TaskEventSupport --- src/jalview/ws2/helpers/TaskEventSupport.java | 65 ++++++++++++++++--------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/src/jalview/ws2/helpers/TaskEventSupport.java b/src/jalview/ws2/helpers/TaskEventSupport.java index c7b6052..dcf965a 100644 --- a/src/jalview/ws2/helpers/TaskEventSupport.java +++ b/src/jalview/ws2/helpers/TaskEventSupport.java @@ -1,6 +1,7 @@ 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; @@ -9,52 +10,70 @@ import jalview.ws2.api.JobStatus; public class TaskEventSupport { - TaskI source; - TaskEventListener handler; - - public TaskEventSupport(TaskI source, TaskEventListener handler) + private TaskI source; + + private List> listeners = new CopyOnWriteArrayList<>(); + + public TaskEventSupport(TaskI source) { this.source = source; - this.handler = handler; + } + + public TaskEventSupport(TaskI source, TaskEventListener handler) + { + this(source); + addListener(handler); + } + + public void addListener(TaskEventListener listener) + { + listeners.add(listener); } + public void removeListener(TaskEventListener listener) + { + listeners.remove(listener); + } + public void fireTaskStarted(List 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); } } -- 1.7.10.2