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();
115 addMouseMotionListener(new MouseMotionAdapter()
118 public void mouseDragged(MouseEvent evt)
120 if (!av.getWrapAlignment())
122 od.updateViewportFromMouse(evt.getX(), evt.getY(), av
123 .getAlignment().getHiddenSequences(), av
124 .getColumnSelection(), av.getRanges());
125 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
130 addMouseListener(new MouseAdapter()
133 public void mousePressed(MouseEvent evt)
135 if (!av.getWrapAlignment())
137 od.updateViewportFromMouse(evt.getX(), evt.getY(), av
138 .getAlignment().getHiddenSequences(), av
139 .getColumnSelection(), av.getRanges());
140 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
145 updateOverviewImage();
149 * Updates the overview image when the related alignment panel is updated
151 public void updateOverviewImage()
161 if ((getWidth() > 0) && (getHeight() > 0))
163 od.setWidth(getWidth());
164 od.setHeight(getHeight());
167 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
169 Thread thread = new Thread(this);
179 if (av.isShowSequenceFeatures())
181 fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
184 // why do we need to set preferred size again? was set in
185 // updateOverviewImage
186 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
188 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
189 BufferedImage.TYPE_INT_RGB);
191 Graphics mg = miniMe.getGraphics();
192 mg.setColor(Color.orange);
193 mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
195 // calculate sampleCol and sampleRow
196 // alignment width is max number of residues/bases
197 // alignment height is number of sequences
198 int alwidth = av.getAlignment().getWidth();
199 int alheight = av.getAlignment().getAbsoluteHeight();
201 // sampleCol or sampleRow is the width/height allocated to each residue
202 // in particular, sometimes we may need more than one row/col of the
203 // BufferedImage allocated
204 // sampleCol is how much of a residue to assign to each pixel
205 // sampleRow is how many sequences to assign to each pixel
206 float sampleCol = alwidth / (float) od.getWidth();
207 float sampleRow = alheight / (float) od.getSequencesHeight();
209 buildImage(sampleRow, sampleCol);
211 // check for conservation annotation to make sure overview works for DNA too
212 if (av.isShowAnnotation()
213 && (av.getAlignmentConservationAnnotation() != null))
215 renderer.updateFromAlignViewport(av);
216 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
218 mg.translate(col, od.getSequencesHeight());
219 renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
220 av.getAlignmentConservationAnnotation().annotations,
221 (int) (sampleCol) + 1, od.getGraphHeight(),
222 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
223 mg.translate(-col, -od.getSequencesHeight());
234 updateOverviewImage();
245 * Build the overview panel image
247 private void buildImage(float sampleRow, float sampleCol)
251 int rgbColour = Color.white.getRGB();
253 SequenceI seq = null;
254 FeatureColourFinder finder = new FeatureColourFinder(fr);
256 final boolean hasHiddenCols = av.hasHiddenColumns();
257 boolean hiddenRow = false;
258 // get hidden row and hidden column map once at beginning.
259 // clone featureRenderer settings to avoid race conditions... if state is
260 // updated just need to refresh again
261 for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
263 boolean doCopy = true;
264 int currentrow = (int) (row * sampleRow);
265 if (currentrow != lastrow)
269 lastrow = currentrow;
271 // get the sequence which would be at alignment index 'lastrow' if no
272 // rows were hidden, and determine whether it is hidden or not
273 hiddenRow = av.getAlignment().isHidden(lastrow);
274 seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
277 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
281 rgbColour = miniMe.getRGB(col, row - 1);
283 else if ((int) (col * sampleCol) != lastcol
284 || (int) (row * sampleRow) != lastrow)
286 lastcol = (int) (col * sampleCol);
287 rgbColour = getColumnColourFromSequence(seq, hiddenRow,
288 hasHiddenCols, lastcol, finder);
290 // else we just use the color we already have , so don't need to set it
292 miniMe.setRGB(col, row, rgbColour);
298 * Find the colour of a sequence at a specified column position
300 private int getColumnColourFromSequence(
301 jalview.datamodel.SequenceI seq,
302 boolean hiddenRow, boolean hasHiddenCols, int lastcol,
303 FeatureColourFinder finder)
305 Color color = Color.white;
307 if ((seq != null) && (seq.getLength() > lastcol))
309 color = sr.getResidueColour(seq, lastcol, finder);
313 || (hasHiddenCols && !av.getColumnSelection()
314 .isVisible(lastcol)))
316 color = color.darker().darker();
319 return color.getRGB();
323 * Update the overview panel box when the associated alignment panel is
327 public void setBoxPosition()
329 od.setBoxPosition(av.getAlignment()
330 .getHiddenSequences(), av.getColumnSelection(), av.getRanges());
336 public void paintComponent(Graphics g)
338 if (resizing || resizeAgain)
340 if (lastMiniMe == null)
342 g.setColor(Color.white);
343 g.fillRect(0, 0, getWidth(), getHeight());
347 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
349 g.setColor(TRANS_GREY);
350 g.fillRect(0, 0, getWidth(), getHeight());
352 else if (lastMiniMe != null)
354 g.drawImage(lastMiniMe, 0, 0, this);
355 if (lastMiniMe != miniMe)
357 g.setColor(TRANS_GREY);
358 g.fillRect(0, 0, getWidth(), getHeight());
362 g.setColor(Color.red);
367 public void propertyChange(PropertyChangeEvent evt)