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