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