2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SequenceI;
26 import java.awt.Component;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.Map.Entry;
34 * Route datamodel/view update events for a sequence set to any display
35 * components involved TODO: JV3 refactor to abstract gui/view package
40 public class PaintRefresher
42 static Map<String, List<Component>> components = new HashMap<String, List<Component>>();
45 * Add the given component to those registered under the given sequence set
46 * id. Does nothing if already added.
51 public static void Register(Component comp, String seqSetId)
53 if (components.containsKey(seqSetId))
55 List<Component> comps = components.get(seqSetId);
56 if (!comps.contains(comp))
63 List<Component> vcoms = new ArrayList<Component>();
65 components.put(seqSetId, vcoms);
70 * Remove this component from all registrations. Also removes a registered
71 * sequence set id if there are no remaining components registered against it.
75 public static void RemoveComponent(Component comp)
77 List<String> emptied = new ArrayList<String>();
78 for (Entry<String, List<Component>> registered : components.entrySet())
80 String id = registered.getKey();
81 List<Component> comps = components.get(id);
90 * Remove now empty ids after the above (to avoid
91 * ConcurrentModificationException).
93 for (String id : emptied)
95 components.remove(id);
99 public static void Refresh(Component source, String id)
101 Refresh(source, id, false, false);
104 public static void Refresh(Component source, String id,
105 boolean alignmentChanged, boolean validateSequences)
107 List<Component> comps = components.get(id);
114 for (Component comp : comps)
121 if (validateSequences && comp instanceof AlignmentPanel
122 && source instanceof AlignmentPanel)
124 validateSequences(((AlignmentPanel) source).av.getAlignment(),
125 ((AlignmentPanel) comp).av.getAlignment());
128 if (comp instanceof AlignmentPanel && alignmentChanged)
130 ((AlignmentPanel) comp).alignmentChanged();
137 static void validateSequences(AlignmentI source, AlignmentI comp)
140 if (source.getHiddenSequences().getSize() > 0)
142 a1 = source.getHiddenSequences().getFullAlignment()
143 .getSequencesArray();
147 a1 = source.getSequencesArray();
151 if (comp.getHiddenSequences().getSize() > 0)
153 a2 = comp.getHiddenSequences().getFullAlignment().getSequencesArray();
157 a2 = comp.getSequencesArray();
160 int i, iSize = a1.length, j, jSize = a2.length;
167 boolean exists = false;
168 for (i = 0; i < iSize; i++)
172 for (j = 0; j < jSize; j++)
183 if (i < comp.getHeight())
185 // TODO: the following does not trigger any recalculation of
186 // height/etc, or maintain the dataset
187 if (comp.getDataset() != source.getDataset())
189 // raise an implementation warning here - not sure if this situation
192 .println("IMPLEMENTATION PROBLEM: DATASET out of sync due to an insert whilst calling PaintRefresher.validateSequences(AlignmentI, ALignmentI)");
194 List<SequenceI> alsq;
195 synchronized (alsq = comp.getSequences())
202 comp.addSequence(a1[i]);
205 if (comp.getHiddenSequences().getSize() > 0)
207 a2 = comp.getHiddenSequences().getFullAlignment()
208 .getSequencesArray();
212 a2 = comp.getSequencesArray();
222 for (j = 0; j < jSize; j++)
225 for (i = 0; i < iSize; i++)
236 comp.deleteSequence(a2[j]);
241 static AlignmentPanel[] getAssociatedPanels(String id)
243 List<Component> comps = components.get(id);
246 return new AlignmentPanel[0];
248 List<AlignmentPanel> tmp = new ArrayList<AlignmentPanel>();
249 for (Component comp : comps)
251 if (comp instanceof AlignmentPanel)
253 tmp.add((AlignmentPanel) comp);
256 return tmp.toArray(new AlignmentPanel[tmp.size()]);