b886552e410640dac1c8b94374e83392dbcdaa30
[jalview.git] / src / jalview / gui / IdCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import jalview.datamodel.SequenceI;
24 import jalview.viewmodel.ViewportListenerI;
25 import jalview.viewmodel.ViewportRanges;
26
27 import java.awt.BorderLayout;
28 import java.awt.Color;
29 import java.awt.Font;
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;
37
38 import javax.swing.JPanel;
39
40 /**
41  * DOCUMENT ME!
42  * 
43  * @author $author$
44  * @version $Revision$
45  */
46 public class IdCanvas extends JPanel implements ViewportListenerI
47 {
48   protected AlignViewport av;
49
50   protected boolean showScores = true;
51
52   protected int maxIdLength = -1;
53
54   protected String maxIdStr = null;
55
56   BufferedImage image;
57
58   Graphics2D gg;
59
60   int imgHeight = 0;
61
62   boolean fastPaint = false;
63
64   AnnotationPanel ap;
65
66   private Font idfont;
67
68   /**
69    * Creates a new IdCanvas object.
70    * 
71    * @param av
72    *          DOCUMENT ME!
73    */
74   public IdCanvas(AlignViewport av)
75   {
76     setLayout(new BorderLayout());
77     this.av = av;
78     PaintRefresher.Register(this, av.getSequenceSetId());
79     av.getRanges().addPropertyChangeListener(this);
80     }
81
82   /**
83    * DOCUMENT ME!
84    * 
85    * @param g
86    *          DOCUMENT ME!
87    * @param hiddenRows
88    *          true - check and display hidden row marker if need be
89    * @param s
90    *          DOCUMENT ME!
91    * @param i
92    *          DOCUMENT ME!
93    * @param starty
94    *          DOCUMENT ME!
95    * @param ypos
96    *          DOCUMENT ME!
97    */
98   public void drawIdString(Graphics2D g, boolean hiddenRows, SequenceI s,
99           int i, int starty, int ypos)
100   {
101     int xPos = 0;
102     int panelWidth = getWidth();
103     int charHeight = av.getCharHeight();
104     List<SequenceI> searchResults = av.getHighlightedSeqs();
105     if ((searchResults != null) && searchResults.contains(s))
106     {
107       g.setColor(Color.black);
108       g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
109               charHeight);
110       g.setColor(Color.white);
111     }
112     else if ((av.getSelectionGroup() != null)
113             && av.getSelectionGroup().getSequences(null).contains(s))
114     {
115       g.setColor(Color.lightGray);
116       g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
117               charHeight);
118       g.setColor(Color.white);
119     }
120     else
121     {
122       g.setColor(av.getSequenceColour(s));
123       g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
124               charHeight);
125       g.setColor(Color.black);
126     }
127
128     if (av.isRightAlignIds())
129     {
130       FontMetrics fm = g.getFontMetrics();
131       xPos = panelWidth
132               - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
133     }
134
135     g.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
136             (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
137
138     if (hiddenRows)
139     {
140       drawMarker(g, av, i, starty, ypos);
141     }
142
143   }
144
145   /**
146    * DOCUMENT ME!
147    * 
148    * @param vertical
149    *          DOCUMENT ME!
150    */
151   public void fastPaint(int vertical)
152   {
153     /*
154      * for now, not attempting fast paint of wrapped ids...
155      */
156     if (gg == null || av.getWrapAlignment())
157     {
158       repaint();
159
160       return;
161     }
162
163     ViewportRanges ranges = av.getRanges();
164     List<SequenceI> searchResults = av.getHighlightedSeqs();
165
166     gg.copyArea(0, 0, getWidth(), imgHeight, 0,
167             -vertical * av.getCharHeight());
168
169     int ss = ranges.getStartSeq();
170     int es = ranges.getEndSeq();
171     int transY = 0;
172
173     if (vertical > 0) // scroll down
174     {
175       ss = es - vertical;
176
177       if (ss < ranges.getStartSeq())
178       { // ie scrolling too fast, more than a page at a time
179         ss = ranges.getStartSeq();
180       }
181       else
182       {
183         transY = imgHeight - ((vertical + 1) * av.getCharHeight());
184       }
185     }
186     else if (vertical < 0) // scroll up
187     {
188       es = ss - vertical;
189
190       if (es > ranges.getEndSeq())
191       {
192         es = ranges.getEndSeq();
193       }
194     }
195
196     gg.translate(0, transY);
197
198     drawIds(gg, av, ss, es, searchResults);
199
200     gg.translate(0, -transY);
201
202     fastPaint = true;
203
204     // Call repaint on alignment panel so that repaints from other alignment
205     // panel components can be aggregated. Otherwise performance of the overview
206     // window and others may be adversely affected.
207     av.getAlignPanel().repaint();
208   }
209
210   /**
211    * DOCUMENT ME!
212    * 
213    * @param g
214    *          DOCUMENT ME!
215    */
216   @Override
217   public void paintComponent(Graphics g)
218   {
219     super.paintComponent(g);
220
221     g.setColor(Color.white);
222     g.fillRect(0, 0, getWidth(), getHeight());
223     
224     if (fastPaint)
225     {
226       fastPaint = false;
227       g.drawImage(image, 0, 0, this);
228     
229       return;
230     }
231     
232     int oldHeight = imgHeight;
233     
234     imgHeight = getHeight();
235     imgHeight -= (imgHeight % av.getCharHeight());
236     
237     if (imgHeight < 1)
238     {
239       return;
240     }
241     
242     if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
243     {
244         image = new BufferedImage(getWidth(), imgHeight,
245                 BufferedImage.TYPE_INT_RGB);
246     }
247     
248     gg = (Graphics2D) image.getGraphics();
249     
250     // Fill in the background
251     gg.setColor(Color.white);
252     gg.fillRect(0, 0, getWidth(), imgHeight);
253     
254     drawIds(gg, av, av.getRanges().getStartSeq(),
255             av.getRanges().getEndSeq(), av.getHighlightedSeqs());
256     
257     g.drawImage(image, 0, 0, this);
258   }
259
260   /**
261    * Draws sequence ids from sequence index startSeq to endSeq (inclusive), with
262    * the font and other display settings configured on the viewport. Ids of
263    * sequences included in the selection are coloured grey, otherwise the
264    * current id colour for the sequence id is used.
265    * 
266    * @param g
267    * @param alignViewport
268    * @param startSeq
269    * @param endSeq
270    * @param selection
271    */
272   void drawIds(Graphics2D g, AlignViewport alignViewport, final int startSeq,
273           final int endSeq, List<SequenceI> selection)
274   {
275     Font font = alignViewport.getFont();
276     if (alignViewport.isSeqNameItalics())
277     {
278       setIdfont(new Font(font.getName(), Font.ITALIC,
279               font.getSize()));
280     }
281     else
282     {
283       setIdfont(font);
284     }
285
286     g.setFont(getIdfont());
287     FontMetrics fm = g.getFontMetrics();
288
289     if (alignViewport.antiAlias)
290     {
291       g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
292               RenderingHints.VALUE_ANTIALIAS_ON);
293     }
294
295     Color currentColor = Color.white;
296     Color currentTextColor = Color.black;
297
298     boolean hasHiddenRows = alignViewport.hasHiddenRows();
299
300     if (alignViewport.getWrapAlignment())
301     {
302       drawIdsWrapped(g, alignViewport, startSeq, getHeight());
303       return;
304     }
305
306     // Now draw the id strings
307     int panelWidth = getWidth();
308     int xPos = 0;
309
310     // Now draw the id strings
311     for (int i = startSeq; i <= endSeq; i++)
312     {
313       SequenceI sequence = alignViewport.getAlignment().getSequenceAt(i);
314
315       if (sequence == null)
316       {
317         continue;
318       }
319
320       if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
321       {
322         g.setFont(getHiddenFont(sequence, alignViewport));
323       }
324
325       // Selected sequence colours
326       if (selection != null && selection.contains(sequence))
327       {
328         currentColor = Color.black;
329         currentTextColor = Color.white;
330       }
331       else if ((alignViewport.getSelectionGroup() != null) && alignViewport
332               .getSelectionGroup().getSequences(null).contains(sequence))
333       {
334         currentColor = Color.lightGray;
335         currentTextColor = Color.black;
336       }
337       else
338       {
339         currentColor = alignViewport.getSequenceColour(sequence);
340         currentTextColor = Color.black;
341       }
342
343       g.setColor(currentColor);
344
345       int charHeight = alignViewport.getCharHeight();
346       g.fillRect(0, (i - startSeq) * charHeight,
347               getWidth(), charHeight);
348
349       g.setColor(currentTextColor);
350
351       String string = sequence
352               .getDisplayId(alignViewport.getShowJVSuffix());
353
354       if (alignViewport.isRightAlignIds())
355       {
356         xPos = panelWidth - fm.stringWidth(string) - 4;
357       }
358
359       g.drawString(string, xPos, (((i - startSeq) * charHeight) + charHeight)
360               - (charHeight / 5));
361
362       if (hasHiddenRows)
363       {
364         drawMarker(g, alignViewport, i, startSeq, 0);
365       }
366     }
367   }
368
369   /**
370    * Draws sequence ids, and annotation labels if annotations are shown, in
371    * wrapped mode
372    * 
373    * @param g
374    * @param alignViewport
375    * @param startSeq
376    */
377   void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
378           int startSeq, int pageHeight)
379   {
380     int alignmentWidth = alignViewport.getAlignment().getWidth();
381     final int alheight = alignViewport.getAlignment().getHeight();
382
383     if (alignViewport.hasHiddenColumns())
384     {
385       alignmentWidth = alignViewport.getAlignment().getHiddenColumns()
386               .absoluteToVisibleColumn(alignmentWidth) - 1;
387     }
388
389     int annotationHeight = 0;
390
391     AnnotationLabels labels = null;
392     if (alignViewport.isShowAnnotation())
393     {
394       if (ap == null)
395       {
396         ap = new AnnotationPanel(alignViewport);
397       }
398       annotationHeight = ap.adjustPanelHeight();
399       labels = new AnnotationLabels(alignViewport);
400     }
401
402     final int charHeight = alignViewport.getCharHeight();
403     int hgap = charHeight;
404     if (alignViewport.getScaleAboveWrapped())
405     {
406       hgap += charHeight;
407     }
408
409     /*
410      * height of alignment + gap + annotations (if shown)
411      */
412     int cHeight = alheight * charHeight + hgap
413             + annotationHeight;
414
415     ViewportRanges ranges = alignViewport.getRanges();
416
417     int rowSize = ranges.getViewportWidth();
418
419     /*
420      * draw repeating sequence ids until out of sequence data or
421      * out of visible space, whichever comes first
422      */
423     boolean hasHiddenRows = alignViewport.hasHiddenRows();
424     int ypos = hgap;
425     int rowStartRes = ranges.getStartRes();
426     while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
427     {
428       for (int i = startSeq; i < alheight; i++)
429       {
430         SequenceI s = alignViewport.getAlignment().getSequenceAt(i);
431         if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
432         {
433           g.setFont(getHiddenFont(s, alignViewport));
434         }
435         else
436         {
437           g.setFont(getIdfont());
438         }
439         drawIdString(g, hasHiddenRows, s, i, 0, ypos);
440       }
441
442       if (labels != null && alignViewport.isShowAnnotation())
443       {
444         g.translate(0, ypos + (alheight * charHeight));
445         labels.drawComponent(g, getWidth());
446         g.translate(0, -ypos - (alheight * charHeight));
447       }
448
449       ypos += cHeight;
450       rowStartRes += rowSize;
451     }
452   }
453
454   /**
455    * Draws a marker (a blue right-pointing triangle) between sequences to
456    * indicate hidden sequences.
457    * 
458    * @param g
459    * @param alignViewport
460    * @param seqIndex
461    * @param starty
462    * @param yoffset
463    */
464   void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex, int starty, int yoffset)
465   {
466     SequenceI[] hseqs = alignViewport.getAlignment()
467             .getHiddenSequences().hiddenSequences;
468     // Use this method here instead of calling hiddenSeq adjust
469     // 3 times.
470     int hSize = hseqs.length;
471
472     int hiddenIndex = seqIndex;
473     int lastIndex = seqIndex - 1;
474     int nextIndex = seqIndex + 1;
475
476     for (int j = 0; j < hSize; j++)
477     {
478       if (hseqs[j] != null)
479       {
480         if (j - 1 < hiddenIndex)
481         {
482           hiddenIndex++;
483         }
484         if (j - 1 < lastIndex)
485         {
486           lastIndex++;
487         }
488         if (j - 1 < nextIndex)
489         {
490           nextIndex++;
491         }
492       }
493     }
494
495     /*
496      * are we below or above the hidden sequences?
497      */
498     boolean below = (hiddenIndex > lastIndex + 1);
499     boolean above = (nextIndex > hiddenIndex + 1);
500
501     g.setColor(Color.blue);
502     int charHeight = av.getCharHeight();
503
504     /*
505      * vertices of the triangle, below or above hidden seqs
506      */
507     int[] xPoints = new int[]
508     { getWidth() - charHeight,
509         getWidth() - charHeight, getWidth() };
510     int yShift = seqIndex - starty;
511
512     if (below)
513     {
514       int[] yPoints = new int[] { yShift * charHeight + yoffset,
515           yShift * charHeight + yoffset + charHeight / 4,
516           yShift * charHeight + yoffset };
517       g.fillPolygon(xPoints, yPoints, 3);
518     }
519     if (above)
520     {
521       yShift++;
522       int[] yPoints = new int[] { yShift * charHeight + yoffset,
523           yShift * charHeight + yoffset - charHeight / 4,
524           yShift * charHeight + yoffset };
525       g.fillPolygon(xPoints, yPoints, 3);
526     }
527   }
528
529   /**
530    * Answers the standard sequence id font, or a bold font if the sequence is
531    * set as reference or a hidden group representative
532    * 
533    * @param seq
534    * @param alignViewport
535    * @return
536    */
537   private Font getHiddenFont(SequenceI seq, AlignViewport alignViewport)
538   {
539     if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
540     {
541       return new Font(av.getFont().getName(), Font.BOLD,
542               av.getFont().getSize());
543     }
544     return getIdfont();
545   }
546
547   public Font getIdfont()
548   {
549     return idfont;
550   }
551
552   public void setIdfont(Font idfont)
553   {
554     this.idfont = idfont;
555   }
556
557   /**
558    * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
559    * scrolling and resizing change viewport ranges. Scrolling changes both start
560    * and end points, but resize only changes end values. Here we only want to
561    * fastpaint on a scroll, with resize using a normal paint, so scroll events
562    * are identified as changes to the horizontal or vertical start value.
563    * <p>
564    * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
565    * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
566    * provided, but it generates a change of "startres" which does require an
567    * update here.
568    */
569   @Override
570   public void propertyChange(PropertyChangeEvent evt)
571   {
572     String propertyName = evt.getPropertyName();
573     if (propertyName.equals(ViewportRanges.STARTSEQ)
574             || (av.getWrapAlignment()
575                     && propertyName.equals(ViewportRanges.STARTRES)))
576     {
577       fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
578     }
579     else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
580     {
581       fastPaint(((int[]) evt.getNewValue())[1]
582               - ((int[]) evt.getOldValue())[1]);
583     }
584     else if (propertyName.equals(ViewportRanges.MOVE_VIEWPORT))
585     {
586       repaint();
587     }
588   }
589 }