a7202e96a941789fda786062f4a57de3a4022819
[jalview.git] / src / jalview / ws2 / gui / AnnotationServiceGuiHandler.java
1 package jalview.ws2.gui;
2
3 import jalview.api.FeatureColourI;
4 import jalview.datamodel.features.FeatureMatcherSetI;
5 import jalview.gui.AlignFrame;
6 import jalview.gui.AlignmentPanel;
7 import jalview.gui.IProgressIndicator;
8 import jalview.gui.IProgressIndicatorHandler;
9 import jalview.schemes.FeatureSettingsAdapter;
10 import jalview.util.MathUtils;
11 import jalview.ws2.actions.annotation.AlignCalcWorkerAdapter;
12 import jalview.ws2.actions.annotation.AnnotationAction;
13 import jalview.ws2.actions.annotation.AnnotationResult;
14
15 public class AnnotationServiceGuiHandler
16     implements AlignCalcWorkerAdapter.WorkerListener
17 {
18   private final long progressId = MathUtils.getUID();
19
20   private final AlignFrame alignFrame;
21
22   private final AlignmentPanel alignPanel;
23
24   private final IProgressIndicator progressIndicator;
25
26   private final AnnotationAction action;
27
28   public AnnotationServiceGuiHandler(AnnotationAction action, AlignFrame frame)
29   {
30     this.alignFrame = frame;
31     this.alignPanel = frame.alignPanel;
32     this.progressIndicator = frame;
33     this.action = action;
34   }
35
36   @Override
37   public void workerStarted(AlignCalcWorkerAdapter source)
38   {
39     progressIndicator.addProgressBar(progressId, action.getFullName());
40     progressIndicator.registerHandler(progressId,
41         new IProgressIndicatorHandler()
42         {
43           @Override
44           public boolean cancelActivity(long id)
45           {
46             source.cancel();
47             return true;
48           }
49
50           @Override
51           public boolean canCancel()
52           {
53             return true;
54           }
55         });
56   }
57
58   @Override
59   public void workerStopped(AlignCalcWorkerAdapter source)
60   {
61     progressIndicator.removeProgressBar(progressId);
62   }
63
64   @Override
65   public void workerHasResult(AlignCalcWorkerAdapter source, final AnnotationResult result)
66   {
67     if (result == null)
68       return;
69     if (result.getTransferFeatures())
70     {
71       alignFrame.getViewport().applyFeaturesStyle(new FeatureSettingsAdapter()
72       {
73         @Override
74         public FeatureColourI getFeatureColour(String type)
75         {
76           return result.getFeatureColours().get(type);
77         }
78
79         @Override
80         public FeatureMatcherSetI getFeatureFilters(String type)
81         {
82           return result.getFeatureFilters().get(type);
83         }
84
85         @Override
86         public boolean isFeatureDisplayed(String type)
87         {
88           return result.getFeatureColours().containsKey(type);
89         }
90       });
91       if (alignFrame.alignPanel == alignPanel)
92       {
93         alignFrame.getViewport().setShowSequenceFeatures(true);
94         alignFrame.setMenusForViewport();
95       }
96     }
97     alignPanel.adjustAnnotationHeight();
98   }
99 }