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;
70 private boolean allowFastPaint;
73 * Creates a new IdCanvas object.
78 public IdCanvas(AlignViewport av)
80 setLayout(new BorderLayout());
82 PaintRefresher.Register(this, av.getSequenceSetId());
83 av.getRanges().addPropertyChangeListener(this);
92 * true - check and display hidden row marker if need be
102 public void drawIdString(Graphics2D g, boolean hiddenRows, SequenceI s,
103 int i, int starty, int ypos)
106 int panelWidth = getWidth();
107 int charHeight = av.getCharHeight();
109 if ((searchResults != null) && searchResults.contains(s))
111 g.setColor(Color.black);
112 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
114 g.setColor(Color.white);
116 else if ((av.getSelectionGroup() != null)
117 && av.getSelectionGroup().getSequences(null).contains(s))
119 g.setColor(Color.lightGray);
120 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
122 g.setColor(Color.white);
126 g.setColor(av.getSequenceColour(s));
127 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
129 g.setColor(Color.black);
132 if (av.isRightAlignIds())
134 FontMetrics fm = g.getFontMetrics();
136 - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
139 g.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
140 (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
142 // JAL-3253-applet was just hiddenRows here. ccfc48e (gmungoc) added getShowHiddenMarkers test
143 if (hiddenRows && av.getShowHiddenMarkers())
145 drawMarker(g, av, i, starty, ypos);
156 public void fastPaint(int vertical)
160 * for now, not attempting fast paint of wrapped ids...
162 if (image == null || av.getWrapAlignment())
169 ViewportRanges ranges = av.getRanges();
171 Graphics2D gg = image.createGraphics();
172 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
173 -vertical * av.getCharHeight());
175 int ss = ranges.getStartSeq();
176 int es = ranges.getEndSeq();
179 if (vertical > 0) // scroll down
183 if (ss < ranges.getStartSeq())
184 { // ie scrolling too fast, more than a page at a time
185 ss = ranges.getStartSeq();
189 transY = imgHeight - ((vertical + 1) * av.getCharHeight());
192 else if (vertical < 0) // scroll up
196 if (es > ranges.getEndSeq())
198 es = ranges.getEndSeq();
202 gg.translate(0, transY);
204 drawIds(gg, av, ss, es, searchResults);
206 gg.translate(0, -transY);
212 // Call repaint on alignment panel so that repaints from other alignment
213 // panel components can be aggregated. Otherwise performance of the overview
214 // window and others may be adversely affected.
215 av.getAlignPanel().repaint();
225 public void paintComponent(Graphics g)
227 if (av.getAlignPanel().getHoldRepaint())
232 g.setColor(Color.white);
233 g.fillRect(0, 0, getWidth(), getHeight());
235 if (allowFastPaint && fastPaint)
238 g.drawImage(image, 0, 0, this);
243 int oldHeight = imgHeight;
245 imgHeight = getHeight();
246 imgHeight -= (imgHeight % av.getCharHeight());
253 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
255 image = new BufferedImage(getWidth(), imgHeight,
256 BufferedImage.TYPE_INT_RGB);
259 Graphics2D gg = image.createGraphics();
261 // Fill in the background
262 gg.setColor(Color.white);
263 gg.fillRect(0, 0, getWidth(), imgHeight);
265 drawIds(gg, av, av.getRanges().getStartSeq(), av.getRanges().getEndSeq(), searchResults);
269 g.drawImage(image, 0, 0, this);
273 * Draws sequence ids from sequence index startSeq to endSeq (inclusive), with
274 * the font and other display settings configured on the viewport. Ids of
275 * sequences included in the selection are coloured grey, otherwise the
276 * current id colour for the sequence id is used.
279 * @param alignViewport
284 void drawIds(Graphics2D g, AlignViewport alignViewport, final int startSeq,
285 final int endSeq, List<SequenceI> selection)
287 Font font = alignViewport.getFont();
288 if (alignViewport.isSeqNameItalics())
290 setIdfont(new Font(font.getName(), Font.ITALIC,
298 g.setFont(getIdfont());
299 FontMetrics fm = g.getFontMetrics();
301 if (alignViewport.antiAlias)
303 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
304 RenderingHints.VALUE_ANTIALIAS_ON);
307 Color currentColor = Color.white;
308 Color currentTextColor = Color.black;
310 boolean hasHiddenRows = alignViewport.hasHiddenRows();
312 if (alignViewport.getWrapAlignment())
314 drawIdsWrapped(g, alignViewport, startSeq, getHeight());
318 // Now draw the id strings
319 int panelWidth = getWidth();
322 // Now draw the id strings
323 for (int i = startSeq; i <= endSeq; i++)
325 SequenceI sequence = alignViewport.getAlignment().getSequenceAt(i);
327 if (sequence == null)
332 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
334 g.setFont(getHiddenFont(sequence, alignViewport));
337 // Selected sequence colours
338 if (selection != null && selection.contains(sequence))
340 currentColor = Color.black;
341 currentTextColor = Color.white;
343 else if ((alignViewport.getSelectionGroup() != null) && alignViewport
344 .getSelectionGroup().getSequences(null).contains(sequence))
346 currentColor = Color.lightGray;
347 currentTextColor = Color.black;
351 currentColor = alignViewport.getSequenceColour(sequence);
352 currentTextColor = Color.black;
355 g.setColor(currentColor);
357 int charHeight = alignViewport.getCharHeight();
358 g.fillRect(0, (i - startSeq) * charHeight,
359 getWidth(), charHeight);
361 g.setColor(currentTextColor);
363 String string = sequence
364 .getDisplayId(alignViewport.getShowJVSuffix());
366 if (alignViewport.isRightAlignIds())
368 xPos = panelWidth - fm.stringWidth(string) - 4;
371 g.drawString(string, xPos, (((i - startSeq) * charHeight) + charHeight)
374 if (hasHiddenRows && av.getShowHiddenMarkers())
376 drawMarker(g, alignViewport, i, startSeq, 0);
382 * Draws sequence ids, and annotation labels if annotations are shown, in
386 * @param alignViewport
389 void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
390 int startSeq, int pageHeight)
392 int alignmentWidth = alignViewport.getAlignment().getWidth();
393 final int alheight = alignViewport.getAlignment().getHeight();
396 // int annotationHeight = 0;
399 * assumption: SeqCanvas.calculateWrappedGeometry has been called
401 * was based on the fact that SeqCanvas was added as a child prior to IdCanvas,
402 * and children are processed in order of addition.
404 * It's probably fine. But...
407 SeqCanvas seqCanvas = alignViewport.getAlignPanel()
408 .getSeqPanel().seqCanvas;
409 // ...better: let's call it now
410 seqCanvas.calculateWrappedGeometry();
412 final int charHeight = alignViewport.getCharHeight();
414 AnnotationLabels labels = null;
415 if (alignViewport.isShowAnnotation())
417 // BH when was ap == null?
420 ap = new AnnotationPanel(alignViewport);
422 // annotationHeight = ap.adjustPanelHeight();
423 labels = new AnnotationLabels(alignViewport);
426 // int hgap = charHeight;
427 // if (alignViewport.getScaleAboveWrapped())
429 // hgap += charHeight;
433 // * height of alignment + gap + annotations (if shown)
435 // int cHeight = alheight * charHeight + hgap
436 // + annotationHeight;
438 ViewportRanges ranges = alignViewport.getRanges();
440 int rowSize = ranges.getViewportWidth();
443 * draw repeating sequence ids until out of sequence data or
444 * out of visible space, whichever comes first
446 boolean hasHiddenRows = alignViewport.hasHiddenRows();
447 int ypos = seqCanvas.wrappedSpaceAboveAlignment;
448 int rowStartRes = ranges.getStartRes();
449 while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
451 for (int i = startSeq; i < alheight; i++)
453 SequenceI s = alignViewport.getAlignment().getSequenceAt(i);
454 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
456 g.setFont(getHiddenFont(s, alignViewport));
460 g.setFont(getIdfont());
462 drawIdString(g, hasHiddenRows, s, i, 0, ypos);
465 if (labels != null && alignViewport.isShowAnnotation())
467 g.translate(0, ypos + (alheight * charHeight));
468 labels.drawComponent(g, getWidth());
469 g.translate(0, -ypos - (alheight * charHeight));
472 ypos += seqCanvas.wrappedRepeatHeightPx;
473 rowStartRes += rowSize;
478 * Draws a marker (a blue right-pointing triangle) between sequences to
479 * indicate hidden sequences.
482 * @param alignViewport
487 void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex, int starty, int yoffset)
489 SequenceI[] hseqs = alignViewport.getAlignment()
490 .getHiddenSequences().hiddenSequences;
491 // Use this method here instead of calling hiddenSeq adjust
493 int hSize = hseqs.length;
495 int hiddenIndex = seqIndex;
496 int lastIndex = seqIndex - 1;
497 int nextIndex = seqIndex + 1;
499 for (int j = 0; j < hSize; j++)
501 if (hseqs[j] != null)
503 if (j - 1 < hiddenIndex)
507 if (j - 1 < lastIndex)
511 if (j - 1 < nextIndex)
519 * are we below or above the hidden sequences?
521 boolean below = (hiddenIndex > lastIndex + 1);
522 boolean above = (nextIndex > hiddenIndex + 1);
524 g.setColor(Color.blue);
525 int charHeight = av.getCharHeight();
528 * vertices of the triangle, below or above hidden seqs
530 int[] xPoints = new int[]
531 { getWidth() - charHeight,
532 getWidth() - charHeight, getWidth() };
533 int yShift = seqIndex - starty;
537 int[] yPoints = new int[] { yShift * charHeight + yoffset,
538 yShift * charHeight + yoffset + charHeight / 4,
539 yShift * charHeight + yoffset };
540 g.fillPolygon(xPoints, yPoints, 3);
545 int[] yPoints = new int[] { yShift * charHeight + yoffset,
546 yShift * charHeight + yoffset - charHeight / 4,
547 yShift * charHeight + yoffset };
548 g.fillPolygon(xPoints, yPoints, 3);
553 * Answers the standard sequence id font, or a bold font if the sequence is
554 * set as reference or a hidden group representative
557 * @param alignViewport
560 private Font getHiddenFont(SequenceI seq, AlignViewport alignViewport)
562 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
564 return new Font(av.getFont().getName(), Font.BOLD,
565 av.getFont().getSize());
576 public void setHighlighted(List<SequenceI> list)
578 searchResults = list;
582 public Font getIdfont()
587 public void setIdfont(Font idfont)
589 this.idfont = idfont;
593 * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
594 * scrolling and resizing change viewport ranges. Scrolling changes both start
595 * and end points, but resize only changes end values. Here we only want to
596 * fastpaint on a scroll, with resize using a normal paint, so scroll events
597 * are identified as changes to the horizontal or vertical start value.
599 * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
600 * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
601 * provided, but it generates a change of "startres" which does require an
605 public void propertyChange(PropertyChangeEvent evt)
607 String propertyName = evt.getPropertyName();
608 switch (propertyName)
610 case ViewportRanges.STARTSEQ:
611 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
613 case ViewportRanges.STARTRES:
614 if (av.getWrapAlignment())
616 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
619 case ViewportRanges.STARTRESANDSEQ:
620 fastPaint(((int[]) evt.getNewValue())[1]
621 - ((int[]) evt.getOldValue())[1]);
623 case ViewportRanges.MOVE_VIEWPORT:
631 * Clears the flag that allows a 'fast paint' on the next repaint, so
632 * requiring a full repaint
634 public void setNoFastPaint()
636 allowFastPaint = false;