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;
40 import jalview.viewmodel.seqfeatures.IdColumn;
41 import jalview.viewmodel.seqfeatures.IdColumns;
42 import jalview.viewmodel.seqfeatures.IdColumns.ColumnCell;
50 public class IdCanvas extends JPanel implements ViewportListenerI
52 protected AlignViewport av;
54 protected boolean showScores = true;
56 protected int maxIdLength = -1;
58 protected String maxIdStr = null;
66 boolean fastPaint = false;
68 List<SequenceI> searchResults;
75 * Creates a new IdCanvas object.
80 public IdCanvas(AlignViewport av)
82 setLayout(new BorderLayout());
84 PaintRefresher.Register(this, av.getSequenceSetId());
85 av.getRanges().addPropertyChangeListener(this);
94 * true - check and display hidden row marker if need be
104 public void drawIdString(Graphics2D g, boolean hiddenRows, SequenceI s,
105 int i, int starty, int ypos)
108 int panelWidth = getWidth();
109 int charHeight = av.getCharHeight();
111 if ((searchResults != null) && searchResults.contains(s))
113 g.setColor(Color.black);
114 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
116 g.setColor(Color.white);
118 else if ((av.getSelectionGroup() != null)
119 && av.getSelectionGroup().getSequences(null).contains(s))
121 g.setColor(Color.lightGray);
122 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
124 g.setColor(Color.white);
128 g.setColor(av.getSequenceColour(s));
129 g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
131 g.setColor(Color.black);
134 if (av.isRightAlignIds())
136 FontMetrics fm = g.getFontMetrics();
138 - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
141 g.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
142 (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
144 if (hiddenRows && av.getShowHiddenMarkers())
146 drawMarker(g, av, i, starty, ypos);
157 public void fastPaint(int vertical)
161 * for now, not attempting fast paint of wrapped ids...
163 if (image == null || av.getWrapAlignment())
170 ViewportRanges ranges = av.getRanges();
172 Graphics2D gg = image.createGraphics();
173 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
174 -vertical * av.getCharHeight());
176 int ss = ranges.getStartSeq();
177 int es = ranges.getEndSeq();
180 if (vertical > 0) // scroll down
184 if (ss < ranges.getStartSeq())
185 { // ie scrolling too fast, more than a page at a time
186 ss = ranges.getStartSeq();
190 transY = imgHeight - ((vertical + 1) * av.getCharHeight());
193 else if (vertical < 0) // scroll up
197 if (es > ranges.getEndSeq())
199 es = ranges.getEndSeq();
203 gg.translate(0, transY);
205 drawIds(gg, av, ss, es, searchResults,true);
207 gg.translate(0, -transY);
213 // Call repaint on alignment panel so that repaints from other alignment
214 // panel components can be aggregated. Otherwise performance of the overview
215 // window and others may be adversely affected.
216 av.getAlignPanel().repaint();
226 public void paintComponent(Graphics g)
228 g.setColor(Color.white);
229 g.fillRect(0, 0, getWidth(), getHeight());
234 g.drawImage(image, 0, 0, this);
239 int oldHeight = imgHeight;
241 imgHeight = getHeight();
242 imgHeight -= (imgHeight % av.getCharHeight());
249 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
251 image = new BufferedImage(getWidth(), imgHeight,
252 BufferedImage.TYPE_INT_RGB);
255 Graphics2D gg = image.createGraphics();
257 // Fill in the background
258 gg.setColor(Color.white);
259 gg.fillRect(0, 0, getWidth(), imgHeight);
261 drawIds(gg, av, av.getRanges().getStartSeq(),
262 av.getRanges().getEndSeq(), searchResults,true);
266 g.drawImage(image, 0, 0, this);
270 * Draws sequence ids from sequence index startSeq to endSeq (inclusive), with
271 * the font and other display settings configured on the viewport. Ids of
272 * sequences included in the selection are coloured grey, otherwise the
273 * current id colour for the sequence id is used.
276 * @param alignViewport
281 void drawIds(Graphics2D g, AlignViewport alignViewport,
282 final int startSeq, final int endSeq, List<SequenceI> selection, boolean forGUI)
284 Font font = alignViewport.getFont();
285 if (alignViewport.isSeqNameItalics())
287 setIdfont(new Font(font.getName(), Font.ITALIC, font.getSize()));
294 g.setFont(getIdfont());
295 FontMetrics fm = g.getFontMetrics();
297 if (alignViewport.antiAlias)
299 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
300 RenderingHints.VALUE_ANTIALIAS_ON);
303 Color currentColor = Color.white;
304 Color currentTextColor = Color.black;
306 boolean hasHiddenRows = alignViewport.hasHiddenRows();
308 if (alignViewport.getWrapAlignment())
310 drawIdsWrapped(g, alignViewport, startSeq, getHeight());
314 // Now draw the id strings
315 int fullPanelWidth = getWidth();
317 IdColumns id_cols = alignViewport.getIdColumns();
318 List<IdColumn> visible = id_cols.getVisible();
320 * width of an idColumn
323 int panelWidth = Math.max(fullPanelWidth / 2,
324 fullPanelWidth - (colWid * visible.size()));
326 // Now draw the id strings
327 for (int i = startSeq; i <= endSeq; i++)
330 SequenceI sequence = alignViewport.getAlignment().getSequenceAt(i);
332 if (sequence == null)
336 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
338 g.setFont(getHiddenFont(sequence, alignViewport));
339 fm = g.getFontMetrics();
342 // Selected sequence colours
343 if (selection != null && selection.contains(sequence))
345 currentColor = Color.black;
346 currentTextColor = Color.white;
348 else if ((alignViewport.getSelectionGroup() != null) && alignViewport
349 .getSelectionGroup().getSequences(null).contains(sequence))
351 currentColor = Color.lightGray;
352 currentTextColor = Color.black;
356 currentColor = alignViewport.getSequenceColour(sequence);
357 currentTextColor = Color.black;
360 g.setColor(currentColor);
362 int charHeight = alignViewport.getCharHeight();
363 g.fillRect(0, (i - startSeq) * charHeight, getWidth(), charHeight);
365 g.setColor(currentTextColor);
367 String string = sequence
368 .getDisplayId(alignViewport.getShowJVSuffix());
370 if (alignViewport.isRightAlignIds())
372 xPos = panelWidth - fm.stringWidth(string) - 4;
375 g.drawString(string, xPos,
376 (((i - startSeq) * charHeight) + charHeight)
379 if (visible != null && visible.size() > 0)
383 xPos = panelWidth + 2;
384 for (IdColumn col : visible)
386 ColumnCell col_cell = id_cols.getCellFor(sequence, col);
387 if (col_cell == null)
389 g.setColor(Color.gray);
390 g.fillRect(xPos + 1, (i - startSeq) * charHeight,
391 xPos + colWid - 3, charHeight);
395 g.setColor(col_cell.bg);
396 g.fillRect(xPos + 1, (i - startSeq) * charHeight,
397 xPos + colWid - 3, charHeight);
398 g.setColor(col_cell.fg);
399 g.drawString(col_cell.label, xPos,
400 (((i - startSeq) * charHeight) + charHeight)
404 g.setColor(currentTextColor);
406 } catch (Exception q)
410 if (hasHiddenRows && av.getShowHiddenMarkers())
412 drawMarker(g, alignViewport, i, startSeq, 0);
418 * Draws sequence ids, and annotation labels if annotations are shown, in
422 * @param alignViewport
425 void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
426 int startSeq, int pageHeight)
428 drawIdsWrapped(g, alignViewport, startSeq, pageHeight, -1, true);
432 * render sequence IDs and annotation labels when wrapped - without GUI junk
438 public void drawIdsWrappedNoGUI(Graphics2D g, AlignViewport av2, int i,
441 drawIdsWrapped(g, av2, totalHeight, totalHeight, i,false);
444 void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
445 int startSeq, int pageHeight, int idWidth, boolean forGUI)
447 int alignmentWidth = alignViewport.getAlignment().getWidth();
448 final int alheight = alignViewport.getAlignment().getHeight();
451 * assumption: SeqCanvas.calculateWrappedGeometry has been called
453 SeqCanvas seqCanvas = alignViewport.getAlignPanel()
454 .getSeqPanel().seqCanvas;
456 final int charHeight = alignViewport.getCharHeight();
458 AnnotationLabels labels = null;
459 if (alignViewport.isShowAnnotation())
461 labels = new AnnotationLabels(alignViewport);
464 ViewportRanges ranges = alignViewport.getRanges();
466 int rowSize = ranges.getViewportWidth();
469 * draw repeating sequence ids until out of sequence data or
470 * out of visible space, whichever comes first
472 boolean hasHiddenRows = alignViewport.hasHiddenRows();
473 int ypos = seqCanvas.wrappedSpaceAboveAlignment;
474 int rowStartRes = ranges.getStartRes();
475 while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
477 for (int i = startSeq; i < alheight; i++)
479 SequenceI s = alignViewport.getAlignment().getSequenceAt(i);
480 if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
482 g.setFont(getHiddenFont(s, alignViewport));
486 g.setFont(getIdfont());
488 drawIdString(g, hasHiddenRows, s, i, 0, ypos);
491 if (labels != null && alignViewport.isShowAnnotation())
493 int getWidth = getWidth();
494 int thisIdWidth = getWidth;
495 g.translate(0, ypos + (alheight * charHeight));
496 if (!manuallyAdjusted())
498 int getAnnotationsIdWidth = labels.drawLabels(g, false, -1, false,forGUI,
500 thisIdWidth = idWidth < 0 ? getAnnotationsIdWidth : idWidth;
501 if (thisIdWidth > getWidth)
503 this.setPreferredSize(
504 new Dimension(thisIdWidth, this.getHeight()));
506 alignViewport.setIdWidth(thisIdWidth);
509 labels.drawComponent(g, false, thisIdWidth, forGUI);
510 g.translate(0, -ypos - (alheight * charHeight));
513 ypos += seqCanvas.wrappedRepeatHeightPx;
514 rowStartRes += rowSize;
519 * Draws a marker (a blue right-pointing triangle) between sequences to
520 * indicate hidden sequences.
523 * @param alignViewport
528 void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex,
529 int starty, int yoffset)
531 SequenceI[] hseqs = alignViewport.getAlignment()
532 .getHiddenSequences().hiddenSequences;
533 // Use this method here instead of calling hiddenSeq adjust
535 int hSize = hseqs.length;
537 int hiddenIndex = seqIndex;
538 int lastIndex = seqIndex - 1;
539 int nextIndex = seqIndex + 1;
541 for (int j = 0; j < hSize; j++)
543 if (hseqs[j] != null)
545 if (j - 1 < hiddenIndex)
549 if (j - 1 < lastIndex)
553 if (j - 1 < nextIndex)
561 * are we below or above the hidden sequences?
563 boolean below = (hiddenIndex > lastIndex + 1);
564 boolean above = (nextIndex > hiddenIndex + 1);
566 g.setColor(Color.blue);
567 int charHeight = av.getCharHeight();
570 * vertices of the triangle, below or above hidden seqs
572 int[] xPoints = new int[] { getWidth() - charHeight,
573 getWidth() - charHeight, getWidth() };
574 int yShift = seqIndex - starty;
578 int[] yPoints = new int[] { yShift * charHeight + yoffset,
579 yShift * charHeight + yoffset + charHeight / 4,
580 yShift * charHeight + yoffset };
581 g.fillPolygon(xPoints, yPoints, 3);
586 int[] yPoints = new int[] { yShift * charHeight + yoffset,
587 yShift * charHeight + yoffset - charHeight / 4,
588 yShift * charHeight + yoffset };
589 g.fillPolygon(xPoints, yPoints, 3);
594 * Answers the standard sequence id font, or a bold font if the sequence is
595 * set as reference or a hidden group representative
598 * @param alignViewport
601 private Font getHiddenFont(SequenceI seq, AlignViewport alignViewport)
603 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
605 return new Font(av.getFont().getName(), Font.BOLD,
606 av.getFont().getSize());
617 public void setHighlighted(List<SequenceI> list)
619 searchResults = list;
623 public Font getIdfont()
628 public void setIdfont(Font idfont)
630 this.idfont = idfont;
634 * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
635 * scrolling and resizing change viewport ranges. Scrolling changes both start
636 * and end points, but resize only changes end values. Here we only want to
637 * fastpaint on a scroll, with resize using a normal paint, so scroll events
638 * are identified as changes to the horizontal or vertical start value.
640 * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
641 * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
642 * provided, but it generates a change of "startres" which does require an
646 public void propertyChange(PropertyChangeEvent evt)
648 String propertyName = evt.getPropertyName();
649 if (propertyName.equals(ViewportRanges.STARTSEQ)
650 || (av.getWrapAlignment()
651 && propertyName.equals(ViewportRanges.STARTRES)))
653 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
655 else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
657 fastPaint(((int[]) evt.getNewValue())[1]
658 - ((int[]) evt.getOldValue())[1]);
660 else if (propertyName.equals(ViewportRanges.MOVE_VIEWPORT))
666 private boolean manuallyAdjusted = false;
668 public boolean manuallyAdjusted()
670 return manuallyAdjusted;
673 public void setManuallyAdjusted(boolean b)
675 manuallyAdjusted = b;