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.viewmodel.ViewportListenerI;
25 import jalview.viewmodel.ViewportRanges;
27 import java.awt.BorderLayout;
28 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
32 import java.awt.Graphics2D;
33 import java.awt.RenderingHints;
34 import java.awt.image.BufferedImage;
35 import java.beans.PropertyChangeEvent;
36 import java.util.List;
38 import javax.swing.JPanel;
46 public class IdCanvas extends JPanel implements ViewportListenerI
48 protected AlignViewport av;
50 protected boolean showScores = true;
52 protected int maxIdLength = -1;
54 protected String maxIdStr = null;
62 boolean fastPaint = false;
64 List<SequenceI> searchResults;
68 AnnotationLabels labels = null;
75 * Creates a new IdCanvas object.
80 public IdCanvas(AlignViewport av)
82 setLayout(new BorderLayout());
84 PaintRefresher.Register(this, av.getSequenceSetId());
85 av.getRanges().addPropertyChangeListener(this);
94 * true - check and display hidden row marker if need be
104 public void drawIdString(Graphics2D gg, boolean hiddenRows, SequenceI s,
105 int i, int starty, int ypos)
108 int panelWidth = getWidth();
109 int charHeight = av.getCharHeight();
111 if ((searchResults != null) && searchResults.contains(s))
113 gg.setColor(Color.black);
114 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
116 gg.setColor(Color.white);
118 else if ((av.getSelectionGroup() != null)
119 && av.getSelectionGroup().getSequences(null).contains(s))
121 gg.setColor(Color.lightGray);
122 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
124 gg.setColor(Color.white);
128 gg.setColor(av.getSequenceColour(s));
129 gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
131 gg.setColor(Color.black);
134 if (av.isRightAlignIds())
137 - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
140 gg.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
141 (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
145 drawMarker(i, starty, ypos);
156 public void fastPaint(int vertical)
159 * for now, not attempting fast paint of wrapped ids...
161 if (gg == null || av.getWrapAlignment())
168 ViewportRanges ranges = av.getRanges();
170 gg.copyArea(0, 0, getWidth(), imgHeight, 0,
171 -vertical * av.getCharHeight());
173 int ss = ranges.getStartSeq();
174 int es = ranges.getEndSeq();
177 if (vertical > 0) // scroll down
181 if (ss < ranges.getStartSeq())
182 { // ie scrolling too fast, more than a page at a time
183 ss = ranges.getStartSeq();
187 transY = imgHeight - ((vertical + 1) * av.getCharHeight());
190 else if (vertical < 0) // scroll up
194 if (es > ranges.getEndSeq())
196 es = ranges.getEndSeq();
200 gg.translate(0, transY);
204 gg.translate(0, -transY);
208 // Call repaint on alignment panel so that repaints from other alignment
209 // panel components can be aggregated. Otherwise performance of the overview
210 // window and others may be adversely affected.
211 av.getAlignPanel().repaint();
221 public void paintComponent(Graphics g)
223 super.paintComponent(g);
225 g.setColor(Color.white);
226 g.fillRect(0, 0, getWidth(), getHeight());
231 g.drawImage(image, 0, 0, this);
236 int oldHeight = imgHeight;
238 imgHeight = getHeight();
239 imgHeight -= (imgHeight % av.getCharHeight());
246 if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
248 image = new BufferedImage(getWidth(), imgHeight,
249 BufferedImage.TYPE_INT_RGB);
252 gg = (Graphics2D) image.getGraphics();
254 // Fill in the background
255 gg.setColor(Color.white);
256 gg.fillRect(0, 0, getWidth(), imgHeight);
258 drawIds(av.getRanges().getStartSeq(), av.getRanges().getEndSeq());
260 g.drawImage(image, 0, 0, this);
271 void drawIds(int starty, int endy)
273 if (av.isSeqNameItalics())
275 setIdfont(new Font(av.getFont().getName(), Font.ITALIC,
276 av.getFont().getSize()));
280 setIdfont(av.getFont());
283 gg.setFont(getIdfont());
284 fm = gg.getFontMetrics();
288 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
289 RenderingHints.VALUE_ANTIALIAS_ON);
292 Color currentColor = Color.white;
293 Color currentTextColor = Color.black;
295 boolean hasHiddenRows = av.hasHiddenRows();
297 if (av.getWrapAlignment())
299 drawIdsWrapped(starty, hasHiddenRows);
303 // No need to hang on to labels if we're not wrapped
306 // Now draw the id strings
307 int panelWidth = getWidth();
311 // Now draw the id strings
312 for (int i = starty; i <= endy; i++)
314 sequence = av.getAlignment().getSequenceAt(i);
316 if (sequence == null)
321 if (hasHiddenRows || av.isDisplayReferenceSeq())
323 setHiddenFont(sequence);
326 // Selected sequence colours
327 if ((searchResults != null) && searchResults.contains(sequence))
329 currentColor = Color.black;
330 currentTextColor = Color.white;
332 else if ((av.getSelectionGroup() != null) && av.getSelectionGroup()
333 .getSequences(null).contains(sequence))
335 currentColor = Color.lightGray;
336 currentTextColor = Color.black;
340 currentColor = av.getSequenceColour(sequence);
341 currentTextColor = Color.black;
344 gg.setColor(currentColor);
346 gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
349 gg.setColor(currentTextColor);
351 String string = sequence.getDisplayId(av.getShowJVSuffix());
353 if (av.isRightAlignIds())
355 xPos = panelWidth - fm.stringWidth(string) - 4;
358 gg.drawString(string, xPos,
359 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
360 - (av.getCharHeight() / 5));
364 drawMarker(i, starty, 0);
370 * Draws sequence ids in wrapped mode
373 * @param hasHiddenRows
375 protected void drawIdsWrapped(int starty, boolean hasHiddenRows)
377 int maxwidth = av.getAlignment().getWidth();
378 int alheight = av.getAlignment().getHeight();
380 if (av.hasHiddenColumns())
382 maxwidth = av.getAlignment().getHiddenColumns()
383 .absoluteToVisibleColumn(maxwidth) - 1;
386 int annotationHeight = 0;
388 if (av.isShowAnnotation())
392 ap = new AnnotationPanel(av);
395 annotationHeight = ap.adjustPanelHeight();
398 labels = new AnnotationLabels(av);
402 int hgap = av.getCharHeight();
403 if (av.getScaleAboveWrapped())
405 hgap += av.getCharHeight();
408 int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
410 ViewportRanges ranges = av.getRanges();
412 int rowSize = ranges.getViewportWidth();
415 * draw repeating sequence ids until out of sequence data or
416 * out of visible space, whichever comes first
419 int row = ranges.getStartRes();
420 while ((ypos <= getHeight()) && (row < maxwidth))
422 for (int i = starty; i < alheight; i++)
424 SequenceI s = av.getAlignment().getSequenceAt(i);
425 if (hasHiddenRows || av.isDisplayReferenceSeq())
431 gg.setFont(getIdfont());
434 drawIdString(gg, hasHiddenRows, s, i, 0, ypos);
437 if (labels != null && av.isShowAnnotation())
439 gg.translate(0, ypos + (alheight * av.getCharHeight()));
440 labels.drawComponent(gg, getWidth());
441 gg.translate(0, -ypos - (alheight * av.getCharHeight()));
449 void drawMarker(int i, int starty, int yoffset)
452 SequenceI[] hseqs = av.getAlignment()
453 .getHiddenSequences().hiddenSequences;
454 // Use this method here instead of calling hiddenSeq adjust
456 int hSize = hseqs.length;
459 int lastIndex = i - 1;
460 int nextIndex = i + 1;
462 for (int j = 0; j < hSize; j++)
464 if (hseqs[j] != null)
466 if (j - 1 < hiddenIndex)
470 if (j - 1 < lastIndex)
474 if (j - 1 < nextIndex)
481 boolean below = (hiddenIndex > lastIndex + 1);
482 boolean above = (nextIndex > hiddenIndex + 1);
484 gg.setColor(Color.blue);
489 { getWidth() - av.getCharHeight(),
490 getWidth() - av.getCharHeight(), getWidth() },
492 { (i - starty) * av.getCharHeight() + yoffset,
493 (i - starty) * av.getCharHeight() + yoffset
494 + av.getCharHeight() / 4,
495 (i - starty) * av.getCharHeight() + yoffset },
502 { getWidth() - av.getCharHeight(),
503 getWidth() - av.getCharHeight(), getWidth() },
505 { (i - starty + 1) * av.getCharHeight() + yoffset,
506 (i - starty + 1) * av.getCharHeight() + yoffset
507 - av.getCharHeight() / 4,
508 (i - starty + 1) * av.getCharHeight() + yoffset },
514 void setHiddenFont(SequenceI seq)
516 Font bold = new Font(av.getFont().getName(), Font.BOLD,
517 av.getFont().getSize());
519 if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
525 gg.setFont(getIdfont());
535 public void setHighlighted(List<SequenceI> list)
537 searchResults = list;
541 public Font getIdfont()
546 public void setIdfont(Font idfont)
548 this.idfont = idfont;
552 * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
553 * scrolling and resizing change viewport ranges. Scrolling changes both start
554 * and end points, but resize only changes end values. Here we only want to
555 * fastpaint on a scroll, with resize using a normal paint, so scroll events
556 * are identified as changes to the horizontal or vertical start value.
558 * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
559 * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
560 * provided, but it generates a change of "startres" which does require an
564 public void propertyChange(PropertyChangeEvent evt)
566 String propertyName = evt.getPropertyName();
567 if (propertyName.equals(ViewportRanges.STARTSEQ)
568 || (av.getWrapAlignment()
569 && propertyName.equals(ViewportRanges.STARTRES)))
571 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
573 else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
575 fastPaint(((int[]) evt.getNewValue())[1]
576 - ((int[]) evt.getOldValue())[1]);
578 else if (propertyName.equals(ViewportRanges.MOVE_VIEWPORT))