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.getColumnSelection().findColumnPosition(maxwidth) - 1;
299 int annotationHeight = 0;
301 if (av.isShowAnnotation())
305 ap = new AnnotationPanel(av);
308 annotationHeight = ap.adjustPanelHeight();
311 labels = new AnnotationLabels(av);
315 int hgap = av.getCharHeight();
316 if (av.getScaleAboveWrapped())
318 hgap += av.getCharHeight();
321 int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
323 int rowSize = av.getRanges().getEndRes()
324 - av.getRanges().getStartRes();
326 // Draw the rest of the panels
327 for (int ypos = hgap, row = av.getRanges().getStartRes(); (ypos <= getHeight())
328 && (row < maxwidth); ypos += cHeight, row += rowSize)
330 for (int i = starty; i < alheight; i++)
332 SequenceI s = av.getAlignment().getSequenceAt(i);
339 gg.setFont(getIdfont());
342 drawIdString(gg, hiddenRows, s, i, 0, ypos);
345 if (labels != null && av.isShowAnnotation())
347 gg.translate(0, ypos + (alheight * av.getCharHeight()));
348 labels.drawComponent(gg, getWidth());
349 gg.translate(0, -ypos - (alheight * av.getCharHeight()));
355 // No need to hang on to labels if we're not wrapped
358 // Now draw the id strings
359 int panelWidth = getWidth();
363 // Now draw the id strings
364 for (int i = starty; i <= endy; i++)
366 sequence = av.getAlignment().getSequenceAt(i);
368 if (sequence == null)
375 setHiddenFont(sequence);
378 // Selected sequence colours
379 if ((searchResults != null) && searchResults.contains(sequence))
381 currentColor = Color.black;
382 currentTextColor = Color.white;
384 else if ((av.getSelectionGroup() != null)
385 && av.getSelectionGroup().getSequences(null)
388 currentColor = Color.lightGray;
389 currentTextColor = Color.black;
393 currentColor = av.getSequenceColour(sequence);
394 currentTextColor = Color.black;
397 gg.setColor(currentColor);
399 gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
402 gg.setColor(currentTextColor);
404 String string = sequence.getDisplayId(av.getShowJVSuffix());
406 if (av.isRightAlignIds())
408 xPos = panelWidth - fm.stringWidth(string) - 4;
411 gg.drawString(string, xPos,
412 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
413 - (av.getCharHeight() / 5));
417 drawMarker(i, starty, 0);
425 void drawMarker(int i, int starty, int yoffset)
428 SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
429 // Use this method here instead of calling hiddenSeq adjust
431 int hSize = hseqs.length;
434 int lastIndex = i - 1;
435 int nextIndex = i + 1;
437 for (int j = 0; j < hSize; j++)
439 if (hseqs[j] != null)
441 if (j - 1 < hiddenIndex)
445 if (j - 1 < lastIndex)
449 if (j - 1 < nextIndex)
456 boolean below = (hiddenIndex > lastIndex + 1);
457 boolean above = (nextIndex > hiddenIndex + 1);
459 gg.setColor(Color.blue);
463 new int[] { getWidth() - av.getCharHeight(),
464 getWidth() - av.getCharHeight(), getWidth() },
466 (i - starty) * av.getCharHeight() + yoffset,
467 (i - starty) * av.getCharHeight() + yoffset
468 + av.getCharHeight() / 4,
469 (i - starty) * av.getCharHeight() + yoffset }, 3);
474 new int[] { getWidth() - av.getCharHeight(),
475 getWidth() - av.getCharHeight(), getWidth() },
477 (i - starty + 1) * av.getCharHeight() + yoffset,
478 (i - starty + 1) * av.getCharHeight() + yoffset
479 - av.getCharHeight() / 4,
480 (i - starty + 1) * av.getCharHeight() + yoffset }, 3);
485 void setHiddenFont(SequenceI seq)
487 Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
490 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
496 gg.setFont(getIdfont());
506 public void setHighlighted(List<SequenceI> list)
508 searchResults = list;
512 public Font getIdfont()
517 public void setIdfont(Font idfont)
519 this.idfont = idfont;
523 public void propertyChange(PropertyChangeEvent evt)
525 // Respond to viewport range changes (e.g. alignment panel was scrolled)
526 if (evt.getPropertyName().equals("startseq")
527 || evt.getPropertyName().equals("endseq"))
529 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());