sequence may be null if hidden or deleted
[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(sequence==null)\r
290                 continue;\r
291 \r
292               if (av.hasHiddenRows)\r
293               {\r
294                 setHiddenFont(i);\r
295               }\r
296 \r
297                 // Selected sequence colours\r
298                 if ( (searchResults != null) &&\r
299                     searchResults.contains(sequence))\r
300                 {\r
301                   currentColor = Color.black;\r
302                   currentTextColor = Color.white;\r
303                 }\r
304                 else if ( (av.getSelectionGroup() != null) &&\r
305                          av.getSelectionGroup().getSequences(false).contains(\r
306                              sequence))\r
307                 {\r
308                   currentColor = Color.lightGray;\r
309                   currentTextColor = Color.black;\r
310                 }\r
311                 else\r
312                 {\r
313                   currentColor = sequence.getColor();\r
314                   currentTextColor = Color.black;\r
315                 }\r
316 \r
317                 gg.setColor(currentColor);\r
318 \r
319                 gg.fillRect(0, (i - starty) * av.charHeight, getWidth(),\r
320                             av.charHeight);\r
321 \r
322                 gg.setColor(currentTextColor);\r
323 \r
324                 String string = sequence.getDisplayId( av.getShowJVSuffix());\r
325 \r
326                 gg.drawString(string, 0,\r
327                     (((i - starty) * av.charHeight) + av.charHeight) -\r
328                     (av.charHeight / 5));\r
329 \r
330                if(av.hasHiddenRows && av.showHiddenMarkers)\r
331                  drawMarker(i, starty, 0);\r
332 \r
333             }\r
334 \r
335         }\r
336     }\r
337 \r
338     void drawMarker(int i, int starty, int yoffset)\r
339     {\r
340 \r
341       SequenceI [] hseqs = av.alignment.getHiddenSequences().hiddenSequences;\r
342       //Use this method here instead of calling hiddenSeq adjust\r
343       //3 times.\r
344       int hSize = hseqs.length;\r
345 \r
346       int hiddenIndex = i;\r
347       int lastIndex = i - 1;\r
348       int nextIndex = i + 1;\r
349 \r
350       for(int j=0; j<hSize; j++)\r
351       {\r
352         if (hseqs[j] != null)\r
353         {\r
354           if(j-1<hiddenIndex)\r
355             hiddenIndex++;\r
356           if(j-1<lastIndex)\r
357             lastIndex++;\r
358           if(j-1<nextIndex)\r
359             nextIndex++;\r
360         }\r
361       }\r
362 \r
363       boolean below = (hiddenIndex > lastIndex + 1);\r
364       boolean above = (nextIndex > hiddenIndex+1);\r
365 \r
366 \r
367         gg.setColor(Color.blue);\r
368         if(below)\r
369         {\r
370           gg.fillPolygon(new int[]\r
371                          {getWidth()- av.charHeight,\r
372                          getWidth()- av.charHeight,\r
373                          getWidth()},\r
374                          new int[]\r
375                          {\r
376                          (i - starty) * av.charHeight +yoffset,\r
377                          (i - starty) * av.charHeight +yoffset+ av.charHeight / 4,\r
378                          (i - starty) * av.charHeight+yoffset\r
379           }, 3);\r
380         }\r
381         if(above)\r
382         {\r
383           gg.fillPolygon(new int[]\r
384                         {getWidth()- av.charHeight,\r
385                         getWidth()- av.charHeight,\r
386                         getWidth() },\r
387                         new int[]\r
388                         {\r
389                         (i - starty+1) * av.charHeight +yoffset,\r
390                         (i - starty+1) * av.charHeight +yoffset- av.charHeight / 4,\r
391                         (i - starty+1) * av.charHeight +yoffset\r
392          }, 3);\r
393 \r
394         }\r
395     }\r
396 \r
397     void setHiddenFont(int i)\r
398     {\r
399       Font italic = new Font(av.getFont().getName(), Font.ITALIC,\r
400                              av.getFont().getSize());\r
401       Font bold = new Font(av.getFont().getName(), Font.BOLD,\r
402                            av.getFont().getSize());\r
403 \r
404 \r
405       if (av.alignment.getSequenceAt(i)!=null\r
406           && av.alignment.getSequenceAt(i).getHiddenSequences() != null)\r
407         gg.setFont(bold);\r
408       else\r
409         gg.setFont(italic);\r
410     }\r
411 \r
412     /**\r
413      * DOCUMENT ME!\r
414      *\r
415      * @param found DOCUMENT ME!\r
416      */\r
417     public void setHighlighted(java.util.Vector found)\r
418     {\r
419         searchResults = found;\r
420         repaint();\r
421     }\r
422 }\r