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.Iterator;
30 import java.util.List;
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 private static final int ALIGNMENT_CHANGED = 1 << 0;
43 private static final int VALIDATE_SEQUENCES = 1 << 1;
45 static Map<String, List<Component>> components = new HashMap<>();
48 * Add the given component to those registered under the given sequence set
49 * id. Does nothing if already added.
54 public static void Register(Component comp, String seqSetId)
56 if (components.containsKey(seqSetId))
58 List<Component> comps = components.get(seqSetId);
59 if (!comps.contains(comp))
66 List<Component> vcoms = new ArrayList<>();
68 components.put(seqSetId, vcoms);
73 * Remove this component from all registrations. Also removes a registered
74 * sequence set id if there are no remaining components registered against it.
78 public static void RemoveComponent(Component comp)
80 if (components == null)
85 Iterator<String> it = components.keySet().iterator();
88 List<Component> comps = components.get(it.next());
97 public static void Refresh(Component source, String id)
99 Refresh(source, id, false, false);
102 public static void Refresh(Component source, String id,
103 boolean alignmentChanged, boolean validateSequences)
105 List<Component> comps = components.get(id);
107 int mode = (alignmentChanged ? ALIGNMENT_CHANGED : 0) | (validateSequences ? VALIDATE_SEQUENCES : 0);
112 repaintComponents(source, mode, comps.toArray(new Component[comps.size()]));
115 public static void repaintComponents(Component source, int mode,
118 for (int i = 0; i < comps.length; i++)
120 Component comp = comps[i];
125 if (comp instanceof AlignmentPanel)
127 if ((mode & VALIDATE_SEQUENCES) != 0 && source instanceof AlignmentPanel)
129 validateSequences(((AlignmentPanel) source).av.getAlignment(),
130 ((AlignmentPanel) comp).av.getAlignment());
132 if ((mode & ALIGNMENT_CHANGED) != 0)
134 ((AlignmentPanel) comp).alignmentChanged();
137 else if (comp instanceof IdCanvas)
139 // BH 2019.04.22 fixes JS problem of repaint() consolidation
140 // that occurs in JavaScript but not Java [JAL-3226]
141 ((IdCanvas) comp).setNoFastPaint();
143 else if (comp instanceof SeqCanvas)
145 // BH 2019.04.22 fixes JS problem of repaint() consolidation
146 // that occurs in JavaScript but not Java [JAL-3226]
147 ((SeqCanvas) comp).setNoFastPaint();
153 static void validateSequences(AlignmentI source, AlignmentI comp)
156 if (source.getHiddenSequences().getSize() > 0)
158 a1 = source.getHiddenSequences().getFullAlignment()
159 .getSequencesArray();
163 a1 = source.getSequencesArray();
167 if (comp.getHiddenSequences().getSize() > 0)
169 a2 = comp.getHiddenSequences().getFullAlignment().getSequencesArray();
173 a2 = comp.getSequencesArray();
176 int i, iSize = a1.length, j, jSize = a2.length;
183 boolean exists = false;
184 for (i = 0; i < iSize; i++)
188 for (j = 0; j < jSize; j++)
199 if (i < comp.getHeight())
201 // TODO: the following does not trigger any recalculation of
202 // height/etc, or maintain the dataset
203 if (comp.getDataset() != source.getDataset())
205 // raise an implementation warning here - not sure if this situation
208 "IMPLEMENTATION PROBLEM: DATASET out of sync due to an insert whilst calling PaintRefresher.validateSequences(AlignmentI, ALignmentI)");
210 List<SequenceI> alsq = comp.getSequences();
218 comp.addSequence(a1[i]);
221 if (comp.getHiddenSequences().getSize() > 0)
223 a2 = comp.getHiddenSequences().getFullAlignment()
224 .getSequencesArray();
228 a2 = comp.getSequencesArray();
238 for (j = 0; j < jSize; j++)
241 for (i = 0; i < iSize; i++)
252 comp.deleteSequence(a2[j]);
257 static AlignmentPanel[] getAssociatedPanels(String id)
259 List<Component> comps = components.get(id);
262 return new AlignmentPanel[0];
264 List<AlignmentPanel> tmp = new ArrayList<>();
265 for (Component comp : comps)
267 if (comp instanceof AlignmentPanel)
269 tmp.add((AlignmentPanel) comp);
272 return tmp.toArray(new AlignmentPanel[tmp.size()]);