2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.charHeight;
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.showHiddenMarkers)
139 drawMarker(i, starty, ypos);
150 public void fastPaint(int vertical)
159 gg.copyArea(0, 0, getWidth(), imgHeight, 0, -vertical * av.charHeight);
161 int ss = av.startSeq;
165 if (vertical > 0) // scroll down
169 if (ss < av.startSeq)
170 { // ie scrolling too fast, more than a page at a time
175 transY = imgHeight - (vertical * av.charHeight);
178 else if (vertical < 0)
188 gg.translate(0, transY);
192 gg.translate(0, -transY);
204 public void paintComponent(Graphics g)
206 g.setColor(Color.white);
207 g.fillRect(0, 0, getWidth(), getHeight());
212 g.drawImage(image, 0, 0, this);
217 int oldHeight = imgHeight;
219 imgHeight = getHeight();
220 imgHeight -= (imgHeight % av.charHeight);
227 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
229 image = new BufferedImage(getWidth(), imgHeight,
230 BufferedImage.TYPE_INT_RGB);
233 gg = (Graphics2D) image.getGraphics();
235 // Fill in the background
236 gg.setColor(Color.white);
237 gg.fillRect(0, 0, getWidth(), imgHeight);
239 drawIds(av.getStartSeq(), av.endSeq);
241 g.drawImage(image, 0, 0, this);
252 void drawIds(int starty, int endy)
254 if (av.seqNameItalics)
256 setIdfont(new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
261 setIdfont(av.getFont());
264 gg.setFont(getIdfont());
265 fm = gg.getFontMetrics();
269 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
270 RenderingHints.VALUE_ANTIALIAS_ON);
273 Color currentColor = Color.white;
274 Color currentTextColor = Color.black;
276 if (av.getWrapAlignment())
278 int maxwidth = av.getAlignment().getWidth();
279 int alheight = av.getAlignment().getHeight();
281 if (av.hasHiddenColumns())
283 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
286 int annotationHeight = 0;
288 if (av.isShowAnnotation())
292 ap = new AnnotationPanel(av);
295 annotationHeight = ap.adjustPanelHeight();
298 labels = new AnnotationLabels(av);
302 int hgap = av.charHeight;
303 if (av.scaleAboveWrapped)
305 hgap += av.charHeight;
308 int cHeight = alheight * av.charHeight + hgap + annotationHeight;
310 int rowSize = av.getEndRes() - av.getStartRes();
312 // Draw the rest of the panels
313 for (int ypos = hgap, row = av.startRes; (ypos <= getHeight())
314 && (row < maxwidth); ypos += cHeight, row += rowSize)
316 for (int i = starty; i < alheight; i++)
318 SequenceI s = av.getAlignment().getSequenceAt(i);
319 if (av.isDisplayReferenceSeq() || av.hasHiddenRows())
325 gg.setFont(getIdfont());
328 drawIdString(gg, s, i, 0, ypos);
331 if (labels != null && av.isShowAnnotation())
333 gg.translate(0, ypos + (alheight * av.charHeight));
334 labels.drawComponent(gg, getWidth());
335 gg.translate(0, -ypos - (alheight * av.charHeight));
341 // No need to hang on to labels if we're not wrapped
344 // Now draw the id strings
345 int panelWidth = getWidth();
349 // Now draw the id strings
350 for (int i = starty; i < endy; i++)
352 sequence = av.getAlignment().getSequenceAt(i);
354 if (sequence == null)
359 if (av.isDisplayReferenceSeq() || av.hasHiddenRows())
361 setHiddenFont(sequence);
364 // Selected sequence colours
365 if ((searchResults != null) && searchResults.contains(sequence))
367 currentColor = Color.black;
368 currentTextColor = Color.white;
370 else if ((av.getSelectionGroup() != null)
371 && av.getSelectionGroup().getSequences(null)
374 currentColor = Color.lightGray;
375 currentTextColor = Color.black;
379 currentColor = av.getSequenceColour(sequence);
380 currentTextColor = Color.black;
383 gg.setColor(currentColor);
385 gg.fillRect(0, (i - starty) * av.charHeight, getWidth(),
388 gg.setColor(currentTextColor);
390 String string = sequence.getDisplayId(av.getShowJVSuffix());
392 if (av.isRightAlignIds())
394 xPos = panelWidth - fm.stringWidth(string) - 4;
397 gg.drawString(string, xPos,
398 (((i - starty) * av.charHeight) + av.charHeight)
399 - (av.charHeight / 5));
401 if (av.hasHiddenRows() && av.showHiddenMarkers)
403 drawMarker(i, starty, 0);
411 void drawMarker(int i, int starty, int yoffset)
414 SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
415 // Use this method here instead of calling hiddenSeq adjust
417 int hSize = hseqs.length;
420 int lastIndex = i - 1;
421 int nextIndex = i + 1;
423 for (int j = 0; j < hSize; j++)
425 if (hseqs[j] != null)
427 if (j - 1 < hiddenIndex)
431 if (j - 1 < lastIndex)
435 if (j - 1 < nextIndex)
442 boolean below = (hiddenIndex > lastIndex + 1);
443 boolean above = (nextIndex > hiddenIndex + 1);
445 gg.setColor(Color.blue);
450 { getWidth() - av.charHeight, getWidth() - av.charHeight,
451 getWidth() }, new int[]
453 (i - starty) * av.charHeight + yoffset,
454 (i - starty) * av.charHeight + yoffset + av.charHeight
455 / 4, (i - starty) * av.charHeight + yoffset }, 3);
461 { getWidth() - av.charHeight, getWidth() - av.charHeight,
462 getWidth() }, new int[]
464 (i - starty + 1) * av.charHeight + yoffset,
465 (i - starty + 1) * av.charHeight + yoffset
467 (i - starty + 1) * av.charHeight + yoffset }, 3);
472 void setHiddenFont(SequenceI seq)
474 Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
477 if (av.isHiddenRepSequence(seq))
483 gg.setFont(getIdfont());
493 public void setHighlighted(List<SequenceI> list)
495 searchResults = list;
499 public Font getIdfont()
504 public void setIdfont(Font idfont)
506 this.idfont = idfont;