JAL-3369 JAL-3253-applet adds embedded dim checking for overview frame
[jalview.git] / src / jalview / viewmodel / OverviewDimensionsHideHidden.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.viewmodel;
22
23 import jalview.api.AlignmentColsCollectionI;
24 import jalview.api.AlignmentRowsCollectionI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.HiddenColumns;
27 import jalview.datamodel.HiddenSequences;
28 import jalview.datamodel.VisibleColsCollection;
29 import jalview.datamodel.VisibleRowsCollection;
30
31 import java.awt.Dimension;
32
33 public class OverviewDimensionsHideHidden extends OverviewDimensions
34 {
35   private ViewportRanges ranges;
36
37   private int xdiff; // when dragging, difference in alignment units between
38                      // start residue and original mouse click position
39
40   private int ydiff; // when dragging, difference in alignment units between
41                      // start sequence and original mouse click position
42
43   public OverviewDimensionsHideHidden(ViewportRanges vpranges,
44           boolean showAnnotationPanel)
45   {
46     this(vpranges, showAnnotationPanel, null);
47   }
48
49   public OverviewDimensionsHideHidden(ViewportRanges vpranges,
50           boolean showAnnotationPanel, Dimension dim)
51   {
52     super(vpranges, showAnnotationPanel, dim);
53     ranges = vpranges;
54     resetAlignmentDims();
55   }
56
57   @Override
58   public void updateViewportFromMouse(int mousex, int mousey,
59           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
60   {
61     resetAlignmentDims();
62
63     int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
64     int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
65
66     updateViewportFromTopLeft(xAsRes, yAsSeq, hiddenSeqs, hiddenCols);
67
68   }
69
70   @Override
71   public void adjustViewportFromMouse(int mousex, int mousey,
72           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
73   {
74     resetAlignmentDims();
75
76     // calculate translation in pixel terms:
77     // get mouse location in viewport coords, add translation in viewport
78     // coords, and update viewport as usual
79     int vpx = Math.round(mousex * widthRatio);
80     int vpy = Math.round(mousey * heightRatio);
81
82     updateViewportFromTopLeft(vpx + xdiff, vpy + ydiff, hiddenSeqs,
83             hiddenCols);
84
85   }
86
87   /**
88    * {@inheritDoc} Callers should have already called resetAlignmentDims to
89    * refresh alwidth, alheight and width/height ratios
90    */
91   @Override
92   protected void updateViewportFromTopLeft(int leftx, int topy,
93           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
94   {
95     int xAsRes = leftx;
96     int yAsSeq = topy;
97
98     if (xAsRes < 0)
99     {
100       xAsRes = 0;
101     }
102
103     if (yAsSeq < 0)
104     {
105       yAsSeq = 0;
106     }
107
108     if (ranges.isWrappedMode())
109     {
110       yAsSeq = 0; // sorry, no vertical scroll when wrapped
111     }
112
113     // get viewport width in residues
114     int vpwidth = ranges.getViewportWidth();
115
116     if (xAsRes + vpwidth > alwidth)
117     {
118       // went past the end of the alignment, adjust backwards
119
120       // if last position was before the end of the alignment, need to update
121       if (ranges.getStartRes() < alwidth)
122       {
123         xAsRes = alwidth - vpwidth;
124       }
125       else
126       {
127         xAsRes = ranges.getStartRes();
128       }
129     }
130
131     // Determine where scrollRow should be, given visYAsSeq
132
133     // get viewport height in sequences
134     // add 1 because height includes both endSeq and startSeq
135     int vpheight = ranges.getViewportHeight();
136
137     if (yAsSeq + vpheight > alheight)
138     {
139       // went past the end of the alignment, adjust backwards
140       if (ranges.getEndSeq() < alheight)
141       {
142         yAsSeq = alheight - vpheight;
143       }
144       else
145       {
146         yAsSeq = ranges.getStartSeq();
147       }
148     }
149
150     ranges.setStartResAndSeq(xAsRes, yAsSeq);
151   }
152
153   @Override
154   public void setBoxPosition(HiddenSequences hiddenSeqs,
155           HiddenColumns hiddenCols)
156   {
157     setBoxPosition(ranges.getStartRes(), ranges.getStartSeq(),
158             ranges.getViewportWidth(), ranges.getViewportHeight());
159   }
160
161   @Override
162   public AlignmentColsCollectionI getColumns(AlignmentI al)
163   {
164     return new VisibleColsCollection(0,
165             ranges.getAbsoluteAlignmentWidth() - 1, al.getHiddenColumns());
166   }
167
168   @Override
169   public AlignmentRowsCollectionI getRows(AlignmentI al)
170   {
171     return new VisibleRowsCollection(0,
172             ranges.getAbsoluteAlignmentHeight() - 1, al);
173   }
174
175   @Override
176   protected void resetAlignmentDims()
177   {
178     alwidth = ranges.getVisibleAlignmentWidth();
179     alheight = ranges.getVisibleAlignmentHeight();
180
181     widthRatio = (float) alwidth / width;
182     heightRatio = (float) alheight / sequencesHeight;
183   }
184
185   /**
186    * {@inheritDoc} Callers should have already called resetAlignmentDims to
187    * refresh widthRatio
188    */
189   @Override
190   protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
191   {
192     int vpx = Math.round(mousex * widthRatio);
193     return vpx - ranges.getViewportWidth() / 2;
194   }
195
196   /**
197    * {@inheritDoc} Callers should have already called resetAlignmentDims to
198    * refresh heightRatio
199    */
200   @Override
201   protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
202   {
203     int vpy = Math.round(mousey * heightRatio);
204     return vpy - ranges.getViewportHeight() / 2;
205   }
206
207   @Override
208   public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs,
209           HiddenColumns hiddenCols)
210   {
211     resetAlignmentDims();
212
213     // get alignment position of x and box (can get directly from vpranges) and
214     // calculate difference between the positions
215     int vpx = Math.round(x * widthRatio);
216     int vpy = Math.round(y * heightRatio);
217
218     xdiff = ranges.getStartRes() - vpx;
219     ydiff = ranges.getStartSeq() - vpy;
220   }
221
222 }