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 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
101 public void drawIdString(Graphics2D g, boolean hiddenRows, SequenceI s,
102 int i, int starty, int ypos, int idWidth)
105 int panelWidth = getWidth();
106 int charHeight = av.getCharHeight();
108 if ((searchResults != null) && searchResults.contains(s))
110 g.setColor(Color.black);
111 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
113 g.setColor(Color.white);
115 else if ((av.getSelectionGroup() != null)
116 && av.getSelectionGroup().getSequences(null).contains(s))
118 g.setColor(Color.lightGray);
119 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
121 g.setColor(Color.white);
125 g.setColor(av.getSequenceColour(s));
126 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
128 g.setColor(Color.black);
131 if (av.isRightAlignIds())
133 FontMetrics fm = g.getFontMetrics();
135 - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
138 g.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
139 (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
143 drawMarker(g, av, i, starty, ypos, idWidth);
154 public void fastPaint(int vertical)
157 * for now, not attempting fast paint of wrapped ids...
159 if (gg == null || av.getWrapAlignment())
166 ViewportRanges ranges = av.getRanges();
168 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
169 -vertical * av.getCharHeight());
171 int ss = ranges.getStartSeq();
172 int es = ranges.getEndSeq();
175 if (vertical > 0) // scroll down
179 if (ss < ranges.getStartSeq())
180 { // ie scrolling too fast, more than a page at a time
181 ss = ranges.getStartSeq();
185 transY = imgHeight - ((vertical + 1) * av.getCharHeight());
188 else if (vertical < 0) // scroll up
192 if (es > ranges.getEndSeq())
194 es = ranges.getEndSeq();
198 gg.translate(0, transY);
200 drawIds(gg, av, ss, es, searchResults, getWidth());
202 gg.translate(0, -transY);
206 // Call repaint on alignment panel so that repaints from other alignment
207 // panel components can be aggregated. Otherwise performance of the overview
208 // window and others may be adversely affected.
209 av.getAlignPanel().repaint();
219 public void paintComponent(Graphics g)
221 super.paintComponent(g);
223 g.setColor(Color.white);
224 g.fillRect(0, 0, getWidth(), getHeight());
229 g.drawImage(image, 0, 0, this);
234 int oldHeight = imgHeight;
236 imgHeight = getHeight();
237 imgHeight -= (imgHeight % av.getCharHeight());
244 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
246 image = new BufferedImage(getWidth(), imgHeight,
247 BufferedImage.TYPE_INT_RGB);
250 gg = (Graphics2D) image.getGraphics();
252 // Fill in the background
253 gg.setColor(Color.white);
254 gg.fillRect(0, 0, getWidth(), imgHeight);
256 drawIds(gg, av, av.getRanges().getStartSeq(), av.getRanges().getEndSeq(), searchResults, getWidth());
258 g.drawImage(image, 0, 0, this);
262 * Draws sequence ids from sequence index startSeq to endSeq (inclusive), with
263 * the font and other display settings configured on the viewport. Ids of
264 * sequences included in the selection are coloured grey, otherwise the current
265 * id colour for the sequence id is used.
268 * @param alignViewport
274 void drawIds(Graphics2D g, AlignViewport alignViewport, final int startSeq,
275 final int endSeq, List<SequenceI> selection, int idWidth)
277 Font font = alignViewport.getFont();
278 if (alignViewport.isSeqNameItalics())
280 setIdfont(new Font(font.getName(), Font.ITALIC,
288 g.setFont(getIdfont());
289 FontMetrics fm = g.getFontMetrics();
291 if (alignViewport.antiAlias)
293 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
294 RenderingHints.VALUE_ANTIALIAS_ON);
297 Color currentColor = Color.white;
298 Color currentTextColor = Color.black;
300 boolean hasHiddenRows = alignViewport.hasHiddenRows();
302 if (alignViewport.getWrapAlignment())
304 drawIdsWrapped(g, alignViewport, startSeq, getHeight(), getWidth());
308 // Now draw the id strings
309 int panelWidth = getWidth();
312 // Now draw the id strings
313 for (int i = startSeq; i <= endSeq; i++)
315 SequenceI sequence = alignViewport.getAlignment().getSequenceAt(i);
317 if (sequence == null)
322 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
324 g.setFont(getHiddenFont(sequence, alignViewport));
327 // Selected sequence colours
328 if (selection != null && selection.contains(sequence))
330 currentColor = Color.black;
331 currentTextColor = Color.white;
333 else if ((alignViewport.getSelectionGroup() != null) && alignViewport
334 .getSelectionGroup().getSequences(null).contains(sequence))
336 currentColor = Color.lightGray;
337 currentTextColor = Color.black;
341 currentColor = alignViewport.getSequenceColour(sequence);
342 currentTextColor = Color.black;
345 g.setColor(currentColor);
347 int charHeight = alignViewport.getCharHeight();
348 g.fillRect(0, (i - startSeq) * charHeight,
349 getWidth(), charHeight);
351 g.setColor(currentTextColor);
353 String string = sequence
354 .getDisplayId(alignViewport.getShowJVSuffix());
356 if (alignViewport.isRightAlignIds())
358 xPos = panelWidth - fm.stringWidth(string) - 4;
361 g.drawString(string, xPos, (((i - startSeq) * charHeight) + charHeight)
366 drawMarker(g, alignViewport, i, startSeq, 0, idWidth);
372 * Draws sequence ids, and annotation labels if annotations are shown, in
376 * @param alignViewport
380 void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
381 int startSeq, int pageHeight, int idWidth)
383 int alignmentWidth = alignViewport.getAlignment().getWidth();
384 final int alheight = alignViewport.getAlignment().getHeight();
387 * assumption: SeqCanvas.calculateWrappedGeometry has been called
389 SeqCanvas seqCanvas = alignViewport.getAlignPanel()
390 .getSeqPanel().seqCanvas;
392 final int charHeight = alignViewport.getCharHeight();
394 AnnotationLabels labels = null;
395 if (alignViewport.isShowAnnotation())
397 labels = new AnnotationLabels(alignViewport);
400 ViewportRanges ranges = alignViewport.getRanges();
402 int rowSize = ranges.getViewportWidth();
405 * draw repeating sequence ids until out of sequence data or
406 * out of visible space, whichever comes first
408 boolean hasHiddenRows = alignViewport.hasHiddenRows();
409 int ypos = seqCanvas.wrappedSpaceAboveAlignment;
410 int rowStartRes = ranges.getStartRes();
411 while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
413 for (int i = startSeq; i < alheight; i++)
415 SequenceI s = alignViewport.getAlignment().getSequenceAt(i);
416 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
418 g.setFont(getHiddenFont(s, alignViewport));
422 g.setFont(getIdfont());
424 drawIdString(g, hasHiddenRows, s, i, 0, ypos, idWidth);
427 if (labels != null && alignViewport.isShowAnnotation())
429 g.translate(0, ypos + (alheight * charHeight));
430 labels.drawComponent(g, getWidth());
431 g.translate(0, -ypos - (alheight * charHeight));
434 ypos += seqCanvas.wrappedRepeatHeightPx;
435 rowStartRes += rowSize;
440 * Draws a marker (a blue right-pointing triangle) between sequences to indicate
444 * @param alignViewport
450 void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex, int starty, int yoffset, int idWidth)
452 SequenceI[] hseqs = alignViewport.getAlignment()
453 .getHiddenSequences().hiddenSequences;
454 // Use this method here instead of calling hiddenSeq adjust
456 int hSize = hseqs.length;
458 int hiddenIndex = seqIndex;
459 int lastIndex = seqIndex - 1;
460 int nextIndex = seqIndex + 1;
462 for (int j = 0; j < hSize; j++)
464 if (hseqs[j] != null)
466 if (j - 1 < hiddenIndex)
470 if (j - 1 < lastIndex)
474 if (j - 1 < nextIndex)
482 * are we below or above the hidden sequences?
484 boolean below = (hiddenIndex > lastIndex + 1);
485 boolean above = (nextIndex > hiddenIndex + 1);
487 g.setColor(Color.blue);
488 int charHeight = av.getCharHeight();
491 * vertices of the triangle, below or above hidden seqs
493 int[] xPoints = new int[] { idWidth - charHeight, idWidth - charHeight,
495 int yShift = seqIndex - starty;
499 int[] yPoints = new int[] { yShift * charHeight + yoffset,
500 yShift * charHeight + yoffset + charHeight / 4,
501 yShift * charHeight + yoffset };
502 g.fillPolygon(xPoints, yPoints, 3);
507 int[] yPoints = new int[] { yShift * charHeight + yoffset,
508 yShift * charHeight + yoffset - charHeight / 4,
509 yShift * charHeight + yoffset };
510 g.fillPolygon(xPoints, yPoints, 3);
515 * Answers the standard sequence id font, or a bold font if the sequence is
516 * set as reference or a hidden group representative
519 * @param alignViewport
522 private Font getHiddenFont(SequenceI seq, AlignViewport alignViewport)
524 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
526 return new Font(av.getFont().getName(), Font.BOLD,
527 av.getFont().getSize());
538 public void setHighlighted(List<SequenceI> list)
540 searchResults = list;
544 public Font getIdfont()
549 public void setIdfont(Font idfont)
551 this.idfont = idfont;
555 * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
556 * scrolling and resizing change viewport ranges. Scrolling changes both start
557 * and end points, but resize only changes end values. Here we only want to
558 * fastpaint on a scroll, with resize using a normal paint, so scroll events
559 * are identified as changes to the horizontal or vertical start value.
561 * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
562 * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
563 * provided, but it generates a change of "startres" which does require an
567 public void propertyChange(PropertyChangeEvent evt)
569 String propertyName = evt.getPropertyName();
570 if (propertyName.equals(ViewportRanges.STARTSEQ)
571 || (av.getWrapAlignment()
572 && propertyName.equals(ViewportRanges.STARTRES)))
574 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
576 else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
578 fastPaint(((int[]) evt.getNewValue())[1]
579 - ((int[]) evt.getOldValue())[1]);
581 else if (propertyName.equals(ViewportRanges.MOVE_VIEWPORT))