47c781ba4783b07a670894993d1be392433b7192
[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     fr = new FeatureRenderer(av);\r
61     fr.drawText = false;\r
62 \r
63 \r
64 \r
65     // scale the initial size of overviewpanel to shape of alignment\r
66     float initialScale = (float) av.alignment.getWidth() /\r
67         (float) av.alignment.getHeight();\r
68 \r
69     if(av.vconsensus==null)\r
70           graphHeight = 0;\r
71 \r
72     if (av.alignment.getWidth() > av.alignment.getHeight())\r
73     {\r
74       // wider\r
75       width = 400;\r
76       sequencesHeight = (int) (400f / initialScale);\r
77       if(sequencesHeight<40)\r
78               sequencesHeight = 40;\r
79     }\r
80     else\r
81     {\r
82       // taller\r
83       width = (int) (400f * initialScale);\r
84       sequencesHeight = 300;\r
85       if (width < 120)\r
86       {\r
87         width = 120;\r
88       }\r
89     }\r
90 \r
91     setSize(new Dimension(width, sequencesHeight + graphHeight));\r
92     addComponentListener(new ComponentAdapter()\r
93     {\r
94 \r
95       public void componentResized(ComponentEvent evt)\r
96       {\r
97         if (getSize().width != width ||\r
98             getSize().height != sequencesHeight + graphHeight)\r
99         {\r
100           updateOverviewImage();\r
101         }\r
102       }\r
103     });\r
104 \r
105     addMouseMotionListener(this);\r
106 \r
107     addMouseListener(this);\r
108 \r
109     updateOverviewImage();\r
110 \r
111   }\r
112 \r
113 \r
114   public void mouseEntered(MouseEvent evt)\r
115   {}\r
116   public void mouseExited(MouseEvent evt)\r
117   {}\r
118   public void mouseClicked(MouseEvent evt)\r
119   {}\r
120   public void mouseMoved(MouseEvent evt)\r
121   {}\r
122   public void mousePressed(MouseEvent evt)\r
123   {\r
124     boxX = evt.getX();\r
125     boxY = evt.getY();\r
126 \r
127     checkValid();\r
128     repaint();\r
129   }\r
130 \r
131   public void mouseReleased(MouseEvent evt)\r
132   {\r
133     boxX = evt.getX();\r
134     boxY = evt.getY();\r
135     checkValid();\r
136     ap.setScrollValues( (int) (boxX / scalew / av.getCharWidth()),\r
137                          (int) (boxY / scaleh / av.getCharHeight()));\r
138   }\r
139 \r
140   public void mouseDragged(MouseEvent evt)\r
141   {\r
142     boxX = evt.getX();\r
143     boxY = evt.getY();\r
144     checkValid();\r
145     ap.setScrollValues( (int) (boxX / scalew / av.getCharWidth()),\r
146                          (int) (boxY / scaleh / av.getCharHeight()));\r
147 \r
148     repaint();\r
149     ap.repaint();\r
150   }\r
151 \r
152   void checkValid()\r
153   {\r
154     if (boxY < 0)\r
155     {\r
156       boxY = 0;\r
157     }\r
158 \r
159     if (boxY > sequencesHeight - boxHeight)\r
160     {\r
161       boxY = sequencesHeight - boxHeight + 1;\r
162     }\r
163 \r
164     if (boxX < 0)\r
165     {\r
166       boxX = 0;\r
167     }\r
168 \r
169     if (boxX > width - boxWidth)\r
170     {\r
171       boxX = width - boxWidth;\r
172     }\r
173   }\r
174 \r
175   /**\r
176    * DOCUMENT ME!\r
177    */\r
178   public void updateOverviewImage()\r
179   {\r
180     if (resizing)\r
181     {\r
182         resizeAgain = true;\r
183         return;\r
184     }\r
185 \r
186     if (av.showSequenceFeatures)\r
187    {\r
188      fr.featuresDisplayed = ap.seqPanel.seqCanvas.getFeatureRenderer().\r
189          featuresDisplayed;\r
190      fr.featureGroups = ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups;\r
191      fr.featureColours = ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours;\r
192    }\r
193 \r
194 \r
195     resizing = true;\r
196 \r
197     if ( (getSize().width > 0) && (getSize().height > 0))\r
198     {\r
199       width = getSize().width;\r
200       sequencesHeight = getSize().height - graphHeight;\r
201     }\r
202     setSize(new Dimension(width, sequencesHeight + graphHeight));\r
203     setBoxPosition();\r
204 \r
205     Thread thread = new Thread(this);\r
206     thread.start();\r
207         repaint();\r
208   }\r
209 \r
210   // This is set true if the user resizes whilst\r
211   // the overview is being calculated\r
212     boolean resizeAgain = false;\r
213 \r
214   public void run()\r
215   {\r
216     miniMe = null;\r
217     int alwidth = av.alignment.getWidth();\r
218     int alheight = av.alignment.getHeight();\r
219 \r
220     if (getSize().width > 0 && getSize().height > 0)\r
221     {\r
222       width = getSize().width;\r
223       sequencesHeight = getSize().height - graphHeight;\r
224     }\r
225 \r
226     setSize(new Dimension(width, sequencesHeight + graphHeight));\r
227 \r
228     int fullsizeWidth = alwidth * av.getCharWidth();\r
229     int fullsizeHeight = alheight * av.getCharHeight();\r
230 \r
231     scalew = (float) width / (float) fullsizeWidth;\r
232     scaleh = (float) sequencesHeight / (float) fullsizeHeight;\r
233 \r
234     miniMe = nullFrame.createImage(width, sequencesHeight + graphHeight);\r
235 \r
236     Graphics mg = miniMe.getGraphics();\r
237     float sampleCol = (float) alwidth / (float) width;\r
238     float sampleRow = (float) alheight / (float) sequencesHeight;\r
239 \r
240     int lastcol=-1, lastrow=-1;\r
241     Color color = Color.yellow;\r
242     int row, col;\r
243     for (col = 0; col < width; col++)\r
244     {\r
245       for (row = 0; row < sequencesHeight; row++)\r
246       {\r
247         if((int)(col*sampleCol) == lastcol && (int)(row*sampleRow)==lastrow)\r
248         {\r
249           mg.drawLine(col, row, col, row);\r
250           continue;\r
251         }\r
252 \r
253         if (av.conservation != null)\r
254         {\r
255           mg.translate(col, sequencesHeight);\r
256           ap.annotationPanel.drawGraph(mg, av.conservation,\r
257                                        (int) (sampleCol) + 1,\r
258                                        graphHeight,\r
259                                        (int) (col * sampleCol),\r
260                                        (int) (col * sampleCol) + 1);\r
261           mg.translate( -col, -sequencesHeight);\r
262         }\r
263 \r
264         lastrow = (int)(row*sampleRow);\r
265         lastcol = (int)(col*sampleCol);\r
266 \r
267         if(av.alignment.getSequenceAt(lastrow).getLength()>lastcol)\r
268         {\r
269           color = sr.findSequenceColour(av.alignment.getSequenceAt(lastrow), lastcol);\r
270 \r
271 \r
272           if (av.showSequenceFeatures)\r
273             color = fr.findFeatureColour(color,\r
274                                         av.alignment.getSequenceAt(lastrow),\r
275                                          lastcol);\r
276         }\r
277 \r
278         mg.setColor(color);\r
279         mg.drawLine(col,row,col,row);\r
280 \r
281       }\r
282     }\r
283 \r
284 \r
285     System.gc();\r
286 \r
287     resizing = false;\r
288 \r
289     setBoxPosition();\r
290 \r
291     if(resizeAgain)\r
292     {\r
293       resizeAgain = false;\r
294       updateOverviewImage();\r
295     }\r
296   }\r
297 \r
298   public void setBoxPosition()\r
299   {\r
300     int fullsizeWidth = av.alignment.getWidth() * av.getCharWidth();\r
301     int fullsizeHeight = av.alignment.getHeight() * av.getCharHeight();\r
302 \r
303     scalew = (float) width / (float) fullsizeWidth;\r
304     scaleh = (float) sequencesHeight / (float) fullsizeHeight;\r
305 \r
306     boxX = (int) (av.getStartRes() * av.getCharWidth() * scalew);\r
307     boxY = (int) (av.getStartSeq() * av.getCharHeight() * scaleh);\r
308     boxWidth = (int) ( (av.getEndRes() - av.getStartRes() + 1) *\r
309                       av.getCharWidth() * scalew);\r
310     boxHeight = (int) (av.getEndSeq() * av.getCharHeight() * scaleh) - boxY;\r
311     repaint();\r
312   }\r
313 \r
314   public void update(Graphics g)\r
315   {\r
316     paint(g);\r
317   }\r
318 \r
319   public void paint(Graphics g)\r
320   {\r
321     if (miniMe != null)\r
322     {\r
323       g.drawImage(miniMe, 0, 0, this);\r
324     }\r
325     else\r
326     {\r
327       g.setColor(Color.white);\r
328       g.fillRect(0, 0, getSize().width, getSize().height);\r
329       g.setColor(Color.black);\r
330       g.setFont(new Font("Verdana", Font.BOLD, 15));\r
331       g.drawString("Recalculating", 5, sequencesHeight / 2);\r
332       g.drawString("Overview.....", 5, (sequencesHeight / 2) + 20);\r
333     }\r
334 \r
335     g.setColor(Color.red);\r
336     g.drawRect(boxX, boxY, boxWidth, boxHeight);\r
337     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);\r
338   }\r
339 \r
340 }\r