package jalview.datamodel; import java.util.List; public class HiddenColumnsCursor { // absolute position of first hidden column private int firstColumn; // absolute position of last hidden column private int lastColumn; // index of last visited region private int regionIndex; // number of hidden columns before last visited region private int hiddenSoFar; private List hiddenColumns; protected HiddenColumnsCursor() { } /** * Set the cursor to a position * * @param first * absolute position of first hidden column * @param last * absolute position of last hidden column * @param index * index of last visited region * @param hiddenCount * number of hidden columns before last visited region */ protected void resetCursor(List hiddenCols) { synchronized (this) { if ((hiddenCols != null) && (!hiddenCols.isEmpty())) { hiddenColumns = hiddenCols; firstColumn = hiddenColumns.get(0)[0]; lastColumn = hiddenColumns.get(hiddenColumns.size() - 1)[1]; regionIndex = 0; hiddenSoFar = 0; } } } protected void updateCursor(int index, int hiddenCount) { synchronized (this) { regionIndex = index; hiddenSoFar = hiddenCount; } } private synchronized int getIndex() { return regionIndex; } private synchronized int getHiddenSoFar() { return hiddenSoFar; } /** * Get the index of the region that column is within (if column is hidden) or * which is to the right of column (if column is visible). If no hidden * columns are to the right, will return size of hiddenColumns. If hidden * columns is empty returns -1. * * @param column * absolute position of a column in the alignment * @return region index */ protected int findRegionForColumn(int column) { if (hiddenColumns == null) { return -1; } int index = getIndex(); int hiddenCount = getHiddenSoFar(); if (index == hiddenColumns.size()) { // went past the end of hiddenColumns collection last time index--; int[] region = hiddenColumns.get(index); hiddenCount -= region[1] - region[0] + 1; } if ((hiddenColumns.get(index)[0] <= column) && (hiddenColumns.get(index)[1] >= column)) { // we hit the jackpot // don't need to move index } else if (column < firstColumn) { index = 0; hiddenCount = 0; } else if (column > lastColumn) { index = hiddenColumns.size(); // TODO resolve here - need full hidden count } else if (column > hiddenColumns.get(index)[1]) { // iterate from where we are now, if we're lucky we'll be close by // (but still better than iterating from 0) while ((index < hiddenColumns.size()) && (hiddenColumns.get(index)[0] <= column)) { int[] region = hiddenColumns.get(index); hiddenCount += region[1] - region[0] + 1; index++; } } else // (column < hiddenColumns.get(regionIndex)[0]) { while ((index > 0) && (hiddenColumns.get(index)[1] > column)) { index--; int[] region = hiddenColumns.get(index); hiddenCount -= region[1] - region[0] + 1; } } updateCursor(index, hiddenCount); return index; } protected int getHiddenOffset(int column) { if (hiddenColumns == null) { return -1; } int index = getIndex(); int hiddenCount = getHiddenSoFar(); if (column < firstColumn) { index = 0; hiddenCount = 0; } else if ((index < hiddenColumns.size()) && (hiddenColumns.get(index)[0] <= column + hiddenCount)) { // iterate from where we are now, if we're lucky we'll be close by // (but still better than iterating from 0) while ((index < hiddenColumns.size()) && (hiddenColumns.get(index)[0] <= column + hiddenCount)) { int[] region = hiddenColumns.get(index); hiddenCount += region[1] - region[0] + 1; index++; } } else if (index < hiddenColumns.size()) { while ((index > 0) && (hiddenColumns.get(index)[1] > column + hiddenCount)) { index--; int[] region = hiddenColumns.get(index); hiddenCount -= region[1] - region[0] + 1; } } updateCursor(index, hiddenCount); return hiddenCount; } }