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