JAL-2759 convert findColumnPosition to use cursor
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index 084ea55..8c1a0fb 100644 (file)
@@ -291,40 +291,33 @@ public class HiddenColumns
     {
       LOCK.readLock().lock();
       int result = hiddenColumn;
-      int[] region = null;
+      // int[] region = null;
       if (hiddenColumns != null)
       {
-        Iterator<int[]> it = new RegionsIterator(0,
-                hiddenColumn, hiddenColumns, cursor);
-        while (it.hasNext())
-        {
-          region = it.next();
-          if (hiddenColumn > region[1])
-          {
-            result -= region[1] + 1 - region[0];
-          }
-        }
+        int index = cursor.findRegionForColumn(hiddenColumn);
+        int hiddenBeforeCol = cursor.getHiddenSoFar();
+
+        // just subtract hidden cols count - this works fine if column is
+        // visible
+        result = hiddenColumn - hiddenBeforeCol;
 
-        if (region != null && hiddenColumn >= region[0]
-                && hiddenColumn <= region[1])
+        // now check in case column is hidden - it will be in the returned
+        // hidden region
+        if (index < hiddenColumns.size())
         {
-          // 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
+          int[] region = hiddenColumns.get(index);
+          if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
           {
-            return result - (hiddenColumn - region[0] + 1);
+            // actually col is hidden, return region[0]-1
+            // unless region[0]==0 in which case return 0
+            if (region[0] == 0)
+            {
+              result = 0;
+            }
+            else
+            {
+              result = region[0] - 1 - hiddenBeforeCol;
+            }
           }
         }
       }