JAL-4199 Organise AlignCalcWorkerAdapter imports
[jalview.git] / src / jalview / ws2 / actions / annotation / AlignCalcWorkerAdapter.java
1 package jalview.ws2.actions.annotation;
2
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.concurrent.CopyOnWriteArrayList;
6
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;
17
18 public class AlignCalcWorkerAdapter extends AlignCalcWorker implements PollableAlignCalcWorkerI
19 {
20   private final AnnotationAction action;
21
22   private final List<ArgumentI> args;
23
24   private final Credentials credentials;
25
26   private TaskI<AnnotationResult> currentTask = null;
27
28   private TaskEventListener<AnnotationResult> internalTaskListener = new TaskEventListener<>()
29   {
30     @Override
31     public void taskCompleted(TaskI source, AnnotationResult result)
32     {
33       updateOurAnnots(result.getAnnotations());
34     }
35   };
36
37   // task event listeners managed by this worker, passed to each creates task
38   private List<TaskEventListener<AnnotationResult>> taskListeners = new CopyOnWriteArrayList<>();
39   {
40     taskListeners.add(internalTaskListener);
41   }
42
43   public AlignCalcWorkerAdapter(AlignViewportI alignViewport, AlignmentViewPanel alignPanel, AnnotationAction action,
44       List<ArgumentI> args, Credentials credentials)
45   {
46     super(alignViewport, alignPanel);
47     this.action = action;
48     this.args = args;
49     this.credentials = credentials;
50   }
51
52   @Override
53   public void updateAnnotation()
54   {
55
56   }
57
58   private void updateOurAnnots(List<AlignmentAnnotation> newAnnots)
59   {
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);
69   }
70
71   @Override
72   public synchronized void startUp() throws Throwable
73   {
74     if (alignViewport.isClosed())
75     {
76       calcMan.disableWorker(this);
77       abortAndDestroy();
78       throw new IllegalStateException("Starting calculation for closed viewport");
79     }
80     currentTask = action.createTask(alignViewport, args, credentials);
81     for (var listener : taskListeners)
82       currentTask.addTaskEventListener(listener);
83     currentTask.init();
84   }
85
86   @Override
87   public boolean poll() throws Throwable
88   {
89     return currentTask.poll();
90   }
91
92   @Override
93   public synchronized void cancel()
94   {
95     try
96     {
97       currentTask.cancel();
98     } finally
99     {
100       for (var listener : taskListeners)
101         currentTask.removeTaskEventListener(listener);
102     }
103   }
104
105   @Override
106   public synchronized void done()
107   {
108     try
109     {
110       currentTask.complete();
111     } catch (Exception e)
112     {
113     } finally
114     {
115       for (var listener : taskListeners)
116         currentTask.removeTaskEventListener(listener);
117     }
118   }
119
120   public void addTaskEventListener(TaskEventListener<AnnotationResult> listener)
121   {
122     taskListeners.add(listener);
123     if (currentTask != null)
124       currentTask.addTaskEventListener(listener);
125   }
126
127   public void removeTaskEventListener(TaskEventListener<AnnotationResult> listener)
128   {
129     taskListeners.remove(listener);
130     if (currentTask != null)
131       currentTask.removeTaskEventListener(listener);
132   }
133 }