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;
25 import java.awt.BorderLayout;
26 import java.awt.Color;
28 import java.awt.FontMetrics;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.RenderingHints;
32 import java.awt.image.BufferedImage;
33 import java.util.List;
35 import javax.swing.JPanel;
43 public class IdCanvas extends JPanel
45 protected AlignViewport av;
47 protected boolean showScores = true;
49 protected int maxIdLength = -1;
51 protected String maxIdStr = null;
59 boolean fastPaint = false;
61 List<SequenceI> searchResults;
65 AnnotationLabels labels = null;
72 * Creates a new IdCanvas object.
77 public IdCanvas(AlignViewport av)
79 setLayout(new BorderLayout());
81 PaintRefresher.Register(this, av.getSequenceSetId());
98 public void drawIdString(Graphics2D gg, SequenceI s, int i, int starty,
102 int panelWidth = getWidth();
103 int charHeight = av.getCharHeight();
105 if ((searchResults != null) && searchResults.contains(s))
107 gg.setColor(Color.black);
108 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
110 gg.setColor(Color.white);
112 else if ((av.getSelectionGroup() != null)
113 && av.getSelectionGroup().getSequences(null).contains(s))
115 gg.setColor(Color.lightGray);
116 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
118 gg.setColor(Color.white);
122 gg.setColor(av.getSequenceColour(s));
123 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
125 gg.setColor(Color.black);
128 if (av.isRightAlignIds())
131 - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
134 gg.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
135 (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
137 if (av.hasHiddenRows() && av.getShowHiddenMarkers())
139 drawMarker(i, starty, ypos);
150 public void fastPaint(int vertical)
159 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
160 -vertical * av.getCharHeight());
162 int ss = av.startSeq;
166 if (vertical > 0) // scroll down
170 if (ss < av.startSeq)
171 { // ie scrolling too fast, more than a page at a time
176 transY = imgHeight - (vertical * av.getCharHeight());
179 else if (vertical < 0)
189 gg.translate(0, transY);
193 gg.translate(0, -transY);
205 public void paintComponent(Graphics g)
207 g.setColor(Color.white);
208 g.fillRect(0, 0, getWidth(), getHeight());
213 g.drawImage(image, 0, 0, this);
218 int oldHeight = imgHeight;
220 imgHeight = getHeight();
221 imgHeight -= (imgHeight % av.getCharHeight());
228 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
230 image = new BufferedImage(getWidth(), imgHeight,
231 BufferedImage.TYPE_INT_RGB);
234 gg = (Graphics2D) image.getGraphics();
236 // Fill in the background
237 gg.setColor(Color.white);
238 gg.fillRect(0, 0, getWidth(), imgHeight);
240 drawIds(av.getStartSeq(), av.endSeq);
242 g.drawImage(image, 0, 0, this);
253 void drawIds(int starty, int endy)
255 if (av.isSeqNameItalics())
257 setIdfont(new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
262 setIdfont(av.getFont());
265 gg.setFont(getIdfont());
266 fm = gg.getFontMetrics();
270 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
271 RenderingHints.VALUE_ANTIALIAS_ON);
274 Color currentColor = Color.white;
275 Color currentTextColor = Color.black;
277 if (av.getWrapAlignment())
279 int maxwidth = av.getAlignment().getWidth();
280 int alheight = av.getAlignment().getHeight();
282 if (av.hasHiddenColumns())
284 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
287 int annotationHeight = 0;
289 if (av.isShowAnnotation())
293 ap = new AnnotationPanel(av);
296 annotationHeight = ap.adjustPanelHeight();
299 labels = new AnnotationLabels(av);
303 int hgap = av.getCharHeight();
304 if (av.getScaleAboveWrapped())
306 hgap += av.getCharHeight();
309 int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
311 int rowSize = av.getEndRes() - av.getStartRes();
313 // Draw the rest of the panels
314 for (int ypos = hgap, row = av.startRes; (ypos <= getHeight())
315 && (row < maxwidth); ypos += cHeight, row += rowSize)
317 for (int i = starty; i < alheight; i++)
319 SequenceI s = av.getAlignment().getSequenceAt(i);
320 if (av.isDisplayReferenceSeq() || av.hasHiddenRows())
326 gg.setFont(getIdfont());
329 drawIdString(gg, s, i, 0, ypos);
332 if (labels != null && av.isShowAnnotation())
334 gg.translate(0, ypos + (alheight * av.getCharHeight()));
335 labels.drawComponent(gg, getWidth());
336 gg.translate(0, -ypos - (alheight * av.getCharHeight()));
342 // No need to hang on to labels if we're not wrapped
345 // Now draw the id strings
346 int panelWidth = getWidth();
350 // Now draw the id strings
351 for (int i = starty; i < endy; i++)
353 sequence = av.getAlignment().getSequenceAt(i);
355 if (sequence == null)
360 if (av.isDisplayReferenceSeq() || av.hasHiddenRows())
362 setHiddenFont(sequence);
365 // Selected sequence colours
366 if ((searchResults != null) && searchResults.contains(sequence))
368 currentColor = Color.black;
369 currentTextColor = Color.white;
371 else if ((av.getSelectionGroup() != null)
372 && av.getSelectionGroup().getSequences(null)
375 currentColor = Color.lightGray;
376 currentTextColor = Color.black;
380 currentColor = av.getSequenceColour(sequence);
381 currentTextColor = Color.black;
384 gg.setColor(currentColor);
386 gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
389 gg.setColor(currentTextColor);
391 String string = sequence.getDisplayId(av.getShowJVSuffix());
393 if (av.isRightAlignIds())
395 xPos = panelWidth - fm.stringWidth(string) - 4;
398 gg.drawString(string, xPos,
399 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
400 - (av.getCharHeight() / 5));
402 if (av.hasHiddenRows() && av.getShowHiddenMarkers())
404 drawMarker(i, starty, 0);
412 void drawMarker(int i, int starty, int yoffset)
415 SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
416 // Use this method here instead of calling hiddenSeq adjust
418 int hSize = hseqs.length;
421 int lastIndex = i - 1;
422 int nextIndex = i + 1;
424 for (int j = 0; j < hSize; j++)
426 if (hseqs[j] != null)
428 if (j - 1 < hiddenIndex)
432 if (j - 1 < lastIndex)
436 if (j - 1 < nextIndex)
443 boolean below = (hiddenIndex > lastIndex + 1);
444 boolean above = (nextIndex > hiddenIndex + 1);
446 gg.setColor(Color.blue);
451 { getWidth() - av.getCharHeight(),
452 getWidth() - av.getCharHeight(),
453 getWidth() }, new int[]
455 (i - starty) * av.getCharHeight() + yoffset,
456 (i - starty) * av.getCharHeight() + yoffset
457 + av.getCharHeight() / 4,
458 (i - starty) * av.getCharHeight() + yoffset }, 3);
464 { getWidth() - av.getCharHeight(),
465 getWidth() - av.getCharHeight(),
466 getWidth() }, new int[]
468 (i - starty + 1) * av.getCharHeight() + yoffset,
469 (i - starty + 1) * av.getCharHeight() + yoffset
470 - av.getCharHeight() / 4,
471 (i - starty + 1) * av.getCharHeight() + yoffset }, 3);
476 void setHiddenFont(SequenceI seq)
478 Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
481 if (av.isHiddenRepSequence(seq))
487 gg.setFont(getIdfont());
497 public void setHighlighted(List<SequenceI> list)
499 searchResults = list;
503 public Font getIdfont()
508 public void setIdfont(Font idfont)
510 this.idfont = idfont;