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.viewmodel.OverviewDimensions;
27 import java.awt.Color;
28 import java.awt.Dimension;
29 import java.awt.Graphics;
30 import java.awt.event.ComponentAdapter;
31 import java.awt.event.ComponentEvent;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
34 import java.awt.event.MouseMotionAdapter;
35 import java.awt.image.BufferedImage;
37 import javax.swing.JPanel;
40 * Panel displaying an overview of the full alignment, with an interactive box
41 * representing the viewport onto the alignment.
46 public class OverviewPanel extends JPanel implements Runnable
48 private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
50 private final AnnotationRenderer renderer = new AnnotationRenderer();
52 private OverviewDimensions od;
54 private BufferedImage miniMe;
56 private BufferedImage lastMiniMe = null;
58 private AlignViewport av;
60 private AlignmentPanel ap;
63 private boolean resizing = false;
65 // This is set true if the user resizes whilst
66 // the overview is being calculated
67 private boolean resizeAgain = false;
69 // Can set different properties in this seqCanvas than
70 // main visible SeqCanvas
71 private SequenceRenderer sr;
73 private jalview.renderer.seqfeatures.FeatureRenderer fr;
76 * Creates a new OverviewPanel object.
79 * The alignment panel which is shown in the overview panel
81 public OverviewPanel(AlignmentPanel alPanel)
87 sr = new SequenceRenderer(av);
88 sr.renderGaps = false;
89 sr.forOverview = true;
90 fr = new FeatureRenderer(alPanel);
92 od = new OverviewDimensions(av.getRanges(), av.isShowAnnotation());
94 addComponentListener(new ComponentAdapter()
97 public void componentResized(ComponentEvent evt)
99 if ((getWidth() != od.getWidth())
100 || (getHeight() != (od.getHeight())))
102 updateOverviewImage();
107 addMouseMotionListener(new MouseMotionAdapter()
110 public void mouseDragged(MouseEvent evt)
112 if (!av.getWrapAlignment())
114 od.updateViewportFromMouse(evt.getX(), evt.getY(), av
115 .getAlignment().getHiddenSequences(), av
116 .getColumnSelection(), av.getRanges());
117 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
122 addMouseListener(new MouseAdapter()
125 public void mousePressed(MouseEvent evt)
127 if (!av.getWrapAlignment())
129 od.updateViewportFromMouse(evt.getX(), evt.getY(), av
130 .getAlignment().getHiddenSequences(), av
131 .getColumnSelection(), av.getRanges());
132 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
137 updateOverviewImage();
141 * Updates the overview image when the related alignment panel is updated
143 public void updateOverviewImage()
153 if ((getWidth() > 0) && (getHeight() > 0))
155 od.setWidth(getWidth());
156 od.setHeight(getHeight());
159 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
161 Thread thread = new Thread(this);
171 if (av.isShowSequenceFeatures())
173 fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
176 // why do we need to set preferred size again? was set in
177 // updateOverviewImage
178 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
180 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
181 BufferedImage.TYPE_INT_RGB);
183 Graphics mg = miniMe.getGraphics();
184 mg.setColor(Color.orange);
185 mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
187 // calculate sampleCol and sampleRow
188 // alignment width is max number of residues/bases
189 // alignment height is number of sequences
190 int alwidth = av.getAlignment().getWidth();
191 int alheight = av.getAlignment().getAbsoluteHeight();
193 // sampleCol or sampleRow is the width/height allocated to each residue
194 // in particular, sometimes we may need more than one row/col of the
195 // BufferedImage allocated
196 // sampleCol is how much of a residue to assign to each pixel
197 // sampleRow is how many sequences to assign to each pixel
198 float sampleCol = alwidth / (float) od.getWidth();
199 float sampleRow = alheight / (float) od.getSequencesHeight();
201 buildImage(sampleRow, sampleCol);
203 if (av.isShowAnnotation())
205 renderer.updateFromAlignViewport(av);
206 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
208 mg.translate(col, od.getSequencesHeight());
209 renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
210 av.getAlignmentConservationAnnotation().annotations,
211 (int) (sampleCol) + 1, od.getGraphHeight(),
212 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
213 mg.translate(-col, -od.getSequencesHeight());
224 updateOverviewImage();
235 * Build the overview panel image
237 private void buildImage(float sampleRow, float sampleCol)
241 int color = Color.white.getRGB();
243 SequenceI seq = null;
245 final boolean hasHiddenCols = av.hasHiddenColumns();
246 boolean hiddenRow = false;
247 // get hidden row and hidden column map once at beginning.
248 // clone featureRenderer settings to avoid race conditions... if state is
249 // updated just need to refresh again
250 for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
252 boolean doCopy = true;
253 int currentrow = (int) (row * sampleRow);
254 if (currentrow != lastrow)
258 lastrow = currentrow;
260 // get the sequence which would be at alignment index 'lastrow' if no
261 // rows were hidden, and determine whether it is hidden or not
262 hiddenRow = av.getAlignment().isHidden(lastrow);
263 seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
266 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
270 color = miniMe.getRGB(col, row - 1);
272 else if ((int) (col * sampleCol) != lastcol
273 || (int) (row * sampleRow) != lastrow)
275 lastcol = (int) (col * sampleCol);
276 color = getColumnColourFromSequence(seq, hiddenRow, hasHiddenCols,
279 // else we just use the color we already have , so don't need to set it
281 miniMe.setRGB(col, row, color);
287 * Find the colour of a sequence at a specified column position
289 private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
290 boolean hiddenRow, boolean hasHiddenCols, int lastcol)
296 color = Color.white.getRGB();
298 else if (seq.getLength() > lastcol)
300 color = sr.getResidueBoxColour(seq, lastcol).getRGB();
302 if (av.isShowSequenceFeatures())
304 color = fr.findFeatureColour(color, seq, lastcol);
309 color = Color.white.getRGB();
313 || (hasHiddenCols && !av.getColumnSelection()
314 .isVisible(lastcol)))
316 color = new Color(color).darker().darker().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);