*/
public int calcPanelHeight();
+ /**
+ * Answers true if the viewport has at least one column selected
+ *
+ * @return
+ */
+ boolean hasSelectedColumns();
+
+ /**
+ * Answers true if the viewport has at least one hidden column
+ *
+ * @return
+ */
boolean hasHiddenColumns();
boolean isValidCharWidth();
// Hide everything by the current selection - this is a hack - we do the
// invert and then hide
// first check that there will be visible columns after the invert.
- if ((viewport.getColumnSelection() != null
- && viewport.getColumnSelection().getSelected() != null && viewport
- .getColumnSelection().getSelected().size() > 0)
+ if (viewport.hasSelectedColumns()
|| (sg != null && sg.getSize() > 0 && sg.getStartRes() <= sg
.getEndRes()))
{
hide = true;
viewport.hideAllSelectedSeqs();
}
- else if (!(toggleCols && viewport.getColumnSelection().getSelected()
- .size() > 0))
+ else if (!(toggleCols && viewport.hasSelectedColumns()))
{
viewport.showAllHiddenSeqs();
}
if (toggleCols)
{
- if (viewport.getColumnSelection().getSelected().size() > 0)
+ if (viewport.hasSelectedColumns())
{
viewport.hideSelectedColumns();
if (!toggleSeqs)
SequenceI rs = sel.getSequenceAt(0);
start = rs.findIndex(start);
end = rs.findIndex(end);
- List<Integer> cs = csel.getSelected();
+ List<Integer> cs = new ArrayList<Integer>(csel.getSelected());
csel.clear();
for (Integer selectedCol : cs)
{
/*
* list of selected columns (ordered by selection order, not column order)
*/
- private List<Integer> order = new ArrayList<Integer>();
+ private List<Integer> order;
+
+ /*
+ * an unmodifiable view of the selected columns list
+ */
+ private List<Integer> _uorder;
/**
* bitfield for column selection - allows quick lookup
*/
- private BitSet selected = new BitSet();
+ private BitSet selected;
+
+ /**
+ * Constructor
+ */
+ IntList()
+ {
+ order = new ArrayList<Integer>();
+ _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
*/
- public void add(int i)
+ void add(int i)
{
if (!selected.get(i))
{
}
}
- public void clear()
+ void clear()
{
order.clear();
selected.clear();
}
- public void remove(int col)
+ void remove(int col)
{
Integer colInt = new Integer(col);
}
}
- public boolean contains(Integer colInt)
+ boolean contains(Integer colInt)
{
return selected.get(colInt);
}
- public boolean isEmpty()
+ boolean isEmpty()
{
return order.isEmpty();
}
- public List<Integer> getList()
+ /**
+ * Returns a read-only view of the selected columns list
+ *
+ * @return
+ */
+ List<Integer> getList()
{
- return order;
+ return _uorder;
}
- public int size()
+ int size()
{
return order.size();
}
* @param i
* @return
*/
- public int elementAt(int i)
+ int elementAt(int i)
{
return order.get(i);
}
* @param change
* - delta for shift
*/
- public void compensateForEdits(int start, int change)
+ void compensateForEdits(int start, int change)
{
BitSet mask = new BitSet();
for (int i = 0; i < order.size(); i++)
selected.or(mask);
}
- public boolean isSelected(int column)
+ boolean isSelected(int column)
{
return selected.get(column);
}
- public int getMaxColumn()
+ int getMaxColumn()
{
return selected.length() - 1;
}
- public int getMinColumn()
+ int getMinColumn()
{
return selected.get(0) ? 0 : selected.nextSetBit(0);
}
/**
* @return a series of selection intervals along the range
*/
- public List<int[]> getRanges()
+ List<int[]> getRanges()
{
List<int[]> rlist = new ArrayList<int[]>();
if (selected.isEmpty())
}
/**
- * Returns a list of selected columns. The list contains no duplicates but is
- * not necessarily ordered. It also may include columns hidden from the
- * current view. This returns a copy of the actual list, and changes to the
- * copy will not affect the selection.
+ * Returns a read-only view of the (possibly empty) list of selected columns
+ * <p>
+ * 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.
+ * <p>
+ * The list is not thread-safe: iterating over it could result in
+ * ConcurrentModificationException if it is modified by another thread.
*/
public List<Integer> getSelected()
{
- return new ArrayList<Integer>(selection.getList());
+ return selection.getList();
}
/**
{
if (copy != null)
{
- if (copy.selection != null)
- {
- selection = new IntList();
- for (int i = 0, j = copy.selection.size(); i < j; i++)
- {
- selection.add(copy.selection.elementAt(i));
- }
- }
+ selection = new IntList(copy.selection);
if (copy.hiddenColumns != null)
{
hiddenColumns = new Vector<int[]>(copy.hiddenColumns.size());
// Hide everything by the current selection - this is a hack - we do the
// invert and then hide
// first check that there will be visible columns after the invert.
- if ((viewport.getColumnSelection() != null
- && viewport.getColumnSelection().getSelected() != null && viewport
- .getColumnSelection().getSelected().size() > 0)
+ if (viewport.hasSelectedColumns()
|| (sg != null && sg.getSize() > 0 && sg.getStartRes() <= sg
.getEndRes()))
{
hideSelSequences_actionPerformed(null);
hide = true;
}
- else if (!(toggleCols && viewport.getColumnSelection().getSelected()
- .size() > 0))
+ else if (!(toggleCols && viewport.hasSelectedColumns()))
{
showAllSeqs_actionPerformed(null);
}
if (toggleCols)
{
- if (viewport.getColumnSelection().getSelected().size() > 0)
+ if (viewport.hasSelectedColumns())
{
hideSelColumns_actionPerformed(null);
if (!toggleSeqs)
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
StringBuilder collatedInput = new StringBuilder(64);
String last = "";
ColumnSelection viscols = av.getColumnSelection();
- // TODO: refactor and save av.getColumnSelection for efficiency
- List<Integer> selected = viscols.getSelected();
+
+ /*
+ * the selection list (read-only view) is in selection order, not
+ * column order; make a copy so we can sort it
+ */
+ List<Integer> selected = new ArrayList<Integer>(viscols.getSelected());
Collections.sort(selected);
for (int index : selected)
{
Thread udthread = new Thread(new Runnable()
{
+ @Override
public void run()
{
Cache.log.info("Jalview updating to the Vamsas Session.");
final VamsasApplication client = this;
vclient.addDocumentUpdateHandler(new PropertyChangeListener()
{
+ @Override
public void propertyChange(PropertyChangeEvent evt)
{
Cache.log.debug("Dealing with document update event.");
uk.ac.vamsas.client.Events.DOCUMENT_REQUESTTOCLOSE,
new PropertyChangeListener()
{
+ @Override
public void propertyChange(PropertyChangeEvent evt)
{
if (client.promptUser)
{
String last = null;
+ @Override
public void handleMessage(Message message)
{
if (vobj2jv == null)
selecter = new SelectionListener()
{
+ @Override
public void selection(SequenceGroup seqsel,
ColumnSelection colsel, SelectionSource source)
{
{
// gather selected columns outwith the sequence positions
// too
- for (Object obj : colsel.getSelected())
+ for (Integer ival : colsel.getSelected())
{
- int ival = ((Integer) obj).intValue();
Pos p = new Pos();
- p.setI(ival + 1);
+ p.setI(ival.intValue() + 1);
range.addPos(p);
}
}
}
@Override
+ public boolean hasSelectedColumns()
+ {
+ ColumnSelection columnSelection = getColumnSelection();
+ return columnSelection != null && columnSelection.hasSelectedColumns();
+ }
+
+ @Override
public boolean hasHiddenColumns()
{
return colSel != null && colSel.hasHiddenColumns();
if (sg != null
&& (sgs = sg.getStartRes()) >= 0
&& sg.getStartRes() <= (sge = sg.getEndRes())
- && (colSel == null || colSel.getSelected() == null || colSel
- .getSelected().size() == 0))
+ && !this.hasSelectedColumns())
{
if (!wholewidth && alignment.getWidth() == (1 + sge - sgs))
{
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
-import static org.testng.AssertJUnit.assertNotSame;
import static org.testng.AssertJUnit.assertSame;
import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.AssertJUnit.fail;
import java.util.Arrays;
import java.util.BitSet;
+import java.util.Collections;
+import java.util.ConcurrentModificationException;
import java.util.List;
import org.testng.annotations.Test;
// removing an element in the list removes it
cs.removeElement(2);
- // ...but not from the copy list!
- assertEquals(2, sel.size());
+ // ...and also from the read-only view
+ assertEquals(1, sel.size());
sel = cs.getSelected();
assertEquals(1, sel.size());
assertEquals(new Integer(5), sel.get(0));
* were added
*/
@Test(groups = { "Functional" })
- public void testGetSelection()
+ public void testGetSelected()
{
ColumnSelection cs = new ColumnSelection();
int[] sel = { 4, 3, 7, 21 };
cs.addElement(col);
}
- List<Integer> selected1 = cs.getSelected();
- assertEquals(4, selected1.size());
+ List<Integer> selected = cs.getSelected();
+ assertEquals(4, selected.size());
+ assertEquals("[4, 3, 7, 21]", selected.toString());
/*
- * getSelected returns a copy, verify the list
- * is externally immutable
+ * getSelected returns a read-only view of the list
+ * verify the view follows any changes in it
*/
- selected1.clear();
- List<Integer> selected2 = cs.getSelected();
- assertNotSame(selected1, selected2);
- assertEquals(4, selected2.size());
- int i = 0;
- for (int col : sel)
+ cs.removeElement(7);
+ cs.addElement(1);
+ cs.removeElement(4);
+ assertEquals("[3, 21, 1]", selected.toString());
+ }
+
+ /**
+ * Test to verify that the list returned by getSelection cannot be modified
+ */
+ @Test(groups = { "Functional" })
+ public void testGetSelected_isReadOnly()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(3);
+
+ List<Integer> selected = cs.getSelected();
+ try
+ {
+ selected.clear();
+ fail("expected exception");
+ } catch (UnsupportedOperationException e)
+ {
+ // expected
+ }
+ try
+ {
+ selected.add(1);
+ fail("expected exception");
+ } catch (UnsupportedOperationException e)
+ {
+ // expected
+ }
+ try
+ {
+ selected.remove(3);
+ fail("expected exception");
+ } catch (UnsupportedOperationException e)
{
- assertEquals(col, selected2.get(i++).intValue());
+ // expected
}
+ try
+ {
+ Collections.sort(selected);
+ fail("expected exception");
+ } catch (UnsupportedOperationException e)
+ {
+ // expected
+ }
+ }
- cs.removeElement(7);
+ /**
+ * Test that demonstrates a ConcurrentModificationException is thrown if you
+ * change the selection while iterating over it
+ */
+ @Test(
+ groups = "Functional",
+ expectedExceptions = { ConcurrentModificationException.class })
+ public void testGetSelected_concurrentModification()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(0);
cs.addElement(1);
- cs.removeElement(4);
+ cs.addElement(2);
- List<Integer> selected3 = cs.getSelected();
- assertEquals(3, selected3.size());
- assertEquals(3, selected3.get(0).intValue());
- assertEquals(21, selected3.get(1).intValue());
- assertEquals(1, selected3.get(2).intValue());
+ /*
+ * simulate changing the list under us (e.g. in a separate
+ * thread) while iterating over it -> ConcurrentModificationException
+ */
+ List<Integer> selected = cs.getSelected();
+ for (Integer col : selected)
+ {
+ if (col.intValue() == 0)
+ {
+ cs.removeElement(1);
+ }
+ }
}
@Test(groups = "Functional")
assertTrue(selected.contains(6));
assertTrue(selected.contains(10));
}
+
+ @Test(groups = "Functional")
+ public void testCopyConstructor()
+ {
+ ColumnSelection cs = new ColumnSelection();
+ cs.addElement(3);
+ cs.addElement(1);
+ cs.hideColumns(10, 11);
+ cs.hideColumns(5, 7);
+ assertEquals("[5, 7]", Arrays.toString(cs.getHiddenColumns().get(0)));
+
+ ColumnSelection cs2 = new ColumnSelection(cs);
+ assertTrue(cs2.hasSelectedColumns());
+ assertTrue(cs2.hasHiddenColumns());
+ // order of column selection is preserved
+ assertEquals("[3, 1]", cs2.getSelected().toString());
+ assertEquals(2, cs2.getHiddenColumns().size());
+ // hidden columns are held in column order
+ assertEquals("[5, 7]", Arrays.toString(cs2.getHiddenColumns().get(0)));
+ assertEquals("[10, 11]", Arrays.toString(cs2.getHiddenColumns().get(1)));
+ }
}