JAL-2439 Fixes to out-by-one sequence indexing in gui classes
[jalview.git] / src / jalview / appletgui / SeqCanvas.java
index eaa3a50..cd5aa74 100755 (executable)
 package jalview.appletgui;
 
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.SearchResults;
+import jalview.datamodel.SearchResultsI;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.renderer.ScaleRenderer;
+import jalview.renderer.ScaleRenderer.ScaleMark;
+import jalview.viewmodel.AlignmentViewport;
 
 import java.awt.Color;
 import java.awt.FontMetrics;
@@ -47,8 +50,6 @@ public class SeqCanvas extends Panel
 
   AlignViewport av;
 
-  SearchResults searchResults = null;
-
   boolean fastPaint = false;
 
   int cursorX = 0;
@@ -61,9 +62,18 @@ public class SeqCanvas extends Panel
     fr = new FeatureRenderer(av);
     sr = new SequenceRenderer(av);
     PaintRefresher.Register(this, av.getSequenceSetId());
+    updateViewport();
+  }
+
+  int avcharHeight = 0, avcharWidth = 0;
+
+  private void updateViewport()
+  {
+    avcharHeight = av.getCharHeight();
+    avcharWidth = av.getCharWidth();
   }
 
-  public AlignViewport getViewport()
+  public AlignmentViewport getViewport()
   {
     return av;
   }
@@ -78,35 +88,38 @@ public class SeqCanvas extends Panel
     return sr;
   }
 
-  void drawNorthScale(Graphics g, int startx, int endx, int ypos)
+  private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
   {
-    int scalestartx = startx - startx % 10 + 10;
-
+    updateViewport();
     g.setColor(Color.black);
-
-    // NORTH SCALE
-    for (int i = scalestartx; i < endx; i += 10)
+    for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
+            endx))
     {
-      int value = i;
-      if (av.hasHiddenColumns())
+      int mpos = mark.column; // (i - startx - 1)
+      if (mpos < 0)
       {
-        value = av.getColumnSelection().adjustForHiddenColumns(value);
+        continue;
       }
+      String mstring = mark.text;
 
-      g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
-              ypos - (av.charHeight / 2));
-
-      g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
-              (ypos + 2) - (av.charHeight / 2),
-              ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
-              ypos - 2);
+      if (mark.major)
+      {
+        if (mstring != null)
+        {
+          g.drawString(mstring, mpos * avcharWidth, ypos
+                  - (avcharHeight / 2));
+        }
+        g.drawLine((mpos * avcharWidth) + (avcharWidth / 2), (ypos + 2)
+                - (avcharHeight / 2), (mpos * avcharWidth)
+                + (avcharWidth / 2), ypos - 2);
+      }
     }
   }
 
-  void drawWestScale(Graphics g, int startx, int endx, int ypos)
+  private void drawWestScale(Graphics g, int startx, int endx, int ypos)
   {
     FontMetrics fm = getFontMetrics(av.getFont());
-    ypos += av.charHeight;
+    ypos += avcharHeight;
     if (av.hasHiddenColumns())
     {
       startx = av.getColumnSelection().adjustForHiddenColumns(startx);
@@ -143,16 +156,16 @@ public class SeqCanvas extends Panel
       if (value != -1)
       {
         int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
-                - av.charWidth / 2;
-        g.drawString(value + "", x, (ypos + (i * av.charHeight))
-                - (av.charHeight / 5));
+                - avcharWidth / 2;
+        g.drawString(value + "", x, (ypos + (i * avcharHeight))
+                - (avcharHeight / 5));
       }
     }
   }
 
-  void drawEastScale(Graphics g, int startx, int endx, int ypos)
+  private void drawEastScale(Graphics g, int startx, int endx, int ypos)
   {
-    ypos += av.charHeight;
+    ypos += avcharHeight;
 
     if (av.hasHiddenColumns())
     {
@@ -183,8 +196,8 @@ public class SeqCanvas extends Panel
 
       if (value != -1)
       {
-        g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
-                - (av.charHeight / 5));
+        g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
+                - (avcharHeight / 5));
       }
     }
   }
@@ -198,27 +211,30 @@ public class SeqCanvas extends Panel
       return;
     }
 
+    updateViewport();
+
     // Its possible on certain browsers that the call to fastpaint
     // is faster than it can paint, so this check here catches
     // this possibility
-    if (lastsr + horizontal != av.startRes)
+    if (lastsr + horizontal != av.getStartRes())
     {
-      horizontal = av.startRes - lastsr;
+      horizontal = av.getStartRes() - lastsr;
     }
 
-    lastsr = av.startRes;
+    lastsr = av.getStartRes();
 
     fastPaint = true;
-    gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
-            imgWidth - horizontal * av.charWidth, imgHeight - vertical
-                    * av.charHeight, -horizontal * av.charWidth, -vertical
-                    * av.charHeight);
+    gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
+            - horizontal * avcharWidth,
+            imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
+            -vertical * avcharHeight);
 
-    int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
+    int sr = av.getStartRes(), er = av.getEndRes(), ss = av.getStartSeq(), es = av
+            .getEndSeq(), transX = 0, transY = 0;
 
     if (horizontal > 0) // scrollbar pulled right, image to the left
     {
-      transX = (er - sr - horizontal) * av.charWidth;
+      transX = (er - sr - horizontal) * avcharWidth;
       sr = er - horizontal;
     }
     else if (horizontal < 0)
@@ -229,21 +245,22 @@ public class SeqCanvas extends Panel
     else if (vertical > 0) // scroll down
     {
       ss = es - vertical;
-      if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
+      if (ss < av.getStartSeq()) // ie scrolling too fast, more than a page at a
+                                 // time
       {
-        ss = av.startSeq;
+        ss = av.getStartSeq();
       }
       else
       {
-        transY = imgHeight - vertical * av.charHeight;
+        transY = imgHeight - ((vertical + 1) * avcharHeight);
       }
     }
     else if (vertical < 0)
     {
       es = ss - vertical;
-      if (es > av.endSeq)
+      if (es > av.getEndSeq())
       {
-        es = av.endSeq;
+        es = av.getEndSeq();
       }
     }
 
@@ -264,11 +281,13 @@ public class SeqCanvas extends Panel
    * at 0). NOTE 1: The av limits are set in setFont in this class and in the
    * adjustment listener in SeqPanel when the scrollbars move.
    */
+  @Override
   public void update(Graphics g)
   {
     paint(g);
   }
 
+  @Override
   public void paint(Graphics g)
   {
 
@@ -288,12 +307,13 @@ public class SeqCanvas extends Panel
       return;
     }
 
+    updateViewport();
     // this draws the whole of the alignment
     imgWidth = this.getSize().width;
     imgHeight = this.getSize().height;
 
-    imgWidth -= imgWidth % av.charWidth;
-    imgHeight -= imgHeight % av.charHeight;
+    imgWidth -= imgWidth % avcharWidth;
+    imgHeight -= imgHeight % avcharHeight;
 
     if (imgWidth < 1 || imgHeight < 1)
     {
@@ -313,11 +333,12 @@ public class SeqCanvas extends Panel
 
     if (av.getWrapAlignment())
     {
-      drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
+      drawWrappedPanel(gg, imgWidth, imgHeight, av.getStartRes());
     }
     else
     {
-      drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
+      drawPanel(gg, av.getStartRes(), av.getEndRes(), av.getStartSeq(),
+              av.getEndSeq(), 0);
     }
 
     g.drawImage(img, 0, 0, this);
@@ -328,24 +349,24 @@ public class SeqCanvas extends Panel
 
   public int getWrappedCanvasWidth(int cwidth)
   {
-    cwidth -= cwidth % av.charWidth;
+    cwidth -= cwidth % av.getCharWidth();
 
     FontMetrics fm = getFontMetrics(av.getFont());
 
     LABEL_EAST = 0;
     LABEL_WEST = 0;
 
-    if (av.scaleRightWrapped)
+    if (av.getScaleRightWrapped())
     {
       LABEL_EAST = fm.stringWidth(getMask());
     }
 
-    if (av.scaleLeftWrapped)
+    if (av.getScaleLeftWrapped())
     {
       LABEL_WEST = fm.stringWidth(getMask());
     }
 
-    return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
+    return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
   }
 
   /**
@@ -375,35 +396,35 @@ public class SeqCanvas extends Panel
     return mask;
   }
 
-  public void drawWrappedPanel(Graphics g, int canvasWidth,
+  private void drawWrappedPanel(Graphics g, int canvasWidth,
           int canvasHeight, int startRes)
   {
     AlignmentI al = av.getAlignment();
 
     FontMetrics fm = getFontMetrics(av.getFont());
 
-    if (av.scaleRightWrapped)
+    if (av.getScaleRightWrapped())
     {
       LABEL_EAST = fm.stringWidth(getMask());
     }
 
-    if (av.scaleLeftWrapped)
+    if (av.getScaleLeftWrapped())
     {
       LABEL_WEST = fm.stringWidth(getMask());
     }
 
-    int hgap = av.charHeight;
-    if (av.scaleAboveWrapped)
+    int hgap = avcharHeight;
+    if (av.getScaleAboveWrapped())
     {
-      hgap += av.charHeight;
+      hgap += avcharHeight;
     }
 
-    int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
-    int cHeight = av.getAlignment().getHeight() * av.charHeight;
+    int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
+    int cHeight = av.getAlignment().getHeight() * avcharHeight;
 
     av.setWrappedWidth(cWidth);
 
-    av.endRes = av.startRes + cWidth;
+    av.setEndRes(av.getStartRes() + cWidth);
 
     int endx;
     int ypos = hgap;
@@ -426,12 +447,12 @@ public class SeqCanvas extends Panel
 
       g.setColor(Color.black);
 
-      if (av.scaleLeftWrapped)
+      if (av.getScaleLeftWrapped())
       {
         drawWestScale(g, startRes, endx, ypos);
       }
 
-      if (av.scaleRightWrapped)
+      if (av.getScaleRightWrapped())
       {
         g.translate(canvasWidth - LABEL_EAST, 0);
         drawEastScale(g, startRes, endx, ypos);
@@ -440,11 +461,11 @@ public class SeqCanvas extends Panel
 
       g.translate(LABEL_WEST, 0);
 
-      if (av.scaleAboveWrapped)
+      if (av.getScaleAboveWrapped())
       {
         drawNorthScale(g, startRes, endx, ypos);
       }
-      if (av.hasHiddenColumns() && av.showHiddenMarkers)
+      if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
       {
         g.setColor(Color.blue);
         int res;
@@ -459,25 +480,24 @@ public class SeqCanvas extends Panel
             continue;
           }
 
-          gg.fillPolygon(new int[]
-          { res * av.charWidth - av.charHeight / 4,
-              res * av.charWidth + av.charHeight / 4, res * av.charWidth },
-                  new int[]
-                  { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
-                      ypos - (av.charHeight / 2) + 8 }, 3);
+          gg.fillPolygon(new int[] { res * avcharWidth - avcharHeight / 4,
+              res * avcharWidth + avcharHeight / 4, res * avcharWidth },
+                  new int[] { ypos - (avcharHeight / 2),
+                      ypos - (avcharHeight / 2),
+                      ypos - (avcharHeight / 2) + 8 }, 3);
 
         }
       }
 
       if (g.getClip() == null)
       {
-        g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
+        g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
       }
 
       drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
       g.setClip(null);
 
-      if (av.showAnnotation)
+      if (av.isShowAnnotation())
       {
         g.translate(0, cHeight + ypos + 4);
         if (annotations == null)
@@ -501,7 +521,7 @@ public class SeqCanvas extends Panel
 
   int getAnnotationHeight()
   {
-    if (!av.showAnnotation)
+    if (!av.isShowAnnotation())
     {
       return 0;
     }
@@ -514,11 +534,10 @@ public class SeqCanvas extends Panel
     return annotations.adjustPanelHeight();
   }
 
-  void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
-          int endSeq, int offset)
+  private void drawPanel(Graphics g1, int startRes, int endRes,
+          int startSeq, int endSeq, int offset)
   {
 
-
     if (!av.hasHiddenColumns())
     {
       draw(g1, startRes, endRes, startSeq, endSeq, offset);
@@ -545,31 +564,38 @@ public class SeqCanvas extends Panel
 
           blockEnd = hideStart - 1;
 
-          g1.translate(screenY * av.charWidth, 0);
+          g1.translate(screenY * avcharWidth, 0);
 
           draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
 
           if (av.getShowHiddenMarkers())
           {
             g1.setColor(Color.blue);
-            g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
-                    0 + offset, (blockEnd - blockStart + 1) * av.charWidth
-                            - 1, (endSeq - startSeq) * av.charHeight
+            g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
+                    0 + offset, (blockEnd - blockStart + 1) * avcharWidth
+                            - 1, (endSeq - startSeq) * avcharHeight
                             + offset);
           }
 
-          g1.translate(-screenY * av.charWidth, 0);
+          g1.translate(-screenY * avcharWidth, 0);
           screenY += blockEnd - blockStart + 1;
           blockStart = hideEnd + 1;
+
+          if (screenY > (endRes - startRes))
+          {
+            // already rendered last block
+            return;
+          }
         }
       }
       if (screenY <= (endRes - startRes))
       {
+        // remaining visible region to render
         blockEnd = blockStart + (endRes - startRes) - screenY;
-        g1.translate(screenY * av.charWidth, 0);
+        g1.translate(screenY * avcharWidth, 0);
         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
 
-        g1.translate(-screenY * av.charWidth, 0);
+        g1.translate(-screenY * avcharWidth, 0);
       }
     }
 
@@ -581,13 +607,13 @@ public class SeqCanvas extends Panel
           int offset)
   {
     g.setFont(av.getFont());
-    sr.prepare(g, av.renderGaps);
-
+    sr.prepare(g, av.isRenderGaps());
+    updateViewport();
     SequenceI nextSeq;
 
     // / First draw the sequences
     // ///////////////////////////
-    for (int i = startSeq; i < endSeq; i++)
+    for (int i = startSeq; i <= endSeq; i++)
     {
       nextSeq = av.getAlignment().getSequenceAt(i);
 
@@ -597,19 +623,20 @@ public class SeqCanvas extends Panel
       }
 
       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
-              startRes, endRes, offset + ((i - startSeq) * av.charHeight));
+              startRes, endRes, offset + ((i - startSeq) * avcharHeight));
 
       if (av.isShowSequenceFeatures())
       {
         fr.drawSequence(g, nextSeq, startRes, endRes, offset
-                + ((i - startSeq) * av.charHeight));
+                + ((i - startSeq) * avcharHeight));
       }
 
       // / Highlight search Results once all sequences have been drawn
       // ////////////////////////////////////////////////////////
-      if (searchResults != null)
+      if (av.hasSearchResults())
       {
-        int[] visibleResults = searchResults.getResults(nextSeq, startRes,
+        int[] visibleResults = av.getSearchResults().getResults(nextSeq,
+                startRes,
                 endRes);
         if (visibleResults != null)
         {
@@ -617,8 +644,8 @@ public class SeqCanvas extends Panel
           {
             sr.drawHighlightedText(nextSeq, visibleResults[r],
                     visibleResults[r + 1], (visibleResults[r] - startRes)
-                            * av.charWidth, offset
-                            + ((i - startSeq) * av.charHeight));
+                            * avcharWidth, offset
+                            + ((i - startSeq) * avcharHeight));
           }
         }
       }
@@ -626,9 +653,8 @@ public class SeqCanvas extends Panel
       if (av.cursorMode && cursorY == i && cursorX >= startRes
               && cursorX <= endRes)
       {
-        sr.drawCursor(nextSeq, cursorX,
-                (cursorX - startRes) * av.charWidth, offset
-                        + ((i - startSeq) * av.charHeight));
+        sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
+                offset + ((i - startSeq) * avcharHeight));
       }
     }
 
@@ -640,7 +666,7 @@ public class SeqCanvas extends Panel
 
   }
 
-  void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
+  private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
           int startSeq, int endSeq, int offset)
   {
     //
@@ -671,18 +697,18 @@ public class SeqCanvas extends Panel
         int bottom = -1;
         int alHeight = av.getAlignment().getHeight() - 1;
 
-        for (i = startSeq; i < endSeq; i++)
+        for (i = startSeq; i <= endSeq; i++)
         {
-          sx = (group.getStartRes() - startRes) * av.charWidth;
-          sy = offset + ((i - startSeq) * av.charHeight);
-          ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
+          sx = (group.getStartRes() - startRes) * avcharWidth;
+          sy = offset + ((i - startSeq) * avcharHeight);
+          ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
 
           if (sx + ex < 0 || sx > imgWidth)
           {
             continue;
           }
 
-          if ((sx <= (endRes - startRes) * av.charWidth)
+          if ((sx <= (endRes - startRes) * avcharWidth)
                   && group.getSequences(null).contains(
                           av.getAlignment().getSequenceAt(i)))
           {
@@ -691,7 +717,7 @@ public class SeqCanvas extends Panel
                             .contains(
                                     av.getAlignment().getSequenceAt(i + 1))))
             {
-              bottom = sy + av.charHeight;
+              bottom = sy + avcharHeight;
             }
 
             if (!inGroup)
@@ -741,9 +767,9 @@ public class SeqCanvas extends Panel
                 ex = imgWidth;
               }
 
-              else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
+              else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
               {
-                ex = (endRes - startRes + 1) * av.charWidth;
+                ex = (endRes - startRes + 1) * avcharWidth;
               }
 
               if (top != -1)
@@ -765,7 +791,7 @@ public class SeqCanvas extends Panel
 
         if (inGroup)
         {
-          sy = offset + ((i - startSeq) * av.charHeight);
+          sy = offset + ((i - startSeq) * avcharHeight);
           if (sx >= 0 && sx < imgWidth)
           {
             g.drawLine(sx, oldY, sx, sy);
@@ -786,9 +812,9 @@ public class SeqCanvas extends Panel
           {
             ex = imgWidth;
           }
-          else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
+          else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
           {
-            ex = (endRes - startRes + 1) * av.charWidth;
+            ex = (endRes - startRes + 1) * avcharWidth;
           }
 
           if (top != -1)
@@ -813,17 +839,15 @@ public class SeqCanvas extends Panel
           break;
         }
 
-        group = av.getAlignment().getGroups()
-                .get(groupIndex);
+        group = av.getAlignment().getGroups().get(groupIndex);
       } while (groupIndex < av.getAlignment().getGroups().size());
 
     }
   }
 
-  public void highlightSearchResults(SearchResults results)
+  public void highlightSearchResults(SearchResultsI results)
   {
-    searchResults = results;
-
+    av.setSearchResults(results);
     repaint();
   }