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