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.ViewportRanges;
26 import java.awt.BorderLayout;
27 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.RenderingHints;
33 import java.awt.image.BufferedImage;
34 import java.util.List;
36 import javax.swing.JPanel;
44 public class IdCanvas extends JPanel
46 protected AlignViewport av;
48 protected boolean showScores = true;
50 protected int maxIdLength = -1;
52 protected String maxIdStr = null;
60 boolean fastPaint = false;
62 List<SequenceI> searchResults;
66 AnnotationLabels labels = null;
73 * Creates a new IdCanvas object.
78 public IdCanvas(AlignViewport av)
80 setLayout(new BorderLayout());
82 PaintRefresher.Register(this, av.getSequenceSetId());
91 * true - check and display hidden row marker if need be
101 public void drawIdString(Graphics2D gg, 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 gg.setColor(Color.black);
111 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
113 gg.setColor(Color.white);
115 else if ((av.getSelectionGroup() != null)
116 && av.getSelectionGroup().getSequences(null).contains(s))
118 gg.setColor(Color.lightGray);
119 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
121 gg.setColor(Color.white);
125 gg.setColor(av.getSequenceColour(s));
126 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
128 gg.setColor(Color.black);
131 if (av.isRightAlignIds())
134 - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
137 gg.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
138 (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
142 drawMarker(i, starty, ypos);
153 public void fastPaint(int vertical)
162 ViewportRanges ranges = av.getRanges();
164 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
165 -vertical * av.getCharHeight());
167 int ss = ranges.getStartSeq();
168 int es = ranges.getEndSeq();
171 if (vertical > 0) // scroll down
175 if (ss < ranges.getStartSeq())
176 { // ie scrolling too fast, more than a page at a time
177 ss = ranges.getStartSeq();
181 transY = imgHeight - ((vertical + 1) * av.getCharHeight());
184 else if (vertical < 0) // scroll up
188 if (es > ranges.getEndSeq())
190 es = ranges.getEndSeq();
194 gg.translate(0, transY);
198 gg.translate(0, -transY);
211 public void paintComponent(Graphics g)
213 g.setColor(Color.white);
214 g.fillRect(0, 0, getWidth(), getHeight());
219 g.drawImage(image, 0, 0, this);
224 int oldHeight = imgHeight;
226 imgHeight = getHeight();
227 imgHeight -= (imgHeight % av.getCharHeight());
234 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
236 image = new BufferedImage(getWidth(), imgHeight,
237 BufferedImage.TYPE_INT_RGB);
240 gg = (Graphics2D) image.getGraphics();
242 // Fill in the background
243 gg.setColor(Color.white);
244 gg.fillRect(0, 0, getWidth(), imgHeight);
246 drawIds(av.getRanges().getStartSeq(), av.getRanges().getEndSeq());
248 g.drawImage(image, 0, 0, this);
259 void drawIds(int starty, int endy)
261 if (av.isSeqNameItalics())
263 setIdfont(new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
268 setIdfont(av.getFont());
271 gg.setFont(getIdfont());
272 fm = gg.getFontMetrics();
276 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
277 RenderingHints.VALUE_ANTIALIAS_ON);
280 Color currentColor = Color.white;
281 Color currentTextColor = Color.black;
283 final boolean doHiddenCheck = av.isDisplayReferenceSeq()
284 || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows();
286 if (av.getWrapAlignment())
288 int maxwidth = av.getAlignment().getWidth();
289 int alheight = av.getAlignment().getHeight();
291 if (av.hasHiddenColumns())
293 maxwidth = av.getAlignment().getHiddenColumns()
294 .findColumnPosition(maxwidth) - 1;
297 int annotationHeight = 0;
299 if (av.isShowAnnotation())
303 ap = new AnnotationPanel(av);
306 annotationHeight = ap.adjustPanelHeight();
309 labels = new AnnotationLabels(av);
313 int hgap = av.getCharHeight();
314 if (av.getScaleAboveWrapped())
316 hgap += av.getCharHeight();
319 int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
321 int rowSize = av.getRanges().getEndRes()
322 - av.getRanges().getStartRes();
324 // Draw the rest of the panels
325 for (int ypos = hgap, row = av.getRanges().getStartRes(); (ypos <= getHeight())
326 && (row < maxwidth); ypos += cHeight, row += rowSize)
328 for (int i = starty; i < alheight; i++)
330 SequenceI s = av.getAlignment().getSequenceAt(i);
337 gg.setFont(getIdfont());
340 drawIdString(gg, hiddenRows, s, i, 0, ypos);
343 if (labels != null && av.isShowAnnotation())
345 gg.translate(0, ypos + (alheight * av.getCharHeight()));
346 labels.drawComponent(gg, getWidth());
347 gg.translate(0, -ypos - (alheight * av.getCharHeight()));
353 // No need to hang on to labels if we're not wrapped
356 // Now draw the id strings
357 int panelWidth = getWidth();
361 // Now draw the id strings
362 for (int i = starty; i <= endy; i++)
364 sequence = av.getAlignment().getSequenceAt(i);
366 if (sequence == null)
373 setHiddenFont(sequence);
376 // Selected sequence colours
377 if ((searchResults != null) && searchResults.contains(sequence))
379 currentColor = Color.black;
380 currentTextColor = Color.white;
382 else if ((av.getSelectionGroup() != null)
383 && av.getSelectionGroup().getSequences(null)
386 currentColor = Color.lightGray;
387 currentTextColor = Color.black;
391 currentColor = av.getSequenceColour(sequence);
392 currentTextColor = Color.black;
395 gg.setColor(currentColor);
397 gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
400 gg.setColor(currentTextColor);
402 String string = sequence.getDisplayId(av.getShowJVSuffix());
404 if (av.isRightAlignIds())
406 xPos = panelWidth - fm.stringWidth(string) - 4;
409 gg.drawString(string, xPos,
410 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
411 - (av.getCharHeight() / 5));
415 drawMarker(i, starty, 0);
423 void drawMarker(int i, int starty, int yoffset)
426 SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
427 // Use this method here instead of calling hiddenSeq adjust
429 int hSize = hseqs.length;
432 int lastIndex = i - 1;
433 int nextIndex = i + 1;
435 for (int j = 0; j < hSize; j++)
437 if (hseqs[j] != null)
439 if (j - 1 < hiddenIndex)
443 if (j - 1 < lastIndex)
447 if (j - 1 < nextIndex)
454 boolean below = (hiddenIndex > lastIndex + 1);
455 boolean above = (nextIndex > hiddenIndex + 1);
457 gg.setColor(Color.blue);
461 new int[] { getWidth() - av.getCharHeight(),
462 getWidth() - av.getCharHeight(), getWidth() },
464 (i - starty) * av.getCharHeight() + yoffset,
465 (i - starty) * av.getCharHeight() + yoffset
466 + av.getCharHeight() / 4,
467 (i - starty) * av.getCharHeight() + yoffset }, 3);
472 new int[] { getWidth() - av.getCharHeight(),
473 getWidth() - av.getCharHeight(), getWidth() },
475 (i - starty + 1) * av.getCharHeight() + yoffset,
476 (i - starty + 1) * av.getCharHeight() + yoffset
477 - av.getCharHeight() / 4,
478 (i - starty + 1) * av.getCharHeight() + yoffset }, 3);
483 void setHiddenFont(SequenceI seq)
485 Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
488 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
494 gg.setFont(getIdfont());
504 public void setHighlighted(List<SequenceI> list)
506 searchResults = list;
510 public Font getIdfont()
515 public void setIdfont(Font idfont)
517 this.idfont = idfont;