Merge branch 'develop' into feature/JAL-2611
authorkiramt <k.mourao@dundee.ac.uk>
Sat, 22 Jul 2017 09:18:41 +0000 (11:18 +0200)
committerkiramt <k.mourao@dundee.ac.uk>
Sat, 22 Jul 2017 09:18:41 +0000 (11:18 +0200)
Conflicts:
src/jalview/gui/OverviewPanel.java
src/jalview/viewmodel/OverviewDimensionsHideHidden.java
src/jalview/viewmodel/OverviewDimensionsShowHidden.java

src/jalview/appletgui/OverviewPanel.java
src/jalview/datamodel/HiddenSequences.java
src/jalview/gui/OverviewPanel.java
src/jalview/viewmodel/OverviewDimensions.java
src/jalview/viewmodel/OverviewDimensionsHideHidden.java
src/jalview/viewmodel/OverviewDimensionsShowHidden.java
test/jalview/viewmodel/OverviewDimensionsHideHiddenTest.java
test/jalview/viewmodel/OverviewDimensionsShowHiddenTest.java

index ccdfee1..456a38c 100755 (executable)
@@ -29,6 +29,7 @@ import jalview.viewmodel.ViewportListenerI;
 
 import java.awt.BorderLayout;
 import java.awt.CheckboxMenuItem;
+import java.awt.Cursor;
 import java.awt.Dimension;
 import java.awt.Panel;
 import java.awt.PopupMenu;
@@ -57,6 +58,8 @@ public class OverviewPanel extends Panel implements Runnable,
 
   private boolean updateRunning = false;
 
+  private boolean draggingBox = false;
+
   public OverviewPanel(AlignmentPanel alPanel)
   {
     this.av = alPanel.av;
@@ -118,28 +121,62 @@ public class OverviewPanel extends Panel implements Runnable,
   @Override
   public void mouseMoved(MouseEvent evt)
   {
+    if (od.isPositionInBox(evt.getX(), evt.getY()))
+    {
+      // display drag cursor at mouse position
+      setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
+    }
+    else
+    {
+      // reset cursor
+      setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
+    }
   }
 
   @Override
   public void mousePressed(MouseEvent evt)
   {
-    mouseAction(evt);
+    if ((evt.getModifiers()
+            & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
+    {
+      if (!Platform.isAMac())
+      {
+        showPopupMenu(evt);
+      }
+    }
+    else
+    {
+      if (!od.isPositionInBox(evt.getX(), evt.getY()))
+      {
+        // don't do anything if the mouse press is in the overview's box
+        // (wait to see if it's a drag instead)
+        // otherwise update the viewport
+        od.updateViewportFromMouse(evt.getX(), evt.getY(),
+                av.getAlignment().getHiddenSequences(),
+                av.getAlignment().getHiddenColumns());
+      }
+      else
+      {
+        draggingBox = true;
+        od.setDragPoint(evt.getX(), evt.getY(),
+                av.getAlignment().getHiddenSequences(),
+                av.getAlignment().getHiddenColumns());
+      }
+    }
   }
 
   @Override
   public void mouseReleased(MouseEvent evt)
   {
-    mouseAction(evt);
+    if (draggingBox)
+    {
+      draggingBox = false;
+    }
   }
 
   @Override
   public void mouseDragged(MouseEvent evt)
   {
-    mouseAction(evt);
-  }
-
-  private void mouseAction(MouseEvent evt)
-  {
     if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
     {
       if (!Platform.isAMac())
@@ -149,8 +186,20 @@ public class OverviewPanel extends Panel implements Runnable,
     }
     else
     {
-      od.updateViewportFromMouse(evt.getX(), evt.getY(), av.getAlignment()
-              .getHiddenSequences(), av.getAlignment().getHiddenColumns());
+      if (draggingBox)
+      {
+        // set the mouse position as a fixed point in the box
+        // and drag relative to that position
+        od.adjustViewportFromMouse(evt.getX(), evt.getY(),
+                av.getAlignment().getHiddenSequences(),
+                av.getAlignment().getHiddenColumns());
+      }
+      else
+      {
+        od.updateViewportFromMouse(evt.getX(), evt.getY(),
+                av.getAlignment().getHiddenSequences(),
+                av.getAlignment().getHiddenColumns());
+      }
       ap.paintAlignment(false);
     }
   }
index 98e9694..32443d8 100755 (executable)
@@ -312,7 +312,7 @@ public class HiddenSequences
       return startRow - visibleDistance;
     }
 
-    int index = startRow;
+    int index = Math.min(startRow, hiddenSequences.length - 1);
     int count = 0;
     while ((index > -1) && (count < visibleDistance))
     {
index 7a4456e..c81ac2f 100755 (executable)
@@ -28,6 +28,7 @@ import jalview.viewmodel.OverviewDimensionsShowHidden;
 import jalview.viewmodel.ViewportListenerI;
 
 import java.awt.BorderLayout;
+import java.awt.Cursor;
 import java.awt.Dimension;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
@@ -65,6 +66,8 @@ public class OverviewPanel extends JPanel implements Runnable,
 
   private boolean showHidden = true;
 
+  private boolean draggingBox = false;
+
   /**
    * Creates a new OverviewPanel object.
    * 
@@ -109,9 +112,35 @@ public class OverviewPanel extends JPanel implements Runnable,
       {
         if (!SwingUtilities.isRightMouseButton(evt))
         {
-          od.updateViewportFromMouse(evt.getX(), evt.getY(), av
+          if (draggingBox)
+          {
+            // set the mouse position as a fixed point in the box
+            // and drag relative to that position
+            od.adjustViewportFromMouse(evt.getX(),
+                    evt.getY(), av.getAlignment().getHiddenSequences(),
+                    av.getAlignment().getHiddenColumns());
+          }
+          else
+          {
+            od.updateViewportFromMouse(evt.getX(), evt.getY(), av
                   .getAlignment().getHiddenSequences(), av.getAlignment()
                   .getHiddenColumns());
+          }
+        }
+      }
+
+      @Override
+      public void mouseMoved(MouseEvent evt)
+      {
+        if (od.isPositionInBox(evt.getX(), evt.getY()))
+        {
+          // display drag cursor at mouse position
+          setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
+        }
+        else
+        {
+          // reset cursor
+          setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
         }
       }
     });
@@ -131,9 +160,31 @@ public class OverviewPanel extends JPanel implements Runnable,
         else
         // if (!av.getWrapAlignment())
         {
-          od.updateViewportFromMouse(evt.getX(), evt.getY(), av
-                  .getAlignment().getHiddenSequences(), av.getAlignment()
-                  .getHiddenColumns());
+          if (!od.isPositionInBox(evt.getX(), evt.getY()))
+          {
+            // don't do anything if the mouse press is in the overview's box
+            // (wait to see if it's a drag instead)
+            // otherwise update the viewport
+            od.updateViewportFromMouse(evt.getX(), evt.getY(),
+                    av.getAlignment().getHiddenSequences(),
+                    av.getAlignment().getHiddenColumns());
+          }
+          else
+          {
+            draggingBox = true;
+            od.setDragPoint(evt.getX(), evt.getY(),
+                    av.getAlignment().getHiddenSequences(),
+                    av.getAlignment().getHiddenColumns());
+          }
+        }
+      }
+
+      @Override
+      public void mouseReleased(MouseEvent evt)
+      {
+        if (draggingBox)
+        {
+          draggingBox = false;
         }
       }
 
index 7ac07ac..f54c1eb 100644 (file)
@@ -192,6 +192,44 @@ public abstract class OverviewDimensions
           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
 
   /**
+   * Update the viewport location from a mouse drag within the overview's box
+   * 
+   * @param mousex
+   *          x location of mouse
+   * @param mousey
+   *          y location of mouse
+   * @param hiddenSeqs
+   *          the alignment's hidden sequences
+   * @param hiddenCols
+   *          the alignment's hidden columns
+   */
+  public abstract void adjustViewportFromMouse(int mousex, int mousey,
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
+
+  /**
+   * Initialise dragging from the mouse - must be called on initial mouse click
+   * before using adjustViewportFromMouse in drag operations
+   * 
+   * @param mousex
+   *          x location of mouse
+   * @param mousey
+   *          y location of mouse
+   * @param hiddenSeqs
+   *          the alignment's hidden sequences
+   * @param hiddenCols
+   *          the alignment's hidden columns
+   */
+  public abstract void setDragPoint(int x, int y,
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
+
+  /*
+   * Move the viewport so that the top left corner of the overview's box 
+   * is at the mouse position (leftx, topy)
+   */
+  protected abstract void updateViewportFromTopLeft(int leftx, int topy,
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
+
+  /**
    * Set the overview panel's box position to match the viewport
    * 
    * @param hiddenSeqs
@@ -225,6 +263,9 @@ public abstract class OverviewDimensions
    */
   protected abstract void resetAlignmentDims();
 
+  /*
+   * Given the box coordinates in residues and sequences, set the box dimensions in the overview window
+   */
   protected void setBoxPosition(int startRes, int startSeq, int vpwidth,
           int vpheight)
   {
@@ -241,4 +282,31 @@ public abstract class OverviewDimensions
     // boxHeight is the height in sequences translated to pixels
     boxHeight = Math.round((float) vpheight * sequencesHeight / alheight);
   }
+
+  /**
+   * Answers if a mouse position is in the overview's red box
+   * 
+   * @param x
+   *          mouse x position
+   * @param y
+   *          mouse y position
+   * @return true if (x,y) is inside the box
+   */
+  public boolean isPositionInBox(int x, int y)
+  {
+    return (x > boxX && y > boxY && x < boxX + boxWidth
+            && y < boxY + boxHeight);
+  }
+
+  /*
+   * Given the centre x position, calculate the box's left x position
+   */
+  protected abstract int getLeftXFromCentreX(int mousex, HiddenColumns hidden);
+
+  /*
+   * Given the centre y position, calculate the box's top y position
+   */
+  protected abstract int getTopYFromCentreY(int mousey,
+          HiddenSequences hidden);
+
 }
\ No newline at end of file
index 4d64f1c..c78b8a3 100644 (file)
@@ -12,6 +12,12 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions
 {
   private ViewportRanges ranges;
 
+  private int xdiff; // when dragging, difference in alignment units between
+                     // start residue and original mouse click position
+
+  private int ydiff; // when dragging, difference in alignment units between
+                     // start sequence and original mouse click position
+
   public OverviewDimensionsHideHidden(ViewportRanges vpranges,
           boolean showAnnotationPanel)
   {
@@ -24,37 +30,51 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions
   public void updateViewportFromMouse(int mousex, int mousey,
           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
   {
-    resetAlignmentDims();
+    int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
+    int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
+
+    updateViewportFromTopLeft(xAsRes, yAsSeq, hiddenSeqs, hiddenCols);
+
+  }
+
+  @Override
+  public void adjustViewportFromMouse(int mousex, int mousey,
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
+  {
+    // calculate translation in pixel terms:
+    // get mouse location in viewport coords, add translation in viewport
+    // coords, and update viewport as usual
+    int vpx = Math.round((float) mousex * alwidth / width);
+    int vpy = Math.round((float) mousey * alheight / sequencesHeight);
 
-    int x = mousex;
-    int y = mousey;
+    updateViewportFromTopLeft(vpx + xdiff, vpy + ydiff, hiddenSeqs,
+            hiddenCols);
 
-    if (x < 0)
+  }
+
+  @Override
+  protected void updateViewportFromTopLeft(int leftx, int topy,
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
+  {
+    int xAsRes = leftx;
+    int yAsSeq = topy;
+    resetAlignmentDims();
+
+    if (xAsRes < 0)
     {
-      x = 0;
+      xAsRes = 0;
     }
 
-    if (y < 0)
+    if (yAsSeq < 0)
     {
-      y = 0;
+      yAsSeq = 0;
     }
 
     if (ranges.isWrappedMode())
     {
-      y = 0; // sorry, no vertical scroll when wrapped
+      yAsSeq = 0; // sorry, no vertical scroll when wrapped
     }
 
-    //
-    // Convert x value to residue position
-    //
-
-    // need to determine where scrollCol should be, given x
-    // to do this also need to know width of viewport, and some hidden column
-    // correction
-
-    // convert x to residues - this is an absolute position
-    int xAsRes = Math.round((float) x * alwidth / width);
-
     // get viewport width in residues
     int vpwidth = ranges.getViewportWidth();
 
@@ -73,13 +93,7 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions
       }
     }
 
-
-    //
-    // Convert y value to sequence position
-    //
-
-    // convert y to residues
-    int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
+    // Determine where scrollRow should be, given visYAsSeq
 
     // get viewport height in sequences
     // add 1 because height includes both endSeq and startSeq
@@ -101,7 +115,6 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions
     // update viewport
     ranges.setStartRes(xAsRes);
     ranges.setStartSeq(yAsSeq);
-
   }
 
   @Override
@@ -132,4 +145,32 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions
     alwidth = ranges.getVisibleAlignmentWidth();
     alheight = ranges.getVisibleAlignmentHeight();
   }
+
+  @Override
+  protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
+  {
+    int vpx = Math.round((float) mousex * alwidth / width);
+    return vpx - ranges.getViewportWidth() / 2;
+  }
+
+  @Override
+  protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
+  {
+    int vpy = Math.round((float) mousey * alheight / sequencesHeight);
+    return vpy - ranges.getViewportHeight() / 2;
+  }
+
+  @Override
+  public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs,
+          HiddenColumns hiddenCols)
+  {
+    // get alignment position of x and box (can get directly from vpranges) and
+    // calculate difference between the positions
+    int vpx = Math.round((float) x * alwidth / width);
+    int vpy = Math.round((float) y * alheight / sequencesHeight);
+
+    xdiff = ranges.getStartRes() - vpx;
+    ydiff = ranges.getStartSeq() - vpy;
+  }
+
 }
index 62e8000..5bd4bba 100644 (file)
@@ -32,6 +32,12 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
 {
   private ViewportRanges ranges;
 
+  private int xdiff; // when dragging, difference in alignment units between
+                     // start residue and original mouse click position
+
+  private int ydiff; // when dragging, difference in alignment units between
+                     // start sequence and original mouse click position
+
   /**
    * Create an OverviewDimensions object
    * 
@@ -66,46 +72,66 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
   public void updateViewportFromMouse(int mousex, int mousey,
           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
   {
-    int x = mousex;
-    int y = mousey;
+    // convert mousex and mousey to alignment units as well as
+    // translating to top left corner of viewport - this is an absolute position
+    int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
+    int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
 
-    resetAlignmentDims();
+    // convert to visible positions
+    int visXAsRes = hiddenCols.findColumnPosition(xAsRes);
+    yAsSeq = hiddenSeqs.adjustForHiddenSeqs(
+            hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq));
+    yAsSeq = Math.max(yAsSeq, 0); // -1 if before first visible sequence
+    int visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
+    visYAsSeq = Math.max(visYAsSeq, 0); // -1 if before first visible sequence
 
-    if (x < 0)
-    {
-      x = 0;
-    }
+    // update viewport accordingly
+    updateViewportFromTopLeft(visXAsRes, visYAsSeq, hiddenSeqs, hiddenCols);
+  }
+
+  @Override
+  public void adjustViewportFromMouse(int mousex, int mousey,
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
+  {
+    // calculate translation in pixel terms:
+    // get mouse location in viewport coords, add translation in viewport
+    // coords,
+    // convert back to pixel coords
+    int vpx = Math.round((float) mousex * alwidth / width);
+    int visXAsRes = hiddenCols.findColumnPosition(vpx) + xdiff;
+
+    int vpy = Math.round((float) mousey * alheight / sequencesHeight);
+    int visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(vpy) + ydiff;
+
+    // update viewport accordingly
+    updateViewportFromTopLeft(visXAsRes, visYAsRes,
+            hiddenSeqs,
+            hiddenCols);
+  }
+
+  @Override
+  protected void updateViewportFromTopLeft(int leftx, int topy,
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
+  {
+    int visXAsRes = leftx;
+    int visYAsSeq = topy;
+    resetAlignmentDims();
 
-    if (y < 0)
+    if (visXAsRes < 0)
     {
-      y = 0;
+      visXAsRes = 0;
     }
 
-    if (ranges.isWrappedMode())
+    if (visYAsSeq < 0)
     {
-      y = 0; // sorry, no vertical scroll when wrapped
+      visYAsSeq = 0;
     }
 
-    //
-    // Convert x value to residue position
-    //
-
-    // need to determine where scrollCol should be, given x
-    // to do this also need to know width of viewport, and some hidden column
-    // correction
-
-    // convert x to residues - this is an absolute position
-    int xAsRes = Math.round((float) x * alwidth / width);
+    // Determine where scrollCol should be, given visXAsRes
 
     // get viewport width in residues
     int vpwidth = ranges.getViewportWidth();
 
-    // get where x should be when accounting for hidden cols
-    // if x is in a hidden col region, shift to left - but we still need
-    // absolute position
-    // so convert back after getting visible region position
-    int visXAsRes = hiddenCols.findColumnPosition(xAsRes);
-
     // check in case we went off the edge of the alignment
     int visAlignWidth = hiddenCols.findColumnPosition(alwidth - 1);
     if (visXAsRes + vpwidth - 1 > visAlignWidth)
@@ -124,28 +150,14 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
       }
     }
 
-    //
-    // Convert y value to sequence position
-    //
-
-    // convert y to residues
-    int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
+    // Determine where scrollRow should be, given visYAsSeq
 
     // get viewport height in sequences
     int vpheight = ranges.getViewportHeight();
 
-    // get where y should be when accounting for hidden rows
-    // if y is in a hidden row region, shift up - but we still need absolute
-    // position,
-    // so convert back after getting visible region position
-    yAsSeq = hiddenSeqs.adjustForHiddenSeqs(hiddenSeqs
-            .findIndexWithoutHiddenSeqs(yAsSeq));
-    yAsSeq = Math.max(yAsSeq, 0); // -1 if before first visible sequence
-
     // check in case we went off the edge of the alignment
     int visAlignHeight = hiddenSeqs.findIndexWithoutHiddenSeqs(alheight);
-    int visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
-    visYAsSeq = Math.max(visYAsSeq, 0); // -1 if before first visible sequence
+
     if (visYAsSeq + vpheight - 1 > visAlignHeight)
     {
       // went past the end of the alignment, adjust backwards
@@ -213,4 +225,34 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
     alwidth = ranges.getAbsoluteAlignmentWidth();
     alheight = ranges.getAbsoluteAlignmentHeight();
   }
+
+  @Override
+  protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
+  {
+    int vpx = Math.round((float) mousex * alwidth / width);
+    return hidden.subtractVisibleColumns(ranges.getViewportWidth() / 2,
+            vpx);
+  }
+
+  @Override
+  protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
+  {
+    int vpy = Math.round((float) mousey * alheight / sequencesHeight);
+    return hidden.subtractVisibleRows(ranges.getViewportHeight() / 2, vpy);
+  }
+
+  @Override
+  public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs,
+          HiddenColumns hiddenCols)
+  {
+    // get alignment position of x and box (can get directly from vpranges) and
+    // calculate difference between the positions
+    int vpx = Math.round((float) x * alwidth / width);
+    int vpy = Math.round((float) y * alheight / sequencesHeight);
+
+    xdiff = ranges.getStartRes() - hiddenCols.findColumnPosition(vpx);
+    ydiff = ranges.getStartSeq()
+            - hiddenSeqs.findIndexWithoutHiddenSeqs(vpy);
+  }
+
 }
index 0e931eb..f6a9d32 100644 (file)
@@ -21,6 +21,8 @@
 package jalview.viewmodel;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
 
 import jalview.analysis.AlignmentGenerator;
 import jalview.datamodel.Alignment;
@@ -56,7 +58,7 @@ public class OverviewDimensionsHideHiddenTest
 
   ViewportRanges vpranges;
 
-  Hashtable<SequenceI, SequenceCollectionI> hiddenRepSequences = new Hashtable<SequenceI, SequenceCollectionI>();
+  Hashtable<SequenceI, SequenceCollectionI> hiddenRepSequences = new Hashtable<>();
 
   HiddenColumns hiddenCols = new HiddenColumns();
 
@@ -210,7 +212,7 @@ public class OverviewDimensionsHideHiddenTest
     assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
-    assertEquals(vpranges.getStartSeq(),
+    assertEquals(vpranges.getStartSeq() + vpranges.getViewportHeight() / 2,
             Math.round((float) 10 * alheight / od.getSequencesHeight()));
     assertEquals(vpranges.getStartRes(), 0);
 
@@ -219,14 +221,13 @@ public class OverviewDimensionsHideHiddenTest
     assertEquals(od.getBoxY(), 0);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
-    assertEquals(vpranges.getStartRes(),
-            Math.round((float) 6 * alwidth / od.getWidth()));
+    assertEquals(vpranges.getStartRes(), 0);
     assertEquals(vpranges.getStartSeq(), 0);
 
     // overly large boxX value reset to width-boxWidth
-    mouseClick(od, 100, 6);
+    mouseClick(od, 101, 6);
     assertEquals(od.getBoxX(), od.getWidth() - od.getBoxWidth());
-    assertEquals(od.getBoxY(), 6);
+    assertEquals(od.getBoxY(), 1);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(vpranges.getStartRes(),
@@ -238,7 +239,7 @@ public class OverviewDimensionsHideHiddenTest
 
     // overly large boxY value reset to sequenceHeight - boxHeight
     mouseClick(od, 10, 520);
-    assertEquals(od.getBoxX(), 10);
+    assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxY(), od.getSequencesHeight() - od.getBoxHeight());
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
@@ -266,18 +267,19 @@ public class OverviewDimensionsHideHiddenTest
                     / od.getSequencesHeight()));
 
     // move viewport so startRes non-zero and then mouseclick
-    moveViewportH(50);
+    moveViewportH(20);
 
     // click at viewport position
     int oldboxx = od.getBoxX();
     int oldboxy = od.getBoxY();
-    mouseClick(od, od.getBoxX() + 5, od.getBoxY() + 2);
-    assertEquals(od.getBoxX(), oldboxx + 5);
+    mouseClick(od, od.getBoxX() + od.getBoxWidth() / 2 + 6,
+            od.getBoxY() + od.getBoxHeight() / 2 + 3);
+    assertEquals(od.getBoxX(), oldboxx + 6);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(vpranges.getStartRes(),
             Math.round((float) od.getBoxX() * alwidth / od.getWidth()));
-    assertEquals(od.getBoxY(), oldboxy + 2);
+    assertEquals(od.getBoxY(), oldboxy + 3);
     assertEquals(
             vpranges.getStartSeq(),
             Math.round((float) od.getBoxY() * alheight
@@ -311,20 +313,20 @@ public class OverviewDimensionsHideHiddenTest
     int lastHiddenCol = 30;
     hiddenCols.hideColumns(0, lastHiddenCol);
 
-    testBoxIsAtClickPoint(0, 0);
+    testBoxIsAtClickPoint(boxWidth / 2, boxHeight / 2);
 
     // click to right of hidden columns, box moves to click point
-    testBoxIsAtClickPoint(40, 0);
+    testBoxIsAtClickPoint(41 + boxWidth / 2, boxHeight / 2);
     assertEquals(vpranges.getStartSeq(), 0);
     assertEquals(vpranges.getStartRes(),
-            Math.round((float) 40 * alwidth / od.getWidth()));
+            Math.round((float) 41 * alwidth / od.getWidth()));
 
     // click to right of hidden columns such that box runs over right hand side
     // of alignment
     // box position is adjusted away from the edge
     // overly large boxX value reset to width-boxWidth
-    int xpos = 100;
-    mouseClick(od, xpos, 0);
+    int xpos = 100 + boxWidth / 2;
+    mouseClick(od, xpos, boxHeight / 2);
     assertEquals(od.getBoxX(), Math.round(od.getWidth()) - boxWidth);
     assertEquals(od.getBoxY(), 0);
     assertEquals(od.getBoxWidth(), boxWidth);
@@ -342,7 +344,7 @@ public class OverviewDimensionsHideHiddenTest
   public void testFromMouseWithHiddenColsInMiddle()
   {
     od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols);
-    testBoxIsAtClickPoint(0, 0);
+    testBoxIsAtClickPoint(boxWidth / 2, boxHeight / 2);
     assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxY(), 0);
     assertEquals(od.getBoxWidth(), boxWidth);
@@ -355,7 +357,7 @@ public class OverviewDimensionsHideHiddenTest
     hiddenCols.hideColumns(firstHidden, lastHidden);
 
     od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
-    testBoxIsAtClickPoint(0, 0);
+    testBoxIsAtClickPoint(boxWidth / 2, boxHeight / 2);
     assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxY(), 0);
     assertEquals(od.getBoxWidth(), boxWidth);
@@ -364,45 +366,51 @@ public class OverviewDimensionsHideHiddenTest
 
     // move box so that it overlaps with hidden cols on one side
     // box width, boxX and scrollCol as for unhidden case
-    int xpos = 55 - boxWidth; // 55 is position in overview approx halfway
+    int xpos = 54 - boxWidth / 2; // 54 is position in overview approx halfway
                               // between cols 60 and 70
-    mouseClick(od, xpos, 0);
-    testBoxIsAtClickPoint(xpos, 0);
-    assertEquals(vpranges.getStartRes(),
-            Math.round(xpos * alwidth / od.getWidth()));
+    mouseClick(od, xpos, boxHeight / 2);
+    testBoxIsAtClickPoint(xpos, boxHeight / 2);
+    assertEquals(vpranges.getStartRes(), 1 + // rounding
+            Math.round((xpos - boxWidth / 2) * alwidth / od.getWidth()));
     assertEquals(vpranges.getStartSeq(), 0);
 
     // move box so that it completely covers hidden cols
     // box width, boxX and scrollCol as for unhidden case
     xpos = 33;
-    mouseClick(od, xpos, 0);
-    testBoxIsAtClickPoint(xpos, 0);
+    mouseClick(od, xpos, boxHeight / 2);
+    testBoxIsAtClickPoint(xpos, boxHeight / 2);
     assertEquals(vpranges.getStartRes(),
-            Math.round((float) xpos * alwidth / od.getWidth()));
+            Math.round((float) (xpos - boxWidth / 2) * alwidth
+                    / od.getWidth()));
     assertEquals(vpranges.getStartSeq(), 0);
 
     // move box so boxX is in hidden cols, box overhangs at right
     // boxX and scrollCol at left of hidden area, box width unchanged
-    xpos = 50;
-    mouseClick(od, xpos, 0);
-    testBoxIsAtClickPoint(xpos, 0);
-    assertEquals(vpranges.getStartRes(),
-            Math.round((float) xpos * alwidth / od.getWidth()));
+    xpos = Math.round((float) 50 * od.getWidth() / alwidth) + boxWidth / 2;
+    mouseClick(od, xpos, boxHeight / 2);
+    assertEquals(od.getBoxX() + od.getBoxWidth() / 2, xpos);
+    assertEquals(od.getBoxY(), 0);
+    assertEquals(od.getBoxWidth(), boxWidth);
+    assertEquals(od.getBoxHeight(), boxHeight);
+    assertEquals(vpranges.getStartRes(), 50);
     assertEquals(vpranges.getStartSeq(), 0);
 
     // move box so boxX is to right of hidden cols, but does not go beyond full
     // width of alignment
     // box width, boxX and scrollCol all as for non-hidden case
-    xpos = 75;
-    testBoxIsAtClickPoint(xpos, 0);
+    xpos = Math.round((float) 75 * od.getWidth() / alwidth) + boxWidth / 2;
+    mouseClick(od, xpos, boxHeight / 2);
+    assertEquals(od.getBoxX() + od.getBoxWidth() / 2, xpos);
+    assertEquals(od.getBoxY(), 0);
+    assertEquals(od.getBoxWidth(), boxWidth);
+    assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(vpranges.getStartSeq(), 0);
-    assertEquals(vpranges.getStartRes(),
-            Math.round(xpos * alwidth / od.getWidth()));
+    assertEquals(vpranges.getStartRes(), 75);
     
     // move box so it goes beyond full width of alignment
     // boxX, scrollCol adjusted back, box width normal
     xpos = 3000;
-    mouseClick(od, xpos, 0);
+    mouseClick(od, xpos, boxHeight / 2);
     assertEquals(od.getBoxX(), Math.round(od.getWidth()) - boxWidth);
     assertEquals(od.getBoxY(), 0);
     assertEquals(od.getBoxWidth(), boxWidth);
@@ -440,19 +448,22 @@ public class OverviewDimensionsHideHiddenTest
 
     // click to left of hidden cols, without overlapping
     // boxX, scrollCol and width as normal
-    int xpos = 5;
-    testBoxIsAtClickPoint(xpos, 0);
-    assertEquals(vpranges.getStartSeq(), 0);
-    assertEquals(vpranges.getStartRes(),
-            Math.round((float) xpos * alwidth / od.getWidth()));
+    int xpos = 30;
+    int ypos = 6;
+    testBoxIsAtClickPoint(xpos, ypos);
+    assertEquals(vpranges.getStartSeq(), Math.round(
+            (float) (ypos - boxHeight / 2) * alheight / od.getHeight()));
+    assertEquals(vpranges.getStartRes(), Math.round(
+            (float) (xpos - boxWidth / 2) * alwidth / od.getWidth()));
 
     // click to left of hidden cols, with overlap
     // boxX and scrollCol adjusted for hidden cols, width normal
-    xpos = Math.round((float) 145 * od.getWidth() / alwidth) - boxWidth;
-    mouseClick(od, xpos, 0);
-    testBoxIsAtClickPoint(xpos, 0);
+    xpos = Math.round((float) 144 * od.getWidth() / alwidth) - boxWidth;
+    mouseClick(od, xpos, boxHeight / 2);
+    testBoxIsAtClickPoint(xpos, boxHeight / 2);
     assertEquals(vpranges.getStartRes(),
-            Math.round((float) xpos * alwidth / od.getWidth()));
+            Math.round((float) (xpos - boxWidth / 2) * alwidth
+                    / od.getWidth()));
     assertEquals(vpranges.getStartSeq(), 0);
 
     // click off end of alignment
@@ -777,9 +788,9 @@ public class OverviewDimensionsHideHiddenTest
     assertEquals(od.getBoxHeight(), boxHeight);
 
     // click below hidden rows
-    mouseClick(od, 0, 150);
+    mouseClick(od, 0, 151 + boxHeight / 2);
     assertEquals(od.getBoxX(), 0);
-    assertEquals(od.getBoxY(), 150);
+    assertEquals(od.getBoxY(), 151);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
   }
@@ -825,22 +836,22 @@ public class OverviewDimensionsHideHiddenTest
     assertEquals(od.getBoxHeight(), boxHeight);
 
     // click above hidden rows, so that box overlaps
-    int ypos = 35; // column value in residues
+    int ypos = 35 + viewHeight / 2; // row value in residues
     mouseClick(od, 0,
             Math.round((float) ypos * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxY(),
-            Math.round((float) ypos * od.getSequencesHeight() / alheight));
+            Math.round((float) 35 * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
 
     // click so that box straddles hidden rows
-    ypos = 44; // column value in residues
+    ypos = 45 + viewHeight / 2; // row value in residues
     mouseClick(od, 0,
             Math.round((float) ypos * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxY(),
-            Math.round((float) ypos * od.getSequencesHeight() / alheight));
+            Math.round((float) 45 * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
   }
@@ -884,18 +895,18 @@ public class OverviewDimensionsHideHiddenTest
     assertEquals(od.getBoxHeight(), boxHeight);
 
     // click above hidden rows
-    int ypos = 40; // row 40
+    int ypos = 41 + viewHeight / 2; // row 41
     mouseClick(od, 0,
             Math.round((float) ypos * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxY(),
-            Math.round((float) ypos * od.getSequencesHeight() / alheight));
+            Math.round((float) 41 * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
 
     // click above hidden rows so box overlaps
     // boxY, boxHeight remains same
-    ypos = 497; // row 497
+    ypos = 497 + viewHeight / 2; // row 497
     mouseClick(od, 0,
             Math.round((float) ypos * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxX(), 0);
@@ -908,6 +919,86 @@ public class OverviewDimensionsHideHiddenTest
     assertEquals(od.getBoxHeight(), boxHeight);
   }
 
+  /**
+   * Test the function to determine if a point is in the overview's box or not
+   */
+  @Test(groups = { "Functional" })
+  public void testPositionInBox()
+  {
+    od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols);
+
+    assertFalse(od.isPositionInBox(0, 0));
+    assertTrue(od.isPositionInBox(10, 9));
+    assertFalse(od.isPositionInBox(0, 9));
+    assertFalse(od.isPositionInBox(9, 0));
+    assertFalse(od.isPositionInBox(75, 20));
+
+    // hide columns in the box area
+    // makes absolutely no difference
+    hiddenCols.hideColumns(1, 4);
+    od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
+    assertFalse(od.isPositionInBox(0, 0));
+    assertTrue(od.isPositionInBox(10, 9));
+    assertFalse(od.isPositionInBox(0, 9));
+    assertFalse(od.isPositionInBox(9, 0));
+    assertFalse(od.isPositionInBox(75, 20));
+
+    // hide sequences in box area
+    // makes absolutely no difference
+    hideSequences(1, 3);
+    od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
+    assertFalse(od.isPositionInBox(0, 0));
+    assertTrue(od.isPositionInBox(10, 9));
+    assertFalse(od.isPositionInBox(0, 9));
+    assertFalse(od.isPositionInBox(9, 0));
+    assertFalse(od.isPositionInBox(75, 20));
+  }
+
+  /**
+   * Test the dragging functionality
+   */
+  @Test(groups = { "Functional" })
+  public void testDragging()
+  {
+    od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols);
+    od.setDragPoint(4, 16, al.getHiddenSequences(),
+            hiddenCols);
+    od.adjustViewportFromMouse(20, 22,
+            al.getHiddenSequences(), hiddenCols);
+
+    // updates require an OverviewPanel to exist which it doesn't here
+    // so call setBoxPosition() as it would be called by the AlignmentPanel
+    // normally
+    od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
+
+    // corner moves 16 (20-4) right and 6 (22-16) up
+    assertEquals(od.getBoxX(), 16);
+    assertEquals(od.getBoxY(), 6);
+
+    // hide columns - makes no difference
+    hiddenCols.hideColumns(1, 4);
+    od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols);
+    od.setDragPoint(4, 16, al.getHiddenSequences(), hiddenCols);
+    od.adjustViewportFromMouse(20, 22, al.getHiddenSequences(), hiddenCols);
+    od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
+
+    // corner moves 16 (20-4) right and 6 (22-16) up
+    assertEquals(od.getBoxX(), 16);
+    assertEquals(od.getBoxY(), 6);
+
+    // hide sequences in box area
+    // makes absolutely no difference
+    hideSequences(1, 3);
+    od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols);
+    od.setDragPoint(4, 16, al.getHiddenSequences(), hiddenCols);
+    od.adjustViewportFromMouse(20, 22, al.getHiddenSequences(), hiddenCols);
+    od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
+
+    // corner moves 16 (20-4) right and 6 (22-16) up
+    assertEquals(od.getBoxX(), 16);
+    assertEquals(od.getBoxY(), 6);
+  }
+
   /*
    * Move viewport horizontally: startRes + previous width gives new horizontal extent. Vertical extent stays the same.
    */
@@ -956,8 +1047,8 @@ public class OverviewDimensionsHideHiddenTest
   private void testBoxIsAtClickPoint(int xpos, int ypos)
   {
     mouseClick(od, xpos, ypos);
-    assertEquals(od.getBoxX(), xpos);
-    assertEquals(od.getBoxY(), ypos);
+    assertEquals(od.getBoxX() + od.getBoxWidth() / 2, xpos);
+    assertEquals(od.getBoxY() + od.getBoxHeight() / 2, ypos);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
 
index 1bc3bfa..df8c162 100644 (file)
@@ -21,6 +21,8 @@
 package jalview.viewmodel;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
 
 import jalview.analysis.AlignmentGenerator;
 import jalview.datamodel.Alignment;
@@ -55,7 +57,7 @@ public class OverviewDimensionsShowHiddenTest
 
   ViewportRanges vpranges;
 
-  Hashtable<SequenceI, SequenceCollectionI> hiddenRepSequences = new Hashtable<SequenceI, SequenceCollectionI>();
+  Hashtable<SequenceI, SequenceCollectionI> hiddenRepSequences = new Hashtable<>();
 
   HiddenColumns hiddenCols = new HiddenColumns();
 
@@ -209,7 +211,7 @@ public class OverviewDimensionsShowHiddenTest
     assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
-    assertEquals(vpranges.getStartSeq(),
+    assertEquals(vpranges.getStartSeq() + vpranges.getViewportHeight() / 2,
             Math.round((float) 10 * alheight / od.getSequencesHeight()));
     assertEquals(vpranges.getStartRes(), 0);
 
@@ -218,14 +220,13 @@ public class OverviewDimensionsShowHiddenTest
     assertEquals(od.getBoxY(), 0);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
-    assertEquals(vpranges.getStartRes(),
-            Math.round((float) 6 * alwidth / od.getWidth()));
+    assertEquals(vpranges.getStartRes(), 0);
     assertEquals(vpranges.getStartSeq(), 0);
 
     // overly large boxX value reset to width-boxWidth
-    mouseClick(od, 100, 6);
+    mouseClick(od, 101, 6);
     assertEquals(od.getBoxX(), od.getWidth() - od.getBoxWidth());
-    assertEquals(od.getBoxY(), 6);
+    assertEquals(od.getBoxY(), 1);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(vpranges.getStartRes(),
@@ -237,11 +238,11 @@ public class OverviewDimensionsShowHiddenTest
 
     // overly large boxY value reset to sequenceHeight - boxHeight
     mouseClick(od, 10, 520);
-    assertEquals(od.getBoxX(), 10);
+    assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxY(), od.getSequencesHeight() - od.getBoxHeight());
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
-    assertEquals(vpranges.getStartRes(),
+    assertEquals(0,
             Math.round((float) od.getBoxX() * alwidth / od.getWidth()));
 
     // here (float) od.getBoxY() * alheight / od.getSequencesHeight() = 507.5
@@ -265,18 +266,19 @@ public class OverviewDimensionsShowHiddenTest
                     / od.getSequencesHeight()));
 
     // move viewport so startRes non-zero and then mouseclick
-    moveViewportH(50);
+    moveViewportH(20);
 
     // click at viewport position
     int oldboxx = od.getBoxX();
     int oldboxy = od.getBoxY();
-    mouseClick(od, od.getBoxX() + 5, od.getBoxY() + 2);
-    assertEquals(od.getBoxX(), oldboxx + 5);
+    mouseClick(od, od.getBoxX() + od.getBoxWidth() / 2 + 6,
+            od.getBoxY() + od.getBoxHeight() / 2 + 3);
+    assertEquals(od.getBoxX(), oldboxx + 6);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(vpranges.getStartRes(),
             Math.round((float) od.getBoxX() * alwidth / od.getWidth()));
-    assertEquals(od.getBoxY(), oldboxy + 2);
+    assertEquals(od.getBoxY(), oldboxy + 3);
     assertEquals(
             vpranges.getStartSeq(),
             Math.round((float) od.getBoxY() * alheight
@@ -332,10 +334,15 @@ public class OverviewDimensionsShowHiddenTest
     assertEquals(vpranges.getStartRes(), 0);
 
     // click to right of hidden columns, box moves to click point
-    testBoxIsAtClickPoint(40, 0);
+    mouseClick(od, 60 + boxWidth / 2, boxHeight / 2);
+    assertEquals(od.getBoxX(), 60);
+    assertEquals(od.getBoxY(), 0);
+    assertEquals(od.getBoxWidth(), boxWidth);
+    assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(vpranges.getStartSeq(), 0);
     assertEquals(vpranges.getStartRes(),
-            Math.round((float) 40 * alwidth / od.getWidth())
+            Math.round(
+                    (float) 60 * alwidth / od.getWidth())
                     - (lastHiddenCol + 1));
 
     // click to right of hidden columns such that box runs over right hand side
@@ -343,7 +350,7 @@ public class OverviewDimensionsShowHiddenTest
     // box position is adjusted away from the edge
     // overly large boxX value reset to width-boxWidth
     xpos = 100;
-    mouseClick(od, xpos, 5);
+    mouseClick(od, xpos + boxWidth / 2, 5 + boxHeight / 2);
     assertEquals(od.getBoxX(), od.getWidth() - od.getBoxWidth());
     assertEquals(od.getBoxY(), 5);
     assertEquals(od.getBoxWidth(), boxWidth);
@@ -385,10 +392,10 @@ public class OverviewDimensionsShowHiddenTest
 
     // move box so that it overlaps with hidden cols on one side
     // box width changes, boxX and scrollCol as for unhidden case
-    int xpos = 55 - boxWidth; // 55 is position in overview approx halfway
+    int xpos = 54 - boxWidth / 2; // 54 is position in overview approx halfway
                               // between cols 60 and 70
-    mouseClick(od, xpos, 0);
-    assertEquals(od.getBoxX(), xpos);
+    mouseClick(od, xpos, boxHeight / 2);
+    assertEquals(od.getBoxX(), xpos - boxWidth / 2);
     assertEquals(od.getBoxY(), 0);
     assertEquals(
             od.getBoxWidth(),
@@ -396,14 +403,15 @@ public class OverviewDimensionsShowHiddenTest
                     * od.getWidth() / alwidth));
     assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(vpranges.getStartRes(),
-            Math.round(xpos * alwidth / od.getWidth()));
+            Math.round((xpos - boxWidth / 2) * alwidth / od.getWidth())
+                    + 1); // +1 for rounding
     assertEquals(vpranges.getStartSeq(), 0);
 
     // move box so that it completely covers hidden cols
     // box width changes, boxX and scrollCol as for hidden case
-    xpos = 33;
+    xpos = 24 + boxWidth / 2;
     mouseClick(od, xpos, 0);
-    assertEquals(od.getBoxX(), xpos);
+    assertEquals(od.getBoxX(), 24);
     assertEquals(od.getBoxY(), 0);
     assertEquals(
             od.getBoxWidth(),
@@ -411,42 +419,28 @@ public class OverviewDimensionsShowHiddenTest
                     * od.getWidth() / alwidth));
     assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(vpranges.getStartRes(),
-            Math.round((float) xpos * alwidth / od.getWidth()));
-    assertEquals(vpranges.getStartSeq(), 0);
-
-    // move box so boxX is in hidden cols, box overhangs at right
-    // boxX and scrollCol at left of hidden area, box width extends across
-    // hidden region
-    xpos = 50;
-    mouseClick(od, xpos, 0);
-    assertEquals(od.getBoxX(),
-            Math.round((float) (firstHidden - 1) * od.getWidth() / alwidth));
-    assertEquals(od.getBoxY(), 0);
-    assertEquals(
-            od.getBoxWidth(),
-            boxWidth
-                    + Math.round((float) (lastHidden - firstHidden + 1)
-                            * od.getWidth() / alwidth));
-    assertEquals(od.getBoxHeight(), boxHeight);
-    assertEquals(vpranges.getStartRes(), firstHidden - 1);
+            Math.round((float) 24 * alwidth / od.getWidth()));
     assertEquals(vpranges.getStartSeq(), 0);
 
     // move box so boxX is to right of hidden cols, but does not go beyond full
     // width of alignment
     // box width, boxX and scrollCol all as for non-hidden case
-    xpos = 75;
-    testBoxIsAtClickPoint(xpos, 0);
+    xpos = Math.round((float) 75 * od.getWidth() / alwidth) + boxWidth / 2;
+    mouseClick(od, xpos, boxHeight / 2);
+    assertEquals(od.getBoxX(), xpos - boxWidth / 2);
+    assertEquals(od.getBoxY(), 0);
+    assertEquals(od.getBoxWidth(), boxWidth);
+    assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(vpranges.getStartSeq(), 0);
     assertEquals(vpranges.getStartRes(),
-            Math.round(xpos * alwidth / od.getWidth())
-                    - (lastHidden - firstHidden + 1));
+            75 - (lastHidden - firstHidden + 1));
     
     // move box so it goes beyond full width of alignment
     // boxX, scrollCol adjusted back, box width normal
     xpos = 3000;
     mouseClick(od, xpos, 5);
     assertEquals(od.getBoxX(), od.getWidth() - od.getBoxWidth());
-    assertEquals(od.getBoxY(), 5);
+    assertEquals(od.getBoxY(), 0);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(
@@ -487,24 +481,29 @@ public class OverviewDimensionsShowHiddenTest
 
     // click to left of hidden cols, without overlapping
     // boxX, scrollCol and width as normal
-    int xpos = 5;
-    testBoxIsAtClickPoint(xpos, 0);
-    assertEquals(vpranges.getStartSeq(), 0);
-    assertEquals(vpranges.getStartRes(),
-            Math.round((float) xpos * alwidth / od.getWidth()));
+    int xpos = 30;
+    int ypos = 6;
+    testBoxIsAtClickPoint(xpos, ypos);
+    assertEquals(vpranges.getStartSeq(), Math
+            .round((float) (ypos - boxHeight / 2) * alheight
+                    / od.getHeight()));
+    assertEquals(vpranges.getStartRes(), Math.round(
+            (float) (xpos - boxWidth / 2) * alwidth / od.getWidth()));
 
     // click to left of hidden cols, with overlap
     // boxX and scrollCol adjusted for hidden cols, width normal
     xpos = Math.round((float) 145 * od.getWidth() / alwidth) - boxWidth;
-    mouseClick(od, xpos, 0);
+    mouseClick(od, xpos + boxWidth / 2, boxHeight / 2);
     assertEquals(od.getBoxX(),
-            Math.round((float) (firstHidden - 1) * od.getWidth() / alwidth)
+            Math.round((float) (firstHidden - 1)
+                    * od.getWidth() / alwidth)
                     - boxWidth + 1);
     assertEquals(od.getBoxY(), 0);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
     assertEquals(vpranges.getStartRes(),
-            Math.round((float) od.getBoxX() * alwidth / od.getWidth()));
+            Math.round((float) (od.getBoxX()) * alwidth
+                    / od.getWidth()));
     assertEquals(vpranges.getStartSeq(), 0);
 
     // click in hidden cols
@@ -823,7 +822,7 @@ public class OverviewDimensionsShowHiddenTest
     assertEquals(od.getBoxHeight(), boxHeight);
 
     // click below hidden rows
-    mouseClick(od, 0, 150);
+    mouseClick(od, 0, 150 + boxHeight/2);
     assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxY(), 150);
     assertEquals(od.getBoxWidth(), boxWidth);
@@ -860,26 +859,15 @@ public class OverviewDimensionsShowHiddenTest
     assertEquals(od.getBoxHeight(), boxHeight);
 
     // click above hidden rows, so that box overlaps
-    int ypos = 35; // column value in residues
-    mouseClick(od, 0,
-            Math.round((float) ypos * od.getSequencesHeight() / alheight));
-    assertEquals(od.getBoxX(), 0);
-    assertEquals(od.getBoxY(),
-            Math.round((float) ypos * od.getSequencesHeight() / alheight));
-    assertEquals(od.getBoxWidth(), boxWidth);
-    assertEquals(
-            od.getBoxHeight(),
-            boxHeight
-                    + Math.round((float) (lastHiddenRow - firstHiddenRow + 1)
-                            * od.getSequencesHeight() / alheight));
-
-    // click so that box straddles hidden rows
-    ypos = 44; // column value in residues
+    int rowpos = 35; // row value in residues
+    int centrepos = 43; // centre row
     mouseClick(od, 0,
-            Math.round((float) ypos * od.getSequencesHeight() / alheight));
+            Math.round((float) centrepos * od.getSequencesHeight()
+                    / alheight));
     assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxY(),
-            Math.round((float) ypos * od.getSequencesHeight() / alheight));
+            Math.round(
+                    (float) rowpos * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(
             od.getBoxHeight(),
@@ -915,19 +903,19 @@ public class OverviewDimensionsShowHiddenTest
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
 
-    // click above hidden rows
-    int ypos = 40; // row 40
+    // click above hidden rows, no overlap
+    int ypos = 40 + viewHeight / 2; // top is row 40
     mouseClick(od, 0,
             Math.round((float) ypos * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxX(), 0);
     assertEquals(od.getBoxY(),
-            Math.round((float) ypos * od.getSequencesHeight() / alheight));
+            Math.round((float) 40 * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);
 
     // click above hidden rows so box overlaps
     // boxY moved upwards, boxHeight remains same
-    ypos = 497; // row 497
+    ypos = 497 + viewHeight / 2; // row 497
     mouseClick(od, 0,
             Math.round((float) ypos * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxX(), 0);
@@ -939,7 +927,7 @@ public class OverviewDimensionsShowHiddenTest
     assertEquals(od.getBoxHeight(), boxHeight);
 
     // click within hidden rows
-    ypos = 505;
+    ypos = 505 + boxHeight / 2;
     mouseClick(od, 0,
             Math.round((float) ypos * od.getSequencesHeight() / alheight));
     assertEquals(od.getBoxX(), 0);
@@ -951,6 +939,105 @@ public class OverviewDimensionsShowHiddenTest
     assertEquals(od.getBoxHeight(), boxHeight);
   }
 
+  /**
+   * Test the function to determine if a point is in the overview's box or not
+   */
+  @Test(groups = { "Functional" })
+  public void testPositionInBox()
+  {
+    od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols);
+
+    assertFalse(od.isPositionInBox(0, 0));
+    assertTrue(od.isPositionInBox(10, 9));
+    assertFalse(od.isPositionInBox(0, 9));
+    assertFalse(od.isPositionInBox(9, 0));
+    assertFalse(od.isPositionInBox(75, 20));
+
+    assertTrue(od.isPositionInBox(47, 6));
+    assertFalse(od.isPositionInBox(48, 6));
+    assertTrue(od.isPositionInBox(47, 9));
+    assertFalse(od.isPositionInBox(47, 10));
+
+    // hide columns in the box area
+    // extends area where a point is considered to be in the box
+    hiddenCols.hideColumns(1, 4);
+    od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
+    assertFalse(od.isPositionInBox(0, 0));
+    assertTrue(od.isPositionInBox(10, 9));
+    assertFalse(od.isPositionInBox(0, 9));
+    assertFalse(od.isPositionInBox(9, 0));
+    assertFalse(od.isPositionInBox(75, 20));
+
+    assertTrue(od.isPositionInBox(47, 6));
+    assertTrue(od.isPositionInBox(48, 6));
+    assertTrue(od.isPositionInBox(47, 9));
+    assertFalse(od.isPositionInBox(47, 10));
+
+    // hide sequences in box area
+    // extends area where a point is considered to be in the box
+    hideSequences(1, 3);
+    ColumnSelection cs = new ColumnSelection();
+    hiddenCols.revealAllHiddenColumns(cs);
+    od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
+    assertFalse(od.isPositionInBox(0, 0));
+    assertTrue(od.isPositionInBox(10, 9));
+    assertFalse(od.isPositionInBox(0, 9));
+    assertFalse(od.isPositionInBox(9, 0));
+    assertFalse(od.isPositionInBox(75, 20));
+
+    assertTrue(od.isPositionInBox(47, 6));
+    assertFalse(od.isPositionInBox(48, 6));
+    assertTrue(od.isPositionInBox(47, 9));
+    assertTrue(od.isPositionInBox(47, 10));
+  }
+
+  /**
+   * Test the dragging functionality
+   */
+  @Test(groups = { "Functional" })
+  public void testDragging()
+  {
+    od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols);
+    od.setDragPoint(4, 16, al.getHiddenSequences(), hiddenCols);
+    od.adjustViewportFromMouse(20, 22, al.getHiddenSequences(), hiddenCols);
+
+    // updates require an OverviewPanel to exist which it doesn't here
+    // so call setBoxPosition() as it would be called by the AlignmentPanel
+    // normally
+    od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
+
+    // corner moves 16 (20-4) right and 6 (22-16) up
+    assertEquals(od.getBoxX(), 16);
+    assertEquals(od.getBoxY(), 6);
+
+    // hide columns - box moves drag distance + hidden cols, vertically makes no
+    // difference
+    hiddenCols.hideColumns(1, 4);
+    od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols);
+    od.setDragPoint(4, 16, al.getHiddenSequences(), hiddenCols);
+    od.adjustViewportFromMouse(20, 22, al.getHiddenSequences(), hiddenCols);
+    od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
+
+    // corner moves 16 (20-4) + hiddenCols right and 6 (22-16) down
+    assertEquals(od.getBoxX(),
+            16 + Math.round((float) 4 * od.getWidth() / alwidth));
+    assertEquals(od.getBoxY(), 6);
+
+    // hide sequences in box area
+    // makes absolutely no difference
+    hideSequences(1, 3);
+    od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols);
+    od.setDragPoint(4, 16, al.getHiddenSequences(), hiddenCols);
+    od.adjustViewportFromMouse(20, 22, al.getHiddenSequences(), hiddenCols);
+    od.setBoxPosition(al.getHiddenSequences(), hiddenCols);
+
+    // corner moves 16 (20-4) + hiddenCols right and 6 (22-16) + hiddenRows down
+    assertEquals(od.getBoxX(),
+            16 + Math.round((float) 4 * od.getWidth() / alwidth));
+    assertEquals(od.getBoxY(),
+            6 + Math.round((float) 3 * od.getHeight() / alheight));
+  }
+
   /*
    * Move viewport horizontally: startRes + previous width gives new horizontal extent. Vertical extent stays the same.
    */
@@ -993,14 +1080,14 @@ public class OverviewDimensionsShowHiddenTest
   }
   
   /*
-   * Test that the box is positioned with the top left corner at xpos, ypos
+   * Test that the box is positioned with the centre at xpos, ypos
    * and with the original width and height
    */
   private void testBoxIsAtClickPoint(int xpos, int ypos)
   {
     mouseClick(od, xpos, ypos);
-    assertEquals(od.getBoxX(), xpos);
-    assertEquals(od.getBoxY(), ypos);
+    assertEquals(od.getBoxX() + od.getBoxWidth() / 2, xpos);
+    assertEquals(od.getBoxY() + od.getBoxHeight() / 2, ypos);
     assertEquals(od.getBoxWidth(), boxWidth);
     assertEquals(od.getBoxHeight(), boxHeight);