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