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;
68 AnnotationLabels labels = null;
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 gg, 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 gg.setColor(Color.black);
114 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
116 gg.setColor(Color.white);
118 else if ((av.getSelectionGroup() != null)
119 && av.getSelectionGroup().getSequences(null).contains(s))
121 gg.setColor(Color.lightGray);
122 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
124 gg.setColor(Color.white);
128 gg.setColor(av.getSequenceColour(s));
129 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
131 gg.setColor(Color.black);
134 if (av.isRightAlignIds())
137 - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
140 gg.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
141 (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
145 drawMarker(i, starty, ypos);
156 public void fastPaint(int vertical)
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);
201 gg.translate(0, -transY);
214 public void paintComponent(Graphics g)
216 g.setColor(Color.white);
217 g.fillRect(0, 0, getWidth(), getHeight());
222 g.drawImage(image, 0, 0, this);
227 int oldHeight = imgHeight;
229 imgHeight = getHeight();
230 imgHeight -= (imgHeight % av.getCharHeight());
237 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
239 image = new BufferedImage(getWidth(), imgHeight,
240 BufferedImage.TYPE_INT_RGB);
243 gg = (Graphics2D) image.getGraphics();
245 // Fill in the background
246 gg.setColor(Color.white);
247 gg.fillRect(0, 0, getWidth(), imgHeight);
249 drawIds(av.getRanges().getStartSeq(), av.getRanges().getEndSeq());
251 g.drawImage(image, 0, 0, this);
262 void drawIds(int starty, int endy)
264 if (av.isSeqNameItalics())
266 setIdfont(new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
271 setIdfont(av.getFont());
274 gg.setFont(getIdfont());
275 fm = gg.getFontMetrics();
279 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
280 RenderingHints.VALUE_ANTIALIAS_ON);
283 Color currentColor = Color.white;
284 Color currentTextColor = Color.black;
286 final boolean doHiddenCheck = av.isDisplayReferenceSeq()
287 || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows();
289 if (av.getWrapAlignment())
291 int maxwidth = av.getAlignment().getWidth();
292 int alheight = av.getAlignment().getHeight();
294 if (av.hasHiddenColumns())
296 maxwidth = av.getAlignment().getHiddenColumns()
297 .findColumnPosition(maxwidth) - 1;
300 int annotationHeight = 0;
302 if (av.isShowAnnotation())
306 ap = new AnnotationPanel(av);
309 annotationHeight = ap.adjustPanelHeight();
312 labels = new AnnotationLabels(av);
316 int hgap = av.getCharHeight();
317 if (av.getScaleAboveWrapped())
319 hgap += av.getCharHeight();
322 int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
324 int rowSize = av.getRanges().getEndRes()
325 - av.getRanges().getStartRes();
327 // Draw the rest of the panels
328 for (int ypos = hgap, row = av.getRanges().getStartRes(); (ypos <= getHeight())
329 && (row < maxwidth); ypos += cHeight, row += rowSize)
331 for (int i = starty; i < alheight; i++)
333 SequenceI s = av.getAlignment().getSequenceAt(i);
340 gg.setFont(getIdfont());
343 drawIdString(gg, hiddenRows, s, i, 0, ypos);
346 if (labels != null && av.isShowAnnotation())
348 gg.translate(0, ypos + (alheight * av.getCharHeight()));
349 labels.drawComponent(gg, getWidth());
350 gg.translate(0, -ypos - (alheight * av.getCharHeight()));
356 // No need to hang on to labels if we're not wrapped
359 // Now draw the id strings
360 int panelWidth = getWidth();
364 // Now draw the id strings
365 for (int i = starty; i <= endy; i++)
367 sequence = av.getAlignment().getSequenceAt(i);
369 if (sequence == null)
376 setHiddenFont(sequence);
379 // Selected sequence colours
380 if ((searchResults != null) && searchResults.contains(sequence))
382 currentColor = Color.black;
383 currentTextColor = Color.white;
385 else if ((av.getSelectionGroup() != null)
386 && av.getSelectionGroup().getSequences(null)
389 currentColor = Color.lightGray;
390 currentTextColor = Color.black;
394 currentColor = av.getSequenceColour(sequence);
395 currentTextColor = Color.black;
398 gg.setColor(currentColor);
400 gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
403 gg.setColor(currentTextColor);
405 String string = sequence.getDisplayId(av.getShowJVSuffix());
407 if (av.isRightAlignIds())
409 xPos = panelWidth - fm.stringWidth(string) - 4;
412 gg.drawString(string, xPos,
413 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
414 - (av.getCharHeight() / 5));
418 drawMarker(i, starty, 0);
426 void drawMarker(int i, int starty, int yoffset)
429 SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
430 // Use this method here instead of calling hiddenSeq adjust
432 int hSize = hseqs.length;
435 int lastIndex = i - 1;
436 int nextIndex = i + 1;
438 for (int j = 0; j < hSize; j++)
440 if (hseqs[j] != null)
442 if (j - 1 < hiddenIndex)
446 if (j - 1 < lastIndex)
450 if (j - 1 < nextIndex)
457 boolean below = (hiddenIndex > lastIndex + 1);
458 boolean above = (nextIndex > hiddenIndex + 1);
460 gg.setColor(Color.blue);
464 new int[] { getWidth() - av.getCharHeight(),
465 getWidth() - av.getCharHeight(), getWidth() },
467 (i - starty) * av.getCharHeight() + yoffset,
468 (i - starty) * av.getCharHeight() + yoffset
469 + av.getCharHeight() / 4,
470 (i - starty) * av.getCharHeight() + yoffset }, 3);
475 new int[] { getWidth() - av.getCharHeight(),
476 getWidth() - av.getCharHeight(), getWidth() },
478 (i - starty + 1) * av.getCharHeight() + yoffset,
479 (i - starty + 1) * av.getCharHeight() + yoffset
480 - av.getCharHeight() / 4,
481 (i - starty + 1) * av.getCharHeight() + yoffset }, 3);
486 void setHiddenFont(SequenceI seq)
488 Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
491 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
497 gg.setFont(getIdfont());
507 public void setHighlighted(List<SequenceI> list)
509 searchResults = list;
513 public Font getIdfont()
518 public void setIdfont(Font idfont)
520 this.idfont = idfont;
524 public void propertyChange(PropertyChangeEvent evt)
526 // Respond to viewport range changes (e.g. alignment panel was scrolled)
527 if (evt.getPropertyName().equals("startseq"))
529 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
531 else if (evt.getPropertyName().equals("endseq"))
533 // resize - do nothing