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
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)
156 * for now, not attempting fast paint of wrapped ids...
158 if (gg == null || av.getWrapAlignment())
165 ViewportRanges ranges = av.getRanges();
167 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
168 -vertical * av.getCharHeight());
170 int ss = ranges.getStartSeq();
171 int es = ranges.getEndSeq();
174 if (vertical > 0) // scroll down
178 if (ss < ranges.getStartSeq())
179 { // ie scrolling too fast, more than a page at a time
180 ss = ranges.getStartSeq();
184 transY = imgHeight - ((vertical + 1) * av.getCharHeight());
187 else if (vertical < 0) // scroll up
191 if (es > ranges.getEndSeq())
193 es = ranges.getEndSeq();
197 gg.translate(0, transY);
199 drawIds(gg, av, ss, es, searchResults);
201 gg.translate(0, -transY);
205 // Call repaint on alignment panel so that repaints from other alignment
206 // panel components can be aggregated. Otherwise performance of the overview
207 // window and others may be adversely affected.
208 av.getAlignPanel().repaint();
218 public void paintComponent(Graphics g)
220 super.paintComponent(g);
222 g.setColor(Color.white);
223 g.fillRect(0, 0, getWidth(), getHeight());
228 g.drawImage(image, 0, 0, this);
233 int oldHeight = imgHeight;
235 imgHeight = getHeight();
236 imgHeight -= (imgHeight % av.getCharHeight());
243 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
245 image = new BufferedImage(getWidth(), imgHeight,
246 BufferedImage.TYPE_INT_RGB);
249 gg = (Graphics2D) image.getGraphics();
251 // Fill in the background
252 gg.setColor(Color.white);
253 gg.fillRect(0, 0, getWidth(), imgHeight);
255 drawIds(gg, av, av.getRanges().getStartSeq(), av.getRanges().getEndSeq(), searchResults);
257 g.drawImage(image, 0, 0, this);
261 * Draws sequence ids from sequence index starty to endy (inclusive), with the
262 * font and other display settings configured on the viewport. Ids of
263 * sequences included in the selection are coloured grey, otherwise the
264 * current id colour for the sequence id is used.
267 * @param alignViewport
272 void drawIds(Graphics2D g, AlignViewport alignViewport, final int starty,
273 final int endy, List<SequenceI> selection)
275 Font font = alignViewport.getFont();
276 if (alignViewport.isSeqNameItalics())
278 setIdfont(new Font(font.getName(), Font.ITALIC,
286 g.setFont(getIdfont());
287 FontMetrics fm = g.getFontMetrics();
289 if (alignViewport.antiAlias)
291 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
292 RenderingHints.VALUE_ANTIALIAS_ON);
295 Color currentColor = Color.white;
296 Color currentTextColor = Color.black;
298 boolean hasHiddenRows = av.hasHiddenRows();
300 if (alignViewport.getWrapAlignment())
302 drawIdsWrapped(starty, hasHiddenRows);
306 // Now draw the id strings
307 int panelWidth = getWidth();
310 // Now draw the id strings
311 for (int i = starty; i <= endy; i++)
313 SequenceI sequence = alignViewport.getAlignment().getSequenceAt(i);
315 if (sequence == null)
320 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
322 g.setFont(getHiddenFont(sequence, alignViewport));
325 // Selected sequence colours
326 if (selection != null && selection.contains(sequence))
328 currentColor = Color.black;
329 currentTextColor = Color.white;
331 else if ((av.getSelectionGroup() != null) && av.getSelectionGroup()
332 .getSequences(null).contains(sequence))
334 currentColor = Color.lightGray;
335 currentTextColor = Color.black;
339 currentColor = alignViewport.getSequenceColour(sequence);
340 currentTextColor = Color.black;
343 g.setColor(currentColor);
345 int charHeight = alignViewport.getCharHeight();
346 g.fillRect(0, (i - starty) * charHeight,
347 getWidth(), charHeight);
349 g.setColor(currentTextColor);
351 String string = sequence
352 .getDisplayId(alignViewport.getShowJVSuffix());
354 if (alignViewport.isRightAlignIds())
356 xPos = panelWidth - fm.stringWidth(string) - 4;
359 g.drawString(string, xPos, (((i - starty) * charHeight) + charHeight)
364 drawMarker(g, alignViewport, i, starty, 0);
370 * Draws sequence ids in wrapped mode
373 * @param hasHiddenRows
375 protected void drawIdsWrapped(int starty, boolean hasHiddenRows)
377 int maxwidth = av.getAlignment().getWidth();
378 int alheight = av.getAlignment().getHeight();
380 if (av.hasHiddenColumns())
382 maxwidth = av.getAlignment().getHiddenColumns()
383 .absoluteToVisibleColumn(maxwidth) - 1;
386 int annotationHeight = 0;
388 AnnotationLabels labels = null;
389 if (av.isShowAnnotation())
393 ap = new AnnotationPanel(av);
396 annotationHeight = ap.adjustPanelHeight();
397 labels = new AnnotationLabels(av);
400 int hgap = av.getCharHeight();
401 if (av.getScaleAboveWrapped())
403 hgap += av.getCharHeight();
406 int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
408 ViewportRanges ranges = av.getRanges();
410 int rowSize = ranges.getViewportWidth();
413 * draw repeating sequence ids until out of sequence data or
414 * out of visible space, whichever comes first
417 int row = ranges.getStartRes();
418 while ((ypos <= getHeight()) && (row < maxwidth))
420 for (int i = starty; i < alheight; i++)
422 SequenceI s = av.getAlignment().getSequenceAt(i);
423 if (hasHiddenRows || av.isDisplayReferenceSeq())
425 gg.setFont(getHiddenFont(s, av));
429 gg.setFont(getIdfont());
432 drawIdString(gg, hasHiddenRows, s, i, 0, ypos);
435 if (labels != null && av.isShowAnnotation())
437 gg.translate(0, ypos + (alheight * av.getCharHeight()));
438 labels.drawComponent(gg, getWidth());
439 gg.translate(0, -ypos - (alheight * av.getCharHeight()));
448 * Draws a marker (a blue right-pointing triangle) between sequences to
449 * indicate hidden sequences.
452 * @param alignViewport
457 void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex, int starty, int yoffset)
459 SequenceI[] hseqs = alignViewport.getAlignment()
460 .getHiddenSequences().hiddenSequences;
461 // Use this method here instead of calling hiddenSeq adjust
463 int hSize = hseqs.length;
465 int hiddenIndex = seqIndex;
466 int lastIndex = seqIndex - 1;
467 int nextIndex = seqIndex + 1;
469 for (int j = 0; j < hSize; j++)
471 if (hseqs[j] != null)
473 if (j - 1 < hiddenIndex)
477 if (j - 1 < lastIndex)
481 if (j - 1 < nextIndex)
489 * are we below or above the hidden sequences?
491 boolean below = (hiddenIndex > lastIndex + 1);
492 boolean above = (nextIndex > hiddenIndex + 1);
494 g.setColor(Color.blue);
495 int charHeight = av.getCharHeight();
498 * vertices of the triangle, below or above hidden seqs
500 int[] xPoints = new int[]
501 { getWidth() - charHeight,
502 getWidth() - charHeight, getWidth() };
503 int yShift = seqIndex - starty;
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 int[] yPoints = new int[] { yShift * charHeight + yoffset,
516 yShift * charHeight + yoffset - charHeight / 4,
517 yShift * charHeight + yoffset };
518 g.fillPolygon(xPoints, yPoints, 3);
523 * Answers the standard sequence id font, or a bold font if the sequence is
524 * set as reference or a hidden group representative
527 * @param alignViewport
530 private Font getHiddenFont(SequenceI seq, AlignViewport alignViewport)
532 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
534 return new Font(av.getFont().getName(), Font.BOLD,
535 av.getFont().getSize());
546 public void setHighlighted(List<SequenceI> list)
548 searchResults = list;
552 public Font getIdfont()
557 public void setIdfont(Font idfont)
559 this.idfont = idfont;
563 * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
564 * scrolling and resizing change viewport ranges. Scrolling changes both start
565 * and end points, but resize only changes end values. Here we only want to
566 * fastpaint on a scroll, with resize using a normal paint, so scroll events
567 * are identified as changes to the horizontal or vertical start value.
569 * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
570 * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
571 * provided, but it generates a change of "startres" which does require an
575 public void propertyChange(PropertyChangeEvent evt)
577 String propertyName = evt.getPropertyName();
578 if (propertyName.equals(ViewportRanges.STARTSEQ)
579 || (av.getWrapAlignment()
580 && propertyName.equals(ViewportRanges.STARTRES)))
582 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
584 else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
586 fastPaint(((int[]) evt.getNewValue())[1]
587 - ((int[]) evt.getOldValue())[1]);
589 else if (propertyName.equals(ViewportRanges.MOVE_VIEWPORT))