24bf616708a6981d05b47310e7893fa8057ce659
[jalview.git] / src / jalview / gui / IdCanvas.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 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 package jalview.gui;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import java.awt.*;\r
24 import java.awt.image.*;\r
25 \r
26 import javax.swing.*;\r
27 \r
28 \r
29 /**\r
30  * DOCUMENT ME!\r
31  *\r
32  * @author $author$\r
33  * @version $Revision$\r
34  */\r
35 public class IdCanvas extends JPanel\r
36 {\r
37     protected AlignViewport av;\r
38     protected boolean showScores = true;\r
39     protected int maxIdLength = -1;\r
40     protected String maxIdStr = null;\r
41     BufferedImage image;\r
42     Graphics2D gg;\r
43     int imgHeight = 0;\r
44     boolean fastPaint = false;\r
45     java.util.Vector searchResults;\r
46 \r
47     /**\r
48      * Creates a new IdCanvas object.\r
49      *\r
50      * @param av DOCUMENT ME!\r
51      */\r
52     public IdCanvas(AlignViewport av)\r
53     {\r
54         setLayout(new BorderLayout());\r
55         this.av = av;\r
56         PaintRefresher.Register(this, av.getSequenceSetId());\r
57     }\r
58 \r
59     /**\r
60      * DOCUMENT ME!\r
61      *\r
62      * @param gg DOCUMENT ME!\r
63      * @param s DOCUMENT ME!\r
64      * @param i DOCUMENT ME!\r
65      * @param starty DOCUMENT ME!\r
66      * @param ypos DOCUMENT ME!\r
67      */\r
68     public void drawIdString(Graphics2D gg, SequenceI s, int i, int starty, int ypos)\r
69     {\r
70         int charHeight = av.charHeight;\r
71 \r
72         if ((searchResults != null) && searchResults.contains(s))\r
73         {\r
74             gg.setColor(Color.black);\r
75             gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),\r
76                 charHeight);\r
77             gg.setColor(Color.white);\r
78         }\r
79         else if ((av.getSelectionGroup() != null) &&\r
80                 av.getSelectionGroup().getSequences(false).contains(s))\r
81         {\r
82             gg.setColor(Color.lightGray);\r
83             gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),\r
84                 charHeight);\r
85             gg.setColor(Color.white);\r
86         }\r
87         else\r
88         {\r
89             gg.setColor(s.getColor());\r
90             gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),\r
91                 charHeight);\r
92             gg.setColor(Color.black);\r
93         }\r
94 \r
95 \r
96         gg.drawString( s.getDisplayId(av.getShowJVSuffix()),\r
97                       0, (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));\r
98 \r
99         if (av.hasHiddenRows && av.showHiddenMarkers)\r
100           drawMarker(i, starty, ypos);\r
101 \r
102 \r
103     }\r
104 \r
105     /**\r
106      * DOCUMENT ME!\r
107      *\r
108      * @param vertical DOCUMENT ME!\r
109      */\r
110     public void fastPaint(int vertical)\r
111     {\r
112         if (gg == null)\r
113         {\r
114             repaint();\r
115 \r
116             return;\r
117         }\r
118 \r
119         gg.copyArea(0, 0, getWidth(), imgHeight, 0, -vertical * av.charHeight);\r
120 \r
121         int ss = av.startSeq;\r
122         int es = av.endSeq;\r
123         int transY = 0;\r
124 \r
125         if (vertical > 0) // scroll down\r
126         {\r
127             ss = es - vertical;\r
128 \r
129             if (ss < av.startSeq)\r
130             { // ie scrolling too fast, more than a page at a time\r
131                 ss = av.startSeq;\r
132             }\r
133             else\r
134             {\r
135                 transY = imgHeight - (vertical * av.charHeight);\r
136             }\r
137         }\r
138         else if (vertical < 0)\r
139         {\r
140             es = ss - vertical;\r
141 \r
142             if (es > av.endSeq)\r
143             {\r
144                 es = av.endSeq;\r
145             }\r
146         }\r
147 \r
148         gg.translate(0, transY);\r
149 \r
150         drawIds(ss, es);\r
151 \r
152         gg.translate(0, -transY);\r
153 \r
154         fastPaint = true;\r
155         repaint();\r
156     }\r
157 \r
158     /**\r
159      * DOCUMENT ME!\r
160      *\r
161      * @param g DOCUMENT ME!\r
162      */\r
163     public void paintComponent(Graphics g)\r
164     {\r
165         g.setColor(Color.white);\r
166         g.fillRect(0, 0, getWidth(), getHeight());\r
167 \r
168         if (fastPaint)\r
169         {\r
170             fastPaint = false;\r
171             g.drawImage(image, 0, 0, this);\r
172 \r
173             return;\r
174         }\r
175 \r
176         int oldHeight = imgHeight;\r
177 \r
178         imgHeight = getHeight();\r
179         imgHeight -= (imgHeight % av.charHeight);\r
180 \r
181         if (imgHeight < 1)\r
182         {\r
183             return;\r
184         }\r
185 \r
186         if(oldHeight!=imgHeight || image.getWidth(this)!=getWidth())\r
187         {\r
188           image = new BufferedImage(getWidth(), imgHeight,\r
189                                     BufferedImage.TYPE_INT_RGB);\r
190         }\r
191 \r
192         gg = (Graphics2D) image.getGraphics();\r
193         //Fill in the background\r
194         gg.setColor(Color.white);\r
195         gg.fillRect(0, 0, getWidth(), imgHeight);\r
196 \r
197         drawIds(av.getStartSeq(), av.endSeq);\r
198 \r
199         g.drawImage(image, 0, 0, this);\r
200     }\r
201 \r
202     /**\r
203      * DOCUMENT ME!\r
204      *\r
205      * @param starty DOCUMENT ME!\r
206      * @param endy DOCUMENT ME!\r
207      */\r
208     void drawIds(int starty, int endy)\r
209     {\r
210       Font italic = new Font(av.getFont().getName(), Font.ITALIC,\r
211                              av.getFont().getSize());\r
212 \r
213       gg.setFont(italic);\r
214 \r
215       if (av.antiAlias)\r
216         gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
217                             RenderingHints.VALUE_ANTIALIAS_ON);\r
218 \r
219         Color currentColor = Color.white;\r
220         Color currentTextColor = Color.black;\r
221 \r
222         if (av.getWrapAlignment())\r
223         {\r
224           int maxwidth = av.alignment.getWidth();\r
225           int alheight = av.alignment.getHeight();\r
226 \r
227           if (av.hasHiddenColumns)\r
228             maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;\r
229 \r
230           int annotationHeight = 0;\r
231           AnnotationLabels labels = null;\r
232 \r
233           if(av.showAnnotation)\r
234           {\r
235             AnnotationPanel ap = new AnnotationPanel(av);\r
236             annotationHeight = ap.adjustPanelHeight();\r
237             labels = new AnnotationLabels(av);\r
238           }\r
239 \r
240           int hgap = av.charHeight;\r
241           if (av.scaleAboveWrapped)\r
242             hgap += av.charHeight;\r
243 \r
244           int cHeight = alheight * av.charHeight\r
245               + hgap\r
246               + annotationHeight;\r
247 \r
248           int rowSize = av.getEndRes() - av.getStartRes();\r
249 \r
250 \r
251             // Draw the rest of the panels\r
252             for (int ypos = hgap, row = av.startRes;\r
253                     (ypos <= getHeight()) && (row < maxwidth);\r
254                     ypos += cHeight, row += rowSize)\r
255             {\r
256               for (int i = starty; i < alheight; i++)\r
257               {\r
258                 if (av.hasHiddenRows)\r
259                 {\r
260                   setHiddenFont(i);\r
261                 }\r
262                 else\r
263                   gg.setFont(italic);\r
264 \r
265                 SequenceI s = av.alignment.getSequenceAt(i);\r
266                 drawIdString(gg, s, i, 0, ypos);\r
267               }\r
268 \r
269                 if(labels!=null)\r
270                 {\r
271                   gg.translate(0, ypos+(alheight * av.charHeight));\r
272                   labels.drawComponent(gg, getWidth());\r
273                   gg.translate(0, -ypos-(alheight * av.charHeight));\r
274                 }\r
275 \r
276 \r
277             }\r
278         }\r
279         else\r
280         {\r
281           //Now draw the id strings\r
282 \r
283             SequenceI sequence;\r
284             //Now draw the id strings\r
285             for (int i = starty; i < endy; i++)\r
286             {\r
287               sequence = av.alignment.getSequenceAt(i);\r
288 \r
289               if (av.hasHiddenRows)\r
290               {\r
291                 setHiddenFont(i);\r
292               }\r
293 \r
294                 // Selected sequence colours\r
295                 if ( (searchResults != null) &&\r
296                     searchResults.contains(sequence))\r
297                 {\r
298                   currentColor = Color.black;\r
299                   currentTextColor = Color.white;\r
300                 }\r
301                 else if ( (av.getSelectionGroup() != null) &&\r
302                          av.getSelectionGroup().getSequences(false).contains(\r
303                              sequence))\r
304                 {\r
305                   currentColor = Color.lightGray;\r
306                   currentTextColor = Color.black;\r
307                 }\r
308                 else\r
309                 {\r
310                   currentColor = sequence.getColor();\r
311                   currentTextColor = Color.black;\r
312                 }\r
313 \r
314                 gg.setColor(currentColor);\r
315 \r
316                 gg.fillRect(0, (i - starty) * av.charHeight, getWidth(),\r
317                             av.charHeight);\r
318 \r
319                 gg.setColor(currentTextColor);\r
320 \r
321                 String string = sequence.getDisplayId( av.getShowJVSuffix());\r
322 \r
323                 gg.drawString(string, 0,\r
324                     (((i - starty) * av.charHeight) + av.charHeight) -\r
325                     (av.charHeight / 5));\r
326 \r
327                if(av.hasHiddenRows && av.showHiddenMarkers)\r
328                  drawMarker(i, starty, 0);\r
329 \r
330             }\r
331 \r
332         }\r
333     }\r
334 \r
335     void drawMarker(int i, int starty, int yoffset)\r
336     {\r
337 \r
338       SequenceI [] hseqs = av.alignment.getHiddenSequences().hiddenSequences;\r
339       //Use this method here instead of calling hiddenSeq adjust\r
340       //3 times.\r
341       int hSize = hseqs.length;\r
342 \r
343       int hiddenIndex = i;\r
344       int lastIndex = i - 1;\r
345       int nextIndex = i + 1;\r
346 \r
347       for(int j=0; j<hSize; j++)\r
348       {\r
349         if (hseqs[j] != null)\r
350         {\r
351           if(j-1<hiddenIndex)\r
352             hiddenIndex++;\r
353           if(j-1<lastIndex)\r
354             lastIndex++;\r
355           if(j-1<nextIndex)\r
356             nextIndex++;\r
357         }\r
358       }\r
359 \r
360       boolean below = (hiddenIndex > lastIndex + 1);\r
361       boolean above = (nextIndex > hiddenIndex+1);\r
362 \r
363 \r
364         gg.setColor(Color.blue);\r
365         if(below)\r
366         {\r
367           gg.fillPolygon(new int[]\r
368                          {getWidth()- av.charHeight,\r
369                          getWidth()- av.charHeight,\r
370                          getWidth()},\r
371                          new int[]\r
372                          {\r
373                          (i - starty) * av.charHeight +yoffset,\r
374                          (i - starty) * av.charHeight +yoffset+ av.charHeight / 4,\r
375                          (i - starty) * av.charHeight+yoffset\r
376           }, 3);\r
377         }\r
378         if(above)\r
379         {\r
380           gg.fillPolygon(new int[]\r
381                         {getWidth()- av.charHeight,\r
382                         getWidth()- av.charHeight,\r
383                         getWidth() },\r
384                         new int[]\r
385                         {\r
386                         (i - starty+1) * av.charHeight +yoffset,\r
387                         (i - starty+1) * av.charHeight +yoffset- av.charHeight / 4,\r
388                         (i - starty+1) * av.charHeight +yoffset\r
389          }, 3);\r
390 \r
391         }\r
392     }\r
393 \r
394     void setHiddenFont(int i)\r
395     {\r
396       Font italic = new Font(av.getFont().getName(), Font.ITALIC,\r
397                              av.getFont().getSize());\r
398       Font bold = new Font(av.getFont().getName(), Font.BOLD,\r
399                            av.getFont().getSize());\r
400 \r
401 \r
402       if (av.alignment.getSequenceAt(i)!=null\r
403           && av.alignment.getSequenceAt(i).getHiddenSequences() != null)\r
404         gg.setFont(bold);\r
405       else\r
406         gg.setFont(italic);\r
407     }\r
408 \r
409     /**\r
410      * DOCUMENT ME!\r
411      *\r
412      * @param found DOCUMENT ME!\r
413      */\r
414     public void setHighlighted(java.util.Vector found)\r
415     {\r
416         searchResults = found;\r
417         repaint();\r
418     }\r
419 }\r