update author list in license for (JAL-826)
[jalview.git] / src / jalview / gui / PaintRefresher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.util.*;
21
22 import java.awt.*;
23
24 import jalview.datamodel.*;
25
26 /**
27  * DOCUMENT ME!
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class PaintRefresher
33 {
34   static Hashtable components;
35
36   /**
37    * DOCUMENT ME!
38    * 
39    * @param comp
40    *          DOCUMENT ME!
41    * @param al
42    *          DOCUMENT ME!
43    */
44   public static void Register(Component comp, String seqSetId)
45   {
46     if (components == null)
47     {
48       components = new Hashtable();
49     }
50
51     if (components.containsKey(seqSetId))
52     {
53       Vector comps = (Vector) components.get(seqSetId);
54       if (!comps.contains(comp))
55       {
56         comps.addElement(comp);
57       }
58     }
59     else
60     {
61       Vector vcoms = new Vector();
62       vcoms.addElement(comp);
63       components.put(seqSetId, vcoms);
64     }
65   }
66
67   public static void RemoveComponent(Component comp)
68   {
69     if (components == null)
70     {
71       return;
72     }
73
74     Enumeration en = components.keys();
75     while (en.hasMoreElements())
76     {
77       String id = en.nextElement().toString();
78       Vector comps = (Vector) components.get(id);
79       comps.remove(comp);
80       if (comps.size() == 0)
81       {
82         components.remove(id);
83       }
84     }
85   }
86
87   public static void Refresh(Component source, String id)
88   {
89     Refresh(source, id, false, false);
90   }
91
92   public static void Refresh(Component source, String id,
93           boolean alignmentChanged, boolean validateSequences)
94   {
95     if (components == null)
96     {
97       return;
98     }
99
100     Component comp;
101     Vector comps = (Vector) components.get(id);
102
103     if (comps == null)
104     {
105       return;
106     }
107
108     Enumeration e = comps.elements();
109     while (e.hasMoreElements())
110     {
111       comp = (Component) e.nextElement();
112
113       if (comp == source)
114       {
115         continue;
116       }
117
118       if (validateSequences && comp instanceof AlignmentPanel
119               && source instanceof AlignmentPanel)
120       {
121         validateSequences(((AlignmentPanel) source).av.alignment,
122                 ((AlignmentPanel) comp).av.alignment);
123       }
124
125       if (comp instanceof AlignmentPanel && alignmentChanged)
126       {
127         ((AlignmentPanel) comp).alignmentChanged();
128       }
129
130       comp.repaint();
131     }
132   }
133
134   static void validateSequences(AlignmentI source, AlignmentI comp)
135   {
136     SequenceI[] a1;
137     if (source.getHiddenSequences().getSize() > 0)
138     {
139       a1 = source.getHiddenSequences().getFullAlignment()
140               .getSequencesArray();
141     }
142     else
143     {
144       a1 = source.getSequencesArray();
145     }
146
147     SequenceI[] a2;
148     if (comp.getHiddenSequences().getSize() > 0)
149     {
150       a2 = comp.getHiddenSequences().getFullAlignment().getSequencesArray();
151     }
152     else
153     {
154       a2 = comp.getSequencesArray();
155     }
156
157     int i, iSize = a1.length, j, jSize = a2.length;
158
159     if (iSize == jSize)
160     {
161       return;
162     }
163
164     boolean exists = false;
165     for (i = 0; i < iSize; i++)
166     {
167       exists = false;
168
169       for (j = 0; j < jSize; j++)
170       {
171         if (a2[j] == a1[i])
172         {
173           exists = true;
174           break;
175         }
176       }
177
178       if (!exists)
179       {
180         if (i < comp.getHeight())
181         {
182           comp.getSequences().insertElementAt(a1[i], i);
183         }
184         else
185         {
186           comp.addSequence(a1[i]);
187         }
188
189         if (comp.getHiddenSequences().getSize() > 0)
190         {
191           a2 = comp.getHiddenSequences().getFullAlignment()
192                   .getSequencesArray();
193         }
194         else
195         {
196           a2 = comp.getSequencesArray();
197         }
198
199         jSize = a2.length;
200       }
201     }
202
203     iSize = a1.length;
204     jSize = a2.length;
205
206     for (j = 0; j < jSize; j++)
207     {
208       exists = false;
209       for (i = 0; i < iSize; i++)
210       {
211         if (a2[j] == a1[i])
212         {
213           exists = true;
214           break;
215         }
216       }
217
218       if (!exists)
219       {
220         comp.deleteSequence(a2[j]);
221       }
222     }
223   }
224
225   static AlignmentPanel[] getAssociatedPanels(String id)
226   {
227     if (components==null) { return new AlignmentPanel[0]; };
228     Vector comps = (Vector) components.get(id);
229     if (comps==null) { return new AlignmentPanel[0]; };
230     Vector tmp = new Vector();
231     int i, iSize = comps.size();
232     for (i = 0; i < iSize; i++)
233     {
234       if (comps.elementAt(i) instanceof AlignmentPanel)
235       {
236         tmp.addElement(((AlignmentPanel) comps.elementAt(i)));
237       }
238     }
239     AlignmentPanel[] result = new AlignmentPanel[tmp.size()];
240     tmp.toArray(result);
241
242     return result;
243   }
244
245 }