JAL-2068 framework and example scripts for pluggable alignment
[jalview.git] / src / jalview / workers / AnnotationWorker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.workers;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.gui.AlignFrame;
26 import jalview.gui.AlignmentPanel;
27 import jalview.gui.FeatureRenderer;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 /**
33  * A class to create and update one or more alignment annotations, given a
34  * 'calculator'.
35  * 
36  */
37 class AnnotationWorker extends AlignCalcWorker
38 {
39   /*
40    * the provider of the annotation calculations
41    */
42   AnnotationProviderI counter;
43
44   /**
45    * Constructor
46    * 
47    * @param af
48    * @param counter
49    */
50   public AnnotationWorker(AlignFrame af, AnnotationProviderI counter)
51   {
52     super(af.getViewport(), af.alignPanel);
53     ourAnnots = new ArrayList<AlignmentAnnotation>();
54     this.counter = counter;
55     calcMan.registerWorker(this);
56   }
57
58   @Override
59   public void run()
60   {
61     try
62     {
63       calcMan.notifyStart(this);
64
65       while (!calcMan.notifyWorking(this))
66       {
67         try
68         {
69           Thread.sleep(200);
70         } catch (InterruptedException ex)
71         {
72           ex.printStackTrace();
73         }
74       }
75       if (alignViewport.isClosed())
76       {
77         abortAndDestroy();
78         return;
79       }
80
81       removeAnnotations();
82       AlignmentI alignment = alignViewport.getAlignment();
83       if (alignment != null)
84       {
85         try
86         {
87           List<AlignmentAnnotation> anns = counter.calculateAnnotation(
88                   alignment, new FeatureRenderer((AlignmentPanel) ap));
89           for (AlignmentAnnotation ann : anns)
90           {
91             ann.showAllColLabels = true;
92             ann.graph = AlignmentAnnotation.BAR_GRAPH;
93             ourAnnots.add(ann);
94             alignment.addAnnotation(ann);
95           }
96         } catch (IndexOutOfBoundsException x)
97         {
98           // probable race condition. just finish and return without any fuss.
99           return;
100         }
101       }
102     } catch (OutOfMemoryError error)
103     {
104       ap.raiseOOMWarning("calculating annotations", error);
105       calcMan.workerCannotRun(this);
106     } finally
107     {
108       calcMan.workerComplete(this);
109     }
110
111     if (ap != null)
112     {
113       ap.adjustAnnotationHeight();
114       ap.paintAlignment(true);
115     }
116
117   }
118
119   /**
120    * Remove all our annotations before re-calculating them
121    */
122   void removeAnnotations()
123   {
124     for (AlignmentAnnotation ann : ourAnnots)
125     {
126       alignViewport.getAlignment().deleteAnnotation(ann);
127     }
128     ourAnnots.clear();
129   }
130
131   @Override
132   public void updateAnnotation()
133   {
134     // do nothing
135   }
136 }