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