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.renderer.AnnotationRenderer;
24 import jalview.viewmodel.OverviewDimensions;
26 import java.awt.Color;
27 import java.awt.Dimension;
28 import java.awt.Graphics;
29 import java.awt.event.ComponentAdapter;
30 import java.awt.event.ComponentEvent;
31 import java.awt.event.MouseAdapter;
32 import java.awt.event.MouseEvent;
33 import java.awt.event.MouseMotionAdapter;
34 import java.awt.image.BufferedImage;
36 import javax.swing.JPanel;
44 public class OverviewPanel extends JPanel implements Runnable
46 private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
48 private final AnnotationRenderer renderer = new AnnotationRenderer();
50 private OverviewDimensions od;
52 private BufferedImage miniMe;
54 private BufferedImage lastMiniMe = null;
56 private AlignViewport av;
58 private AlignmentPanel ap;
61 private boolean resizing = false;
63 // This is set true if the user resizes whilst
64 // the overview is being calculated
65 private boolean resizeAgain = false;
67 // Can set different properties in this seqCanvas than
68 // main visible SeqCanvas
69 private SequenceRenderer sr;
71 private jalview.renderer.seqfeatures.FeatureRenderer fr;
74 * Creates a new OverviewPanel object.
77 * The alignment panel which is shown in the overview panel
79 public OverviewPanel(AlignmentPanel ap)
85 sr = new SequenceRenderer(av);
86 sr.renderGaps = false;
87 sr.forOverview = true;
88 fr = new FeatureRenderer(ap);
90 od = new OverviewDimensions(av);
92 addComponentListener(new ComponentAdapter()
95 public void componentResized(ComponentEvent evt)
97 if ((getWidth() != od.getWidth())
98 || (getHeight() != (od.getHeight())))
100 updateOverviewImage();
105 addMouseMotionListener(new MouseMotionAdapter()
108 public void mouseDragged(MouseEvent evt)
110 if (!av.getWrapAlignment())
112 // TODO: feature: jv2.5 detect shift drag and update selection from
114 od.setBoxX(evt.getX());
115 od.setBoxY(evt.getY());
121 addMouseListener(new MouseAdapter()
124 public void mousePressed(MouseEvent evt)
126 if (!av.getWrapAlignment())
128 od.setBoxX(evt.getX());
129 od.setBoxY(evt.getY());
135 updateOverviewImage();
139 * Check box dimensions and scroll positions and correct if necessary
141 private void checkValid()
144 ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
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()); // width = getWidth();
164 od.setHeight(getHeight()); // sequencesHeight = getHeight() - graphHeight;
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 scale based on current alignment width and height
198 // calculate sampleCol and sampleRow
199 // alignment width is max number of residues/bases
200 // alignment height is number of sequences
201 int alwidth = av.getAlignment().getWidth();
202 int alheight = av.getAlignment().getHeight()
203 + av.getAlignment().getHiddenSequences().getSize();
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 if (av.getAlignmentConservationAnnotation() != null)
217 renderer.updateFromAlignViewport(av);
218 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
220 mg.translate(col, od.getSequencesHeight());
221 renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
222 av.getAlignmentConservationAnnotation().annotations,
223 (int) (sampleCol) + 1, od.getGraphHeight(),
224 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
225 mg.translate(-col, -od.getSequencesHeight());
236 updateOverviewImage();
246 private void buildImage(float sampleRow, float sampleCol)
250 int color = Color.white.getRGB();
252 jalview.datamodel.SequenceI seq = null;
253 final boolean hasHiddenRows = av.hasHiddenRows();
254 final boolean hasHiddenCols = av.hasHiddenColumns();
255 boolean hiddenRow = false;
256 // get hidden row and hidden column map once at beginning.
257 // clone featureRenderer settings to avoid race conditions... if state is
258 // updated just need to refresh again
259 for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
261 boolean doCopy = true;
262 int currentrow = (int) (row * sampleRow);
263 if (currentrow != lastrow)
267 lastrow = currentrow;
270 // should be method(s) in Alignment
273 seq = av.getAlignment().getHiddenSequences()
274 .getHiddenSequence(lastrow);
277 int index = av.getAlignment().getHiddenSequences()
278 .findIndexWithoutHiddenSeqs(lastrow);
280 seq = av.getAlignment().getSequenceAt(index);
289 seq = av.getAlignment().getSequenceAt(lastrow);
291 // end of Alignment method(s)
296 System.out.println(lastrow + " null");
300 for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
304 color = miniMe.getRGB(col, row - 1);
306 else if ((int) (col * sampleCol) != lastcol
307 || (int) (row * sampleRow) != lastrow)
309 lastcol = (int) (col * sampleCol);
310 color = getColumnColourFromSequence(seq, hiddenRow, hasHiddenCols,
313 // else if ((int) (col * sampleCol) == lastcol && (int) (row *
314 // sampleRow) == lastrow))
315 // we just use the color we already have , so don't need to set it
317 miniMe.setRGB(col, row, color);
323 * Find the colour of a sequence at a specified column position
325 private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
326 boolean hiddenRow, boolean hasHiddenCols, int lastcol)
330 if (seq.getLength() > lastcol)
332 color = sr.getResidueBoxColour(seq, lastcol).getRGB();
334 if (av.isShowSequenceFeatures())
336 color = fr.findFeatureColour(color, seq, lastcol);
341 color = Color.white.getRGB(); // White
345 || (hasHiddenCols && !av.getColumnSelection()
346 .isVisible(lastcol)))
348 color = new Color(color).darker().darker().getRGB();
355 * Update the overview panel box when the associated alignment panel is
359 public void setBoxPosition()
367 public void paintComponent(Graphics g)
369 if (resizing || resizeAgain)
371 if (lastMiniMe == null)
373 g.setColor(Color.white);
374 g.fillRect(0, 0, getWidth(), getHeight());
378 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
380 g.setColor(TRANS_GREY);
381 g.fillRect(0, 0, getWidth(), getHeight());
383 else if (lastMiniMe != null)
385 g.drawImage(lastMiniMe, 0, 0, this);
386 if (lastMiniMe != miniMe)
388 g.setColor(TRANS_GREY);
389 g.fillRect(0, 0, getWidth(), getHeight());
392 // TODO: render selected regions
393 g.setColor(Color.red);
394 g.drawRect(od.getBoxX(), od.getBoxY(), od.getBoxWidth(),
396 g.drawRect(od.getBoxX() + 1, od.getBoxY() + 1, od.getBoxWidth() - 2,
397 od.getBoxHeight() - 2);