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.viewmodel.ViewportListenerI;
25 import jalview.viewmodel.ViewportRanges;
27 import java.awt.BorderLayout;
28 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
32 import java.awt.Graphics2D;
33 import java.awt.RenderingHints;
34 import java.awt.image.BufferedImage;
35 import java.beans.PropertyChangeEvent;
36 import java.util.List;
38 import javax.swing.JPanel;
46 public class IdCanvas extends JPanel implements ViewportListenerI
48 protected AlignViewport av;
50 protected boolean showScores = true;
52 protected int maxIdLength = -1;
54 protected String maxIdStr = null;
62 private boolean fastPaint = false;
64 List<SequenceI> searchResults;
71 * Creates a new IdCanvas object.
76 public IdCanvas(AlignViewport av)
78 setLayout(new BorderLayout());
80 PaintRefresher.Register(this, av.getSequenceSetId());
81 av.getRanges().addPropertyChangeListener(this);
90 * true - check and display hidden row marker if need be
100 public void drawIdString(Graphics2D g, boolean hiddenRows, SequenceI s,
101 int i, int starty, int ypos)
104 int panelWidth = getWidth();
105 int charHeight = av.getCharHeight();
107 if ((searchResults != null) && searchResults.contains(s))
109 g.setColor(Color.black);
110 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
112 g.setColor(Color.white);
114 else if ((av.getSelectionGroup() != null)
115 && av.getSelectionGroup().getSequences(null).contains(s))
117 g.setColor(Color.lightGray);
118 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
120 g.setColor(Color.white);
124 g.setColor(av.getSequenceColour(s));
125 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
127 g.setColor(Color.black);
130 if (av.isRightAlignIds())
132 FontMetrics fm = g.getFontMetrics();
134 - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
137 g.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
138 (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
142 drawMarker(g, av, i, starty, ypos);
153 public void fastPaint(int vertical)
157 * for now, not attempting fast paint of wrapped ids...
159 if (image == null || av.getWrapAlignment())
166 ViewportRanges ranges = av.getRanges();
168 Graphics2D gg = image.createGraphics();
169 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
170 -vertical * av.getCharHeight());
172 int ss = ranges.getStartSeq();
173 int es = ranges.getEndSeq();
176 if (vertical > 0) // scroll down
180 if (ss < ranges.getStartSeq())
181 { // ie scrolling too fast, more than a page at a time
182 ss = ranges.getStartSeq();
186 transY = imgHeight - ((vertical + 1) * av.getCharHeight());
189 else if (vertical < 0) // scroll up
193 if (es > ranges.getEndSeq())
195 es = ranges.getEndSeq();
199 gg.translate(0, transY);
201 drawIds(gg, av, ss, es, searchResults);
203 gg.translate(0, -transY);
209 // Call repaint on alignment panel so that repaints from other alignment
210 // panel components can be aggregated. Otherwise performance of the overview
211 // window and others may be adversely affected.
212 av.getAlignPanel().repaint();
222 public void paintComponent(Graphics g)
225 if (av.getAlignPanel().getHoldRepaint())
230 g.setColor(Color.white);
231 g.fillRect(0, 0, getWidth(), getHeight());
236 g.drawImage(image, 0, 0, this);
241 int oldHeight = imgHeight;
243 imgHeight = getHeight();
244 imgHeight -= (imgHeight % av.getCharHeight());
251 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
253 image = new BufferedImage(getWidth(), imgHeight,
254 BufferedImage.TYPE_INT_RGB);
257 Graphics2D gg = image.createGraphics();
259 // Fill in the background
260 gg.setColor(Color.white);
261 gg.fillRect(0, 0, getWidth(), imgHeight);
263 drawIds(gg, av, av.getRanges().getStartSeq(), av.getRanges().getEndSeq(), searchResults);
267 g.drawImage(image, 0, 0, this);
271 * Draws sequence ids from sequence index startSeq to endSeq (inclusive), with
272 * the font and other display settings configured on the viewport. Ids of
273 * sequences included in the selection are coloured grey, otherwise the
274 * current id colour for the sequence id is used.
277 * @param alignViewport
282 void drawIds(Graphics2D g, AlignViewport alignViewport, final int startSeq,
283 final int endSeq, List<SequenceI> selection)
285 Font font = alignViewport.getFont();
286 if (alignViewport.isSeqNameItalics())
288 setIdfont(new Font(font.getName(), Font.ITALIC,
296 g.setFont(getIdfont());
297 FontMetrics fm = g.getFontMetrics();
299 if (alignViewport.antiAlias)
301 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
302 RenderingHints.VALUE_ANTIALIAS_ON);
305 Color currentColor = Color.white;
306 Color currentTextColor = Color.black;
308 boolean hasHiddenRows = alignViewport.hasHiddenRows();
310 if (alignViewport.getWrapAlignment())
312 drawIdsWrapped(g, alignViewport, startSeq, getHeight());
316 // Now draw the id strings
317 int panelWidth = getWidth();
320 // Now draw the id strings
321 for (int i = startSeq; i <= endSeq; i++)
323 SequenceI sequence = alignViewport.getAlignment().getSequenceAt(i);
325 if (sequence == null)
330 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
332 g.setFont(getHiddenFont(sequence, alignViewport));
335 // Selected sequence colours
336 if (selection != null && selection.contains(sequence))
338 currentColor = Color.black;
339 currentTextColor = Color.white;
341 else if ((alignViewport.getSelectionGroup() != null) && alignViewport
342 .getSelectionGroup().getSequences(null).contains(sequence))
344 currentColor = Color.lightGray;
345 currentTextColor = Color.black;
349 currentColor = alignViewport.getSequenceColour(sequence);
350 currentTextColor = Color.black;
353 g.setColor(currentColor);
355 int charHeight = alignViewport.getCharHeight();
356 g.fillRect(0, (i - startSeq) * charHeight,
357 getWidth(), charHeight);
359 g.setColor(currentTextColor);
361 String string = sequence
362 .getDisplayId(alignViewport.getShowJVSuffix());
364 if (alignViewport.isRightAlignIds())
366 xPos = panelWidth - fm.stringWidth(string) - 4;
369 g.drawString(string, xPos, (((i - startSeq) * charHeight) + charHeight)
374 drawMarker(g, alignViewport, i, startSeq, 0);
380 * Draws sequence ids, and annotation labels if annotations are shown, in
384 * @param alignViewport
387 void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
388 int startSeq, int pageHeight)
390 int alignmentWidth = alignViewport.getAlignment().getWidth();
391 final int alheight = alignViewport.getAlignment().getHeight();
393 int annotationHeight = 0;
395 AnnotationLabels labels = null;
396 if (alignViewport.isShowAnnotation())
400 ap = new AnnotationPanel(alignViewport);
402 annotationHeight = ap.adjustPanelHeight();
403 labels = new AnnotationLabels(alignViewport);
406 final int charHeight = alignViewport.getCharHeight();
407 int hgap = charHeight;
408 if (alignViewport.getScaleAboveWrapped())
414 * height of alignment + gap + annotations (if shown)
416 int cHeight = alheight * charHeight + hgap
419 ViewportRanges ranges = alignViewport.getRanges();
421 int rowSize = ranges.getViewportWidth();
424 * draw repeating sequence ids until out of sequence data or
425 * out of visible space, whichever comes first
427 boolean hasHiddenRows = alignViewport.hasHiddenRows();
429 int rowStartRes = ranges.getStartRes();
430 while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
432 for (int i = startSeq; i < alheight; i++)
434 SequenceI s = alignViewport.getAlignment().getSequenceAt(i);
435 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
437 g.setFont(getHiddenFont(s, alignViewport));
441 g.setFont(getIdfont());
443 drawIdString(g, hasHiddenRows, s, i, 0, ypos);
446 if (labels != null && alignViewport.isShowAnnotation())
448 g.translate(0, ypos + (alheight * charHeight));
449 labels.drawComponent(g, getWidth());
450 g.translate(0, -ypos - (alheight * charHeight));
454 rowStartRes += rowSize;
459 * Draws a marker (a blue right-pointing triangle) between sequences to
460 * indicate hidden sequences.
463 * @param alignViewport
468 void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex, int starty, int yoffset)
470 SequenceI[] hseqs = alignViewport.getAlignment()
471 .getHiddenSequences().hiddenSequences;
472 // Use this method here instead of calling hiddenSeq adjust
474 int hSize = hseqs.length;
476 int hiddenIndex = seqIndex;
477 int lastIndex = seqIndex - 1;
478 int nextIndex = seqIndex + 1;
480 for (int j = 0; j < hSize; j++)
482 if (hseqs[j] != null)
484 if (j - 1 < hiddenIndex)
488 if (j - 1 < lastIndex)
492 if (j - 1 < nextIndex)
500 * are we below or above the hidden sequences?
502 boolean below = (hiddenIndex > lastIndex + 1);
503 boolean above = (nextIndex > hiddenIndex + 1);
505 g.setColor(Color.blue);
506 int charHeight = av.getCharHeight();
509 * vertices of the triangle, below or above hidden seqs
511 int[] xPoints = new int[]
512 { getWidth() - charHeight,
513 getWidth() - charHeight, getWidth() };
514 int yShift = seqIndex - starty;
518 int[] yPoints = new int[] { yShift * charHeight + yoffset,
519 yShift * charHeight + yoffset + charHeight / 4,
520 yShift * charHeight + yoffset };
521 g.fillPolygon(xPoints, yPoints, 3);
526 int[] yPoints = new int[] { yShift * charHeight + yoffset,
527 yShift * charHeight + yoffset - charHeight / 4,
528 yShift * charHeight + yoffset };
529 g.fillPolygon(xPoints, yPoints, 3);
534 * Answers the standard sequence id font, or a bold font if the sequence is
535 * set as reference or a hidden group representative
538 * @param alignViewport
541 private Font getHiddenFont(SequenceI seq, AlignViewport alignViewport)
543 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
545 return new Font(av.getFont().getName(), Font.BOLD,
546 av.getFont().getSize());
557 public void setHighlighted(List<SequenceI> list)
559 searchResults = list;
563 public Font getIdfont()
568 public void setIdfont(Font idfont)
570 this.idfont = idfont;
574 * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
575 * scrolling and resizing change viewport ranges. Scrolling changes both start
576 * and end points, but resize only changes end values. Here we only want to
577 * fastpaint on a scroll, with resize using a normal paint, so scroll events
578 * are identified as changes to the horizontal or vertical start value.
580 * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
581 * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
582 * provided, but it generates a change of "startres" which does require an
586 public void propertyChange(PropertyChangeEvent evt)
588 String propertyName = evt.getPropertyName();
589 switch (propertyName)
591 case ViewportRanges.STARTSEQ:
592 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
594 case ViewportRanges.STARTRES:
595 if (av.getWrapAlignment())
597 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
600 case ViewportRanges.STARTRESANDSEQ:
601 fastPaint(((int[]) evt.getNewValue())[1]
602 - ((int[]) evt.getOldValue())[1]);
604 case ViewportRanges.MOVE_VIEWPORT:
612 * Clears the flag that allows a 'fast paint' on the next repaint, so
613 * requiring a full repaint
615 public void setNoFastPaint()