JAL-2491 sort of working overview/find interaction with split panels
[jalview.git] / src / jalview / viewmodel / ViewportRanges.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.datamodel.AlignmentI;
24
25 /**
26  * Embryonic class which: Supplies and updates viewport properties relating to
27  * position such as: start and end residues and sequences; ideally will serve
28  * hidden columns/rows too. Intention also to support calculations for
29  * positioning, scrolling etc. such as finding the middle of the viewport,
30  * checking for scrolls off screen
31  */
32 public class ViewportRanges extends ViewportProperties
33 {
34   // start residue of viewport
35   private int startRes;
36
37   // end residue of viewport
38   private int endRes;
39
40   // start sequence of viewport
41   private int startSeq;
42
43   // end sequence of viewport
44   private int endSeq;
45
46   // alignment
47   private AlignmentI al;
48
49   /**
50    * Constructor
51    * 
52    * @param alignment
53    *          the viewport's alignment
54    */
55   public ViewportRanges(AlignmentI alignment)
56   {
57     // initial values of viewport settings
58     this.startRes = 0;
59     this.endRes = alignment.getWidth() - 1;
60     this.startSeq = 0;
61     this.endSeq = alignment.getHeight() - 1;
62     this.al = alignment;
63   }
64
65   /**
66    * Get alignment width in cols, including hidden cols
67    */
68   public int getAbsoluteAlignmentWidth()
69   {
70     return al.getWidth();
71   }
72
73   /**
74    * Get alignment height in rows, including hidden rows
75    */
76   public int getAbsoluteAlignmentHeight()
77   {
78     return al.getHeight() + al.getHiddenSequences().getSize();
79   }
80
81   /**
82    * Set first residue visible in the viewport, and retain the current width.
83    * 
84    * @param res
85    *          residue position
86    */
87   public void setStartRes(int res)
88   {
89     int oldres = this.startRes;
90     setStartResInternal(res);
91     changeSupport.firePropertyChange("startres", oldres, res);
92   }
93
94   private void setStartResInternal(int res)
95   {
96     if (res > al.getWidth() - 1)
97     {
98       res = al.getWidth() - 1;
99     }
100     else if (res < 0)
101     {
102       res = 0;
103     }
104     this.startRes = res;
105   }
106
107   public void setStartEndRes(int startres, int endres)
108   {
109     int oldres = this.startRes;
110     setStartResInternal(startres);
111     setEndResInternal(endres);
112     changeSupport.firePropertyChange("startres", oldres, startres);
113   }
114
115   /**
116    * Set last residue visible in the viewport
117    * 
118    * @param res
119    *          residue position
120    */
121   public void setEndRes(int res)
122   {
123     int oldres = this.endRes;
124     setEndResInternal(res);
125
126     changeSupport.firePropertyChange("endres", oldres, res);
127   }
128
129   private void setEndResInternal(int res)
130   {
131     if (res >= al.getWidth())
132     {
133       res = al.getWidth() - 1;
134     }
135     else if (res < 0)
136     {
137       res = 0;
138     }
139     this.endRes = res;
140   }
141
142   /**
143    * Set the first sequence visible in the viewport
144    * 
145    * @param seq
146    *          sequence position
147    */
148   public void setStartSeq(int seq)
149   {
150     int oldseq = this.startSeq;
151     setStartSeqInternal(seq);
152     changeSupport.firePropertyChange("startseq", oldseq, seq);
153   }
154
155   private void setStartSeqInternal(int seq)
156   {
157     if (seq > al.getHeight() - 1)
158     {
159       seq = al.getHeight() - 1;
160     }
161     else if (seq < 0)
162     {
163       seq = 0;
164     }
165     this.startSeq = seq;
166   }
167
168   public void setStartEndSeq(int startseq, int endseq)
169   {
170     int oldseq = this.startSeq;
171     setStartSeqInternal(startseq);
172     setEndSeqInternal(endseq);
173     changeSupport.firePropertyChange("startseq", oldseq, startseq);
174   }
175
176   /**
177    * Set the last sequence visible in the viewport
178    * 
179    * @param seq
180    *          sequence position
181    */
182   public void setEndSeq(int seq)
183   {
184     int oldseq = this.endSeq;
185     setEndSeqInternal(seq);
186     changeSupport.firePropertyChange("endseq", oldseq, seq);
187   }
188
189   private void setEndSeqInternal(int seq)
190   {
191     if (seq >= al.getHeight())
192     {
193       seq = al.getHeight() - 1;
194     }
195     else if (seq < 0)
196     {
197       seq = 0;
198     }
199     this.endSeq = seq;
200   }
201
202   /**
203    * Get start residue of viewport
204    */
205   public int getStartRes()
206   {
207     return startRes;
208   }
209
210   /**
211    * Get end residue of viewport
212    */
213   public int getEndRes()
214   {
215     return endRes;
216   }
217
218   /**
219    * Get start sequence of viewport
220    */
221   public int getStartSeq()
222   {
223     return startSeq;
224   }
225
226   /**
227    * Get end sequence of viewport
228    */
229   public int getEndSeq()
230   {
231     return endSeq;
232   }
233 }