4d64f1caf5caf7c90229ce920104a0168056d3a8
[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 x = mousex;
30     int y = mousey;
31
32     if (x < 0)
33     {
34       x = 0;
35     }
36
37     if (y < 0)
38     {
39       y = 0;
40     }
41
42     if (ranges.isWrappedMode())
43     {
44       y = 0; // sorry, no vertical scroll when wrapped
45     }
46
47     //
48     // Convert x value to residue position
49     //
50
51     // need to determine where scrollCol should be, given x
52     // to do this also need to know width of viewport, and some hidden column
53     // correction
54
55     // convert x to residues - this is an absolute position
56     int xAsRes = Math.round((float) x * alwidth / width);
57
58     // get viewport width in residues
59     int vpwidth = ranges.getViewportWidth();
60
61     if (xAsRes + vpwidth > alwidth)
62     {
63       // went past the end of the alignment, adjust backwards
64
65       // if last position was before the end of the alignment, need to update
66       if (ranges.getStartRes() < alwidth)
67       {
68         xAsRes = alwidth - vpwidth;
69       }
70       else
71       {
72         xAsRes = ranges.getStartRes();
73       }
74     }
75
76
77     //
78     // Convert y value to sequence position
79     //
80
81     // convert y to residues
82     int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
83
84     // get viewport height in sequences
85     // add 1 because height includes both endSeq and startSeq
86     int vpheight = ranges.getViewportHeight();
87
88     if (yAsSeq + vpheight > alheight)
89     {
90       // went past the end of the alignment, adjust backwards
91       if (ranges.getEndSeq() < alheight)
92       {
93         yAsSeq = alheight - vpheight;
94       }
95       else
96       {
97         yAsSeq = ranges.getStartSeq();
98       }
99     }
100
101     // update viewport
102     ranges.setStartRes(xAsRes);
103     ranges.setStartSeq(yAsSeq);
104
105   }
106
107   @Override
108   public void setBoxPosition(HiddenSequences hiddenSeqs,
109           HiddenColumns hiddenCols)
110   {
111     setBoxPosition(ranges.getStartRes(), ranges.getStartSeq(),
112             ranges.getViewportWidth(), ranges.getViewportHeight());
113   }
114
115   @Override
116   public AlignmentColsCollectionI getColumns(AlignmentI al)
117   {
118     return new VisibleColsCollection(0,
119             ranges.getAbsoluteAlignmentWidth() - 1, al);
120   }
121
122   @Override
123   public AlignmentRowsCollectionI getRows(AlignmentI al)
124   {
125     return new VisibleRowsCollection(0,
126             ranges.getAbsoluteAlignmentHeight() - 1, al);
127   }
128
129   @Override
130   protected void resetAlignmentDims()
131   {
132     alwidth = ranges.getVisibleAlignmentWidth();
133     alheight = ranges.getVisibleAlignmentHeight();
134   }
135 }