JAL-4128 Catch RasterFormatException caused whilst resizing Overview window, recalcul...
[jalview.git] / src / jalview / viewmodel / OverviewDimensions.java
index 68318ee..1251207 100644 (file)
  */
 package jalview.viewmodel;
 
+import java.awt.Graphics;
+
 import jalview.api.AlignmentColsCollectionI;
 import jalview.api.AlignmentRowsCollectionI;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.HiddenSequences;
 
-import java.awt.Graphics;
-
 public abstract class OverviewDimensions
 {
   protected static final int MAX_WIDTH = 400;
@@ -58,6 +58,10 @@ public abstract class OverviewDimensions
 
   protected int alheight;
 
+  protected float widthRatio;
+
+  protected float heightRatio;
+
   /**
    * Create an OverviewDimensions object
    * 
@@ -157,23 +161,25 @@ public abstract class OverviewDimensions
   public float getPixelsPerCol()
   {
     resetAlignmentDims();
-    return (float) width / alwidth;
+    return 1 / widthRatio;
   }
 
   public float getPixelsPerSeq()
   {
     resetAlignmentDims();
-    return (float) sequencesHeight / alheight;
+    return 1 / heightRatio;
   }
 
   public void setWidth(int w)
   {
-    width = w;
+    width = w < 1 ? 1 : w;
+    widthRatio = (float) alwidth / width;
   }
 
   public void setHeight(int h)
   {
-    sequencesHeight = h - graphHeight;
+    sequencesHeight = (h < 1 ? 1 : h) - graphHeight;
+    heightRatio = (float) alheight / sequencesHeight;
   }
 
   /**
@@ -192,6 +198,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,20 +269,24 @@ 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)
   {
     resetAlignmentDims();
 
     // boxX, boxY is the x,y location equivalent to startRes, startSeq
-    boxX = Math.round((float) startRes * width / alwidth);
-    boxY = Math.round((float) startSeq * sequencesHeight / alheight);
+    int xPos = Math.min(startRes, alwidth - vpwidth + 1);
+    boxX = Math.round(xPos / widthRatio);
+    boxY = Math.round(startSeq / heightRatio);
 
     // boxWidth is the width in residues translated to pixels
-    boxWidth = Math.round((float) vpwidth * width / alwidth);
+    boxWidth = Math.round(vpwidth / widthRatio);
 
     // boxHeight is the height in sequences translated to pixels
-    boxHeight = Math.round((float) vpheight * sequencesHeight / alheight);
+    boxHeight = Math.round(vpheight / heightRatio);
   }
 
   /**
@@ -252,17 +300,19 @@ public abstract class OverviewDimensions
    */
   public boolean isPositionInBox(int x, int y)
   {
-    return (x > boxX && y > boxY && boxX + x < boxWidth
-            && boxY + y < boxHeight);
-  }
-
-  public void setMousePosition(int x, int y)
-  {
-
+    return (x > boxX && y > boxY && x < boxX + boxWidth
+            && y < boxY + boxHeight);
   }
 
-  protected abstract int getLeftXFromCentreX(int mousex, HiddenColumns hidden);
+  /*
+   * 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);