JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.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 java.util.List;
24
25 import swingjs.JSThread;
26
27 import jalview.api.AlignCalcManagerI;
28 import jalview.api.AlignCalcWorkerI;
29 import jalview.api.AlignViewportI;
30 import jalview.api.AlignmentViewPanel;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.AlignmentI;
33
34 /**
35  * Base class for alignment calculation workers
36  * 
37  * @author jimp
38  * 
39  */
40 public abstract class AlignCalcWorker extends JSThread implements AlignCalcWorkerI
41 {
42   /**
43    * manager and data source for calculations
44    */
45         protected final static int LOOP_STANDBY = DONE + 1;
46         protected final static int LOOP_CALCULATE = DONE + 2;
47
48         protected int iFirst;
49         protected int iLast;
50         protected int nPer = 2;
51
52         protected final static int MS_MAX = 500;
53         
54   protected long started;
55         protected AlignmentI alignment;
56         protected int aWidth;
57
58         protected AlignViewportI alignViewport;
59
60   protected AlignCalcManagerI calcMan;
61
62   protected AlignmentViewPanel ap;
63
64   protected List<AlignmentAnnotation> ourAnnots = null;
65
66   public AlignCalcWorker(AlignViewportI alignViewport,
67           AlignmentViewPanel alignPanel)
68   {
69         super(null, "AlignCalcWorker");
70     this.alignViewport = alignViewport;
71     calcMan = alignViewport.getCalcManager();
72     ap = alignPanel;
73   }
74
75   protected void abortAndDestroy()
76   {
77     if (calcMan != null)
78     {
79       calcMan.workerComplete(this);
80     }
81     alignViewport = null;
82     calcMan = null;
83     ap = null;
84
85   }
86
87   public boolean involves(AlignmentAnnotation i)
88   {
89     return ourAnnots != null && ourAnnots.contains(i);
90   }
91
92   /**
93    * permanently remove from the alignment all annotation rows managed by this
94    * worker
95    */
96   @Override
97   public void removeOurAnnotation()
98   {
99     if (ourAnnots != null && alignViewport != null)
100     {
101       AlignmentI alignment = alignViewport.getAlignment();
102       synchronized (ourAnnots)
103       {
104         for (AlignmentAnnotation aa : ourAnnots)
105         {
106           alignment.deleteAnnotation(aa, true);
107         }
108       }
109     }
110   }
111   // TODO: allow GUI to query workers associated with annotation to add items to
112   // annotation label panel popup menu
113
114         protected void notifyDone() {
115                 if (ap != null) {
116                         ap.paintAlignment(true);
117                 }
118                 /*
119                  * e.g. ArrayIndexOutOfBoundsException can happen due to a race
120                  * condition - alignment was edited at same time as calculation was
121                  * running
122                  */
123                 calcMan.workerComplete(this);
124         }
125
126 }