/**
* Set first residue visible in the viewport, and retain the current width.
+ * Fires a property change event.
*
* @param res
* residue position
*/
public void setStartEndRes(int start, int end)
{
- int oldres = this.startRes;
+ int oldstartres = this.startRes;
if (start > al.getWidth() - 1)
{
startRes = al.getWidth() - 1;
startRes = start;
}
+ int oldendres = this.endRes;
if (end < 0)
{
endRes = 0;
endRes = end;
}
- changeSupport.firePropertyChange("startres", oldres, start);
+ changeSupport.firePropertyChange("startres", oldstartres, start);
+ if (oldstartres == start)
+ {
+ // event won't be fired if start positions are same
+ // fire an event for the end positions in case they changed
+ changeSupport.firePropertyChange("endres", oldendres, end);
+ }
}
/**
- * Set last residue visible in the viewport
+ * Set last residue visible in the viewport. Fires a property change event.
*
* @param res
* residue position
}
/**
- * Set the first sequence visible in the viewport
+ * Set the first sequence visible in the viewport. Fires a property change
+ * event.
*
* @param seq
* sequence position
*/
public void setStartEndSeq(int start, int end)
{
- int oldseq = this.startSeq;
+ int oldstartseq = this.startSeq;
if (start > al.getHeight() - 1)
{
startSeq = al.getHeight() - 1;
startSeq = start;
}
+ int oldendseq = this.endSeq;
if (end >= al.getHeight())
{
endSeq = al.getHeight() - 1;
endSeq = end;
}
- changeSupport.firePropertyChange("startseq", oldseq, start);
+ changeSupport.firePropertyChange("startseq", oldstartseq, start);
+ if (oldstartseq == start)
+ {
+ // event won't be fired if start positions are the same
+ // fire in case the end positions changed
+ changeSupport.firePropertyChange("endseq", oldendseq, end);
+ }
}
/**
- * Set the last sequence visible in the viewport
+ * Set the last sequence visible in the viewport. Fires a property change
+ * event.
*
* @param seq
* sequence position
/**
* Set viewport width in residues, without changing startRes. Use in
* preference to calculating endRes from the width, to avoid out by one
- * errors!
+ * errors! Fires a property change event.
*
* @param w
* width in residues
/**
* Set viewport height in residues, without changing startSeq. Use in
* preference to calculating endSeq from the height, to avoid out by one
- * errors!
+ * errors! Fires a property change event.
*
* @param h
* height in sequences
/**
* Set viewport horizontal start position and width. Use in preference to
- * calculating endRes from the width, to avoid out by one errors!
+ * calculating endRes from the width, to avoid out by one errors! Fires a
+ * property change event.
*
* @param start
* start residue
*/
public void setViewportStartAndWidth(int start, int w)
{
- setStartEndRes(start, start + w - 1);
+ int vpstart = start;
+ if (vpstart < 0)
+ {
+ vpstart = 0;
+ }
+ else if (vpstart + w - 1 > al.getWidth() - 1)
+ {
+ vpstart = al.getWidth() - 1;
+ }
+ setStartEndRes(vpstart, vpstart + w - 1);
}
/**
* Set viewport vertical start position and height. Use in preference to
- * calculating endSeq from the height, to avoid out by one errors!
+ * calculating endSeq from the height, to avoid out by one errors! Fires a
+ * property change event.
*
* @param start
* start sequence
*/
public void setViewportStartAndHeight(int start, int h)
{
- setStartEndSeq(start, start + h - 1);
+ int vpstart = start;
+ if (vpstart < 0)
+ {
+ vpstart = 0;
+ }
+ else if (vpstart + h - 1 > al.getHeight() - 1)
+ {
+ vpstart = al.getHeight() - h;
+ }
+ setStartEndSeq(vpstart, vpstart + h - 1);
}
/**
}
/**
- * Scroll the viewport range vertically
+ * Scroll the viewport range vertically. Fires a property change event.
*
* @param up
* true if scrolling up, false if down
}
/**
- * Scroll the viewport range horizontally
+ * Scroll the viewport range horizontally. Fires a property change event.
*
* @param right
* true if scrolling right, false if left
}
/**
- * Scroll a wrapped alignment so that the specified residue is visible
+ * Scroll a wrapped alignment so that the specified residue is visible. Fires
+ * a property change event.
*
* @param res
* residue position to scroll to
}
/**
- * Scroll so that (x,y) is visible
+ * Scroll so that (x,y) is visible. Fires a property change event.
*
* @param x
* x position in alignment
}
}
}
-
+
+ /**
+ * Adjust sequence position for page up. Fires a property change event.
+ */
+ public void pageUp()
+ {
+ setViewportStartAndHeight(2 * startSeq - endSeq, getViewportHeight());
+ }
+
+ /**
+ * Adjust sequence position for page down. Fires a property change event.
+ */
+ public void pageDown()
+ {
+ setViewportStartAndHeight(endSeq, getViewportHeight());
+ }
}
package jalview.viewmodel;
import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
import jalview.analysis.AlignmentGenerator;
import jalview.datamodel.AlignmentI;
+import java.beans.PropertyChangeEvent;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
import org.testng.annotations.Test;
public class ViewportRangesTest {
AlignmentI al = gen.generate(20, 30, 1, 5, 5);
- @Test
+ @Test(groups = { "Functional" })
public void testViewportRanges()
{
ViewportRanges vr = new ViewportRanges(al);
assertEquals(vr.getEndSeq(), al.getHeight() - 1);
}
- @Test
+ @Test(groups = { "Functional" })
public void testGetAbsoluteAlignmentHeight()
{
ViewportRanges vr = new ViewportRanges(al);
assertEquals(vr.getAbsoluteAlignmentHeight(), al.getHeight() + 1);
}
- @Test
+ @Test(groups = { "Functional" })
public void testGetAbsoluteAlignmentWidth()
{
ViewportRanges vr = new ViewportRanges(al);
assertEquals(vr.getAbsoluteAlignmentWidth(), al.getWidth());
}
- @Test
+ @Test(groups = { "Functional" })
public void testSetEndRes()
{
ViewportRanges vr = new ViewportRanges(al);
vr.setEndRes(-1);
assertEquals(vr.getEndRes(), 0);
- vr.setEndRes(al.getWidth());
- assertEquals(vr.getEndRes(), al.getWidth() - 1);
-
vr.setEndRes(al.getWidth() - 1);
assertEquals(vr.getEndRes(), al.getWidth() - 1);
}
- @Test
+ @Test(groups = { "Functional" })
public void testSetEndSeq()
{
ViewportRanges vr = new ViewportRanges(al);
assertEquals(vr.getEndSeq(), al.getHeight() - 1);
}
- @Test
+ @Test(groups = { "Functional" })
public void testSetStartRes()
{
ViewportRanges vr = new ViewportRanges(al);
assertEquals(vr.getStartRes(), al.getWidth() - 1);
}
- @Test
+ @Test(groups = { "Functional" })
public void testSetStartSeq()
{
ViewportRanges vr = new ViewportRanges(al);
vr.setStartSeq(al.getHeight() - 1);
assertEquals(vr.getStartSeq(), al.getHeight() - 1);
}
+
+ @Test(groups = { "Functional" })
+ public void testSetStartEndRes()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setStartEndRes(-1, -1);
+ assertEquals(vr.getStartRes(), 0);
+ assertEquals(vr.getEndRes(), 0);
+
+ vr.setStartEndRes(5, 19);
+ assertEquals(vr.getStartRes(), 5);
+ assertEquals(vr.getEndRes(), 19);
+
+ vr.setStartEndRes(al.getWidth(), al.getWidth());
+ assertEquals(vr.getEndRes(), al.getWidth());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testSetStartEndSeq()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setStartEndSeq(-1, -1);
+ assertEquals(vr.getStartSeq(), 0);
+ assertEquals(vr.getEndSeq(), 0);
+
+ vr.setStartEndSeq(5, 19);
+ assertEquals(vr.getStartSeq(), 5);
+ assertEquals(vr.getEndSeq(), 19);
+
+ vr.setStartEndSeq(al.getHeight(), al.getHeight());
+ assertEquals(vr.getEndSeq(), al.getHeight() - 1);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testSetViewportHeight()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setViewportHeight(13);
+ assertEquals(vr.getViewportHeight(), 13);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testSetViewportWidth()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setViewportWidth(13);
+ assertEquals(vr.getViewportWidth(), 13);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testSetViewportStartAndHeight()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setViewportStartAndHeight(2, 6);
+ assertEquals(vr.getViewportHeight(), 6);
+ assertEquals(vr.getStartSeq(), 2);
+
+ // reset -ve values of start to 0
+ vr.setViewportStartAndHeight(-1, 7);
+ assertEquals(vr.getViewportHeight(), 7);
+ assertEquals(vr.getStartSeq(), 0);
+
+ // reset out of bounds start values to within bounds
+ vr.setViewportStartAndHeight(35, 5);
+ assertEquals(vr.getViewportHeight(), 5);
+ assertEquals(vr.getStartSeq(), 24);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testSetViewportStartAndWidth()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setViewportStartAndWidth(2, 6);
+ assertEquals(vr.getViewportWidth(), 6);
+ assertEquals(vr.getStartRes(), 2);
+
+ // reset -ve values of start to 0
+ vr.setViewportStartAndWidth(-1, 7);
+ assertEquals(vr.getViewportWidth(), 7);
+ assertEquals(vr.getStartRes(), 0);
+
+ // reset out of bounds start values to within bounds
+ vr.setViewportStartAndWidth(35, 5);
+ assertEquals(vr.getViewportWidth(), 5);
+ assertEquals(vr.getStartRes(), 20);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testPageUpDown()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setViewportStartAndHeight(8, 6);
+ vr.pageDown();
+ assertEquals(vr.getStartSeq(), 13);
+
+ vr.pageUp();
+ assertEquals(vr.getStartSeq(), 8);
+
+ vr.pageUp();
+ assertEquals(vr.getStartSeq(), 3);
+
+ vr.pageUp();
+ // pageup does not go beyond 0, viewport height stays the same
+ assertEquals(vr.getStartSeq(), 0);
+ assertEquals(vr.getViewportHeight(), 6);
+
+ vr.pageDown();
+ vr.pageDown();
+ vr.pageDown();
+ vr.pageDown();
+ vr.pageDown();
+
+ // pagedown to bottom does not go beyond end, and height stays same
+ assertEquals(vr.getStartSeq(), 23);
+ assertEquals(vr.getViewportHeight(), 6);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testScrollUp()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setViewportStartAndHeight(1, 5);
+ vr.scrollUp(true);
+ assertEquals(vr.getStartSeq(), 0);
+ // can't scroll above top
+ vr.scrollUp(true);
+ assertEquals(vr.getStartSeq(), 0);
+
+ vr.setViewportStartAndHeight(23, 5);
+ vr.scrollUp(false);
+ assertEquals(vr.getStartSeq(), 24);
+ // can't scroll beyond bottom
+ vr.scrollUp(false);
+ assertEquals(vr.getStartSeq(), 24);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testScrollRight()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setViewportStartAndWidth(1, 5);
+ vr.scrollRight(false);
+ assertEquals(vr.getStartRes(), 0);
+ // can't scroll left past start
+ vr.scrollRight(false);
+ assertEquals(vr.getStartRes(), 0);
+
+ vr.setViewportStartAndWidth(19, 5);
+ vr.scrollRight(true);
+ assertEquals(vr.getStartRes(), 20);
+ // can't scroll right past end
+ vr.scrollRight(true);
+ assertEquals(vr.getStartRes(), 20);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testScrollToWrappedVisible()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setViewportStartAndWidth(5, 10);
+
+ vr.scrollToWrappedVisible(0);
+ assertEquals(vr.getStartRes(), 0);
+
+ vr.scrollToWrappedVisible(10);
+ assertEquals(vr.getStartRes(), 10);
+
+ vr.scrollToWrappedVisible(15);
+ assertEquals(vr.getStartRes(), 10);
+ }
+
+ // leave until JAL-2388 is merged and we can do without viewport
+ /*@Test(groups = { "Functional" })
+ public void testScrollToVisible()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ vr.setViewportStartAndWidth(12,5);
+ vr.setViewportStartAndHeight(10,6);
+ vr.scrollToVisible(13,14)
+
+ // no change
+ assertEquals(vr.getStartRes(), 12);
+ assertEquals(vr.getStartSeq(), 10);
+
+ vr.scrollToVisible(5,6);
+ assertEquals(vr.getStartRes(), 5);
+ assertEquals(vr.getStartSeq(), 6);
+
+ // test for hidden columns too
+ }*/
+
+ @Test(groups = { "Functional" })
+ public void testEventFiring()
+ {
+ ViewportRanges vr = new ViewportRanges(al);
+ MockPropChangeListener l = new MockPropChangeListener(vr);
+ List<String> emptylist = new ArrayList<String>();
+
+ // one event fired when startRes is called with new value
+ vr.setStartRes(4);
+ assertTrue(l.verify(1, Arrays.asList("startres")));
+ l.reset();
+
+ // no event fired for same value
+ vr.setStartRes(4);
+ assertTrue(l.verify(0, emptylist));
+ l.reset();
+
+ vr.setEndRes(10);
+ assertTrue(l.verify(1, Arrays.asList("startres")));
+ l.reset();
+
+ // no event fired for same value
+ vr.setEndRes(10);
+ assertTrue(l.verify(0, emptylist));
+ l.reset();
+
+ vr.setStartSeq(4);
+ assertTrue(l.verify(1, Arrays.asList("startseq")));
+ l.reset();
+
+ vr.setStartSeq(4);
+ assertTrue(l.verify(0, emptylist));
+ l.reset();
+
+ vr.setEndSeq(10);
+ assertTrue(l.verify(1, Arrays.asList("startseq")));
+ l.reset();
+
+ vr.setEndSeq(10);
+ assertTrue(l.verify(0, emptylist));
+ l.reset();
+
+ vr.setStartEndRes(2, 15);
+ assertTrue(l.verify(1, Arrays.asList("startres")));
+ l.reset();
+
+ vr.setStartEndRes(2, 15);
+ assertTrue(l.verify(0, emptylist));
+ l.reset();
+
+ vr.setStartEndSeq(2, 15);
+ assertTrue(l.verify(1, Arrays.asList("startseq")));
+ l.reset();
+
+ vr.setStartEndSeq(2, 15);
+ assertTrue(l.verify(0, emptylist));
+ l.reset();
+
+ // test viewport height and width setting triggers event
+ vr.setViewportHeight(10);
+ assertTrue(l.verify(1, Arrays.asList("endseq")));
+ l.reset();
+
+ vr.setViewportWidth(18);
+ assertTrue(l.verify(1, Arrays.asList("endres")));
+ l.reset();
+
+ // already has seq start set to 2, so triggers endseq
+ vr.setViewportStartAndHeight(2, 16);
+ assertTrue(l.verify(1, Arrays.asList("endseq")));
+ l.reset();
+
+ vr.setViewportStartAndWidth(1, 14);
+ assertTrue(l.verify(1, Arrays.asList("startres")));
+ l.reset();
+
+ // test page up/down triggers event
+ vr.pageUp();
+ assertTrue(l.verify(1, Arrays.asList("startseq")));
+ l.reset();
+
+ vr.pageDown();
+ assertTrue(l.verify(1, Arrays.asList("startseq")));
+ l.reset();
+
+ // test scrolling triggers event
+ vr.scrollUp(true);
+ assertTrue(l.verify(1, Arrays.asList("startseq")));
+ l.reset();
+
+ vr.scrollUp(false);
+ assertTrue(l.verify(1, Arrays.asList("startseq")));
+ l.reset();
+
+ vr.scrollRight(true);
+ assertTrue(l.verify(1, Arrays.asList("startres")));
+ l.reset();
+
+ vr.scrollRight(false);
+ assertTrue(l.verify(1, Arrays.asList("startres")));
+ l.reset();
+
+ // TODO test scrollToVisibble once hidden columns JAL-2388 merged in
+ // to avoid somersaults with align viewport
+ /*vr.scrollToVisible(10, 10);
+ assertTrue(l.verify(1, Arrays.asList("startres")));
+ l.reset();*/
+
+ vr.scrollToWrappedVisible(5);
+ assertTrue(l.verify(1, Arrays.asList("startres")));
+ l.reset();
+ }
+}
+
+// mock listener for property change events
+class MockPropChangeListener implements ViewportListenerI
+{
+ private int firecount = 0;
+
+ private List<String> events = new ArrayList<String>();
+
+ public MockPropChangeListener(ViewportRanges vr)
+ {
+ vr.addPropertyChangeListener(this);
+ }
+
+ @Override
+ public void propertyChange(PropertyChangeEvent evt)
+ {
+ firecount++;
+ events.add(evt.getPropertyName());
+ }
+
+ public boolean verify(int count, List<String> eventslist)
+ {
+ return (count == firecount) && events.equals(eventslist);
+ }
+
+ public void reset()
+ {
+ firecount = 0;
+ events.clear();
+ }
}