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());
90 * true - check and display hidden row marker if need be
100 public void drawIdString(Graphics2D gg, boolean hiddenRows, SequenceI s,
101 int i, int starty, int ypos)
104 int panelWidth = getWidth();
105 int charHeight = av.getCharHeight();
107 if ((searchResults != null) && searchResults.contains(s))
109 gg.setColor(Color.black);
110 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
112 gg.setColor(Color.white);
114 else if ((av.getSelectionGroup() != null)
115 && av.getSelectionGroup().getSequences(null).contains(s))
117 gg.setColor(Color.lightGray);
118 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
120 gg.setColor(Color.white);
124 gg.setColor(av.getSequenceColour(s));
125 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
127 gg.setColor(Color.black);
130 if (av.isRightAlignIds())
133 - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
136 gg.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
137 (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
141 drawMarker(i, starty, ypos);
152 public void fastPaint(int vertical)
161 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
162 -vertical * av.getCharHeight());
164 int ss = av.startSeq;
168 if (vertical > 0) // scroll down
172 if (ss < av.startSeq)
173 { // ie scrolling too fast, more than a page at a time
178 transY = imgHeight - (vertical * av.getCharHeight());
181 else if (vertical < 0)
191 gg.translate(0, transY);
195 gg.translate(0, -transY);
207 public void paintComponent(Graphics g)
209 g.setColor(Color.white);
210 g.fillRect(0, 0, getWidth(), getHeight());
215 g.drawImage(image, 0, 0, this);
220 int oldHeight = imgHeight;
222 imgHeight = getHeight();
223 imgHeight -= (imgHeight % av.getCharHeight());
230 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
232 image = new BufferedImage(getWidth(), imgHeight,
233 BufferedImage.TYPE_INT_RGB);
236 gg = (Graphics2D) image.getGraphics();
238 // Fill in the background
239 gg.setColor(Color.white);
240 gg.fillRect(0, 0, getWidth(), imgHeight);
242 drawIds(av.getStartSeq(), av.endSeq);
244 g.drawImage(image, 0, 0, this);
255 void drawIds(int starty, int endy)
257 if (av.isSeqNameItalics())
259 setIdfont(new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
264 setIdfont(av.getFont());
267 gg.setFont(getIdfont());
268 fm = gg.getFontMetrics();
272 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
273 RenderingHints.VALUE_ANTIALIAS_ON);
276 Color currentColor = Color.white;
277 Color currentTextColor = Color.black;
279 final boolean doHiddenCheck = av.isDisplayReferenceSeq()
280 || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows();
282 if (av.getWrapAlignment())
284 int maxwidth = av.getAlignment().getWidth();
285 int alheight = av.getAlignment().getHeight();
287 if (av.hasHiddenColumns())
289 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
292 int annotationHeight = 0;
294 if (av.isShowAnnotation())
298 ap = new AnnotationPanel(av);
301 annotationHeight = ap.adjustPanelHeight();
304 labels = new AnnotationLabels(av);
308 int hgap = av.getCharHeight();
309 if (av.getScaleAboveWrapped())
311 hgap += av.getCharHeight();
314 int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
316 int rowSize = av.getEndRes() - av.getStartRes();
318 // Draw the rest of the panels
319 for (int ypos = hgap, row = av.startRes; (ypos <= getHeight())
320 && (row < maxwidth); ypos += cHeight, row += rowSize)
322 for (int i = starty; i < alheight; i++)
324 SequenceI s = av.getAlignment().getSequenceAt(i);
331 gg.setFont(getIdfont());
334 drawIdString(gg, hiddenRows, s, i, 0, ypos);
337 if (labels != null && av.isShowAnnotation())
339 gg.translate(0, ypos + (alheight * av.getCharHeight()));
340 labels.drawComponent(gg, getWidth());
341 gg.translate(0, -ypos - (alheight * av.getCharHeight()));
347 // No need to hang on to labels if we're not wrapped
350 // Now draw the id strings
351 int panelWidth = getWidth();
355 // Now draw the id strings
356 for (int i = starty; i < endy; i++)
358 sequence = av.getAlignment().getSequenceAt(i);
360 if (sequence == null)
367 setHiddenFont(sequence);
370 // Selected sequence colours
371 if ((searchResults != null) && searchResults.contains(sequence))
373 currentColor = Color.black;
374 currentTextColor = Color.white;
376 else if ((av.getSelectionGroup() != null)
377 && av.getSelectionGroup().getSequences(null)
380 currentColor = Color.lightGray;
381 currentTextColor = Color.black;
385 currentColor = av.getSequenceColour(sequence);
386 currentTextColor = Color.black;
389 gg.setColor(currentColor);
391 gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
394 gg.setColor(currentTextColor);
396 String string = sequence.getDisplayId(av.getShowJVSuffix());
398 if (av.isRightAlignIds())
400 xPos = panelWidth - fm.stringWidth(string) - 4;
403 gg.drawString(string, xPos,
404 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
405 - (av.getCharHeight() / 5));
409 drawMarker(i, starty, 0);
417 void drawMarker(int i, int starty, int yoffset)
420 SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
421 // Use this method here instead of calling hiddenSeq adjust
423 int hSize = hseqs.length;
426 int lastIndex = i - 1;
427 int nextIndex = i + 1;
429 for (int j = 0; j < hSize; j++)
431 if (hseqs[j] != null)
433 if (j - 1 < hiddenIndex)
437 if (j - 1 < lastIndex)
441 if (j - 1 < nextIndex)
448 boolean below = (hiddenIndex > lastIndex + 1);
449 boolean above = (nextIndex > hiddenIndex + 1);
451 gg.setColor(Color.blue);
455 new int[] { getWidth() - av.getCharHeight(),
456 getWidth() - av.getCharHeight(), getWidth() },
458 (i - starty) * av.getCharHeight() + yoffset,
459 (i - starty) * av.getCharHeight() + yoffset
460 + av.getCharHeight() / 4,
461 (i - starty) * av.getCharHeight() + yoffset }, 3);
466 new int[] { getWidth() - av.getCharHeight(),
467 getWidth() - av.getCharHeight(), getWidth() },
469 (i - starty + 1) * av.getCharHeight() + yoffset,
470 (i - starty + 1) * av.getCharHeight() + yoffset
471 - av.getCharHeight() / 4,
472 (i - starty + 1) * av.getCharHeight() + yoffset }, 3);
477 void setHiddenFont(SequenceI seq)
479 Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
482 if (av.isHiddenRepSequence(seq))
488 gg.setFont(getIdfont());
498 public void setHighlighted(List<SequenceI> list)
500 searchResults = list;
504 public Font getIdfont()
509 public void setIdfont(Font idfont)
511 this.idfont = idfont;