7cd5affb4a82a0ada93883194e82a1b22ccd9d7d
[jalview.git] / src / jalview / appletgui / PaintRefresher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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 import java.util.List;
22
23 import java.awt.*;
24
25 import jalview.datamodel.*;
26
27 /**
28  * DOCUMENT ME!
29  * 
30  * @author $author$
31  * @version $Revision$
32  */
33 public class PaintRefresher
34 {
35   static Hashtable components;
36
37   /**
38    * DOCUMENT ME!
39    * 
40    * @param comp
41    *          DOCUMENT ME!
42    * @param al
43    *          DOCUMENT ME!
44    */
45   public static void Register(Component comp, String seqSetId)
46   {
47     if (components == null)
48     {
49       components = new Hashtable();
50     }
51
52     if (components.containsKey(seqSetId))
53     {
54       Vector comps = (Vector) components.get(seqSetId);
55       if (!comps.contains(comp))
56       {
57         comps.addElement(comp);
58       }
59     }
60     else
61     {
62       Vector vcoms = new Vector();
63       vcoms.addElement(comp);
64       components.put(seqSetId, vcoms);
65     }
66   }
67
68   public static void RemoveComponent(Component comp)
69   {
70     if (components == null)
71     {
72       return;
73     }
74
75     Enumeration en = components.keys();
76     while (en.hasMoreElements())
77     {
78       String id = en.nextElement().toString();
79       Vector comps = (Vector) components.get(id);
80       comps.removeElement(comp);
81       if (comps.size() == 0)
82       {
83         components.remove(id);
84       }
85     }
86   }
87
88   public static void Refresh(Component source, String id)
89   {
90     Refresh(source, id, false, false);
91   }
92
93   public static void Refresh(Component source, String id,
94           boolean alignmentChanged, boolean validateSequences)
95   {
96     if (components == null)
97     {
98       return;
99     }
100
101     Component comp;
102     Vector comps = (Vector) components.get(id);
103
104     if (comps == null)
105     {
106       return;
107     }
108
109     Enumeration e = comps.elements();
110     while (e.hasMoreElements())
111     {
112       comp = (Component) e.nextElement();
113
114       if (comp == source)
115       {
116         continue;
117       }
118
119       if (!comp.isValid())
120       {
121         comps.removeElement(comp);
122       }
123       else if (validateSequences && comp instanceof AlignmentPanel
124               && source instanceof AlignmentPanel)
125       {
126         validateSequences(((AlignmentPanel) source).av.getAlignment(),
127                 ((AlignmentPanel) comp).av.getAlignment());
128       }
129
130       if (comp instanceof AlignmentPanel && alignmentChanged)
131       {
132         ((AlignmentPanel) comp).alignmentChanged();
133       }
134
135       comp.repaint();
136     }
137   }
138
139   static void validateSequences(AlignmentI source, AlignmentI comp)
140   {
141     SequenceI[] a1;
142     if (source.getHiddenSequences().getSize() > 0)
143     {
144       a1 = source.getHiddenSequences().getFullAlignment()
145               .getSequencesArray();
146     }
147     else
148     {
149       a1 = source.getSequencesArray();
150     }
151
152     SequenceI[] a2;
153     if (comp.getHiddenSequences().getSize() > 0)
154     {
155       a2 = comp.getHiddenSequences().getFullAlignment().getSequencesArray();
156     }
157     else
158     {
159       a2 = comp.getSequencesArray();
160     }
161
162     int i, iSize = a1.length, j, jSize = a2.length;
163
164     if (iSize == jSize)
165     {
166       return;
167     }
168
169     boolean exists = false;
170     for (i = 0; i < iSize; i++)
171     {
172       exists = false;
173
174       for (j = 0; j < jSize; j++)
175       {
176         if (a2[j] == a1[i])
177         {
178           exists = true;
179           break;
180         }
181       }
182
183       if (!exists)
184       {
185         if (i < comp.getHeight())
186         {
187           // TODO: the following does not trigger any recalculation of
188           // height/etc, or maintain the dataset
189           List<SequenceI> alsq;
190           synchronized (alsq = comp.getSequences())
191           {
192             alsq.add(i, a1[i]);
193           }
194         }
195         else
196         {
197           comp.addSequence(a1[i]);
198         }
199
200         if (comp.getHiddenSequences().getSize() > 0)
201         {
202           a2 = comp.getHiddenSequences().getFullAlignment()
203                   .getSequencesArray();
204         }
205         else
206         {
207           a2 = comp.getSequencesArray();
208         }
209
210         jSize = a2.length;
211       }
212     }
213
214     iSize = a1.length;
215     jSize = a2.length;
216
217     for (j = 0; j < jSize; j++)
218     {
219       exists = false;
220       for (i = 0; i < iSize; i++)
221       {
222         if (a2[j] == a1[i])
223         {
224           exists = true;
225           break;
226         }
227       }
228
229       if (!exists)
230       {
231         comp.deleteSequence(a2[j]);
232       }
233     }
234   }
235
236   public static AlignmentPanel[] getAssociatedPanels(String id)
237   {
238     Vector comps = (Vector) components.get(id);
239     Vector tmp = new Vector();
240     int i, iSize = comps.size();
241     for (i = 0; i < iSize; i++)
242     {
243       if (comps.elementAt(i) instanceof AlignmentPanel)
244       {
245         tmp.addElement(comps.elementAt(i));
246       }
247     }
248     AlignmentPanel[] result = new AlignmentPanel[tmp.size()];
249     for (int ix = 0; ix < result.length; ix++)
250     {
251       result[ix] = (AlignmentPanel) tmp.elementAt(ix);
252     }
253
254     return result;
255   }
256
257 }