JAL-3210 Improvements to eclipse detection. New src tree and SwingJS updated from...
[jalview.git] / src / jalview / viewmodel / OverviewDimensionsShowHidden.java
index 9a8c2a8..0752097 100644 (file)
@@ -28,6 +28,8 @@ import jalview.datamodel.AllRowsCollection;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.HiddenSequences;
 
+import java.awt.Dimension;
+
 public class OverviewDimensionsShowHidden extends OverviewDimensions
 {
   private ViewportRanges ranges;
@@ -38,6 +40,12 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
   private int ydiff; // when dragging, difference in alignment units between
                      // start sequence and original mouse click position
 
+  public OverviewDimensionsShowHidden(ViewportRanges vpranges,
+          boolean showAnnotationPanel) 
+  {
+    this(vpranges, showAnnotationPanel, null);
+  }
+
   /**
    * Create an OverviewDimensions object
    * 
@@ -47,9 +55,9 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
    *          true if the annotation panel is to be shown, false otherwise
    */
   public OverviewDimensionsShowHidden(ViewportRanges vpranges,
-          boolean showAnnotationPanel)
+          boolean showAnnotationPanel, Dimension dim)
   {
-    super(vpranges, showAnnotationPanel);
+    super(vpranges, showAnnotationPanel, dim);
     ranges = vpranges;
     resetAlignmentDims();
   }
@@ -72,6 +80,8 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
   public void updateViewportFromMouse(int mousex, int mousey,
           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
   {
+    resetAlignmentDims();
+
     // 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);
@@ -93,6 +103,8 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
   public void adjustViewportFromMouse(int mousex, int mousey,
           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
   {
+    resetAlignmentDims();
+
     // calculate translation in pixel terms:
     // get mouse location in viewport coords, add translation in viewport
     // coords,
@@ -100,20 +112,23 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
     int vpx = Math.round((float) mousex * alwidth / width);
     int visXAsRes = hiddenCols.absoluteToVisibleColumn(vpx) + xdiff;
 
-    int vpy = Math.round((float) mousey * alheight / sequencesHeight);
+    int vpy = Math.round(mousey * heightRatio);
     int visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(vpy) + ydiff;
 
     // update viewport accordingly
     updateViewportFromTopLeft(visXAsRes, visYAsRes, hiddenSeqs, hiddenCols);
   }
 
+  /**
+   * {@inheritDoc} Callers should have already called resetAlignmentDims to
+   * refresh alwidth, alheight and width/height ratios
+   */
   @Override
   protected void updateViewportFromTopLeft(int leftx, int topy,
           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
   {
     int visXAsRes = leftx;
     int visYAsSeq = topy;
-    resetAlignmentDims();
 
     if (visXAsRes < 0)
     {
@@ -145,7 +160,7 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
       if (ranges.getEndRes() < visAlignWidth)
       {
         visXAsRes = hiddenCols.absoluteToVisibleColumn(hiddenCols
-                .subtractVisibleColumns(vpwidth - 1, alwidth - 1));
+                .offsetByVisibleColumns(-(vpwidth - 1), alwidth - 1));
       }
       else
       {
@@ -225,20 +240,32 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
   {
     alwidth = ranges.getAbsoluteAlignmentWidth();
     alheight = ranges.getAbsoluteAlignmentHeight();
+
+    widthRatio = (float) alwidth / width;
+    heightRatio = (float) alheight / sequencesHeight;
   }
 
+
+  /**
+   * {@inheritDoc} Callers should have already called resetAlignmentDims to
+   * refresh widthRatio
+   */
   @Override
   protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
   {
     int vpx = Math.round((float) mousex * alwidth / width);
-    return hidden.subtractVisibleColumns(ranges.getViewportWidth() / 2,
+    return hidden.offsetByVisibleColumns(-ranges.getViewportWidth() / 2,
             vpx);
   }
 
+  /**
+   * {@inheritDoc} Callers should have already called resetAlignmentDims to
+   * refresh heightRatio
+   */
   @Override
   protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
   {
-    int vpy = Math.round((float) mousey * alheight / sequencesHeight);
+    int vpy = Math.round(mousey * heightRatio);
     return hidden.subtractVisibleRows(ranges.getViewportHeight() / 2, vpy);
   }
 
@@ -246,10 +273,12 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
   public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs,
           HiddenColumns hiddenCols)
   {
+    resetAlignmentDims();
+
     // 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);
+    int vpx = Math.round(x * widthRatio);
+    int vpy = Math.round(y * heightRatio);
 
     xdiff = ranges.getStartRes() - hiddenCols.absoluteToVisibleColumn(vpx);
     ydiff = ranges.getStartSeq()