JAL-1677 commenting use of AlignViewport.gatherViewsHere
[jalview.git] / src / jalview / appletgui / SeqCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.appletgui;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SearchResults;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31 import java.awt.Image;
32 import java.awt.Panel;
33
34 public class SeqCanvas extends Panel
35 {
36   FeatureRenderer fr;
37
38   SequenceRenderer sr;
39
40   Image img;
41
42   Graphics gg;
43
44   int imgWidth;
45
46   int imgHeight;
47
48   AlignViewport av;
49
50   SearchResults searchResults = null;
51
52   boolean fastPaint = false;
53
54   int cursorX = 0;
55
56   int cursorY = 0;
57
58   public SeqCanvas(AlignViewport av)
59   {
60     this.av = av;
61     fr = new FeatureRenderer(av);
62     sr = new SequenceRenderer(av);
63     PaintRefresher.Register(this, av.getSequenceSetId());
64     updateViewport();
65   }
66
67   int avcharHeight = 0, avcharWidth = 0;
68
69   private void updateViewport()
70   {
71     avcharHeight = av.getCharHeight();
72     avcharWidth = av.getCharWidth();
73   }
74
75   public AlignViewport getViewport()
76   {
77     return av;
78   }
79
80   public FeatureRenderer getFeatureRenderer()
81   {
82     return fr;
83   }
84
85   public SequenceRenderer getSequenceRenderer()
86   {
87     return sr;
88   }
89
90   private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
91   {
92     int scalestartx = startx - startx % 10 + 10;
93
94     g.setColor(Color.black);
95
96     // NORTH SCALE
97     for (int i = scalestartx; i < endx; i += 10)
98     {
99       int value = i;
100       if (av.hasHiddenColumns())
101       {
102         value = av.getColumnSelection().adjustForHiddenColumns(value);
103       }
104
105       g.drawString(String.valueOf(value), (i - startx - 1) * avcharWidth,
106               ypos - (avcharHeight / 2));
107
108       g.drawLine(((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
109               (ypos + 2) - (avcharHeight / 2),
110               ((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
111               ypos - 2);
112     }
113   }
114
115   private void drawWestScale(Graphics g, int startx, int endx, int ypos)
116   {
117     FontMetrics fm = getFontMetrics(av.getFont());
118     ypos += avcharHeight;
119     if (av.hasHiddenColumns())
120     {
121       startx = av.getColumnSelection().adjustForHiddenColumns(startx);
122       endx = av.getColumnSelection().adjustForHiddenColumns(endx);
123     }
124
125     int maxwidth = av.getAlignment().getWidth();
126     if (av.hasHiddenColumns())
127     {
128       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
129     }
130
131     // WEST SCALE
132     for (int i = 0; i < av.getAlignment().getHeight(); i++)
133     {
134       SequenceI seq = av.getAlignment().getSequenceAt(i);
135       int index = startx;
136       int value = -1;
137
138       while (index < endx)
139       {
140         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
141         {
142           index++;
143
144           continue;
145         }
146
147         value = av.getAlignment().getSequenceAt(i).findPosition(index);
148
149         break;
150       }
151
152       if (value != -1)
153       {
154         int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
155                 - avcharWidth / 2;
156         g.drawString(value + "", x, (ypos + (i * avcharHeight))
157                 - (avcharHeight / 5));
158       }
159     }
160   }
161
162   private void drawEastScale(Graphics g, int startx, int endx, int ypos)
163   {
164     ypos += avcharHeight;
165
166     if (av.hasHiddenColumns())
167     {
168       endx = av.getColumnSelection().adjustForHiddenColumns(endx);
169     }
170
171     SequenceI seq;
172     // EAST SCALE
173     for (int i = 0; i < av.getAlignment().getHeight(); i++)
174     {
175       seq = av.getAlignment().getSequenceAt(i);
176       int index = endx;
177       int value = -1;
178
179       while (index > startx)
180       {
181         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
182         {
183           index--;
184
185           continue;
186         }
187
188         value = seq.findPosition(index);
189
190         break;
191       }
192
193       if (value != -1)
194       {
195         g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
196                 - (avcharHeight / 5));
197       }
198     }
199   }
200
201   int lastsr = 0;
202
203   void fastPaint(int horizontal, int vertical)
204   {
205     if (fastPaint || gg == null)
206     {
207       return;
208     }
209
210     updateViewport();
211
212     // Its possible on certain browsers that the call to fastpaint
213     // is faster than it can paint, so this check here catches
214     // this possibility
215     if (lastsr + horizontal != av.startRes)
216     {
217       horizontal = av.startRes - lastsr;
218     }
219
220     lastsr = av.startRes;
221
222     fastPaint = true;
223     gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
224             - horizontal * avcharWidth,
225             imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
226             -vertical * avcharHeight);
227
228     int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
229
230     if (horizontal > 0) // scrollbar pulled right, image to the left
231     {
232       transX = (er - sr - horizontal) * avcharWidth;
233       sr = er - horizontal;
234     }
235     else if (horizontal < 0)
236     {
237       er = sr - horizontal;
238     }
239
240     else if (vertical > 0) // scroll down
241     {
242       ss = es - vertical;
243       if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
244       {
245         ss = av.startSeq;
246       }
247       else
248       {
249         transY = imgHeight - vertical * avcharHeight;
250       }
251     }
252     else if (vertical < 0)
253     {
254       es = ss - vertical;
255       if (es > av.endSeq)
256       {
257         es = av.endSeq;
258       }
259     }
260
261     gg.translate(transX, transY);
262
263     drawPanel(gg, sr, er, ss, es, 0);
264     gg.translate(-transX, -transY);
265
266     repaint();
267
268   }
269
270   /**
271    * Definitions of startx and endx (hopefully): SMJS This is what I'm working
272    * towards! startx is the first residue (starting at 0) to display. endx is
273    * the last residue to display (starting at 0). starty is the first sequence
274    * to display (starting at 0). endy is the last sequence to display (starting
275    * at 0). NOTE 1: The av limits are set in setFont in this class and in the
276    * adjustment listener in SeqPanel when the scrollbars move.
277    */
278   public void update(Graphics g)
279   {
280     paint(g);
281   }
282
283   public void paint(Graphics g)
284   {
285
286     if (img != null
287             && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
288                     .getClipBounds().height)))
289     {
290       g.drawImage(img, 0, 0, this);
291       fastPaint = false;
292       return;
293     }
294
295     if (fastPaint)
296     {
297       g.drawImage(img, 0, 0, this);
298       fastPaint = false;
299       return;
300     }
301
302     updateViewport();
303     // this draws the whole of the alignment
304     imgWidth = this.getSize().width;
305     imgHeight = this.getSize().height;
306
307     imgWidth -= imgWidth % avcharWidth;
308     imgHeight -= imgHeight % avcharHeight;
309
310     if (imgWidth < 1 || imgHeight < 1)
311     {
312       return;
313     }
314
315     if (img == null || imgWidth != img.getWidth(this)
316             || imgHeight != img.getHeight(this))
317     {
318       img = createImage(imgWidth, imgHeight);
319       gg = img.getGraphics();
320       gg.setFont(av.getFont());
321     }
322
323     gg.setColor(Color.white);
324     gg.fillRect(0, 0, imgWidth, imgHeight);
325
326     if (av.getWrapAlignment())
327     {
328       drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
329     }
330     else
331     {
332       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
333     }
334
335     g.drawImage(img, 0, 0, this);
336
337   }
338
339   int LABEL_WEST, LABEL_EAST;
340
341   public int getWrappedCanvasWidth(int cwidth)
342   {
343     cwidth -= cwidth % av.getCharWidth();
344
345     FontMetrics fm = getFontMetrics(av.getFont());
346
347     LABEL_EAST = 0;
348     LABEL_WEST = 0;
349
350     if (av.getScaleRightWrapped())
351     {
352       LABEL_EAST = fm.stringWidth(getMask());
353     }
354
355     if (av.getScaleLeftWrapped())
356     {
357       LABEL_WEST = fm.stringWidth(getMask());
358     }
359
360     return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
361   }
362
363   /**
364    * Generates a string of zeroes.
365    * 
366    * @return String
367    */
368   String getMask()
369   {
370     String mask = "0";
371     int maxWidth = 0;
372     int tmp;
373     AlignmentI alignment = av.getAlignment();
374     for (int i = 0; i < alignment.getHeight(); i++)
375     {
376       tmp = alignment.getSequenceAt(i).getEnd();
377       if (tmp > maxWidth)
378       {
379         maxWidth = tmp;
380       }
381     }
382
383     for (int i = maxWidth; i > 0; i /= 10)
384     {
385       mask += "0";
386     }
387     return mask;
388   }
389
390   private void drawWrappedPanel(Graphics g, int canvasWidth,
391           int canvasHeight, int startRes)
392   {
393     AlignmentI al = av.getAlignment();
394
395     FontMetrics fm = getFontMetrics(av.getFont());
396
397     if (av.getScaleRightWrapped())
398     {
399       LABEL_EAST = fm.stringWidth(getMask());
400     }
401
402     if (av.getScaleLeftWrapped())
403     {
404       LABEL_WEST = fm.stringWidth(getMask());
405     }
406
407     int hgap = avcharHeight;
408     if (av.getScaleAboveWrapped())
409     {
410       hgap += avcharHeight;
411     }
412
413     int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
414     int cHeight = av.getAlignment().getHeight() * avcharHeight;
415
416     av.setWrappedWidth(cWidth);
417
418     av.endRes = av.startRes + cWidth;
419
420     int endx;
421     int ypos = hgap;
422
423     int maxwidth = av.getAlignment().getWidth() - 1;
424
425     if (av.hasHiddenColumns())
426     {
427       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
428     }
429
430     while ((ypos <= canvasHeight) && (startRes < maxwidth))
431     {
432       endx = startRes + cWidth - 1;
433
434       if (endx > maxwidth)
435       {
436         endx = maxwidth;
437       }
438
439       g.setColor(Color.black);
440
441       if (av.getScaleLeftWrapped())
442       {
443         drawWestScale(g, startRes, endx, ypos);
444       }
445
446       if (av.getScaleRightWrapped())
447       {
448         g.translate(canvasWidth - LABEL_EAST, 0);
449         drawEastScale(g, startRes, endx, ypos);
450         g.translate(-(canvasWidth - LABEL_EAST), 0);
451       }
452
453       g.translate(LABEL_WEST, 0);
454
455       if (av.getScaleAboveWrapped())
456       {
457         drawNorthScale(g, startRes, endx, ypos);
458       }
459       if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
460       {
461         g.setColor(Color.blue);
462         int res;
463         for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
464                 .size(); i++)
465         {
466           res = av.getColumnSelection().findHiddenRegionPosition(i)
467                   - startRes;
468
469           if (res < 0 || res > endx - startRes)
470           {
471             continue;
472           }
473
474           gg.fillPolygon(new int[]
475           { res * avcharWidth - avcharHeight / 4,
476               res * avcharWidth + avcharHeight / 4, res * avcharWidth },
477                   new int[]
478                   { ypos - (avcharHeight / 2), ypos - (avcharHeight / 2),
479                       ypos - (avcharHeight / 2) + 8 }, 3);
480
481         }
482       }
483
484       if (g.getClip() == null)
485       {
486         g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
487       }
488
489       drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
490       g.setClip(null);
491
492       if (av.isShowAnnotation())
493       {
494         g.translate(0, cHeight + ypos + 4);
495         if (annotations == null)
496         {
497           annotations = new AnnotationPanel(av);
498         }
499
500         annotations.drawComponent(g, startRes, endx + 1);
501         g.translate(0, -cHeight - ypos - 4);
502       }
503       g.translate(-LABEL_WEST, 0);
504
505       ypos += cHeight + getAnnotationHeight() + hgap;
506
507       startRes += cWidth;
508     }
509
510   }
511
512   AnnotationPanel annotations;
513
514   int getAnnotationHeight()
515   {
516     if (!av.isShowAnnotation())
517     {
518       return 0;
519     }
520
521     if (annotations == null)
522     {
523       annotations = new AnnotationPanel(av);
524     }
525
526     return annotations.adjustPanelHeight();
527   }
528
529   private void drawPanel(Graphics g1, int startRes, int endRes,
530           int startSeq,
531           int endSeq, int offset)
532   {
533
534
535     if (!av.hasHiddenColumns())
536     {
537       draw(g1, startRes, endRes, startSeq, endSeq, offset);
538     }
539     else
540     {
541
542       int screenY = 0;
543       int blockStart = startRes;
544       int blockEnd = endRes;
545
546       if (av.hasHiddenColumns())
547       {
548         for (int[] region : av.getColumnSelection().getHiddenColumns())
549         {
550           int hideStart = region[0];
551           int hideEnd = region[1];
552
553           if (hideStart <= blockStart)
554           {
555             blockStart += (hideEnd - hideStart) + 1;
556             continue;
557           }
558
559           blockEnd = hideStart - 1;
560
561           g1.translate(screenY * avcharWidth, 0);
562
563           draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
564
565           if (av.getShowHiddenMarkers())
566           {
567             g1.setColor(Color.blue);
568             g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
569                     0 + offset, (blockEnd - blockStart + 1) * avcharWidth
570                             - 1, (endSeq - startSeq) * avcharHeight
571                             + offset);
572           }
573
574           g1.translate(-screenY * avcharWidth, 0);
575           screenY += blockEnd - blockStart + 1;
576           blockStart = hideEnd + 1;
577         }
578       }
579       if (screenY <= (endRes - startRes))
580       {
581         blockEnd = blockStart + (endRes - startRes) - screenY;
582         g1.translate(screenY * avcharWidth, 0);
583         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
584
585         g1.translate(-screenY * avcharWidth, 0);
586       }
587     }
588
589   }
590
591   // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
592   // int x1, int x2, int y1, int y2, int startx, int starty,
593   void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
594           int offset)
595   {
596     g.setFont(av.getFont());
597     sr.prepare(g, av.isRenderGaps());
598     updateViewport();
599     SequenceI nextSeq;
600
601     // / First draw the sequences
602     // ///////////////////////////
603     for (int i = startSeq; i < endSeq; i++)
604     {
605       nextSeq = av.getAlignment().getSequenceAt(i);
606
607       if (nextSeq == null)
608       {
609         continue;
610       }
611
612       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
613               startRes, endRes, offset + ((i - startSeq) * avcharHeight));
614
615       if (av.isShowSequenceFeatures())
616       {
617         fr.drawSequence(g, nextSeq, startRes, endRes, offset
618                 + ((i - startSeq) * avcharHeight));
619       }
620
621       // / Highlight search Results once all sequences have been drawn
622       // ////////////////////////////////////////////////////////
623       if (searchResults != null)
624       {
625         int[] visibleResults = searchResults.getResults(nextSeq, startRes,
626                 endRes);
627         if (visibleResults != null)
628         {
629           for (int r = 0; r < visibleResults.length; r += 2)
630           {
631             sr.drawHighlightedText(nextSeq, visibleResults[r],
632                     visibleResults[r + 1], (visibleResults[r] - startRes)
633                             * avcharWidth, offset
634                             + ((i - startSeq) * avcharHeight));
635           }
636         }
637       }
638
639       if (av.cursorMode && cursorY == i && cursorX >= startRes
640               && cursorX <= endRes)
641       {
642         sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
643                 offset + ((i - startSeq) * avcharHeight));
644       }
645     }
646
647     if (av.getSelectionGroup() != null
648             || av.getAlignment().getGroups().size() > 0)
649     {
650       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
651     }
652
653   }
654
655   private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
656           int startSeq, int endSeq, int offset)
657   {
658     //
659     // ///////////////////////////////////
660     // Now outline any areas if necessary
661     // ///////////////////////////////////
662     SequenceGroup group = av.getSelectionGroup();
663
664     int sx = -1;
665     int sy = -1;
666     int ex = -1;
667     int groupIndex = -1;
668
669     if ((group == null) && (av.getAlignment().getGroups().size() > 0))
670     {
671       group = av.getAlignment().getGroups().get(0);
672       groupIndex = 0;
673     }
674
675     if (group != null)
676     {
677       do
678       {
679         int oldY = -1;
680         int i = 0;
681         boolean inGroup = false;
682         int top = -1;
683         int bottom = -1;
684         int alHeight = av.getAlignment().getHeight() - 1;
685
686         for (i = startSeq; i < endSeq; i++)
687         {
688           sx = (group.getStartRes() - startRes) * avcharWidth;
689           sy = offset + ((i - startSeq) * avcharHeight);
690           ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
691
692           if (sx + ex < 0 || sx > imgWidth)
693           {
694             continue;
695           }
696
697           if ((sx <= (endRes - startRes) * avcharWidth)
698                   && group.getSequences(null).contains(
699                           av.getAlignment().getSequenceAt(i)))
700           {
701             if ((bottom == -1)
702                     && (i >= alHeight || !group.getSequences(null)
703                             .contains(
704                                     av.getAlignment().getSequenceAt(i + 1))))
705             {
706               bottom = sy + avcharHeight;
707             }
708
709             if (!inGroup)
710             {
711               if (((top == -1) && (i == 0))
712                       || !group.getSequences(null).contains(
713                               av.getAlignment().getSequenceAt(i - 1)))
714               {
715                 top = sy;
716               }
717
718               oldY = sy;
719               inGroup = true;
720
721               if (group == av.getSelectionGroup())
722               {
723                 g.setColor(Color.red);
724               }
725               else
726               {
727                 g.setColor(group.getOutlineColour());
728               }
729             }
730           }
731           else
732           {
733             if (inGroup)
734             {
735               if (sx >= 0 && sx < imgWidth)
736               {
737                 g.drawLine(sx, oldY, sx, sy);
738               }
739
740               if (sx + ex < imgWidth)
741               {
742                 g.drawLine(sx + ex, oldY, sx + ex, sy);
743               }
744
745               if (sx < 0)
746               {
747                 ex += sx;
748                 sx = 0;
749               }
750
751               if (sx + ex > imgWidth)
752               {
753                 ex = imgWidth;
754               }
755
756               else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
757               {
758                 ex = (endRes - startRes + 1) * avcharWidth;
759               }
760
761               if (top != -1)
762               {
763                 g.drawLine(sx, top, sx + ex, top);
764                 top = -1;
765               }
766
767               if (bottom != -1)
768               {
769                 g.drawLine(sx, bottom, sx + ex, bottom);
770                 bottom = -1;
771               }
772
773               inGroup = false;
774             }
775           }
776         }
777
778         if (inGroup)
779         {
780           sy = offset + ((i - startSeq) * avcharHeight);
781           if (sx >= 0 && sx < imgWidth)
782           {
783             g.drawLine(sx, oldY, sx, sy);
784           }
785
786           if (sx + ex < imgWidth)
787           {
788             g.drawLine(sx + ex, oldY, sx + ex, sy);
789           }
790
791           if (sx < 0)
792           {
793             ex += sx;
794             sx = 0;
795           }
796
797           if (sx + ex > imgWidth)
798           {
799             ex = imgWidth;
800           }
801           else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
802           {
803             ex = (endRes - startRes + 1) * avcharWidth;
804           }
805
806           if (top != -1)
807           {
808             g.drawLine(sx, top, sx + ex, top);
809             top = -1;
810           }
811
812           if (bottom != -1)
813           {
814             g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
815             bottom = -1;
816           }
817
818           inGroup = false;
819         }
820
821         groupIndex++;
822
823         if (groupIndex >= av.getAlignment().getGroups().size())
824         {
825           break;
826         }
827
828         group = av.getAlignment().getGroups()
829                 .get(groupIndex);
830       } while (groupIndex < av.getAlignment().getGroups().size());
831
832     }
833   }
834
835   public void highlightSearchResults(SearchResults results)
836   {
837     searchResults = results;
838
839     repaint();
840   }
841
842 }