JAL-4128 Catch RasterFormatException caused whilst resizing Overview window, recalcul...
[jalview.git] / src / jalview / viewmodel / OverviewDimensions.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 java.awt.Graphics;
24
25 import jalview.api.AlignmentColsCollectionI;
26 import jalview.api.AlignmentRowsCollectionI;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.HiddenSequences;
30
31 public abstract class OverviewDimensions
32 {
33   protected static final int MAX_WIDTH = 400;
34
35   protected static final int MIN_WIDTH = 120;
36
37   protected static final int MIN_SEQ_HEIGHT = 40;
38
39   protected static final int MAX_SEQ_HEIGHT = 300;
40
41   private static final int DEFAULT_GRAPH_HEIGHT = 20;
42
43   protected int width;
44
45   protected int sequencesHeight;
46
47   protected int graphHeight = DEFAULT_GRAPH_HEIGHT;
48
49   protected int boxX = -1;
50
51   protected int boxY = -1;
52
53   protected int boxWidth = -1;
54
55   protected int boxHeight = -1;
56
57   protected int alwidth;
58
59   protected int alheight;
60
61   protected float widthRatio;
62
63   protected float heightRatio;
64
65   /**
66    * Create an OverviewDimensions object
67    * 
68    * @param ranges
69    *          positional properties of the viewport
70    * @param showAnnotationPanel
71    *          true if the annotation panel is to be shown, false otherwise
72    */
73   public OverviewDimensions(ViewportRanges ranges,
74           boolean showAnnotationPanel)
75   {
76     // scale the initial size of overviewpanel to shape of alignment
77     float initialScale = (float) ranges.getAbsoluteAlignmentWidth()
78             / (float) ranges.getAbsoluteAlignmentHeight();
79
80     if (!showAnnotationPanel)
81     {
82       graphHeight = 0;
83     }
84
85     if (ranges.getAbsoluteAlignmentWidth() > ranges
86             .getAbsoluteAlignmentHeight())
87     {
88       // wider
89       width = MAX_WIDTH;
90       sequencesHeight = Math.round(MAX_WIDTH / initialScale);
91       if (sequencesHeight < MIN_SEQ_HEIGHT)
92       {
93         sequencesHeight = MIN_SEQ_HEIGHT;
94       }
95     }
96     else
97     {
98       // taller
99       width = Math.round(MAX_WIDTH * initialScale);
100       sequencesHeight = MAX_SEQ_HEIGHT;
101
102       if (width < MIN_WIDTH)
103       {
104         width = MIN_WIDTH;
105       }
106     }
107   }
108
109   /**
110    * Draw the overview panel's viewport box on a graphics object
111    * 
112    * @param g
113    *          the graphics object to draw on
114    */
115   public void drawBox(Graphics g)
116   {
117     g.drawRect(boxX, boxY, boxWidth, boxHeight);
118     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
119   }
120
121   public int getBoxX()
122   {
123     return boxX;
124   }
125
126   public int getBoxY()
127   {
128     return boxY;
129   }
130
131   public int getBoxWidth()
132   {
133     return boxWidth;
134   }
135
136   public int getBoxHeight()
137   {
138     return boxHeight;
139   }
140
141   public int getWidth()
142   {
143     return width;
144   }
145
146   public int getHeight()
147   {
148     return sequencesHeight + graphHeight;
149   }
150
151   public int getSequencesHeight()
152   {
153     return sequencesHeight;
154   }
155
156   public int getGraphHeight()
157   {
158     return graphHeight;
159   }
160
161   public float getPixelsPerCol()
162   {
163     resetAlignmentDims();
164     return 1 / widthRatio;
165   }
166
167   public float getPixelsPerSeq()
168   {
169     resetAlignmentDims();
170     return 1 / heightRatio;
171   }
172
173   public void setWidth(int w)
174   {
175     width = w < 1 ? 1 : w;
176     widthRatio = (float) alwidth / width;
177   }
178
179   public void setHeight(int h)
180   {
181     sequencesHeight = (h < 1 ? 1 : h) - graphHeight;
182     heightRatio = (float) alheight / sequencesHeight;
183   }
184
185   /**
186    * Update the viewport location from a mouse click in the overview panel
187    * 
188    * @param mousex
189    *          x location of mouse
190    * @param mousey
191    *          y location of mouse
192    * @param hiddenSeqs
193    *          the alignment's hidden sequences
194    * @param hiddenCols
195    *          the alignment's hidden columns
196    */
197   public abstract void updateViewportFromMouse(int mousex, int mousey,
198           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
199
200   /**
201    * Update the viewport location from a mouse drag within the overview's box
202    * 
203    * @param mousex
204    *          x location of mouse
205    * @param mousey
206    *          y location of mouse
207    * @param hiddenSeqs
208    *          the alignment's hidden sequences
209    * @param hiddenCols
210    *          the alignment's hidden columns
211    */
212   public abstract void adjustViewportFromMouse(int mousex, int mousey,
213           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
214
215   /**
216    * Initialise dragging from the mouse - must be called on initial mouse click
217    * before using adjustViewportFromMouse in drag operations
218    * 
219    * @param mousex
220    *          x location of mouse
221    * @param mousey
222    *          y location of mouse
223    * @param hiddenSeqs
224    *          the alignment's hidden sequences
225    * @param hiddenCols
226    *          the alignment's hidden columns
227    */
228   public abstract void setDragPoint(int x, int y,
229           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
230
231   /*
232    * Move the viewport so that the top left corner of the overview's box 
233    * is at the mouse position (leftx, topy)
234    */
235   protected abstract void updateViewportFromTopLeft(int leftx, int topy,
236           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
237
238   /**
239    * Set the overview panel's box position to match the viewport
240    * 
241    * @param hiddenSeqs
242    *          the alignment's hidden sequences
243    * @param hiddenCols
244    *          the alignment's hidden columns
245    */
246   public abstract void setBoxPosition(HiddenSequences hiddenSeqs,
247           HiddenColumns hiddenCols);
248
249   /**
250    * Get the collection of columns used by this overview dimensions object
251    * 
252    * @param hiddenCols
253    *          the alignment's hidden columns
254    * @return a column collection
255    */
256   public abstract AlignmentColsCollectionI getColumns(AlignmentI al);
257
258   /**
259    * Get the collection of rows used by this overview dimensions object
260    * 
261    * @param al
262    *          the alignment
263    * @return a row collection
264    */
265   public abstract AlignmentRowsCollectionI getRows(AlignmentI al);
266
267   /**
268    * Updates overview dimensions to account for current alignment dimensions
269    */
270   protected abstract void resetAlignmentDims();
271
272   /*
273    * Given the box coordinates in residues and sequences, set the box dimensions in the overview window
274    */
275   protected void setBoxPosition(int startRes, int startSeq, int vpwidth,
276           int vpheight)
277   {
278     resetAlignmentDims();
279
280     // boxX, boxY is the x,y location equivalent to startRes, startSeq
281     int xPos = Math.min(startRes, alwidth - vpwidth + 1);
282     boxX = Math.round(xPos / widthRatio);
283     boxY = Math.round(startSeq / heightRatio);
284
285     // boxWidth is the width in residues translated to pixels
286     boxWidth = Math.round(vpwidth / widthRatio);
287
288     // boxHeight is the height in sequences translated to pixels
289     boxHeight = Math.round(vpheight / heightRatio);
290   }
291
292   /**
293    * Answers if a mouse position is in the overview's red box
294    * 
295    * @param x
296    *          mouse x position
297    * @param y
298    *          mouse y position
299    * @return true if (x,y) is inside the box
300    */
301   public boolean isPositionInBox(int x, int y)
302   {
303     return (x > boxX && y > boxY && x < boxX + boxWidth
304             && y < boxY + boxHeight);
305   }
306
307   /*
308    * Given the centre x position, calculate the box's left x position
309    */
310   protected abstract int getLeftXFromCentreX(int mousex,
311           HiddenColumns hidden);
312
313   /*
314    * Given the centre y position, calculate the box's top y position
315    */
316   protected abstract int getTopYFromCentreY(int mousey,
317           HiddenSequences hidden);
318
319 }