JAL-4199 Update gui handler for annotation tasks
[jalview.git] / src / jalview / ws2 / gui / AnnotationServiceGuiHandler.java
1 package jalview.ws2.gui;
2
3 import java.util.List;
4
5 import jalview.api.FeatureColourI;
6 import jalview.datamodel.features.FeatureMatcherSetI;
7 import jalview.gui.AlignFrame;
8 import jalview.gui.AlignmentPanel;
9 import jalview.gui.IProgressIndicator;
10 import jalview.gui.IProgressIndicatorHandler;
11 import jalview.schemes.FeatureSettingsAdapter;
12 import jalview.ws2.actions.annotation.AnnotationAction;
13 import jalview.ws2.actions.annotation.AnnotationResult;
14 import jalview.ws2.actions.api.JobI;
15 import jalview.ws2.actions.api.TaskEventListener;
16 import jalview.ws2.actions.api.TaskI;
17 import jalview.ws2.api.JobStatus;
18
19 public class AnnotationServiceGuiHandler
20     implements TaskEventListener<AnnotationResult>
21 {
22   private final AlignFrame alignFrame;
23
24   private final AlignmentPanel alignPanel;
25
26   private final IProgressIndicator progressIndicator;
27
28   private final AnnotationAction action;
29
30   public AnnotationServiceGuiHandler(AnnotationAction action, AlignFrame frame)
31   {
32     this.alignFrame = frame;
33     this.alignPanel = frame.alignPanel;
34     this.progressIndicator = frame;
35     this.action = action;
36   }
37
38   @Override
39   public void taskStarted(TaskI<AnnotationResult> source, List<? extends JobI> subJobs)
40   {
41     progressIndicator.registerHandler(source.getUid(),
42         new IProgressIndicatorHandler()
43         {
44           @Override
45           public boolean cancelActivity(long id)
46           {
47             source.cancel();
48             return true;
49           }
50
51           @Override
52           public boolean canCancel()
53           {
54             return true;
55           }
56         });
57   }
58
59   @Override
60   public void taskStatusChanged(TaskI<AnnotationResult> source, JobStatus status)
61   {
62     switch (status)
63     {
64     case INVALID:
65     case COMPLETED:
66     case CANCELLED:
67     case FAILED:
68     case SERVER_ERROR:
69       progressIndicator.removeProgressBar(source.getUid());
70       break;
71     case READY:
72     case SUBMITTED:
73     case QUEUED:
74     case RUNNING:
75     case UNKNOWN:
76       progressIndicator.addProgressBar(source.getUid(), action.getFullName());
77       break;
78     }
79   }
80
81   @Override
82   public void taskCompleted(TaskI<AnnotationResult> source, final AnnotationResult result)
83   {
84     if (result == null)
85       return;
86     if (result.getTransferFeatures())
87     {
88       alignFrame.getViewport().applyFeaturesStyle(new FeatureSettingsAdapter()
89       {
90         @Override
91         public FeatureColourI getFeatureColour(String type)
92         {
93           return result.getFeatureColours().get(type);
94         }
95
96         @Override
97         public FeatureMatcherSetI getFeatureFilters(String type)
98         {
99           return result.getFeatureFilters().get(type);
100         }
101
102         @Override
103         public boolean isFeatureDisplayed(String type)
104         {
105           return result.getFeatureColours().containsKey(type);
106         }
107       });
108       if (alignFrame.alignPanel == alignPanel)
109       {
110         alignFrame.getViewport().setShowSequenceFeatures(true);
111         alignFrame.setMenusForViewport();
112       }
113     }
114     alignPanel.adjustAnnotationHeight();
115   }
116
117   @Override
118   public void taskException(TaskI<AnnotationResult> source, Exception e)
119   {
120
121   }
122
123   @Override
124   public void subJobStatusChanged(TaskI<AnnotationResult> source, JobI job, JobStatus status)
125   {
126
127   }
128
129   @Override
130   public void subJobLogChanged(TaskI<AnnotationResult> source, JobI job, String log)
131   {
132
133   }
134
135   @Override
136   public void subJobErrorLogChanged(TaskI<AnnotationResult> source, JobI job, String log)
137   {
138
139   }
140 }