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;
39 import jalview.viewmodel.seqfeatures.IdColumn;
40 import jalview.viewmodel.seqfeatures.IdColumns;
41 import jalview.viewmodel.seqfeatures.IdColumns.ColumnCell;
49 public class IdCanvas extends JPanel implements ViewportListenerI
51 protected AlignViewport av;
53 protected boolean showScores = true;
55 protected int maxIdLength = -1;
57 protected String maxIdStr = null;
65 boolean fastPaint = false;
67 List<SequenceI> searchResults;
74 * Creates a new IdCanvas object.
79 public IdCanvas(AlignViewport av)
81 setLayout(new BorderLayout());
83 PaintRefresher.Register(this, av.getSequenceSetId());
84 av.getRanges().addPropertyChangeListener(this);
93 * true - check and display hidden row marker if need be
103 public void drawIdString(Graphics2D g, boolean hiddenRows, SequenceI s,
104 int i, int starty, int ypos)
107 int panelWidth = getWidth();
108 int charHeight = av.getCharHeight();
110 if ((searchResults != null) && searchResults.contains(s))
112 g.setColor(Color.black);
113 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
115 g.setColor(Color.white);
117 else if ((av.getSelectionGroup() != null)
118 && av.getSelectionGroup().getSequences(null).contains(s))
120 g.setColor(Color.lightGray);
121 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
123 g.setColor(Color.white);
127 g.setColor(av.getSequenceColour(s));
128 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
130 g.setColor(Color.black);
133 if (av.isRightAlignIds())
135 FontMetrics fm = g.getFontMetrics();
137 - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
140 g.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
141 (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
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 g.setColor(Color.white);
228 g.fillRect(0, 0, getWidth(), getHeight());
233 g.drawImage(image, 0, 0, this);
238 int oldHeight = imgHeight;
240 imgHeight = getHeight();
241 imgHeight -= (imgHeight % av.getCharHeight());
248 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
250 image = new BufferedImage(getWidth(), imgHeight,
251 BufferedImage.TYPE_INT_RGB);
254 Graphics2D gg = image.createGraphics();
256 // Fill in the background
257 gg.setColor(Color.white);
258 gg.fillRect(0, 0, getWidth(), imgHeight);
260 drawIds(gg, av, av.getRanges().getStartSeq(),
261 av.getRanges().getEndSeq(), searchResults);
265 g.drawImage(image, 0, 0, this);
269 * Draws sequence ids from sequence index startSeq to endSeq (inclusive), with
270 * the font and other display settings configured on the viewport. Ids of
271 * sequences included in the selection are coloured grey, otherwise the
272 * current id colour for the sequence id is used.
275 * @param alignViewport
280 void drawIds(Graphics2D g, AlignViewport alignViewport,
281 final int startSeq, final int endSeq, List<SequenceI> selection)
283 Font font = alignViewport.getFont();
284 if (alignViewport.isSeqNameItalics())
286 setIdfont(new Font(font.getName(), Font.ITALIC, font.getSize()));
293 g.setFont(getIdfont());
294 FontMetrics fm = g.getFontMetrics();
296 if (alignViewport.antiAlias)
298 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
299 RenderingHints.VALUE_ANTIALIAS_ON);
302 Color currentColor = Color.white;
303 Color currentTextColor = Color.black;
305 boolean hasHiddenRows = alignViewport.hasHiddenRows();
307 if (alignViewport.getWrapAlignment())
309 drawIdsWrapped(g, alignViewport, startSeq, getHeight());
313 // Now draw the id strings
314 int fullPanelWidth = getWidth();
316 IdColumns id_cols = alignViewport.getIdColumns();
317 List<IdColumn> visible = id_cols.getVisible();
319 * width of an idColumn
322 int panelWidth = Math.max(fullPanelWidth / 2,
323 fullPanelWidth - (colWid * visible.size()));
325 // Now draw the id strings
326 for (int i = startSeq; i <= endSeq; i++)
329 SequenceI sequence = alignViewport.getAlignment().getSequenceAt(i);
331 if (sequence == null)
335 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
337 g.setFont(getHiddenFont(sequence, alignViewport));
338 fm = g.getFontMetrics();
341 // Selected sequence colours
342 if (selection != null && selection.contains(sequence))
344 currentColor = Color.black;
345 currentTextColor = Color.white;
347 else if ((alignViewport.getSelectionGroup() != null) && alignViewport
348 .getSelectionGroup().getSequences(null).contains(sequence))
350 currentColor = Color.lightGray;
351 currentTextColor = Color.black;
355 currentColor = alignViewport.getSequenceColour(sequence);
356 currentTextColor = Color.black;
359 g.setColor(currentColor);
361 int charHeight = alignViewport.getCharHeight();
362 g.fillRect(0, (i - startSeq) * charHeight, getWidth(), charHeight);
364 g.setColor(currentTextColor);
366 String string = sequence
367 .getDisplayId(alignViewport.getShowJVSuffix());
369 if (alignViewport.isRightAlignIds())
371 xPos = panelWidth - fm.stringWidth(string) - 4;
374 g.drawString(string, xPos,
375 (((i - startSeq) * charHeight) + charHeight)
378 if (visible != null && visible.size() > 0)
382 xPos = panelWidth + 2;
383 for (IdColumn col : visible)
385 ColumnCell col_cell = id_cols.getCellFor(sequence, col);
386 if (col_cell == null)
388 g.setColor(Color.gray);
389 g.fillRect(xPos + 1, (i - startSeq) * charHeight,
390 xPos + colWid - 3, charHeight);
394 g.setColor(col_cell.bg);
395 g.fillRect(xPos + 1, (i - startSeq) * charHeight,
396 xPos + colWid - 3, charHeight);
397 g.setColor(col_cell.fg);
398 g.drawString(col_cell.label, xPos,
399 (((i - startSeq) * charHeight) + charHeight)
403 g.setColor(currentTextColor);
405 } catch (Exception q)
409 if (hasHiddenRows && av.getShowHiddenMarkers())
411 drawMarker(g, alignViewport, i, startSeq, 0);
417 * Draws sequence ids, and annotation labels if annotations are shown, in
421 * @param alignViewport
424 void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
425 int startSeq, int pageHeight)
427 int alignmentWidth = alignViewport.getAlignment().getWidth();
428 final int alheight = alignViewport.getAlignment().getHeight();
431 * assumption: SeqCanvas.calculateWrappedGeometry has been called
433 SeqCanvas seqCanvas = alignViewport.getAlignPanel()
434 .getSeqPanel().seqCanvas;
436 final int charHeight = alignViewport.getCharHeight();
438 AnnotationLabels labels = null;
439 if (alignViewport.isShowAnnotation())
441 labels = new AnnotationLabels(alignViewport);
444 ViewportRanges ranges = alignViewport.getRanges();
446 int rowSize = ranges.getViewportWidth();
449 * draw repeating sequence ids until out of sequence data or
450 * out of visible space, whichever comes first
452 boolean hasHiddenRows = alignViewport.hasHiddenRows();
453 int ypos = seqCanvas.wrappedSpaceAboveAlignment;
454 int rowStartRes = ranges.getStartRes();
455 while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
457 for (int i = startSeq; i < alheight; i++)
459 SequenceI s = alignViewport.getAlignment().getSequenceAt(i);
460 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
462 g.setFont(getHiddenFont(s, alignViewport));
466 g.setFont(getIdfont());
468 drawIdString(g, hasHiddenRows, s, i, 0, ypos);
471 if (labels != null && alignViewport.isShowAnnotation())
473 g.translate(0, ypos + (alheight * charHeight));
474 labels.drawComponent(g, getWidth());
475 g.translate(0, -ypos - (alheight * charHeight));
478 ypos += seqCanvas.wrappedRepeatHeightPx;
479 rowStartRes += rowSize;
484 * Draws a marker (a blue right-pointing triangle) between sequences to
485 * indicate hidden sequences.
488 * @param alignViewport
493 void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex,
494 int starty, int yoffset)
496 SequenceI[] hseqs = alignViewport.getAlignment()
497 .getHiddenSequences().hiddenSequences;
498 // Use this method here instead of calling hiddenSeq adjust
500 int hSize = hseqs.length;
502 int hiddenIndex = seqIndex;
503 int lastIndex = seqIndex - 1;
504 int nextIndex = seqIndex + 1;
506 for (int j = 0; j < hSize; j++)
508 if (hseqs[j] != null)
510 if (j - 1 < hiddenIndex)
514 if (j - 1 < lastIndex)
518 if (j - 1 < nextIndex)
526 * are we below or above the hidden sequences?
528 boolean below = (hiddenIndex > lastIndex + 1);
529 boolean above = (nextIndex > hiddenIndex + 1);
531 g.setColor(Color.blue);
532 int charHeight = av.getCharHeight();
535 * vertices of the triangle, below or above hidden seqs
537 int[] xPoints = new int[] { getWidth() - charHeight,
538 getWidth() - charHeight, getWidth() };
539 int yShift = seqIndex - starty;
543 int[] yPoints = new int[] { yShift * charHeight + yoffset,
544 yShift * charHeight + yoffset + charHeight / 4,
545 yShift * charHeight + yoffset };
546 g.fillPolygon(xPoints, yPoints, 3);
551 int[] yPoints = new int[] { yShift * charHeight + yoffset,
552 yShift * charHeight + yoffset - charHeight / 4,
553 yShift * charHeight + yoffset };
554 g.fillPolygon(xPoints, yPoints, 3);
559 * Answers the standard sequence id font, or a bold font if the sequence is
560 * set as reference or a hidden group representative
563 * @param alignViewport
566 private Font getHiddenFont(SequenceI seq, AlignViewport alignViewport)
568 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
570 return new Font(av.getFont().getName(), Font.BOLD,
571 av.getFont().getSize());
582 public void setHighlighted(List<SequenceI> list)
584 searchResults = list;
588 public Font getIdfont()
593 public void setIdfont(Font idfont)
595 this.idfont = idfont;
599 * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
600 * scrolling and resizing change viewport ranges. Scrolling changes both start
601 * and end points, but resize only changes end values. Here we only want to
602 * fastpaint on a scroll, with resize using a normal paint, so scroll events
603 * are identified as changes to the horizontal or vertical start value.
605 * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
606 * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
607 * provided, but it generates a change of "startres" which does require an
611 public void propertyChange(PropertyChangeEvent evt)
613 String propertyName = evt.getPropertyName();
614 if (propertyName.equals(ViewportRanges.STARTSEQ)
615 || (av.getWrapAlignment()
616 && propertyName.equals(ViewportRanges.STARTRES)))
618 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
620 else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
622 fastPaint(((int[]) evt.getNewValue())[1]
623 - ((int[]) evt.getOldValue())[1]);
625 else if (propertyName.equals(ViewportRanges.MOVE_VIEWPORT))