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