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