X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FColumnSelection.java;h=3ccaab869730a770f4463eee8cb7242f136b43c1;hb=bc18effe68ba80213a6d03ca7e6175adc6be71d6;hp=c895b2f3c8b2a1c604f416bf585dbac5ff09e9d8;hpb=5655997c202bfcb4ab2778999d785490d3a7dbae;p=jalview.git diff --git a/src/jalview/datamodel/ColumnSelection.java b/src/jalview/datamodel/ColumnSelection.java index c895b2f..3ccaab8 100644 --- a/src/jalview/datamodel/ColumnSelection.java +++ b/src/jalview/datamodel/ColumnSelection.java @@ -1,49 +1,280 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program 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 2 + * 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. - * - * This program 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. - * + * + * 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.datamodel; -import jalview.util.ShiftList; +import jalview.viewmodel.annotationfilter.AnnotationFilterParameter; +import jalview.viewmodel.annotationfilter.AnnotationFilterParameter.SearchableAnnotationField; -import java.util.*; +import java.util.ArrayList; +import java.util.BitSet; +import java.util.Collections; +import java.util.List; +import java.util.regex.PatternSyntaxException; /** - * NOTE: Columns are zero based. + * Data class holding the selected columns and hidden column ranges for a view. + * Ranges are base 1. */ public class ColumnSelection { - Vector selected = new Vector(); + /** + * A class to hold an efficient representation of selected columns + */ + private class IntList + { + /* + * list of selected columns (ordered by selection order, not column order) + */ + private List order; + + /* + * an unmodifiable view of the selected columns list + */ + private List _uorder; + + /** + * bitfield for column selection - allows quick lookup + */ + private BitSet selected; + + /** + * Constructor + */ + IntList() + { + order = new ArrayList<>(); + _uorder = Collections.unmodifiableList(order); + selected = new BitSet(); + } + + /** + * Copy constructor + * + * @param other + */ + IntList(IntList other) + { + this(); + if (other != null) + { + int j = other.size(); + for (int i = 0; i < j; i++) + { + add(other.elementAt(i)); + } + } + } + + /** + * adds a new column i to the selection - only if i is not already selected + * + * @param i + */ + void add(int i) + { + if (!selected.get(i)) + { + order.add(Integer.valueOf(i)); + selected.set(i); + } + } + + void clear() + { + order.clear(); + selected.clear(); + } + + void remove(int col) + { + + Integer colInt = new Integer(col); + + if (selected.get(col)) + { + // if this ever changes to List.remove(), ensure Integer not int + // argument + // as List.remove(int i) removes the i'th item which is wrong + order.remove(colInt); + selected.clear(col); + } + } + + boolean contains(Integer colInt) + { + return selected.get(colInt); + } + + boolean isEmpty() + { + return order.isEmpty(); + } + + /** + * Returns a read-only view of the selected columns list + * + * @return + */ + List getList() + { + return _uorder; + } + + int size() + { + return order.size(); + } + + /** + * gets the column that was selected first, second or i'th + * + * @param i + * @return + */ + int elementAt(int i) + { + return order.get(i); + } + + protected boolean pruneColumnList(final List shifts) + { + int s = 0, t = shifts.size(); + int[] sr = shifts.get(s++); + boolean pruned = false; + int i = 0, j = order.size(); + while (i < j && s <= t) + { + int c = order.get(i++).intValue(); + if (sr[0] <= c) + { + if (sr[1] + sr[0] >= c) + { // sr[1] -ve means inseriton. + order.remove(--i); + selected.clear(c); + j--; + } + else + { + if (s < t) + { + sr = shifts.get(s); + } + s++; + } + } + } + return pruned; + } + + /** + * shift every selected column at or above start by change + * + * @param start + * - leftmost column to be shifted + * @param change + * - delta for shift + */ + void compensateForEdits(int start, int change) + { + BitSet mask = new BitSet(); + for (int i = 0; i < order.size(); i++) + { + int temp = order.get(i); + + if (temp >= start) + { + // clear shifted bits and update List of selected columns + selected.clear(temp); + mask.set(temp - change); + order.set(i, new Integer(temp - change)); + } + } + // lastly update the bitfield all at once + selected.or(mask); + } + + boolean isSelected(int column) + { + return selected.get(column); + } + + int getMaxColumn() + { + return selected.length() - 1; + } + + int getMinColumn() + { + return selected.get(0) ? 0 : selected.nextSetBit(0); + } + + /** + * @return a series of selection intervals along the range + */ + List getRanges() + { + List rlist = new ArrayList<>(); + if (selected.isEmpty()) + { + return rlist; + } + int next = selected.nextSetBit(0), clear = -1; + while (next != -1) + { + clear = selected.nextClearBit(next); + rlist.add(new int[] { next, clear - 1 }); + next = selected.nextSetBit(clear); + } + return rlist; + } + + @Override + public int hashCode() + { + // TODO Auto-generated method stub + return selected.hashCode(); + } + + @Override + public boolean equals(Object obj) + { + if (obj instanceof IntList) + { + return ((IntList) obj).selected.equals(selected); + } + return false; + } + } - //Vector of int [] {startCol, endCol} - Vector hiddenColumns; + private IntList selection = new IntList(); /** * Add a column to the selection - * - * @param col index of column + * + * @param col + * index of column */ public void addElement(int col) { - Integer column = new Integer(col); - if (!selected.contains(column)) - { - selected.addElement(column); - } + selection.add(col); } /** @@ -51,726 +282,526 @@ public class ColumnSelection */ public void clear() { - selected.removeAllElements(); + selection.clear(); } /** - * removes col from selection - * - * @param col index of column to be removed + * Removes value 'col' from the selection (not the col'th item) + * + * @param col + * index of column to be removed */ public void removeElement(int col) { - Integer colInt = new Integer(col); - - if (selected.contains(colInt)) - { - selected.removeElement(colInt); - } + selection.remove(col); } /** * removes a range of columns from the selection - * @param start int - first column in range to be removed - * @param end int - last col + * + * @param start + * int - first column in range to be removed + * @param end + * int - last col */ public void removeElements(int start, int end) { Integer colInt; - for(int i=start; i + * The list contains no duplicates but is not necessarily ordered. It also may + * include columns hidden from the current view. To modify (for example sort) + * the list, you should first make a copy. + *

+ * The list is not thread-safe: iterating over it could result in + * ConcurrentModificationException if it is modified by another thread. */ - public Vector getSelected() + public List getSelected() { - return selected; + return selection.getList(); } /** - * - * @param col index to search for in column selection - * - * @return true if Integer(col) is in selection. + * @return list of int arrays containing start and end column position for + * runs of selected columns ordered from right to left. */ - public boolean contains(int col) + public List getSelectedRanges() { - return selected.contains(new Integer(col)); + return selection.getRanges(); } /** - * DOCUMENT ME! - * - * @param i DOCUMENT ME! - * - * @return DOCUMENT ME! + * + * @param col + * index to search for in column selection + * + * @return true if col is selected */ - public int columnAt(int i) + public boolean contains(int col) { - return ((Integer) selected.elementAt(i)).intValue(); + return (col > -1) ? selection.isSelected(col) : false; } /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * Answers true if no columns are selected, else false */ - public int size() + public boolean isEmpty() { - return selected.size(); + return selection == null || selection.isEmpty(); } /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * rightmost selected column + * + * @return rightmost column in alignment that is selected */ public int getMax() { - int max = -1; - - for (int i = 0; i < selected.size(); i++) + if (selection.isEmpty()) { - if (columnAt(i) > max) - { - max = columnAt(i); - } + return -1; } - - return max; + return selection.getMaxColumn(); } /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * Leftmost column in selection + * + * @return column index of leftmost column in selection */ public int getMin() { - int min = 1000000000; - - for (int i = 0; i < selected.size(); i++) + if (selection.isEmpty()) { - if (columnAt(i) < min) - { - min = columnAt(i); - } + return 1000000000; } - - return min; + return selection.getMinColumn(); } - - /** - * propagate shift in alignment columns to column selection - * - * @param start beginning of edit - * @param left shift in edit (+ve for removal, or -ve for inserts) - */ - public void compensateForEdit(int start, int change) + public void hideSelectedColumns(AlignmentI al) { - for (int i = 0; i < size(); i++) + synchronized (selection) { - int temp = columnAt(i); - - if (temp >= start) + for (int[] selregions : selection.getRanges()) { - selected.setElementAt(new Integer(temp - change), i); + al.getHiddenColumns().hideColumns(selregions[0], selregions[1]); } + selection.clear(); } - if(hiddenColumns!=null) - { - for(int i=0; i start) - { - region[0] -= change; - region[1] -= change; - } - if(region[0]<0) - region[0] = 0; - if(region[1] <0) - region[1] = 0; - } - } } + /** - * propagate shift in alignment columns to column selection - * special version of compensateForEdit - allowing for edits within hidden regions - * @param start beginning of edit - * @param left shift in edit (+ve for removal, or -ve for inserts) + * Hides the specified column and any adjacent selected columns + * + * @param res + * int */ - private void compensateForDelEdits(int start, int change) + public void hideSelectedColumns(int col, HiddenColumns hidden) { - for (int i = 0; i < size(); i++) + /* + * deselect column (whether selected or not!) + */ + removeElement(col); + + /* + * find adjacent selected columns + */ + int min = col - 1, max = col + 1; + while (contains(min)) { - int temp = columnAt(i); - - if (temp >= start) - { - selected.setElementAt(new Integer(temp - change), i); - } + removeElement(min); + min--; } - if(hiddenColumns!=null) + while (contains(max)) { - for(int i=0; i= start) - { - region[0] -= change; - } - if (region[1]>= start) { - region[1] -=change; - } - if (region[1]

+ * Method refactored from ScalePanel.mouseDragged * - * @param start (first column inclusive from 0) - * @param end (last column - not inclusive) - * @return int[] {i_start, i_end, ..} where intervals lie in start<=i_start<=i_end