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;
25 import java.awt.Dimension;
27 import java.awt.FontMetrics;
28 import java.awt.Graphics;
29 import java.awt.Graphics2D;
30 import java.awt.RenderingHints;
31 import java.awt.image.BufferedImage;
32 import java.beans.PropertyChangeEvent;
33 import java.util.List;
35 import javax.swing.JPanel;
37 import jalview.datamodel.SequenceI;
38 import jalview.viewmodel.ViewportListenerI;
39 import jalview.viewmodel.ViewportRanges;
47 public class IdCanvas extends JPanel implements ViewportListenerI
49 protected AlignViewport av;
51 protected boolean showScores = true;
53 protected int maxIdLength = -1;
55 protected String maxIdStr = null;
63 boolean fastPaint = false;
65 List<SequenceI> searchResults;
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 if (hiddenRows && av.getShowHiddenMarkers())
143 drawMarker(g, av, i, starty, ypos);
154 public void fastPaint(int vertical)
158 * for now, not attempting fast paint of wrapped ids...
160 if (image == null || av.getWrapAlignment())
167 ViewportRanges ranges = av.getRanges();
169 Graphics2D gg = image.createGraphics();
170 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
171 -vertical * av.getCharHeight());
173 int ss = ranges.getStartSeq();
174 int es = ranges.getEndSeq();
177 if (vertical > 0) // scroll down
181 if (ss < ranges.getStartSeq())
182 { // ie scrolling too fast, more than a page at a time
183 ss = ranges.getStartSeq();
187 transY = imgHeight - ((vertical + 1) * av.getCharHeight());
190 else if (vertical < 0) // scroll up
194 if (es > ranges.getEndSeq())
196 es = ranges.getEndSeq();
200 gg.translate(0, transY);
202 drawIds(gg, av, ss, es, searchResults, true, getWidth());
204 gg.translate(0, -transY);
210 // Call repaint on alignment panel so that repaints from other alignment
211 // panel components can be aggregated. Otherwise performance of the overview
212 // window and others may be adversely affected.
213 av.getAlignPanel().repaint();
223 public void paintComponent(Graphics g)
225 g.setColor(Color.white);
226 g.fillRect(0, 0, getWidth(), getHeight());
231 g.drawImage(image, 0, 0, this);
236 int oldHeight = imgHeight;
238 imgHeight = getHeight();
239 imgHeight -= (imgHeight % av.getCharHeight());
246 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
248 image = new BufferedImage(getWidth(), imgHeight,
249 BufferedImage.TYPE_INT_RGB);
252 Graphics2D gg = image.createGraphics();
254 // Fill in the background
255 gg.setColor(Color.white);
256 gg.fillRect(0, 0, getWidth(), imgHeight);
258 drawIds(gg, av, av.getRanges().getStartSeq(),
259 av.getRanges().getEndSeq(), searchResults, true, getWidth());
263 g.drawImage(image, 0, 0, this);
267 * Draws sequence ids from sequence index startSeq to endSeq (inclusive), with
268 * the font and other display settings configured on the viewport. Ids of
269 * sequences included in the selection are coloured grey, otherwise the
270 * current id colour for the sequence id is used.
273 * @param alignViewport
278 * when false rendering for print
280 * width used to calculate righthand margin - usually
281 * idCanvas.getWidth()
284 void drawIds(Graphics2D g, AlignViewport alignViewport,
285 final int startSeq, final int endSeq, List<SequenceI> selection,
286 boolean forGUI, int panelWidth)
288 Font font = alignViewport.getFont();
289 if (alignViewport.isSeqNameItalics())
291 setIdfont(new Font(font.getName(), Font.ITALIC, font.getSize()));
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(),
315 manuallyAdjusted ? panelWidth : -1, forGUI);
319 // Now draw the id strings
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));
335 fm = g.getFontMetrics();
338 // Selected sequence colours
339 if (selection != null && selection.contains(sequence))
341 currentColor = Color.black;
342 currentTextColor = Color.white;
344 else if ((alignViewport.getSelectionGroup() != null) && alignViewport
345 .getSelectionGroup().getSequences(null).contains(sequence))
347 currentColor = Color.lightGray;
348 currentTextColor = Color.black;
352 currentColor = alignViewport.getSequenceColour(sequence);
353 currentTextColor = Color.black;
356 g.setColor(currentColor);
358 int charHeight = alignViewport.getCharHeight();
359 g.fillRect(0, (i - startSeq) * charHeight, 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,
372 (((i - startSeq) * charHeight) + charHeight)
375 if (hasHiddenRows && av.getShowHiddenMarkers())
377 drawMarker(g, alignViewport, i, startSeq, 0);
383 * Draws sequence ids, and annotation labels if annotations are shown, in
387 * @param alignViewport
390 void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
391 int startSeq, int pageHeight)
393 drawIdsWrapped(g, alignViewport, startSeq, pageHeight, -1, true);
397 * render sequence IDs and annotation labels when wrapped - without GUI junk
404 public void drawIdsWrappedNoGUI(Graphics2D g, AlignViewport av2,
405 int startSeq, int totalHeight)
407 drawIdsWrapped(g, av2, startSeq, totalHeight, -1, false);
410 public void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
411 int startSeq, int pageHeight, int idWidth, boolean forGUI)
413 int alignmentWidth = alignViewport.getAlignment().getWidth();
414 final int alheight = alignViewport.getAlignment().getHeight();
417 * assumption: SeqCanvas.calculateWrappedGeometry has been called
419 SeqCanvas seqCanvas = alignViewport.getAlignPanel()
420 .getSeqPanel().seqCanvas;
422 final int charHeight = alignViewport.getCharHeight();
424 AnnotationLabels labels = null;
425 if (alignViewport.isShowAnnotation())
427 // in wrapped mode, no alignPanel reference is available
428 // FIXME: make the renderer not create a new object in wrapped mode
430 labels = new AnnotationLabels(alignViewport);
433 ViewportRanges ranges = alignViewport.getRanges();
435 int rowSize = ranges.getViewportWidth();
438 * draw repeating sequence ids until out of sequence data or
439 * out of visible space, whichever comes first
441 boolean hasHiddenRows = alignViewport.hasHiddenRows();
442 int ypos = seqCanvas.wrappedSpaceAboveAlignment;
443 int rowStartRes = ranges.getStartRes();
444 while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
446 for (int i = startSeq; i < alheight; i++)
448 SequenceI s = alignViewport.getAlignment().getSequenceAt(i);
449 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
451 g.setFont(getHiddenFont(s, alignViewport));
455 g.setFont(getIdfont());
457 drawIdString(g, hasHiddenRows, s, i, 0, ypos);
460 if (labels != null && alignViewport.isShowAnnotation())
462 int getWidth = getWidth();
463 int thisIdWidth = getWidth;
464 g.translate(0, ypos + (alheight * charHeight));
465 if (!isManuallyAdjusted())
467 int getAnnotationsIdWidth = labels.drawLabels(g, false, -1, false,
468 forGUI, null, false);
469 thisIdWidth = idWidth < 0 ? getAnnotationsIdWidth : idWidth;
470 if (thisIdWidth > getWidth)
472 this.setPreferredSize(
473 new Dimension(thisIdWidth, this.getHeight()));
475 alignViewport.setIdWidth(thisIdWidth);
478 labels.drawComponent(g, false, thisIdWidth, forGUI);
479 g.translate(0, -ypos - (alheight * charHeight));
482 ypos += seqCanvas.wrappedRepeatHeightPx;
483 rowStartRes += rowSize;
488 * Draws a marker (a blue right-pointing triangle) between sequences to
489 * indicate hidden sequences.
492 * @param alignViewport
497 void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex,
498 int starty, int yoffset)
500 SequenceI[] hseqs = alignViewport.getAlignment()
501 .getHiddenSequences().hiddenSequences;
502 // Use this method here instead of calling hiddenSeq adjust
504 int hSize = hseqs.length;
506 int hiddenIndex = seqIndex;
507 int lastIndex = seqIndex - 1;
508 int nextIndex = seqIndex + 1;
510 for (int j = 0; j < hSize; j++)
512 if (hseqs[j] != null)
514 if (j - 1 < hiddenIndex)
518 if (j - 1 < lastIndex)
522 if (j - 1 < nextIndex)
530 * are we below or above the hidden sequences?
532 boolean below = (hiddenIndex > lastIndex + 1);
533 boolean above = (nextIndex > hiddenIndex + 1);
535 g.setColor(Color.blue);
536 int charHeight = av.getCharHeight();
539 * vertices of the triangle, below or above hidden seqs
541 int[] xPoints = new int[] { getWidth() - charHeight,
542 getWidth() - charHeight, getWidth() };
543 int yShift = seqIndex - starty;
547 int[] yPoints = new int[] { yShift * charHeight + yoffset,
548 yShift * charHeight + yoffset + charHeight / 4,
549 yShift * charHeight + yoffset };
550 g.fillPolygon(xPoints, yPoints, 3);
555 int[] yPoints = new int[] { yShift * charHeight + yoffset,
556 yShift * charHeight + yoffset - charHeight / 4,
557 yShift * charHeight + yoffset };
558 g.fillPolygon(xPoints, yPoints, 3);
563 * Answers the standard sequence id font, or a bold font if the sequence is
564 * set as reference or a hidden group representative
567 * @param alignViewport
570 private Font getHiddenFont(SequenceI seq, AlignViewport alignViewport)
572 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
574 return new Font(av.getFont().getName(), Font.BOLD,
575 av.getFont().getSize());
586 public void setHighlighted(List<SequenceI> list)
588 searchResults = list;
592 public Font getIdfont()
597 public void setIdfont(Font idfont)
599 this.idfont = idfont;
603 * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
604 * scrolling and resizing change viewport ranges. Scrolling changes both start
605 * and end points, but resize only changes end values. Here we only want to
606 * fastpaint on a scroll, with resize using a normal paint, so scroll events
607 * are identified as changes to the horizontal or vertical start value.
609 * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
610 * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
611 * provided, but it generates a change of "startres" which does require an
615 public void propertyChange(PropertyChangeEvent evt)
617 String propertyName = evt.getPropertyName();
618 if (propertyName.equals(ViewportRanges.STARTSEQ)
619 || (av.getWrapAlignment()
620 && propertyName.equals(ViewportRanges.STARTRES)))
622 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
624 else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
626 fastPaint(((int[]) evt.getNewValue())[1]
627 - ((int[]) evt.getOldValue())[1]);
629 else if (propertyName.equals(ViewportRanges.MOVE_VIEWPORT))
635 private boolean manuallyAdjusted = false;
637 public boolean isManuallyAdjusted()
639 return manuallyAdjusted;
642 public void setManuallyAdjusted(boolean b)
644 manuallyAdjusted = b;