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.SequenceI;
24 import jalview.renderer.AnnotationRenderer;
25 import jalview.renderer.seqfeatures.FeatureColourFinder;
26 import jalview.viewmodel.OverviewDimensions;
27 import jalview.viewmodel.ViewportListenerI;
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.Graphics;
32 import java.awt.event.ComponentAdapter;
33 import java.awt.event.ComponentEvent;
34 import java.awt.event.MouseAdapter;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseMotionAdapter;
37 import java.awt.image.BufferedImage;
38 import java.beans.PropertyChangeEvent;
40 import javax.swing.JPanel;
43 * Panel displaying an overview of the full alignment, with an interactive box
44 * representing the viewport onto the alignment.
49 public class OverviewPanel extends JPanel implements Runnable,
52 private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
54 private final AnnotationRenderer renderer = new AnnotationRenderer();
56 private OverviewDimensions od;
58 private BufferedImage miniMe;
60 private BufferedImage lastMiniMe = null;
62 private AlignViewport av;
64 private AlignmentPanel ap;
67 private boolean resizing = false;
69 // This is set true if the user resizes whilst
70 // the overview is being calculated
71 private boolean resizeAgain = false;
73 // Can set different properties in this seqCanvas than
74 // main visible SeqCanvas
75 private SequenceRenderer sr;
77 jalview.renderer.seqfeatures.FeatureRenderer fr;
80 * Creates a new OverviewPanel object.
83 * The alignment panel which is shown in the overview panel
85 public OverviewPanel(AlignmentPanel alPanel)
91 sr = new SequenceRenderer(av);
92 sr.renderGaps = false;
93 sr.forOverview = true;
94 fr = new FeatureRenderer(ap);
96 od = new OverviewDimensions(av.getRanges(),
97 (av.isShowAnnotation() && av
98 .getAlignmentConservationAnnotation() != null));
100 av.getRanges().addPropertyChangeListener(this);
102 addComponentListener(new ComponentAdapter()
105 public void componentResized(ComponentEvent evt)
107 if ((getWidth() != od.getWidth())
108 || (getHeight() != (od.getHeight())))
110 updateOverviewImage();
116 addMouseMotionListener(new MouseMotionAdapter()
119 public void mouseDragged(MouseEvent evt)
121 if (!av.getWrapAlignment())
123 od.updateViewportFromMouse(evt.getX(), evt.getY(), av
124 .getAlignment().getHiddenSequences(), av
125 .getColumnSelection(), av.getRanges());
126 // TODO set via ViewportRanges in overview dimensions once JAL-2388 is
132 addMouseListener(new MouseAdapter()
135 public void mousePressed(MouseEvent evt)
137 if (!av.getWrapAlignment())
139 od.updateViewportFromMouse(evt.getX(), evt.getY(), av
140 .getAlignment().getHiddenSequences(), av
141 .getColumnSelection(), av.getRanges());
142 // TODO set via ViewportRanges in overview dimensions once JAL-2388 is
148 updateOverviewImage();
153 * Updates the overview image when the related alignment panel is updated
155 public void updateOverviewImage()
165 if ((getWidth() > 0) && (getHeight() > 0))
167 od.setWidth(getWidth());
168 od.setHeight(getHeight());
171 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
173 Thread thread = new Thread(this);
183 if (av.isShowSequenceFeatures())
185 fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
188 // why do we need to set preferred size again? was set in
189 // updateOverviewImage
190 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
192 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
193 BufferedImage.TYPE_INT_RGB);
195 Graphics mg = miniMe.getGraphics();
196 mg.setColor(Color.orange);
197 mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
199 // calculate sampleCol and sampleRow
200 // alignment width is max number of residues/bases
201 // alignment height is number of sequences
202 int alwidth = av.getAlignment().getWidth();
203 int alheight = av.getAlignment().getAbsoluteHeight();
205 // sampleCol or sampleRow is the width/height allocated to each residue
206 // in particular, sometimes we may need more than one row/col of the
207 // BufferedImage allocated
208 // sampleCol is how much of a residue to assign to each pixel
209 // sampleRow is how many sequences to assign to each pixel
210 float sampleCol = alwidth / (float) od.getWidth();
211 float sampleRow = alheight / (float) od.getSequencesHeight();
213 buildImage(sampleRow, sampleCol);
215 // check for conservation annotation to make sure overview works for DNA too
216 if (av.isShowAnnotation()
217 && (av.getAlignmentConservationAnnotation() != null))
219 renderer.updateFromAlignViewport(av);
220 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
222 mg.translate(col, od.getSequencesHeight());
223 renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
224 av.getAlignmentConservationAnnotation().annotations,
225 (int) (sampleCol) + 1, od.getGraphHeight(),
226 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
227 mg.translate(-col, -od.getSequencesHeight());
238 updateOverviewImage();
249 * Build the overview panel image
251 private void buildImage(float sampleRow, float sampleCol)
255 int rgbColour = Color.white.getRGB();
257 SequenceI seq = null;
258 FeatureColourFinder finder = new FeatureColourFinder(fr);
260 final boolean hasHiddenCols = av.hasHiddenColumns();
261 boolean hiddenRow = false;
262 // get hidden row and hidden column map once at beginning.
263 // clone featureRenderer settings to avoid race conditions... if state is
264 // updated just need to refresh again
265 for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
267 boolean doCopy = true;
268 int currentrow = (int) (row * sampleRow);
269 if (currentrow != lastrow)
273 lastrow = currentrow;
275 // get the sequence which would be at alignment index 'lastrow' if no
276 // rows were hidden, and determine whether it is hidden or not
277 hiddenRow = av.getAlignment().isHidden(lastrow);
278 seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
281 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
285 rgbColour = miniMe.getRGB(col, row - 1);
287 else if ((int) (col * sampleCol) != lastcol
288 || (int) (row * sampleRow) != lastrow)
290 lastcol = (int) (col * sampleCol);
291 rgbColour = getColumnColourFromSequence(seq, hiddenRow,
292 hasHiddenCols, lastcol, finder);
294 // else we just use the color we already have , so don't need to set it
296 miniMe.setRGB(col, row, rgbColour);
302 * Find the colour of a sequence at a specified column position
304 private int getColumnColourFromSequence(
305 jalview.datamodel.SequenceI seq,
306 boolean hiddenRow, boolean hasHiddenCols, int lastcol,
307 FeatureColourFinder finder)
309 Color color = Color.white;
311 if ((seq != null) && (seq.getLength() > lastcol))
313 color = sr.getResidueColour(seq, lastcol, finder);
317 || (hasHiddenCols && !av.getColumnSelection()
318 .isVisible(lastcol)))
320 color = color.darker().darker();
323 return color.getRGB();
327 * Update the overview panel box when the associated alignment panel is
331 private void setBoxPosition()
333 od.setBoxPosition(av.getAlignment()
334 .getHiddenSequences(), av.getColumnSelection(), av.getRanges());
340 public void paintComponent(Graphics g)
342 if (resizing || resizeAgain)
344 if (lastMiniMe == null)
346 g.setColor(Color.white);
347 g.fillRect(0, 0, getWidth(), getHeight());
351 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
353 g.setColor(TRANS_GREY);
354 g.fillRect(0, 0, getWidth(), getHeight());
356 else if (lastMiniMe != null)
358 g.drawImage(lastMiniMe, 0, 0, this);
359 if (lastMiniMe != miniMe)
361 g.setColor(TRANS_GREY);
362 g.fillRect(0, 0, getWidth(), getHeight());
366 g.setColor(Color.red);
371 public void propertyChange(PropertyChangeEvent evt)