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,
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 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);
208 public void paintComponent(Graphics g)
210 g.setColor(Color.white);
211 g.fillRect(0, 0, getWidth(), getHeight());
216 g.drawImage(image, 0, 0, this);
221 int oldHeight = imgHeight;
223 imgHeight = getHeight();
224 imgHeight -= (imgHeight % av.getCharHeight());
231 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
233 image = new BufferedImage(getWidth(), imgHeight,
234 BufferedImage.TYPE_INT_RGB);
237 gg = (Graphics2D) image.getGraphics();
239 // Fill in the background
240 gg.setColor(Color.white);
241 gg.fillRect(0, 0, getWidth(), imgHeight);
243 drawIds(av.getStartSeq(), av.endSeq);
245 g.drawImage(image, 0, 0, this);
256 void drawIds(int starty, int endy)
258 if (av.isSeqNameItalics())
260 setIdfont(new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
265 setIdfont(av.getFont());
268 gg.setFont(getIdfont());
269 fm = gg.getFontMetrics();
273 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
274 RenderingHints.VALUE_ANTIALIAS_ON);
277 Color currentColor = Color.white;
278 Color currentTextColor = Color.black;
280 final boolean doHiddenCheck = av.isDisplayReferenceSeq()
281 || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows();
283 if (av.getWrapAlignment())
285 int maxwidth = av.getAlignment().getWidth();
286 int alheight = av.getAlignment().getHeight();
288 if (av.hasHiddenColumns())
290 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
293 int annotationHeight = 0;
295 if (av.isShowAnnotation())
299 ap = new AnnotationPanel(av);
302 annotationHeight = ap.adjustPanelHeight();
305 labels = new AnnotationLabels(av);
309 int hgap = av.getCharHeight();
310 if (av.getScaleAboveWrapped())
312 hgap += av.getCharHeight();
315 int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
317 int rowSize = av.getEndRes() - av.getStartRes();
319 // Draw the rest of the panels
320 for (int ypos = hgap, row = av.startRes; (ypos <= getHeight())
321 && (row < maxwidth); ypos += cHeight, row += rowSize)
323 for (int i = starty; i < alheight; i++)
325 SequenceI s = av.getAlignment().getSequenceAt(i);
332 gg.setFont(getIdfont());
335 drawIdString(gg, hiddenRows, s, i, 0, ypos);
338 if (labels != null && av.isShowAnnotation())
340 gg.translate(0, ypos + (alheight * av.getCharHeight()));
341 labels.drawComponent(gg, getWidth());
342 gg.translate(0, -ypos - (alheight * av.getCharHeight()));
348 // No need to hang on to labels if we're not wrapped
351 // Now draw the id strings
352 int panelWidth = getWidth();
356 // Now draw the id strings
357 for (int i = starty; i < endy; i++)
359 sequence = av.getAlignment().getSequenceAt(i);
361 if (sequence == null)
368 setHiddenFont(sequence);
371 // Selected sequence colours
372 if ((searchResults != null) && searchResults.contains(sequence))
374 currentColor = Color.black;
375 currentTextColor = Color.white;
377 else if ((av.getSelectionGroup() != null)
378 && av.getSelectionGroup().getSequences(null)
381 currentColor = Color.lightGray;
382 currentTextColor = Color.black;
386 currentColor = av.getSequenceColour(sequence);
387 currentTextColor = Color.black;
390 gg.setColor(currentColor);
392 gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
395 gg.setColor(currentTextColor);
397 String string = sequence.getDisplayId(av.getShowJVSuffix());
399 if (av.isRightAlignIds())
401 xPos = panelWidth - fm.stringWidth(string) - 4;
404 gg.drawString(string, xPos,
405 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
406 - (av.getCharHeight() / 5));
410 drawMarker(i, starty, 0);
418 void drawMarker(int i, int starty, int yoffset)
421 SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
422 // Use this method here instead of calling hiddenSeq adjust
424 int hSize = hseqs.length;
427 int lastIndex = i - 1;
428 int nextIndex = i + 1;
430 for (int j = 0; j < hSize; j++)
432 if (hseqs[j] != null)
434 if (j - 1 < hiddenIndex)
438 if (j - 1 < lastIndex)
442 if (j - 1 < nextIndex)
449 boolean below = (hiddenIndex > lastIndex + 1);
450 boolean above = (nextIndex > hiddenIndex + 1);
452 gg.setColor(Color.blue);
457 { getWidth() - av.getCharHeight(),
458 getWidth() - av.getCharHeight(),
459 getWidth() }, new int[]
461 (i - starty) * av.getCharHeight() + yoffset,
462 (i - starty) * av.getCharHeight() + yoffset
463 + av.getCharHeight() / 4,
464 (i - starty) * av.getCharHeight() + yoffset }, 3);
470 { getWidth() - av.getCharHeight(),
471 getWidth() - av.getCharHeight(),
472 getWidth() }, new int[]
474 (i - starty + 1) * av.getCharHeight() + yoffset,
475 (i - starty + 1) * av.getCharHeight() + yoffset
476 - av.getCharHeight() / 4,
477 (i - starty + 1) * av.getCharHeight() + yoffset }, 3);
482 void setHiddenFont(SequenceI seq)
484 Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
487 if (av.isHiddenRepSequence(seq))
493 gg.setFont(getIdfont());
503 public void setHighlighted(List<SequenceI> list)
505 searchResults = list;
509 public Font getIdfont()
514 public void setIdfont(Font idfont)
516 this.idfont = idfont;