1 package jalview.ws2.actions.annotation;
3 import java.util.Collections;
5 import java.util.concurrent.CopyOnWriteArrayList;
7 import jalview.api.AlignViewportI;
8 import jalview.api.AlignmentViewPanel;
9 import jalview.api.PollableAlignCalcWorkerI;
10 import jalview.datamodel.AlignmentAnnotation;
11 import jalview.datamodel.AlignmentI;
12 import jalview.workers.AlignCalcWorker;
13 import jalview.ws.params.ArgumentI;
14 import jalview.ws2.actions.api.TaskEventListener;
15 import jalview.ws2.actions.api.TaskI;
16 import jalview.ws2.api.Credentials;
18 public class AlignCalcWorkerAdapter extends AlignCalcWorker implements PollableAlignCalcWorkerI
20 private final AnnotationAction action;
22 private final List<ArgumentI> args;
24 private final Credentials credentials;
26 private TaskI<AnnotationResult> currentTask = null;
28 private TaskEventListener<AnnotationResult> internalTaskListener = new TaskEventListener<>()
31 public void taskCompleted(TaskI source, AnnotationResult result)
33 updateOurAnnots(result.getAnnotations());
37 // task event listeners managed by this worker, passed to each creates task
38 private List<TaskEventListener<AnnotationResult>> taskListeners = new CopyOnWriteArrayList<>();
40 taskListeners.add(internalTaskListener);
43 public AlignCalcWorkerAdapter(AlignViewportI alignViewport, AlignmentViewPanel alignPanel, AnnotationAction action,
44 List<ArgumentI> args, Credentials credentials)
46 super(alignViewport, alignPanel);
49 this.credentials = credentials;
53 public void updateAnnotation()
58 private void updateOurAnnots(List<AlignmentAnnotation> newAnnots)
60 List<AlignmentAnnotation> oldAnnots = ourAnnots != null ? ourAnnots : List.of();
61 AlignmentI alignment = alignViewport.getAlignment();
62 for (AlignmentAnnotation annotation : oldAnnots)
63 if (!newAnnots.contains(annotation))
64 alignment.deleteAnnotation(annotation);
65 for (AlignmentAnnotation annotation : newAnnots)
66 alignment.validateAnnotation(annotation);
67 ap.adjustAnnotationHeight();
68 ourAnnots = Collections.synchronizedList(newAnnots);
72 public synchronized void startUp() throws Throwable
74 if (alignViewport.isClosed())
76 calcMan.disableWorker(this);
78 throw new IllegalStateException("Starting calculation for closed viewport");
80 currentTask = action.createTask(alignViewport, args, credentials);
81 for (var listener : taskListeners)
82 currentTask.addTaskEventListener(listener);
87 public boolean poll() throws Throwable
89 return currentTask.poll();
93 public synchronized void cancel()
100 for (var listener : taskListeners)
101 currentTask.removeTaskEventListener(listener);
106 public synchronized void done()
110 currentTask.complete();
111 } catch (Exception e)
115 for (var listener : taskListeners)
116 currentTask.removeTaskEventListener(listener);
120 public void addTaskEventListener(TaskEventListener<AnnotationResult> listener)
122 taskListeners.add(listener);
123 if (currentTask != null)
124 currentTask.addTaskEventListener(listener);
127 public void removeTaskEventListener(TaskEventListener<AnnotationResult> listener)
129 taskListeners.remove(listener);
130 if (currentTask != null)
131 currentTask.removeTaskEventListener(listener);