JAL-2429 findColumnPosition returns 0 for cols in region starting at 0
[jalview.git] / src / jalview / datamodel / ColumnSelection.java
index d651c0b..28745f1 100644 (file)
@@ -690,8 +690,8 @@ public class ColumnSelection
    * left-most visible column will always be returned.
    * 
    * @param hiddenColumn
-   *          int
-   * @return int
+   *          the column index in the full alignment including hidden columns
+   * @return the position of the column in the visible alignment
    */
   public int findColumnPosition(int hiddenColumn)
   {
@@ -708,9 +708,26 @@ public class ColumnSelection
           result -= region[1] + 1 - region[0];
         }
       } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
-      if (hiddenColumn > region[0] && hiddenColumn < region[1])
-      {
-        return region[0] + hiddenColumn - result;
+
+      if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
+       {
+         // Here the hidden column is within a region, so
+         // we want to return the position of region[0]-1, adjusted for any
+         // earlier hidden columns.
+         // Calculate the difference between the actual hidden col position
+         // and region[0]-1, and then subtract from result to convert result from
+         // the adjusted hiddenColumn value to the adjusted region[0]-1 value
+
+        // However, if the region begins at 0 we cannot return region[0]-1
+        // just return 0
+        if (region[0] == 0)
+        {
+          return 0;
+        }
+        else
+        {
+          return result - (hiddenColumn - region[0] + 1);
+        }
       }
     }
     return result; // return the shifted position after removing hidden columns.
@@ -866,6 +883,24 @@ public class ColumnSelection
          */
         region[0] = Math.min(region[0], start);
         region[1] = Math.max(region[1], end);
+
+        /*
+         * also update or remove any subsequent ranges 
+         * that are overlapped
+         */
+        while (i < hiddenColumns.size() - 1)
+        {
+          int[] nextRegion = hiddenColumns.get(i + 1);
+          if (nextRegion[0] > end + 1)
+          {
+            /*
+             * gap to next hidden range - no more to update
+             */
+            break;
+          }
+          region[1] = Math.max(nextRegion[1], end);
+          hiddenColumns.remove(i + 1);
+        }
         return;
       }
     }
@@ -1813,4 +1848,55 @@ public class ColumnSelection
     return changed;
   }
 
+  /**
+   * Adjusts column selections, and the given selection group, to match the
+   * range of a stretch (e.g. mouse drag) operation
+   * <p>
+   * Method refactored from ScalePanel.mouseDragged
+   * 
+   * @param res
+   *          current column position, adjusted for hidden columns
+   * @param sg
+   *          current selection group
+   * @param min
+   *          start position of the stretch group
+   * @param max
+   *          end position of the stretch group
+   */
+  public void stretchGroup(int res, SequenceGroup sg, int min, int max)
+  {
+    if (!contains(res))
+    {
+      addElement(res);
+    }
+
+    if (res > sg.getStartRes())
+    {
+      // expand selection group to the right
+      sg.setEndRes(res);
+    }
+    if (res < sg.getStartRes())
+    {
+      // expand selection group to the left
+      sg.setStartRes(res);
+    }
+
+    /*
+     * expand or shrink column selection to match the
+     * range of the drag operation
+     */
+    for (int col = min; col <= max; col++)
+    {
+      if (col < sg.getStartRes() || col > sg.getEndRes())
+      {
+        // shrinking drag - remove from selection
+        removeElement(col);
+      }
+      else
+      {
+        // expanding drag - add to selection
+        addElement(col);
+      }
+    }
+  }
 }