applied copyright 2008
[jalview.git] / src / jalview / gui / PaintRefresher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import java.util.*;
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 DOCUMENT ME!
41    * @param al DOCUMENT ME!
42    */
43   public static void Register(Component comp, String seqSetId)
44   {
45     if (components == null)
46     {
47       components = new Hashtable();
48     }
49
50     if (components.containsKey(seqSetId))
51     {
52       Vector comps = (Vector) components.get(seqSetId);
53       if (!comps.contains(comp))
54       {
55         comps.addElement(comp);
56       }
57     }
58     else
59     {
60       Vector vcoms = new Vector();
61       vcoms.addElement(comp);
62       components.put(seqSetId, vcoms);
63     }
64   }
65
66   public static void RemoveComponent(Component comp)
67   {
68     if (components == null)
69     {
70       return;
71     }
72
73     Enumeration en = components.keys();
74     while (en.hasMoreElements())
75     {
76       String id = en.nextElement().toString();
77       Vector comps = (Vector) components.get(id);
78       comps.remove(comp);
79       if (comps.size() == 0)
80       {
81         components.remove(id);
82       }
83     }
84   }
85
86   public static void Refresh(Component source, String id)
87   {
88     Refresh(source, id, false, false);
89   }
90
91   public static void Refresh(Component source,
92                              String id,
93                              boolean alignmentChanged,
94                              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 (validateSequences
120           && comp instanceof AlignmentPanel
121           && source instanceof AlignmentPanel)
122       {
123         validateSequences( ( (AlignmentPanel) source).av.alignment,
124                           ( (AlignmentPanel) comp).av.alignment);
125       }
126
127       if (comp instanceof AlignmentPanel && alignmentChanged)
128       {
129         ( (AlignmentPanel) comp).alignmentChanged();
130       }
131
132       comp.repaint();
133     }
134   }
135
136   static void validateSequences(AlignmentI source, AlignmentI comp)
137   {
138     SequenceI[] a1;
139     if (source.getHiddenSequences().getSize() > 0)
140     {
141       a1 = source.getHiddenSequences().getFullAlignment().getSequencesArray();
142     }
143     else
144     {
145       a1 = source.getSequencesArray();
146     }
147
148     SequenceI[] a2;
149     if (comp.getHiddenSequences().getSize() > 0)
150     {
151       a2 = comp.getHiddenSequences().getFullAlignment().getSequencesArray();
152     }
153     else
154     {
155       a2 = comp.getSequencesArray();
156     }
157
158     int i, iSize = a1.length, j, jSize = a2.length;
159
160     if (iSize == jSize)
161     {
162       return;
163     }
164
165     boolean exists = false;
166     for (i = 0; i < iSize; i++)
167     {
168       exists = false;
169
170       for (j = 0; j < jSize; j++)
171       {
172         if (a2[j] == a1[i])
173         {
174           exists = true;
175           break;
176         }
177       }
178
179       if (!exists)
180       {
181         if (i < comp.getHeight())
182         {
183           comp.getSequences().insertElementAt(a1[i], i);
184         }
185         else
186         {
187           comp.addSequence(a1[i]);
188         }
189
190         if (comp.getHiddenSequences().getSize() > 0)
191         {
192           a2 = comp.getHiddenSequences().getFullAlignment().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     Vector comps = (Vector) components.get(id);
228     Vector tmp = new Vector();
229     int i, iSize = comps.size();
230     for (i = 0; i < iSize; i++)
231     {
232       if (comps.elementAt(i) instanceof AlignmentPanel)
233       {
234         tmp.addElement( ( (AlignmentPanel) comps.elementAt(i)));
235       }
236     }
237     AlignmentPanel[] result = new AlignmentPanel[tmp.size()];
238     tmp.toArray(result);
239
240     return result;
241   }
242
243 }