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 java.awt.BorderLayout;
24 import java.awt.Color;
26 import java.awt.FontMetrics;
27 import java.awt.Graphics;
28 import java.awt.Graphics2D;
29 import java.awt.RenderingHints;
30 import java.awt.image.BufferedImage;
31 import java.beans.PropertyChangeEvent;
32 import java.util.List;
34 import javax.swing.JPanel;
36 import jalview.datamodel.SequenceI;
37 import jalview.viewmodel.ViewportListenerI;
38 import jalview.viewmodel.ViewportRanges;
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;
69 private boolean allowFastPaint;
72 * Creates a new IdCanvas object.
77 public IdCanvas(AlignViewport av)
79 setLayout(new BorderLayout());
81 PaintRefresher.Register(this, av.getSequenceSetId());
82 av.getRanges().addPropertyChangeListener(this);
91 * 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)
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));
141 // JAL-3253-applet was just hiddenRows here. ccfc48e (gmungoc) added getShowHiddenMarkers test
142 if (hiddenRows && av.getShowHiddenMarkers())
144 drawMarker(g, av, i, starty, ypos);
155 public void fastPaint(int vertical)
159 * for now, not attempting fast paint of wrapped ids...
161 if (image == null || av.getWrapAlignment())
168 ViewportRanges ranges = av.getRanges();
170 Graphics2D gg = image.createGraphics();
171 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
172 -vertical * av.getCharHeight());
174 int ss = ranges.getStartSeq();
175 int es = ranges.getEndSeq();
178 if (vertical > 0) // scroll down
182 if (ss < ranges.getStartSeq())
183 { // ie scrolling too fast, more than a page at a time
184 ss = ranges.getStartSeq();
188 transY = imgHeight - ((vertical + 1) * av.getCharHeight());
191 else if (vertical < 0) // scroll up
195 if (es > ranges.getEndSeq())
197 es = ranges.getEndSeq();
201 gg.translate(0, transY);
203 drawIds(gg, av, ss, es, searchResults);
205 gg.translate(0, -transY);
211 // Call repaint on alignment panel so that repaints from other alignment
212 // panel components can be aggregated. Otherwise performance of the overview
213 // window and others may be adversely affected.
214 av.getAlignPanel().repaint();
224 public void paintComponent(Graphics g)
226 if (av.getAlignPanel().getHoldRepaint())
230 g.setColor(Color.white);
231 g.fillRect(0, 0, getWidth(), getHeight());
233 if (allowFastPaint && fastPaint)
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));
333 fm = g.getFontMetrics();
336 // Selected sequence colours
337 if (selection != null && selection.contains(sequence))
339 currentColor = Color.black;
340 currentTextColor = Color.white;
342 else if ((alignViewport.getSelectionGroup() != null) && alignViewport
343 .getSelectionGroup().getSequences(null).contains(sequence))
345 currentColor = Color.lightGray;
346 currentTextColor = Color.black;
350 currentColor = alignViewport.getSequenceColour(sequence);
351 currentTextColor = Color.black;
354 g.setColor(currentColor);
356 int charHeight = alignViewport.getCharHeight();
357 g.fillRect(0, (i - startSeq) * charHeight,
358 getWidth(), charHeight);
360 g.setColor(currentTextColor);
362 String string = sequence
363 .getDisplayId(alignViewport.getShowJVSuffix());
365 if (alignViewport.isRightAlignIds())
367 xPos = panelWidth - fm.stringWidth(string) - 4;
370 g.drawString(string, xPos, (((i - startSeq) * charHeight) + charHeight)
373 if (hasHiddenRows && av.getShowHiddenMarkers())
375 drawMarker(g, alignViewport, i, startSeq, 0);
381 * Draws sequence ids, and annotation labels if annotations are shown, in
385 * @param alignViewport
388 void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
389 int startSeq, int pageHeight)
391 int alignmentWidth = alignViewport.getAlignment().getWidth();
392 final int alheight = alignViewport.getAlignment().getHeight();
396 * assumption: SeqCanvas.calculateWrappedGeometry has been called
398 * was based on the fact that SeqCanvas was added as a child prior to IdCanvas,
399 * and children are processed in order of addition.
401 * It's probably fine. But...
404 SeqCanvas seqCanvas = alignViewport.getAlignPanel()
405 .getSeqPanel().seqCanvas;
406 // ...better: let's call it now
407 seqCanvas.calculateWrappedGeometry();
409 final int charHeight = alignViewport.getCharHeight();
411 AnnotationLabels labels = null;
412 if (alignViewport.isShowAnnotation())
414 // BH when was ap == null?
417 ap = new AnnotationPanel(alignViewport);
419 labels = new AnnotationLabels(alignViewport);
422 ViewportRanges ranges = alignViewport.getRanges();
424 int rowSize = ranges.getViewportWidth();
427 * draw repeating sequence ids until out of sequence data or
428 * out of visible space, whichever comes first
430 boolean hasHiddenRows = alignViewport.hasHiddenRows();
431 int ypos = seqCanvas.wrappedSpaceAboveAlignment;
432 int rowStartRes = ranges.getStartRes();
433 while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
435 for (int i = startSeq; i < alheight; i++)
437 SequenceI s = alignViewport.getAlignment().getSequenceAt(i);
438 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
440 g.setFont(getHiddenFont(s, alignViewport));
444 g.setFont(getIdfont());
446 drawIdString(g, hasHiddenRows, s, i, 0, ypos);
449 if (labels != null && alignViewport.isShowAnnotation())
451 g.translate(0, ypos + (alheight * charHeight));
452 labels.drawComponent(g, getWidth());
453 g.translate(0, -ypos - (alheight * charHeight));
456 ypos += seqCanvas.wrappedRepeatHeightPx;
457 rowStartRes += rowSize;
462 * Draws a marker (a blue right-pointing triangle) between sequences to
463 * indicate hidden sequences.
466 * @param alignViewport
471 void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex, int starty, int yoffset)
473 SequenceI[] hseqs = alignViewport.getAlignment()
474 .getHiddenSequences().hiddenSequences;
475 // Use this method here instead of calling hiddenSeq adjust
477 int hSize = hseqs.length;
479 int hiddenIndex = seqIndex;
480 int lastIndex = seqIndex - 1;
481 int nextIndex = seqIndex + 1;
483 for (int j = 0; j < hSize; j++)
485 if (hseqs[j] != null)
487 if (j - 1 < hiddenIndex)
491 if (j - 1 < lastIndex)
495 if (j - 1 < nextIndex)
503 * are we below or above the hidden sequences?
505 boolean below = (hiddenIndex > lastIndex + 1);
506 boolean above = (nextIndex > hiddenIndex + 1);
508 g.setColor(Color.blue);
509 int charHeight = av.getCharHeight();
512 * vertices of the triangle, below or above hidden seqs
514 int[] xPoints = new int[]
515 { getWidth() - charHeight,
516 getWidth() - charHeight, getWidth() };
517 int yShift = seqIndex - starty;
521 int[] yPoints = new int[] { yShift * charHeight + yoffset,
522 yShift * charHeight + yoffset + charHeight / 4,
523 yShift * charHeight + yoffset };
524 g.fillPolygon(xPoints, yPoints, 3);
529 int[] yPoints = new int[] { yShift * charHeight + yoffset,
530 yShift * charHeight + yoffset - charHeight / 4,
531 yShift * charHeight + yoffset };
532 g.fillPolygon(xPoints, yPoints, 3);
537 * Answers the standard sequence id font, or a bold font if the sequence is
538 * set as reference or a hidden group representative
541 * @param alignViewport
544 private Font getHiddenFont(SequenceI seq, AlignViewport alignViewport)
546 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
548 return new Font(av.getFont().getName(), Font.BOLD,
549 av.getFont().getSize());
560 public void setHighlighted(List<SequenceI> list)
562 searchResults = list;
566 public Font getIdfont()
571 public void setIdfont(Font idfont)
573 this.idfont = idfont;
577 * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
578 * scrolling and resizing change viewport ranges. Scrolling changes both start
579 * and end points, but resize only changes end values. Here we only want to
580 * fastpaint on a scroll, with resize using a normal paint, so scroll events
581 * are identified as changes to the horizontal or vertical start value.
583 * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
584 * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
585 * provided, but it generates a change of "startres" which does require an
589 public void propertyChange(PropertyChangeEvent evt)
591 String propertyName = evt.getPropertyName();
592 switch (propertyName)
594 case ViewportRanges.STARTSEQ:
595 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
597 case ViewportRanges.STARTRES:
598 if (av.getWrapAlignment())
600 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
603 case ViewportRanges.STARTRESANDSEQ:
604 fastPaint(((int[]) evt.getNewValue())[1]
605 - ((int[]) evt.getOldValue())[1]);
607 case ViewportRanges.MOVE_VIEWPORT:
615 * Clears the flag that allows a 'fast paint' on the next repaint, so
616 * requiring a full repaint
618 public void setNoFastPaint()
620 allowFastPaint = false;