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 String getCalcName()
55 return action.getWebService().getName();
59 public void updateAnnotation()
64 private void updateOurAnnots(List<AlignmentAnnotation> newAnnots)
66 List<AlignmentAnnotation> oldAnnots = ourAnnots != null ? ourAnnots : List.of();
67 AlignmentI alignment = alignViewport.getAlignment();
68 for (AlignmentAnnotation annotation : oldAnnots)
69 if (!newAnnots.contains(annotation))
70 alignment.deleteAnnotation(annotation);
71 for (AlignmentAnnotation annotation : newAnnots)
72 alignment.validateAnnotation(annotation);
73 ap.adjustAnnotationHeight();
74 ourAnnots = Collections.synchronizedList(newAnnots);
78 public synchronized void startUp() throws Throwable
80 if (alignViewport.isClosed())
82 calcMan.disableWorker(this);
84 throw new IllegalStateException("Starting calculation for closed viewport");
86 currentTask = action.createTask(alignViewport, args, credentials);
87 for (var listener : taskListeners)
88 currentTask.addTaskEventListener(listener);
93 public boolean poll() throws Throwable
95 return currentTask.poll();
99 public synchronized void cancel()
103 currentTask.cancel();
106 for (var listener : taskListeners)
107 currentTask.removeTaskEventListener(listener);
112 public synchronized void done()
116 currentTask.complete();
117 } catch (Exception e)
121 for (var listener : taskListeners)
122 currentTask.removeTaskEventListener(listener);
126 public void addTaskEventListener(TaskEventListener<AnnotationResult> listener)
128 taskListeners.add(listener);
129 if (currentTask != null)
130 currentTask.addTaskEventListener(listener);
133 public void removeTaskEventListener(TaskEventListener<AnnotationResult> listener)
135 taskListeners.remove(listener);
136 if (currentTask != null)
137 currentTask.removeTaskEventListener(listener);
141 public String toString()
143 return "AlignCalcWorkerAdapter for " + getCalcName();