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