/* * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jalview. If not, see . * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.datamodel; import java.util.List; public class HiddenColumnsCursor { // absolute position of first hidden column private int firstColumn; // 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]; regionIndex = 0; hiddenSoFar = 0; } } } protected void updateCursor(int index, int hiddenCount) { synchronized (this) { regionIndex = index; hiddenSoFar = hiddenCount; } } protected synchronized int getIndex() { return regionIndex; } protected 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 = regionIndex; int hiddenCount = hiddenSoFar; 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) { // column is in the current region // we hit the jackpot // don't need to move index } else if (column < firstColumn) { index = 0; hiddenCount = 0; } // column is after current region else if (column > hiddenColumns.get(index)[1]) // includes if column > // lastColumn { // iterate from where we are now, if we're lucky we'll be close by // (but still better than iterating from 0) // stop when we find the region *before* column // i.e. the next region starts after column or if not, ends after column while ((index < hiddenColumns.size()) && (column > hiddenColumns.get(index)[1])) { int[] region = hiddenColumns.get(index); hiddenCount += region[1] - region[0] + 1; index++; } } // column is before current region else if (column < hiddenColumns.get(index)[0]) { // column is before or in the previous region if ((index > 0) && (hiddenColumns.get(index - 1)[1] >= column)) { 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; } /** * Get the number of hidden columns in regions before column i.e. excludes * hidden columns in the region column is in, if any * * @param column * index of column in visible alignment * @return */ 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 { while ((index > 0) && (hiddenColumns.get(index - 1)[1] >= column + hiddenCount)) { index--; int[] region = hiddenColumns.get(index); hiddenCount -= region[1] - region[0] + 1; } } updateCursor(index, hiddenCount); return hiddenCount; } }