1 package jalview.ws2.actions.annotation;
3 import java.util.ArrayList;
4 import java.util.Collections;
6 import java.util.concurrent.CopyOnWriteArrayList;
8 import jalview.api.AlignViewportI;
9 import jalview.api.AlignmentViewPanel;
10 import jalview.api.PollableAlignCalcWorkerI;
11 import jalview.datamodel.Alignment;
12 import jalview.datamodel.AlignmentAnnotation;
13 import jalview.datamodel.AlignmentI;
14 import jalview.workers.AlignCalcWorker;
15 import jalview.ws.params.ArgumentI;
16 import jalview.ws2.actions.alignment.AlignmentResult;
17 import jalview.ws2.actions.api.JobI;
18 import jalview.ws2.actions.api.TaskEventListener;
19 import jalview.ws2.actions.api.TaskI;
20 import jalview.ws2.api.Credentials;
21 import jalview.ws2.api.JobStatus;
23 public class AlignCalcWorkerAdapter extends AlignCalcWorker implements PollableAlignCalcWorkerI
25 private final AnnotationAction action;
27 private final List<ArgumentI> args;
29 private final Credentials credentials;
31 private TaskI<AnnotationResult> currentTask = null;
33 private TaskEventListener<AnnotationResult> internalTaskListener = new TaskEventListener<>()
36 public void taskCompleted(TaskI source, AnnotationResult result)
38 updateOurAnnots(result.getAnnotations());
42 // task event listeners managed by this worker, passed to each creates task
43 private List<TaskEventListener<AnnotationResult>> taskListeners = new CopyOnWriteArrayList<>();
45 taskListeners.add(internalTaskListener);
48 public AlignCalcWorkerAdapter(AlignViewportI alignViewport, AlignmentViewPanel alignPanel, AnnotationAction action,
49 List<ArgumentI> args, Credentials credentials)
51 super(alignViewport, alignPanel);
54 this.credentials = credentials;
58 public void updateAnnotation()
63 private void updateOurAnnots(List<AlignmentAnnotation> newAnnots)
65 List<AlignmentAnnotation> oldAnnots = ourAnnots != null ? ourAnnots : List.of();
66 AlignmentI alignment = alignViewport.getAlignment();
67 for (AlignmentAnnotation annotation : oldAnnots)
68 if (!newAnnots.contains(annotation))
69 alignment.deleteAnnotation(annotation);
70 for (AlignmentAnnotation annotation : newAnnots)
71 alignment.validateAnnotation(annotation);
72 ap.adjustAnnotationHeight();
73 ourAnnots = Collections.synchronizedList(newAnnots);
77 public synchronized void startUp() throws Throwable
79 if (alignViewport.isClosed())
81 calcMan.disableWorker(this);
83 throw new IllegalStateException("Starting calculation for closed viewport");
85 currentTask = action.createTask(alignViewport, args, credentials);
86 for (var listener : taskListeners)
87 currentTask.addTaskEventListener(listener);
92 public boolean poll() throws Throwable
94 return currentTask.poll();
98 public synchronized void cancel()
102 currentTask.cancel();
105 for (var listener : taskListeners)
106 currentTask.removeTaskEventListener(listener);
111 public synchronized void done()
115 currentTask.complete();
116 } catch (Exception e)
120 for (var listener : taskListeners)
121 currentTask.removeTaskEventListener(listener);
125 public void addTaskEventListener(TaskEventListener<AnnotationResult> listener)
127 taskListeners.add(listener);
128 if (currentTask != null)
129 currentTask.addTaskEventListener(listener);
132 public void removeTaskEventListener(TaskEventListener<AnnotationResult> listener)
134 taskListeners.remove(listener);
135 if (currentTask != null)
136 currentTask.removeTaskEventListener(listener);