JAL-3093 unit tests for SeqPanel.findMousePosition
[jalview.git] / src / jalview / gui / SeqPanel.java
index 3f740a1..0b62629 100644 (file)
@@ -255,7 +255,7 @@ public class SeqPanel extends JPanel
   int wrappedBlock = -1;
 
   /**
-   * Computes the column and sequence row (or possibly annotation row when in
+   * Computes the column and sequence row (and possibly annotation row when in
    * wrapped mode) for the given mouse position
    * 
    * @param evt
@@ -264,7 +264,7 @@ public class SeqPanel extends JPanel
   MousePos findMousePosition(MouseEvent evt)
   {
     int col = findColumn(evt);
-    int seq = -1;
+    int seqIndex = -1;
     int annIndex = -1;
     int y = evt.getY();
 
@@ -280,20 +280,23 @@ public class SeqPanel extends JPanel
 
       final int alignmentHeightPixels = alignmentHeight * charHeight + hgap;
       final int annotationHeight = seqCanvas.getAnnotationHeight();
-      final int cHeight = alignmentHeightPixels + annotationHeight;
+      final int cHeight = alignmentHeightPixels + annotationHeight
+              + SeqCanvas.SEQS_ANNOTATION_GAP;
 
       int yOffsetPx = y % cHeight; // yPos below repeating width(s)
-      if (yOffsetPx > alignmentHeightPixels)
+      if (yOffsetPx >= alignmentHeightPixels
+              + SeqCanvas.SEQS_ANNOTATION_GAP)
       {
         /*
-         * mouse is over annotations
+         * mouse is over annotations; find annotation index, also
+         * last sequence above (for backwards compatible behaviour)
          */
         AlignmentAnnotation[] anns = av.getAlignment()
                 .getAlignmentAnnotation();
-        int rowOffsetPx = yOffsetPx - alignmentHeightPixels;
+        int rowOffsetPx = yOffsetPx - alignmentHeightPixels
+                - SeqCanvas.SEQS_ANNOTATION_GAP;
         annIndex = AnnotationPanel.getRowIndex(rowOffsetPx, anns);
-        // also last sequence in alignment (for backwards compatible behaviour)
-        seq = alignmentHeight - 1;
+        seqIndex = alignmentHeight - 1;
       }
       else
       {
@@ -301,18 +304,17 @@ public class SeqPanel extends JPanel
          * mouse is over sequence (or the space above sequences)
          */
         yOffsetPx -= hgap;
-        if (yOffsetPx > 0)
+        if (yOffsetPx >= 0)
         {
-          seq = Math.min(yOffsetPx / charHeight, alignmentHeight - 1);
+          seqIndex = Math.min(yOffsetPx / charHeight, alignmentHeight - 1);
         }
       }
     }
     else
     {
-      seq = Math.min((y / charHeight) + av.getRanges().getStartSeq(),
+      seqIndex = Math.min((y / charHeight) + av.getRanges().getStartSeq(),
               alignmentHeight - 1);
     }
-    int seqIndex = seq;
 
     return new MousePos(col, seqIndex, annIndex);
   }