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