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.util.ColorUtils;
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(ColorUtils.getColor(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 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
163 -vertical * av.getCharHeight());
165 int ss = av.startSeq;
169 if (vertical > 0) // scroll down
173 if (ss < av.startSeq)
174 { // ie scrolling too fast, more than a page at a time
179 transY = imgHeight - (vertical * av.getCharHeight());
182 else if (vertical < 0)
192 gg.translate(0, transY);
196 gg.translate(0, -transY);
209 public void paintComponent(Graphics g)
211 g.setColor(Color.white);
212 g.fillRect(0, 0, getWidth(), getHeight());
217 g.drawImage(image, 0, 0, this);
222 int oldHeight = imgHeight;
224 imgHeight = getHeight();
225 imgHeight -= (imgHeight % av.getCharHeight());
232 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
234 image = new BufferedImage(getWidth(), imgHeight,
235 BufferedImage.TYPE_INT_RGB);
238 gg = (Graphics2D) image.getGraphics();
240 // Fill in the background
241 gg.setColor(Color.white);
242 gg.fillRect(0, 0, getWidth(), imgHeight);
244 drawIds(av.getStartSeq(), av.endSeq);
246 g.drawImage(image, 0, 0, this);
257 void drawIds(int starty, int endy)
259 if (av.isSeqNameItalics())
261 setIdfont(new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
266 setIdfont(av.getFont());
269 gg.setFont(getIdfont());
270 fm = gg.getFontMetrics();
274 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
275 RenderingHints.VALUE_ANTIALIAS_ON);
278 Color currentColor = Color.white;
279 Color currentTextColor = Color.black;
281 final boolean doHiddenCheck = av.isDisplayReferenceSeq()
282 || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows();
284 if (av.getWrapAlignment())
286 int maxwidth = av.getAlignment().getWidth();
287 int alheight = av.getAlignment().getHeight();
289 if (av.hasHiddenColumns())
291 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
294 int annotationHeight = 0;
296 if (av.isShowAnnotation())
300 ap = new AnnotationPanel(av);
303 annotationHeight = ap.adjustPanelHeight();
306 labels = new AnnotationLabels(av);
310 int hgap = av.getCharHeight();
311 if (av.getScaleAboveWrapped())
313 hgap += av.getCharHeight();
316 int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
318 int rowSize = av.getEndRes() - av.getStartRes();
320 // Draw the rest of the panels
321 for (int ypos = hgap, row = av.startRes; (ypos <= getHeight())
322 && (row < maxwidth); ypos += cHeight, row += rowSize)
324 for (int i = starty; i < alheight; i++)
326 SequenceI s = av.getAlignment().getSequenceAt(i);
333 gg.setFont(getIdfont());
336 drawIdString(gg, hiddenRows, s, i, 0, ypos);
339 if (labels != null && av.isShowAnnotation())
341 gg.translate(0, ypos + (alheight * av.getCharHeight()));
342 labels.drawComponent(gg, getWidth());
343 gg.translate(0, -ypos - (alheight * av.getCharHeight()));
349 // No need to hang on to labels if we're not wrapped
352 // Now draw the id strings
353 int panelWidth = getWidth();
357 // Now draw the id strings
358 for (int i = starty; i < endy; i++)
360 sequence = av.getAlignment().getSequenceAt(i);
362 if (sequence == null)
369 setHiddenFont(sequence);
372 // Selected sequence colours
373 if ((searchResults != null) && searchResults.contains(sequence))
375 currentColor = Color.black;
376 currentTextColor = Color.white;
378 else if ((av.getSelectionGroup() != null)
379 && av.getSelectionGroup().getSequences(null)
382 currentColor = Color.lightGray;
383 currentTextColor = Color.black;
387 currentColor = ColorUtils
388 .getColor(av.getSequenceColour(sequence));
389 currentTextColor = Color.black;
392 gg.setColor(currentColor);
394 gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
397 gg.setColor(currentTextColor);
399 String string = sequence.getDisplayId(av.getShowJVSuffix());
401 if (av.isRightAlignIds())
403 xPos = panelWidth - fm.stringWidth(string) - 4;
406 gg.drawString(string, xPos,
407 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
408 - (av.getCharHeight() / 5));
412 drawMarker(i, starty, 0);
420 void drawMarker(int i, int starty, int yoffset)
423 SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
424 // Use this method here instead of calling hiddenSeq adjust
426 int hSize = hseqs.length;
429 int lastIndex = i - 1;
430 int nextIndex = i + 1;
432 for (int j = 0; j < hSize; j++)
434 if (hseqs[j] != null)
436 if (j - 1 < hiddenIndex)
440 if (j - 1 < lastIndex)
444 if (j - 1 < nextIndex)
451 boolean below = (hiddenIndex > lastIndex + 1);
452 boolean above = (nextIndex > hiddenIndex + 1);
454 gg.setColor(Color.blue);
458 new int[] { getWidth() - av.getCharHeight(),
459 getWidth() - av.getCharHeight(), getWidth() },
461 (i - starty) * av.getCharHeight() + yoffset,
462 (i - starty) * av.getCharHeight() + yoffset
463 + av.getCharHeight() / 4,
464 (i - starty) * av.getCharHeight() + yoffset }, 3);
469 new int[] { getWidth() - av.getCharHeight(),
470 getWidth() - av.getCharHeight(), getWidth() },
472 (i - starty + 1) * av.getCharHeight() + yoffset,
473 (i - starty + 1) * av.getCharHeight() + yoffset
474 - av.getCharHeight() / 4,
475 (i - starty + 1) * av.getCharHeight() + yoffset }, 3);
480 void setHiddenFont(SequenceI seq)
482 Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
485 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
491 gg.setFont(getIdfont());
501 public void setHighlighted(List<SequenceI> list)
503 searchResults = list;
507 public Font getIdfont()
512 public void setIdfont(Font idfont)
514 this.idfont = idfont;