c94032fb8380ff4d8da6a136abfc954f3f3fa4ab
[jalview.git] / src / jalview / workers / AlignCalcWorker.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.api.AlignCalcManagerI;
24 import jalview.api.AlignCalcManagerI2;
25 import jalview.api.AlignCalcWorkerI;
26 import jalview.api.AlignViewportI;
27 import jalview.api.AlignmentViewPanel;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.Annotation;
31
32 import java.util.List;
33
34 /**
35  * Base class for alignment calculation workers
36  * 
37  * @author jimp
38  * 
39  */
40 public abstract class AlignCalcWorker implements AlignCalcWorkerI
41 {
42   /**
43    * manager and data source for calculations
44    */
45   protected AlignViewportI alignViewport;
46
47   protected AlignCalcManagerI2 calcMan;
48
49   protected AlignmentViewPanel ap;
50
51   protected List<AlignmentAnnotation> ourAnnots;
52
53   public AlignCalcWorker(AlignViewportI alignViewport,
54           AlignmentViewPanel alignPanel)
55   {
56     this.alignViewport = alignViewport;
57     calcMan = alignViewport.getCalcManager();
58     ap = alignPanel;
59   }
60
61   protected void abortAndDestroy()
62   {
63     if (calcMan != null)
64     {
65       calcMan.cancelWorker(this);
66       calcMan.removeWorker(this);
67     }
68     alignViewport = null;
69     calcMan = null;
70     ap = null;
71
72   }
73
74   @Override
75   public boolean involves(AlignmentAnnotation i)
76   {
77     return ourAnnots != null && ourAnnots.contains(i);
78   }
79
80   /**
81    * Permanently removes from the alignment all annotation rows managed by this
82    * worker
83    */
84   @Override
85   public void removeAnnotation()
86   {
87     if (ourAnnots != null && alignViewport != null)
88     {
89       AlignmentI alignment = alignViewport.getAlignment();
90       synchronized (ourAnnots)
91       {
92         for (AlignmentAnnotation aa : ourAnnots)
93         {
94           alignment.deleteAnnotation(aa, true);
95         }
96       }
97       ourAnnots.clear();
98     }
99   }
100
101   // TODO: allow GUI to query workers associated with annotation to add items to
102   // annotation label panel popup menu
103
104   @Override
105   public boolean isDeletable()
106   {
107     return false;
108   }
109
110   /**
111    * Calculate min and max values of annotations and set as graphMin, graphMax
112    * on the AlignmentAnnotation. This is needed because otherwise, well, bad
113    * things happen.
114    * 
115    * @param ann
116    * @param anns
117    */
118   protected void setGraphMinMax(AlignmentAnnotation ann, Annotation[] anns)
119   {
120     // TODO feels like this belongs inside AlignmentAnnotation!
121     float max = Float.MIN_VALUE;
122     float min = Float.MAX_VALUE;
123     boolean set = false;
124     for (Annotation a : anns)
125     {
126       if (a != null)
127       {
128         set = true;
129         float val = a.value;
130         max = Math.max(max, val);
131         min = Math.min(min, val);
132       }
133     }
134     if (set)
135     {
136       ann.graphMin = min;
137       ann.graphMax = max;
138     }
139   }
140
141   public AlignViewportI getAlignViewport()
142   {
143     return alignViewport;
144   }
145 }