JAL-2388 Make startRes,startSeq,endRes,endSeq private with get/setters
[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.SearchResultsI;
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   boolean fastPaint = false;
54
55   int cursorX = 0;
56
57   int cursorY = 0;
58
59   public SeqCanvas(AlignViewport av)
60   {
61     this.av = av;
62     fr = new FeatureRenderer(av);
63     sr = new SequenceRenderer(av);
64     PaintRefresher.Register(this, av.getSequenceSetId());
65     updateViewport();
66   }
67
68   int avcharHeight = 0, avcharWidth = 0;
69
70   private void updateViewport()
71   {
72     avcharHeight = av.getCharHeight();
73     avcharWidth = av.getCharWidth();
74   }
75
76   public AlignmentViewport getViewport()
77   {
78     return av;
79   }
80
81   public FeatureRenderer getFeatureRenderer()
82   {
83     return fr;
84   }
85
86   public SequenceRenderer getSequenceRenderer()
87   {
88     return sr;
89   }
90
91   private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
92   {
93     updateViewport();
94     g.setColor(Color.black);
95     for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
96             endx))
97     {
98       int mpos = mark.column; // (i - startx - 1)
99       if (mpos < 0)
100       {
101         continue;
102       }
103       String mstring = mark.text;
104
105       if (mark.major)
106       {
107         if (mstring != null)
108         {
109           g.drawString(mstring, mpos * avcharWidth, ypos
110                   - (avcharHeight / 2));
111         }
112         g.drawLine((mpos * avcharWidth) + (avcharWidth / 2), (ypos + 2)
113                 - (avcharHeight / 2), (mpos * avcharWidth)
114                 + (avcharWidth / 2), ypos - 2);
115       }
116     }
117   }
118
119   private void drawWestScale(Graphics g, int startx, int endx, int ypos)
120   {
121     FontMetrics fm = getFontMetrics(av.getFont());
122     ypos += avcharHeight;
123     if (av.hasHiddenColumns())
124     {
125       startx = av.getColumnSelection().adjustForHiddenColumns(startx);
126       endx = av.getColumnSelection().adjustForHiddenColumns(endx);
127     }
128
129     int maxwidth = av.getAlignment().getWidth();
130     if (av.hasHiddenColumns())
131     {
132       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
133     }
134
135     // WEST SCALE
136     for (int i = 0; i < av.getAlignment().getHeight(); i++)
137     {
138       SequenceI seq = av.getAlignment().getSequenceAt(i);
139       int index = startx;
140       int value = -1;
141
142       while (index < endx)
143       {
144         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
145         {
146           index++;
147
148           continue;
149         }
150
151         value = av.getAlignment().getSequenceAt(i).findPosition(index);
152
153         break;
154       }
155
156       if (value != -1)
157       {
158         int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
159                 - avcharWidth / 2;
160         g.drawString(value + "", x, (ypos + (i * avcharHeight))
161                 - (avcharHeight / 5));
162       }
163     }
164   }
165
166   private void drawEastScale(Graphics g, int startx, int endx, int ypos)
167   {
168     ypos += avcharHeight;
169
170     if (av.hasHiddenColumns())
171     {
172       endx = av.getColumnSelection().adjustForHiddenColumns(endx);
173     }
174
175     SequenceI seq;
176     // EAST SCALE
177     for (int i = 0; i < av.getAlignment().getHeight(); i++)
178     {
179       seq = av.getAlignment().getSequenceAt(i);
180       int index = endx;
181       int value = -1;
182
183       while (index > startx)
184       {
185         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
186         {
187           index--;
188
189           continue;
190         }
191
192         value = seq.findPosition(index);
193
194         break;
195       }
196
197       if (value != -1)
198       {
199         g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
200                 - (avcharHeight / 5));
201       }
202     }
203   }
204
205   int lastsr = 0;
206
207   void fastPaint(int horizontal, int vertical)
208   {
209     if (fastPaint || gg == null)
210     {
211       return;
212     }
213
214     updateViewport();
215
216     // Its possible on certain browsers that the call to fastpaint
217     // is faster than it can paint, so this check here catches
218     // this possibility
219     if (lastsr + horizontal != av.getStartRes())
220     {
221       horizontal = av.getStartRes() - lastsr;
222     }
223
224     lastsr = av.getStartRes();
225
226     fastPaint = true;
227     gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
228             - horizontal * avcharWidth,
229             imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
230             -vertical * avcharHeight);
231
232     int sr = av.getStartRes(), er = av.getEndRes(), ss = av.getStartSeq(), es = av
233             .getEndSeq(), transX = 0, transY = 0;
234
235     if (horizontal > 0) // scrollbar pulled right, image to the left
236     {
237       transX = (er - sr - horizontal) * avcharWidth;
238       sr = er - horizontal;
239     }
240     else if (horizontal < 0)
241     {
242       er = sr - horizontal;
243     }
244
245     else if (vertical > 0) // scroll down
246     {
247       ss = es - vertical;
248       if (ss < av.getStartSeq()) // ie scrolling too fast, more than a page at a
249                                  // time
250       {
251         ss = av.getStartSeq();
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.getEndSeq())
262       {
263         es = av.getEndSeq();
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.getStartRes());
337     }
338     else
339     {
340       drawPanel(gg, av.getStartRes(), av.getEndRes(), av.getStartSeq(),
341               av.getEndSeq(), 0);
342     }
343
344     g.drawImage(img, 0, 0, this);
345
346   }
347
348   int LABEL_WEST, LABEL_EAST;
349
350   public int getWrappedCanvasWidth(int cwidth)
351   {
352     cwidth -= cwidth % av.getCharWidth();
353
354     FontMetrics fm = getFontMetrics(av.getFont());
355
356     LABEL_EAST = 0;
357     LABEL_WEST = 0;
358
359     if (av.getScaleRightWrapped())
360     {
361       LABEL_EAST = fm.stringWidth(getMask());
362     }
363
364     if (av.getScaleLeftWrapped())
365     {
366       LABEL_WEST = fm.stringWidth(getMask());
367     }
368
369     return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
370   }
371
372   /**
373    * Generates a string of zeroes.
374    * 
375    * @return String
376    */
377   String getMask()
378   {
379     String mask = "0";
380     int maxWidth = 0;
381     int tmp;
382     AlignmentI alignment = av.getAlignment();
383     for (int i = 0; i < alignment.getHeight(); i++)
384     {
385       tmp = alignment.getSequenceAt(i).getEnd();
386       if (tmp > maxWidth)
387       {
388         maxWidth = tmp;
389       }
390     }
391
392     for (int i = maxWidth; i > 0; i /= 10)
393     {
394       mask += "0";
395     }
396     return mask;
397   }
398
399   private void drawWrappedPanel(Graphics g, int canvasWidth,
400           int canvasHeight, int startRes)
401   {
402     AlignmentI al = av.getAlignment();
403
404     FontMetrics fm = getFontMetrics(av.getFont());
405
406     if (av.getScaleRightWrapped())
407     {
408       LABEL_EAST = fm.stringWidth(getMask());
409     }
410
411     if (av.getScaleLeftWrapped())
412     {
413       LABEL_WEST = fm.stringWidth(getMask());
414     }
415
416     int hgap = avcharHeight;
417     if (av.getScaleAboveWrapped())
418     {
419       hgap += avcharHeight;
420     }
421
422     int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
423     int cHeight = av.getAlignment().getHeight() * avcharHeight;
424
425     av.setWrappedWidth(cWidth);
426
427     av.setEndRes(av.getStartRes() + cWidth);
428
429     int endx;
430     int ypos = hgap;
431
432     int maxwidth = av.getAlignment().getWidth() - 1;
433
434     if (av.hasHiddenColumns())
435     {
436       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
437     }
438
439     while ((ypos <= canvasHeight) && (startRes < maxwidth))
440     {
441       endx = startRes + cWidth - 1;
442
443       if (endx > maxwidth)
444       {
445         endx = maxwidth;
446       }
447
448       g.setColor(Color.black);
449
450       if (av.getScaleLeftWrapped())
451       {
452         drawWestScale(g, startRes, endx, ypos);
453       }
454
455       if (av.getScaleRightWrapped())
456       {
457         g.translate(canvasWidth - LABEL_EAST, 0);
458         drawEastScale(g, startRes, endx, ypos);
459         g.translate(-(canvasWidth - LABEL_EAST), 0);
460       }
461
462       g.translate(LABEL_WEST, 0);
463
464       if (av.getScaleAboveWrapped())
465       {
466         drawNorthScale(g, startRes, endx, ypos);
467       }
468       if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
469       {
470         g.setColor(Color.blue);
471         int res;
472         for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
473                 .size(); i++)
474         {
475           res = av.getColumnSelection().findHiddenRegionPosition(i)
476                   - startRes;
477
478           if (res < 0 || res > endx - startRes)
479           {
480             continue;
481           }
482
483           gg.fillPolygon(new int[] { res * avcharWidth - avcharHeight / 4,
484               res * avcharWidth + avcharHeight / 4, res * avcharWidth },
485                   new int[] { ypos - (avcharHeight / 2),
486                       ypos - (avcharHeight / 2),
487                       ypos - (avcharHeight / 2) + 8 }, 3);
488
489         }
490       }
491
492       if (g.getClip() == null)
493       {
494         g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
495       }
496
497       drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
498       g.setClip(null);
499
500       if (av.isShowAnnotation())
501       {
502         g.translate(0, cHeight + ypos + 4);
503         if (annotations == null)
504         {
505           annotations = new AnnotationPanel(av);
506         }
507
508         annotations.drawComponent(g, startRes, endx + 1);
509         g.translate(0, -cHeight - ypos - 4);
510       }
511       g.translate(-LABEL_WEST, 0);
512
513       ypos += cHeight + getAnnotationHeight() + hgap;
514
515       startRes += cWidth;
516     }
517
518   }
519
520   AnnotationPanel annotations;
521
522   int getAnnotationHeight()
523   {
524     if (!av.isShowAnnotation())
525     {
526       return 0;
527     }
528
529     if (annotations == null)
530     {
531       annotations = new AnnotationPanel(av);
532     }
533
534     return annotations.adjustPanelHeight();
535   }
536
537   private void drawPanel(Graphics g1, int startRes, int endRes,
538           int startSeq, int endSeq, int offset)
539   {
540
541     if (!av.hasHiddenColumns())
542     {
543       draw(g1, startRes, endRes, startSeq, endSeq, offset);
544     }
545     else
546     {
547
548       int screenY = 0;
549       int blockStart = startRes;
550       int blockEnd = endRes;
551
552       if (av.hasHiddenColumns())
553       {
554         for (int[] region : av.getColumnSelection().getHiddenColumns())
555         {
556           int hideStart = region[0];
557           int hideEnd = region[1];
558
559           if (hideStart <= blockStart)
560           {
561             blockStart += (hideEnd - hideStart) + 1;
562             continue;
563           }
564
565           blockEnd = hideStart - 1;
566
567           g1.translate(screenY * avcharWidth, 0);
568
569           draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
570
571           if (av.getShowHiddenMarkers())
572           {
573             g1.setColor(Color.blue);
574             g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
575                     0 + offset, (blockEnd - blockStart + 1) * avcharWidth
576                             - 1, (endSeq - startSeq) * avcharHeight
577                             + offset);
578           }
579
580           g1.translate(-screenY * avcharWidth, 0);
581           screenY += blockEnd - blockStart + 1;
582           blockStart = hideEnd + 1;
583
584           if (screenY > (endRes - startRes))
585           {
586             // already rendered last block
587             return;
588           }
589         }
590       }
591       if (screenY <= (endRes - startRes))
592       {
593         // remaining visible region to render
594         blockEnd = blockStart + (endRes - startRes) - screenY;
595         g1.translate(screenY * avcharWidth, 0);
596         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
597
598         g1.translate(-screenY * avcharWidth, 0);
599       }
600     }
601
602   }
603
604   // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
605   // int x1, int x2, int y1, int y2, int startx, int starty,
606   void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
607           int offset)
608   {
609     g.setFont(av.getFont());
610     sr.prepare(g, av.isRenderGaps());
611     updateViewport();
612     SequenceI nextSeq;
613
614     // / First draw the sequences
615     // ///////////////////////////
616     for (int i = startSeq; i < endSeq; i++)
617     {
618       nextSeq = av.getAlignment().getSequenceAt(i);
619
620       if (nextSeq == null)
621       {
622         continue;
623       }
624
625       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
626               startRes, endRes, offset + ((i - startSeq) * avcharHeight));
627
628       if (av.isShowSequenceFeatures())
629       {
630         fr.drawSequence(g, nextSeq, startRes, endRes, offset
631                 + ((i - startSeq) * avcharHeight));
632       }
633
634       // / Highlight search Results once all sequences have been drawn
635       // ////////////////////////////////////////////////////////
636       if (av.hasSearchResults())
637       {
638         int[] visibleResults = av.getSearchResults().getResults(nextSeq,
639                 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(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(SearchResultsI results)
849   {
850     av.setSearchResults(results);
851     repaint();
852   }
853
854 }