JAL-2613 Unit tests and a fix. Phew.
[jalview.git] / src / jalview / viewmodel / OverviewDimensionsHideHidden.java
1 package jalview.viewmodel;
2
3 import jalview.api.AlignmentColsCollectionI;
4 import jalview.api.AlignmentRowsCollectionI;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.HiddenColumns;
7 import jalview.datamodel.HiddenSequences;
8 import jalview.datamodel.VisibleColsCollection;
9 import jalview.datamodel.VisibleRowsCollection;
10
11 public class OverviewDimensionsHideHidden extends OverviewDimensions
12 {
13   private ViewportRanges ranges;
14
15   public OverviewDimensionsHideHidden(ViewportRanges vpranges,
16           boolean showAnnotationPanel)
17   {
18     super(vpranges, showAnnotationPanel);
19     ranges = vpranges;
20     resetAlignmentDims();
21   }
22
23   @Override
24   public void updateViewportFromMouse(int mousex, int mousey,
25           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
26   {
27     resetAlignmentDims();
28
29     int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
30     int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
31
32     if (xAsRes < 0)
33     {
34       xAsRes = 0;
35     }
36
37     if (yAsSeq < 0)
38     {
39       yAsSeq = 0;
40     }
41
42     //
43     // Convert x value to residue position
44     //
45
46     // need to determine where scrollCol should be, given x
47     // to do this also need to know width of viewport, and some hidden column
48     // correction
49
50     // convert x to residues - this is an absolute position
51     // int xAsRes = Math.round((float) x * alwidth / width);
52
53     // get viewport width in residues
54     int vpwidth = ranges.getViewportWidth();
55
56     if (xAsRes + vpwidth > alwidth)
57     {
58       // went past the end of the alignment, adjust backwards
59
60       // if last position was before the end of the alignment, need to update
61       if (ranges.getStartRes() < alwidth)
62       {
63         xAsRes = alwidth - vpwidth;
64       }
65       else
66       {
67         xAsRes = ranges.getStartRes();
68       }
69     }
70
71
72     //
73     // Convert y value to sequence position
74     //
75
76     // get viewport height in sequences
77     // add 1 because height includes both endSeq and startSeq
78     int vpheight = ranges.getViewportHeight();
79
80     if (yAsSeq + vpheight > alheight)
81     {
82       // went past the end of the alignment, adjust backwards
83       if (ranges.getEndSeq() < alheight)
84       {
85         yAsSeq = alheight - vpheight;
86       }
87       else
88       {
89         yAsSeq = ranges.getStartSeq();
90       }
91     }
92
93     // update viewport
94     ranges.setStartRes(xAsRes);
95     ranges.setStartSeq(yAsSeq);
96
97   }
98
99   @Override
100   public void setBoxPosition(HiddenSequences hiddenSeqs,
101           HiddenColumns hiddenCols)
102   {
103     setBoxPosition(ranges.getStartRes(), ranges.getStartSeq(),
104             ranges.getViewportWidth(), ranges.getViewportHeight());
105   }
106
107   @Override
108   public AlignmentColsCollectionI getColumns(AlignmentI al)
109   {
110     return new VisibleColsCollection(0,
111             ranges.getAbsoluteAlignmentWidth() - 1, al);
112   }
113
114   @Override
115   public AlignmentRowsCollectionI getRows(AlignmentI al)
116   {
117     return new VisibleRowsCollection(0,
118             ranges.getAbsoluteAlignmentHeight() - 1, al);
119   }
120
121   @Override
122   protected void resetAlignmentDims()
123   {
124     alwidth = ranges.getVisibleAlignmentWidth();
125     alheight = ranges.getVisibleAlignmentHeight();
126   }
127
128   @Override
129   protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
130   {
131     int vpx = Math.round((float) mousex * alwidth / width);
132     return vpx - ranges.getViewportWidth() / 2;
133   }
134
135   @Override
136   protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
137   {
138     int vpy = Math.round((float) mousey * alheight / sequencesHeight);
139     return vpy - ranges.getViewportHeight() / 2;
140   }
141 }