Applet has hidden regions
[jalview.git] / src / jalview / appletgui / IdCanvas.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 \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.alignment);\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(false).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(s.getColor());\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       for (int i = starty; i < endy; i++)\r
244       {\r
245         if (av.hasHiddenRows)\r
246         {\r
247           setHiddenFont(i);\r
248         }\r
249 \r
250         // Selected sequence colours\r
251         if ( (searchResults != null) &&\r
252             searchResults.contains(av.alignment.getSequenceAt(i)))\r
253         {\r
254           currentColor = Color.black;\r
255           currentTextColor = Color.white;\r
256         }\r
257         else if ( (av.getSelectionGroup() != null) &&\r
258                  av.getSelectionGroup().getSequences(false).contains(\r
259                      av.alignment.getSequenceAt(i)))\r
260         {\r
261           currentColor = Color.lightGray;\r
262           currentTextColor = Color.black;\r
263         }\r
264         else\r
265         {\r
266           currentColor = av.alignment.getSequenceAt(i).getColor();\r
267           currentTextColor = Color.black;\r
268         }\r
269 \r
270         gg.setColor(currentColor);\r
271 \r
272         gg.fillRect(0, (i - starty) * av.charHeight, getSize().width,\r
273                     av.charHeight);\r
274 \r
275         gg.setColor(currentTextColor);\r
276 \r
277         String string = av.alignment.getSequenceAt(i).getDisplayId(av.\r
278             getShowJVSuffix());\r
279 \r
280         gg.drawString(string, 0,\r
281                       ( ( (i - starty) * av.charHeight) + av.charHeight) -\r
282                       (av.charHeight / 5));\r
283 \r
284         if (av.hasHiddenRows && av.showHiddenMarkers)\r
285           drawMarker(i, starty, 0);\r
286       }\r
287     }\r
288   }\r
289 \r
290   public void setHighlighted(java.util.Vector found)\r
291   {\r
292     searchResults = found;\r
293     repaint();\r
294   }\r
295 \r
296   void drawMarker(int i, int starty, int yoffset)\r
297   {\r
298     int hiddenIndex = av.adjustForHiddenSeqs(i);\r
299     int lastIndex = av.adjustForHiddenSeqs(i - 1);\r
300     int nextIndex = av.adjustForHiddenSeqs(i + 1);\r
301 \r
302     boolean below = (hiddenIndex > lastIndex + 1);\r
303     boolean above = (nextIndex>hiddenIndex+1);\r
304 \r
305       gg.setColor(Color.blue);\r
306       if(below)\r
307       {\r
308         gg.fillPolygon(new int[]\r
309                        {getSize().width- av.charHeight,\r
310                        getSize().width- av.charHeight,\r
311                        getSize().width},\r
312                        new int[]\r
313                        {\r
314                        (i - starty) * av.charHeight +yoffset,\r
315                        (i - starty) * av.charHeight +yoffset+ av.charHeight / 4,\r
316                        (i - starty) * av.charHeight+yoffset\r
317         }, 3);\r
318       }\r
319       if(above)\r
320       {\r
321         gg.fillPolygon(new int[]\r
322                       {getSize().width- av.charHeight,\r
323                       getSize().width - av.charHeight,\r
324                       getSize().width },\r
325                       new int[]\r
326                       {\r
327                       (i - starty+1) * av.charHeight +yoffset,\r
328                       (i - starty+1) * av.charHeight +yoffset- av.charHeight / 4,\r
329                       (i - starty+1) * av.charHeight +yoffset\r
330        }, 3);\r
331 \r
332       }\r
333   }\r
334 \r
335   void setHiddenFont(int i)\r
336   {\r
337   /*  System.out.println(i+" "+av.alignment.getHeight());\r
338     if (av.alignment.getSequenceAt(i).getHiddenSequences() != null)\r
339       gg.setFont(new Font(av.getFont().getName(), Font.BOLD,\r
340                           av.getFont().getSize()));\r
341     else\r
342       gg.setFont(new Font(av.getFont().getName(), Font.ITALIC,\r
343                           av.getFont().getSize()));*/\r
344   }\r
345 }\r