a4b7f06bef72dfbd9d385662896aaecb5ef6c41e
[jalview.git] / src / jalview / appletgui / OverviewPanel.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.awt.*;\r
23 import java.awt.event.*;\r
24 \r
25 public class OverviewPanel\r
26     extends Panel implements Runnable, MouseMotionListener, MouseListener\r
27 {\r
28   Image miniMe;\r
29   AlignViewport av;\r
30   AlignmentPanel ap;\r
31   float scalew = 1f;\r
32   float scaleh = 1f;\r
33 \r
34   public int width, sequencesHeight;\r
35   int graphHeight = 20;\r
36   int boxX = -1, boxY = -1, boxWidth = -1, boxHeight = -1;\r
37 \r
38   boolean resizing = false;\r
39 \r
40   // Can set different properties in this seqCanvas than\r
41   // main visible SeqCanvas\r
42   SequenceRenderer sr;\r
43   FeatureRenderer fr;\r
44 \r
45 \r
46   Frame nullFrame;\r
47 \r
48   public OverviewPanel(AlignmentPanel ap)\r
49   {\r
50     this.av = ap.av;\r
51     this.ap = ap;\r
52     setLayout(null);\r
53     nullFrame = new Frame();\r
54     nullFrame.addNotify();\r
55 \r
56 \r
57     sr = new SequenceRenderer(av);\r
58     sr.graphics = nullFrame.getGraphics();\r
59     sr.renderGaps( false );\r
60     sr.forOverview = true;\r
61     fr = new FeatureRenderer(av);\r
62     fr.overview = true;\r
63 \r
64 \r
65 \r
66     // scale the initial size of overviewpanel to shape of alignment\r
67     float initialScale = (float) av.alignment.getWidth() /\r
68         (float) av.alignment.getHeight();\r
69 \r
70     if(av.vconsensus==null)\r
71           graphHeight = 0;\r
72 \r
73     if (av.alignment.getWidth() > av.alignment.getHeight())\r
74     {\r
75       // wider\r
76       width = 400;\r
77       sequencesHeight = (int) (400f / initialScale);\r
78       if(sequencesHeight<40)\r
79               sequencesHeight = 40;\r
80     }\r
81     else\r
82     {\r
83       // taller\r
84       width = (int) (400f * initialScale);\r
85       sequencesHeight = 300;\r
86       if (width < 120)\r
87       {\r
88         width = 120;\r
89       }\r
90     }\r
91 \r
92     setSize(new Dimension(width, sequencesHeight + graphHeight));\r
93     addComponentListener(new ComponentAdapter()\r
94     {\r
95 \r
96       public void componentResized(ComponentEvent evt)\r
97       {\r
98         if (getSize().width != width ||\r
99             getSize().height != sequencesHeight + graphHeight)\r
100         {\r
101           updateOverviewImage();\r
102         }\r
103       }\r
104     });\r
105 \r
106     addMouseMotionListener(this);\r
107 \r
108     addMouseListener(this);\r
109 \r
110     updateOverviewImage();\r
111 \r
112   }\r
113 \r
114 \r
115   public void mouseEntered(MouseEvent evt)\r
116   {}\r
117   public void mouseExited(MouseEvent evt)\r
118   {}\r
119   public void mouseClicked(MouseEvent evt)\r
120   {}\r
121   public void mouseMoved(MouseEvent evt)\r
122   {}\r
123   public void mousePressed(MouseEvent evt)\r
124   {\r
125     boxX = evt.getX();\r
126     boxY = evt.getY();\r
127 \r
128     checkValid();\r
129     repaint();\r
130   }\r
131 \r
132   public void mouseReleased(MouseEvent evt)\r
133   {\r
134     boxX = evt.getX();\r
135     boxY = evt.getY();\r
136     checkValid();\r
137     ap.setScrollValues( (int) (boxX / scalew / av.getCharWidth()),\r
138                          (int) (boxY / scaleh / av.getCharHeight()));\r
139   }\r
140 \r
141   public void mouseDragged(MouseEvent evt)\r
142   {\r
143     boxX = evt.getX();\r
144     boxY = evt.getY();\r
145     checkValid();\r
146     ap.setScrollValues( (int) (boxX / scalew / av.getCharWidth()),\r
147                          (int) (boxY / scaleh / av.getCharHeight()));\r
148 \r
149     repaint();\r
150     ap.repaint();\r
151   }\r
152 \r
153   void checkValid()\r
154   {\r
155     if (boxY < 0)\r
156     {\r
157       boxY = 0;\r
158     }\r
159 \r
160     if (boxY > sequencesHeight - boxHeight)\r
161     {\r
162       boxY = sequencesHeight - boxHeight + 1;\r
163     }\r
164 \r
165     if (boxX < 0)\r
166     {\r
167       boxX = 0;\r
168     }\r
169 \r
170     if (boxX > width - boxWidth)\r
171     {\r
172       boxX = width - boxWidth;\r
173     }\r
174   }\r
175 \r
176   /**\r
177    * DOCUMENT ME!\r
178    */\r
179   public void updateOverviewImage()\r
180   {\r
181     if (resizing)\r
182     {\r
183         resizeAgain = true;\r
184         return;\r
185     }\r
186 \r
187     if (av.showSequenceFeatures)\r
188    {\r
189      fr.featureGroups = ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups;\r
190      fr.featureColours = ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours;\r
191    }\r
192 \r
193     resizing = true;\r
194 \r
195     if ( (getSize().width > 0) && (getSize().height > 0))\r
196     {\r
197       width = getSize().width;\r
198       sequencesHeight = getSize().height - graphHeight;\r
199     }\r
200     setSize(new Dimension(width, sequencesHeight + graphHeight));\r
201     setBoxPosition();\r
202 \r
203     Thread thread = new Thread(this);\r
204     thread.start();\r
205         repaint();\r
206   }\r
207 \r
208   // This is set true if the user resizes whilst\r
209   // the overview is being calculated\r
210     boolean resizeAgain = false;\r
211 \r
212   public void run()\r
213   {\r
214     miniMe = null;\r
215     int alwidth = av.alignment.getWidth();\r
216     int alheight = av.alignment.getHeight();\r
217 \r
218     if (av.showSequenceFeatures)\r
219     {\r
220       fr.renderOrder = ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder;\r
221       fr.featureGroups = ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups;\r
222       fr.featureColours = ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours;\r
223       fr.sequenceFeatures = ap.seqPanel.seqCanvas.getFeatureRenderer().sequenceFeatures;\r
224     }\r
225 \r
226     if (getSize().width > 0 && getSize().height > 0)\r
227     {\r
228       width = getSize().width;\r
229       sequencesHeight = getSize().height - graphHeight;\r
230     }\r
231 \r
232     setSize(new Dimension(width, sequencesHeight + graphHeight));\r
233 \r
234     int fullsizeWidth = alwidth * av.getCharWidth();\r
235     int fullsizeHeight = alheight * av.getCharHeight();\r
236 \r
237     scalew = (float) width / (float) fullsizeWidth;\r
238     scaleh = (float) sequencesHeight / (float) fullsizeHeight;\r
239 \r
240     miniMe = nullFrame.createImage(width, sequencesHeight + graphHeight);\r
241 \r
242     Graphics mg = miniMe.getGraphics();\r
243     float sampleCol = (float) alwidth / (float) width;\r
244     float sampleRow = (float) alheight / (float) sequencesHeight;\r
245 \r
246     int lastcol=0, lastseq=0;\r
247     int xstart=0, ystart=0;\r
248     Color color = Color.yellow;\r
249     int col, sameRow = 0, sameCol = 0;\r
250     jalview.datamodel.SequenceI sequence;\r
251 \r
252     for (int row = 0; row <= sequencesHeight; row++)\r
253     {\r
254       if((int)(row*sampleRow)==lastseq)\r
255       {\r
256         sameRow ++;\r
257         continue;\r
258       }\r
259 \r
260       sequence = av.getAlignment().getSequenceAt(lastseq);\r
261 \r
262       for (col = 0; col < width; col++)\r
263       {\r
264         if((int)(col*sampleCol) == lastcol)\r
265         {\r
266           sameCol ++;\r
267           continue;\r
268         }\r
269 \r
270         lastcol = (int)(col*sampleCol);\r
271 \r
272         if(sequence.getLength()>lastcol)\r
273         {\r
274           color = sr.findSequenceColour(sequence, lastcol);\r
275 \r
276           if (av.showSequenceFeatures)\r
277             color = fr.findFeatureColour(color,\r
278                                         sequence,\r
279                                         lastcol);\r
280         }\r
281         else\r
282           color = color.white;\r
283 \r
284         mg.setColor(color);\r
285         if (sameCol == 1 && sameRow == 1)\r
286           mg.drawLine(xstart, ystart, xstart, ystart);\r
287         else\r
288           mg.fillRect(xstart, ystart, sameCol, sameRow);\r
289 \r
290         xstart = col;\r
291         sameCol = 1;\r
292       }\r
293 \r
294 \r
295       lastseq = (int)(row*sampleRow);\r
296       ystart = row;\r
297       sameRow = 1;\r
298     }\r
299 \r
300     if (av.conservation != null)\r
301     {\r
302       for (col = 0; col < width; col++)\r
303       {\r
304         lastcol = (int) (col * sampleCol);\r
305         {\r
306           mg.translate(col, sequencesHeight);\r
307           ap.annotationPanel.drawGraph(mg, av.conservation,\r
308                                        (int) (sampleCol) + 1,\r
309                                        graphHeight,\r
310                                        (int) (col * sampleCol),\r
311                                        (int) (col * sampleCol) + 1);\r
312           mg.translate( -col, -sequencesHeight);\r
313         }\r
314       }\r
315     }\r
316 \r
317 \r
318 \r
319     System.gc();\r
320 \r
321     resizing = false;\r
322 \r
323     setBoxPosition();\r
324 \r
325     if(resizeAgain)\r
326     {\r
327       resizeAgain = false;\r
328       updateOverviewImage();\r
329     }\r
330   }\r
331 \r
332   public void setBoxPosition()\r
333   {\r
334     int fullsizeWidth = av.alignment.getWidth() * av.getCharWidth();\r
335     int fullsizeHeight = av.alignment.getHeight() * av.getCharHeight();\r
336 \r
337     scalew = (float) width / (float) fullsizeWidth;\r
338     scaleh = (float) sequencesHeight / (float) fullsizeHeight;\r
339 \r
340     boxX = (int) (av.getStartRes() * av.getCharWidth() * scalew);\r
341     boxY = (int) (av.getStartSeq() * av.getCharHeight() * scaleh);\r
342     boxWidth = (int) ( (av.getEndRes() - av.getStartRes() + 1) *\r
343                       av.getCharWidth() * scalew);\r
344     boxHeight = (int) (av.getEndSeq() * av.getCharHeight() * scaleh) - boxY;\r
345     repaint();\r
346   }\r
347 \r
348   public void update(Graphics g)\r
349   {\r
350     paint(g);\r
351   }\r
352 \r
353   public void paint(Graphics g)\r
354   {\r
355     if (miniMe != null)\r
356     {\r
357       g.drawImage(miniMe, 0, 0, this);\r
358     }\r
359     else\r
360     {\r
361       g.setColor(Color.white);\r
362       g.fillRect(0, 0, getSize().width, getSize().height);\r
363       g.setColor(Color.black);\r
364       g.setFont(new Font("Verdana", Font.BOLD, 15));\r
365       g.drawString("Recalculating", 5, sequencesHeight / 2);\r
366       g.drawString("Overview.....", 5, (sequencesHeight / 2) + 20);\r
367     }\r
368 \r
369     g.setColor(Color.red);\r
370     g.drawRect(boxX, boxY, boxWidth, boxHeight);\r
371     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);\r
372   }\r
373 \r
374 }\r