update author list in license for (JAL-826)
[jalview.git] / src / jalview / appletgui / 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.appletgui;
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.removeElement(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 (!comp.isValid())
119       {
120         comps.removeElement(comp);
121       }
122       else if (validateSequences && comp instanceof AlignmentPanel
123               && source instanceof AlignmentPanel)
124       {
125         validateSequences(((AlignmentPanel) source).av.alignment,
126                 ((AlignmentPanel) comp).av.alignment);
127       }
128
129       if (comp instanceof AlignmentPanel && alignmentChanged)
130       {
131         ((AlignmentPanel) comp).alignmentChanged();
132       }
133
134       comp.repaint();
135     }
136   }
137
138   static void validateSequences(AlignmentI source, AlignmentI comp)
139   {
140     SequenceI[] a1;
141     if (source.getHiddenSequences().getSize() > 0)
142     {
143       a1 = source.getHiddenSequences().getFullAlignment()
144               .getSequencesArray();
145     }
146     else
147     {
148       a1 = source.getSequencesArray();
149     }
150
151     SequenceI[] a2;
152     if (comp.getHiddenSequences().getSize() > 0)
153     {
154       a2 = comp.getHiddenSequences().getFullAlignment().getSequencesArray();
155     }
156     else
157     {
158       a2 = comp.getSequencesArray();
159     }
160
161     int i, iSize = a1.length, j, jSize = a2.length;
162
163     if (iSize == jSize)
164     {
165       return;
166     }
167
168     boolean exists = false;
169     for (i = 0; i < iSize; i++)
170     {
171       exists = false;
172
173       for (j = 0; j < jSize; j++)
174       {
175         if (a2[j] == a1[i])
176         {
177           exists = true;
178           break;
179         }
180       }
181
182       if (!exists)
183       {
184         if (i < comp.getHeight())
185         {
186           comp.getSequences().insertElementAt(a1[i], i);
187         }
188         else
189         {
190           comp.addSequence(a1[i]);
191         }
192
193         if (comp.getHiddenSequences().getSize() > 0)
194         {
195           a2 = comp.getHiddenSequences().getFullAlignment()
196                   .getSequencesArray();
197         }
198         else
199         {
200           a2 = comp.getSequencesArray();
201         }
202
203         jSize = a2.length;
204       }
205     }
206
207     iSize = a1.length;
208     jSize = a2.length;
209
210     for (j = 0; j < jSize; j++)
211     {
212       exists = false;
213       for (i = 0; i < iSize; i++)
214       {
215         if (a2[j] == a1[i])
216         {
217           exists = true;
218           break;
219         }
220       }
221
222       if (!exists)
223       {
224         comp.deleteSequence(a2[j]);
225       }
226     }
227   }
228
229   public static AlignmentPanel[] getAssociatedPanels(String id)
230   {
231     Vector comps = (Vector) components.get(id);
232     Vector tmp = new Vector();
233     int i, iSize = comps.size();
234     for (i = 0; i < iSize; i++)
235     {
236       if (comps.elementAt(i) instanceof AlignmentPanel)
237       {
238         tmp.addElement(((AlignmentPanel) comps.elementAt(i)));
239       }
240     }
241     AlignmentPanel[] result = new AlignmentPanel[tmp.size()];
242     for (int ix=0;ix<result.length;ix++) {
243       result[ix] = (AlignmentPanel) tmp.elementAt(ix);
244     }
245
246     return result;
247   }
248
249 }