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