bf54c667b4177c3f7341bb26de413e671b1e705f
[jalview.git] / src / jalview / appletgui / SeqCanvas.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.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   @Override
284   public void paint(Graphics g)
285   {
286
287     if (img != null
288             && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
289                     .getClipBounds().height)))
290     {
291       g.drawImage(img, 0, 0, this);
292       fastPaint = false;
293       return;
294     }
295
296     if (fastPaint)
297     {
298       g.drawImage(img, 0, 0, this);
299       fastPaint = false;
300       return;
301     }
302
303     updateViewport();
304     // this draws the whole of the alignment
305     imgWidth = this.getSize().width;
306     imgHeight = this.getSize().height;
307
308     imgWidth -= imgWidth % avcharWidth;
309     imgHeight -= imgHeight % avcharHeight;
310
311     if (imgWidth < 1 || imgHeight < 1)
312     {
313       return;
314     }
315
316     if (img == null || imgWidth != img.getWidth(this)
317             || imgHeight != img.getHeight(this))
318     {
319       img = createImage(imgWidth, imgHeight);
320       gg = img.getGraphics();
321       gg.setFont(av.getFont());
322     }
323
324     gg.setColor(Color.white);
325     gg.fillRect(0, 0, imgWidth, imgHeight);
326
327     if (av.getWrapAlignment())
328     {
329       drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
330     }
331     else
332     {
333       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
334     }
335
336     g.drawImage(img, 0, 0, this);
337
338   }
339
340   int LABEL_WEST, LABEL_EAST;
341
342   public int getWrappedCanvasWidth(int cwidth)
343   {
344     cwidth -= cwidth % av.getCharWidth();
345
346     FontMetrics fm = getFontMetrics(av.getFont());
347
348     LABEL_EAST = 0;
349     LABEL_WEST = 0;
350
351     if (av.getScaleRightWrapped())
352     {
353       LABEL_EAST = fm.stringWidth(getMask());
354     }
355
356     if (av.getScaleLeftWrapped())
357     {
358       LABEL_WEST = fm.stringWidth(getMask());
359     }
360
361     return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
362   }
363
364   /**
365    * Generates a string of zeroes.
366    * 
367    * @return String
368    */
369   String getMask()
370   {
371     String mask = "0";
372     int maxWidth = 0;
373     int tmp;
374     AlignmentI alignment = av.getAlignment();
375     for (int i = 0; i < alignment.getHeight(); i++)
376     {
377       tmp = alignment.getSequenceAt(i).getEnd();
378       if (tmp > maxWidth)
379       {
380         maxWidth = tmp;
381       }
382     }
383
384     for (int i = maxWidth; i > 0; i /= 10)
385     {
386       mask += "0";
387     }
388     return mask;
389   }
390
391   private void drawWrappedPanel(Graphics g, int canvasWidth,
392           int canvasHeight, int startRes)
393   {
394     AlignmentI al = av.getAlignment();
395
396     FontMetrics fm = getFontMetrics(av.getFont());
397
398     if (av.getScaleRightWrapped())
399     {
400       LABEL_EAST = fm.stringWidth(getMask());
401     }
402
403     if (av.getScaleLeftWrapped())
404     {
405       LABEL_WEST = fm.stringWidth(getMask());
406     }
407
408     int hgap = avcharHeight;
409     if (av.getScaleAboveWrapped())
410     {
411       hgap += avcharHeight;
412     }
413
414     int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
415     int cHeight = av.getAlignment().getHeight() * avcharHeight;
416
417     av.setWrappedWidth(cWidth);
418
419     av.endRes = av.startRes + cWidth;
420
421     int endx;
422     int ypos = hgap;
423
424     int maxwidth = av.getAlignment().getWidth() - 1;
425
426     if (av.hasHiddenColumns())
427     {
428       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
429     }
430
431     while ((ypos <= canvasHeight) && (startRes < maxwidth))
432     {
433       endx = startRes + cWidth - 1;
434
435       if (endx > maxwidth)
436       {
437         endx = maxwidth;
438       }
439
440       g.setColor(Color.black);
441
442       if (av.getScaleLeftWrapped())
443       {
444         drawWestScale(g, startRes, endx, ypos);
445       }
446
447       if (av.getScaleRightWrapped())
448       {
449         g.translate(canvasWidth - LABEL_EAST, 0);
450         drawEastScale(g, startRes, endx, ypos);
451         g.translate(-(canvasWidth - LABEL_EAST), 0);
452       }
453
454       g.translate(LABEL_WEST, 0);
455
456       if (av.getScaleAboveWrapped())
457       {
458         drawNorthScale(g, startRes, endx, ypos);
459       }
460       if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
461       {
462         g.setColor(Color.blue);
463         int res;
464         for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
465                 .size(); i++)
466         {
467           res = av.getColumnSelection().findHiddenRegionPosition(i)
468                   - startRes;
469
470           if (res < 0 || res > endx - startRes)
471           {
472             continue;
473           }
474
475           gg.fillPolygon(new int[]
476           { res * avcharWidth - avcharHeight / 4,
477               res * avcharWidth + avcharHeight / 4, res * avcharWidth },
478                   new int[]
479                   { ypos - (avcharHeight / 2), ypos - (avcharHeight / 2),
480                       ypos - (avcharHeight / 2) + 8 }, 3);
481
482         }
483       }
484
485       if (g.getClip() == null)
486       {
487         g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
488       }
489
490       drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
491       g.setClip(null);
492
493       if (av.isShowAnnotation())
494       {
495         g.translate(0, cHeight + ypos + 4);
496         if (annotations == null)
497         {
498           annotations = new AnnotationPanel(av);
499         }
500
501         annotations.drawComponent(g, startRes, endx + 1);
502         g.translate(0, -cHeight - ypos - 4);
503       }
504       g.translate(-LABEL_WEST, 0);
505
506       ypos += cHeight + getAnnotationHeight() + hgap;
507
508       startRes += cWidth;
509     }
510
511   }
512
513   AnnotationPanel annotations;
514
515   int getAnnotationHeight()
516   {
517     if (!av.isShowAnnotation())
518     {
519       return 0;
520     }
521
522     if (annotations == null)
523     {
524       annotations = new AnnotationPanel(av);
525     }
526
527     return annotations.adjustPanelHeight();
528   }
529
530   private void drawPanel(Graphics g1, int startRes, int endRes,
531           int startSeq,
532           int endSeq, int offset)
533   {
534
535
536     if (!av.hasHiddenColumns())
537     {
538       draw(g1, startRes, endRes, startSeq, endSeq, offset);
539     }
540     else
541     {
542
543       int screenY = 0;
544       int blockStart = startRes;
545       int blockEnd = endRes;
546
547       if (av.hasHiddenColumns())
548       {
549         for (int[] region : av.getColumnSelection().getHiddenColumns())
550         {
551           int hideStart = region[0];
552           int hideEnd = region[1];
553
554           if (hideStart <= blockStart)
555           {
556             blockStart += (hideEnd - hideStart) + 1;
557             continue;
558           }
559
560           blockEnd = hideStart - 1;
561
562           g1.translate(screenY * avcharWidth, 0);
563
564           draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
565
566           if (av.getShowHiddenMarkers())
567           {
568             g1.setColor(Color.blue);
569             g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
570                     0 + offset, (blockEnd - blockStart + 1) * avcharWidth
571                             - 1, (endSeq - startSeq) * avcharHeight
572                             + offset);
573           }
574
575           g1.translate(-screenY * avcharWidth, 0);
576           screenY += blockEnd - blockStart + 1;
577           blockStart = hideEnd + 1;
578         }
579       }
580       if (screenY <= (endRes - startRes))
581       {
582         blockEnd = blockStart + (endRes - startRes) - screenY;
583         g1.translate(screenY * avcharWidth, 0);
584         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
585
586         g1.translate(-screenY * avcharWidth, 0);
587       }
588     }
589
590   }
591
592   // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
593   // int x1, int x2, int y1, int y2, int startx, int starty,
594   void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
595           int offset)
596   {
597     g.setFont(av.getFont());
598     sr.prepare(g, av.isRenderGaps());
599     updateViewport();
600     SequenceI nextSeq;
601
602     // / First draw the sequences
603     // ///////////////////////////
604     for (int i = startSeq; i < endSeq; i++)
605     {
606       nextSeq = av.getAlignment().getSequenceAt(i);
607
608       if (nextSeq == null)
609       {
610         continue;
611       }
612
613       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
614               startRes, endRes, offset + ((i - startSeq) * avcharHeight));
615
616       if (av.isShowSequenceFeatures())
617       {
618         fr.drawSequence(g, nextSeq, startRes, endRes, offset
619                 + ((i - startSeq) * avcharHeight));
620       }
621
622       // / Highlight search Results once all sequences have been drawn
623       // ////////////////////////////////////////////////////////
624       if (searchResults != null)
625       {
626         int[] visibleResults = searchResults.getResults(nextSeq, startRes,
627                 endRes);
628         if (visibleResults != null)
629         {
630           for (int r = 0; r < visibleResults.length; r += 2)
631           {
632             sr.drawHighlightedText(nextSeq, visibleResults[r],
633                     visibleResults[r + 1], (visibleResults[r] - startRes)
634                             * avcharWidth, offset
635                             + ((i - startSeq) * avcharHeight));
636           }
637         }
638       }
639
640       if (av.cursorMode && cursorY == i && cursorX >= startRes
641               && cursorX <= endRes)
642       {
643         sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
644                 offset + ((i - startSeq) * avcharHeight));
645       }
646     }
647
648     if (av.getSelectionGroup() != null
649             || av.getAlignment().getGroups().size() > 0)
650     {
651       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
652     }
653
654   }
655
656   private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
657           int startSeq, int endSeq, int offset)
658   {
659     //
660     // ///////////////////////////////////
661     // Now outline any areas if necessary
662     // ///////////////////////////////////
663     SequenceGroup group = av.getSelectionGroup();
664
665     int sx = -1;
666     int sy = -1;
667     int ex = -1;
668     int groupIndex = -1;
669
670     if ((group == null) && (av.getAlignment().getGroups().size() > 0))
671     {
672       group = av.getAlignment().getGroups().get(0);
673       groupIndex = 0;
674     }
675
676     if (group != null)
677     {
678       do
679       {
680         int oldY = -1;
681         int i = 0;
682         boolean inGroup = false;
683         int top = -1;
684         int bottom = -1;
685         int alHeight = av.getAlignment().getHeight() - 1;
686
687         for (i = startSeq; i < endSeq; i++)
688         {
689           sx = (group.getStartRes() - startRes) * avcharWidth;
690           sy = offset + ((i - startSeq) * avcharHeight);
691           ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
692
693           if (sx + ex < 0 || sx > imgWidth)
694           {
695             continue;
696           }
697
698           if ((sx <= (endRes - startRes) * avcharWidth)
699                   && group.getSequences(null).contains(
700                           av.getAlignment().getSequenceAt(i)))
701           {
702             if ((bottom == -1)
703                     && (i >= alHeight || !group.getSequences(null)
704                             .contains(
705                                     av.getAlignment().getSequenceAt(i + 1))))
706             {
707               bottom = sy + avcharHeight;
708             }
709
710             if (!inGroup)
711             {
712               if (((top == -1) && (i == 0))
713                       || !group.getSequences(null).contains(
714                               av.getAlignment().getSequenceAt(i - 1)))
715               {
716                 top = sy;
717               }
718
719               oldY = sy;
720               inGroup = true;
721
722               if (group == av.getSelectionGroup())
723               {
724                 g.setColor(Color.red);
725               }
726               else
727               {
728                 g.setColor(group.getOutlineColour());
729               }
730             }
731           }
732           else
733           {
734             if (inGroup)
735             {
736               if (sx >= 0 && sx < imgWidth)
737               {
738                 g.drawLine(sx, oldY, sx, sy);
739               }
740
741               if (sx + ex < imgWidth)
742               {
743                 g.drawLine(sx + ex, oldY, sx + ex, sy);
744               }
745
746               if (sx < 0)
747               {
748                 ex += sx;
749                 sx = 0;
750               }
751
752               if (sx + ex > imgWidth)
753               {
754                 ex = imgWidth;
755               }
756
757               else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
758               {
759                 ex = (endRes - startRes + 1) * avcharWidth;
760               }
761
762               if (top != -1)
763               {
764                 g.drawLine(sx, top, sx + ex, top);
765                 top = -1;
766               }
767
768               if (bottom != -1)
769               {
770                 g.drawLine(sx, bottom, sx + ex, bottom);
771                 bottom = -1;
772               }
773
774               inGroup = false;
775             }
776           }
777         }
778
779         if (inGroup)
780         {
781           sy = offset + ((i - startSeq) * avcharHeight);
782           if (sx >= 0 && sx < imgWidth)
783           {
784             g.drawLine(sx, oldY, sx, sy);
785           }
786
787           if (sx + ex < imgWidth)
788           {
789             g.drawLine(sx + ex, oldY, sx + ex, sy);
790           }
791
792           if (sx < 0)
793           {
794             ex += sx;
795             sx = 0;
796           }
797
798           if (sx + ex > imgWidth)
799           {
800             ex = imgWidth;
801           }
802           else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
803           {
804             ex = (endRes - startRes + 1) * avcharWidth;
805           }
806
807           if (top != -1)
808           {
809             g.drawLine(sx, top, sx + ex, top);
810             top = -1;
811           }
812
813           if (bottom != -1)
814           {
815             g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
816             bottom = -1;
817           }
818
819           inGroup = false;
820         }
821
822         groupIndex++;
823
824         if (groupIndex >= av.getAlignment().getGroups().size())
825         {
826           break;
827         }
828
829         group = av.getAlignment().getGroups()
830                 .get(groupIndex);
831       } while (groupIndex < av.getAlignment().getGroups().size());
832
833     }
834   }
835
836   public void highlightSearchResults(SearchResults results)
837   {
838     searchResults = results;
839
840     repaint();
841   }
842
843 }