13177a0481011e256582543697bd7f1ea9f61766
[jalviewjs.git] / unused / appletgui / IdCanvas.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)\r
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors\r
4  * \r
5  * This file is part of Jalview.\r
6  * \r
7  * Jalview is free software: you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License \r
9  * as published by the Free Software Foundation, either version 3\r
10  * of the License, or (at your option) any later version.\r
11  *  \r
12  * Jalview is distributed in the hope that it will be useful, but \r
13  * WITHOUT ANY WARRANTY; without even the implied warranty \r
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
15  * PURPOSE.  See the GNU General Public License for more details.\r
16  * \r
17  * You should have received a copy of the GNU General Public License\r
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
19  * The Jalview Authors are detailed in the 'AUTHORS' file.\r
20  */\r
21 package jalview.appletgui;\r
22 \r
23 import jalview.datamodel.SequenceI;\r
24 \r
25 import java.awt.Color;\r
26 import java.awt.Font;\r
27 import java.awt.Graphics;\r
28 import java.awt.Image;\r
29 import javax.swing.JPanel;\r
30 import java.util.List;\r
31 \r
32 public class IdCanvas extends JPanel\r
33 {\r
34   protected AlignViewport av;\r
35 \r
36   protected boolean showScores = true;\r
37 \r
38   protected int maxIdLength = -1;\r
39 \r
40   protected String maxIdStr = null;\r
41 \r
42   Image image;\r
43 \r
44   Graphics gg;\r
45 \r
46   int imgHeight = 0;\r
47 \r
48   boolean fastPaint = false;\r
49 \r
50   List<SequenceI> searchResults;\r
51 \r
52   public IdCanvas(AlignViewport av)\r
53   {\r
54     setLayout(null);\r
55     this.av = av;\r
56     PaintRefresher.Register(this, av.getSequenceSetId());\r
57   }\r
58 \r
59   public void drawIdString(Graphics gg, boolean hiddenRows, SequenceI s,\r
60           int i, int starty,\r
61           int ypos)\r
62   {\r
63     int charHeight = av.getCharHeight();\r
64 \r
65     if (searchResults != null && searchResults.contains(s))\r
66     {\r
67       gg.setColor(Color.black);\r
68       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getSize().width,\r
69               charHeight);\r
70       gg.setColor(Color.white);\r
71     }\r
72     else if (av.getSelectionGroup() != null\r
73             && av.getSelectionGroup().getSequences(null).contains(s))\r
74     {\r
75       gg.setColor(Color.lightGray);\r
76       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getSize().width,\r
77               charHeight);\r
78       gg.setColor(Color.white);\r
79     }\r
80     else\r
81     {\r
82       gg.setColor(av.getSequenceColour(s));\r
83       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getSize().width,\r
84               charHeight);\r
85       gg.setColor(Color.black);\r
86     }\r
87 \r
88     gg.drawString(s.getDisplayId(av.getShowJVSuffix()), 0,\r
89             ((i - starty) * charHeight) + ypos + charHeight\r
90                     - (charHeight / 5));\r
91 \r
92     if (hiddenRows)\r
93     {\r
94       drawMarker(i, starty, ypos);\r
95     }\r
96 \r
97   }\r
98 \r
99   public void fastPaint(int vertical)\r
100   {\r
101     if (gg == null)\r
102     {\r
103       repaint();\r
104       return;\r
105     }\r
106 \r
107     gg.copyArea(0, 0, getSize().width, imgHeight, 0,\r
108             -vertical * av.getCharHeight());\r
109 \r
110     int ss = av.startSeq, es = av.endSeq, transY = 0;\r
111     if (vertical > 0) // scroll down\r
112     {\r
113       ss = es - vertical;\r
114       if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time\r
115       {\r
116         ss = av.startSeq;\r
117       }\r
118       else\r
119       {\r
120         transY = imgHeight - vertical * av.getCharHeight();\r
121       }\r
122     }\r
123     else if (vertical < 0)\r
124     {\r
125       es = ss - vertical;\r
126       if (es > av.endSeq)\r
127       {\r
128         es = av.endSeq;\r
129       }\r
130     }\r
131 \r
132     gg.translate(0, transY);\r
133 \r
134     drawIds(ss, es);\r
135 \r
136     gg.translate(0, -transY);\r
137 \r
138     fastPaint = true;\r
139     repaint();\r
140   }\r
141 \r
142   public void update(Graphics g)\r
143   {\r
144     paint(g);\r
145   }\r
146 \r
147   public void paint(Graphics g)\r
148   {\r
149     if (getSize().height < 0 || getSize().width < 0)\r
150     {\r
151       return;\r
152     }\r
153     if (fastPaint)\r
154     {\r
155       fastPaint = false;\r
156       g.drawImage(image, 0, 0, this);\r
157       return;\r
158     }\r
159 \r
160     imgHeight = getSize().height;\r
161     imgHeight -= imgHeight % av.getCharHeight();\r
162 \r
163     if (imgHeight < 1)\r
164     {\r
165       return;\r
166     }\r
167 \r
168     if (image == null || imgHeight != image.getHeight(this))\r
169     {\r
170       image = createImage(getSize().width, imgHeight);\r
171       gg = image.getGraphics();\r
172       gg.setFont(av.getFont());\r
173     }\r
174 \r
175     // Fill in the background\r
176     gg.setColor(Color.white);\r
177     Font italic = new Font(av.getFont().getName(), Font.ITALIC, av\r
178             .getFont().getSize());\r
179     gg.setFont(italic);\r
180 \r
181     gg.fillRect(0, 0, getSize().width, getSize().height);\r
182     drawIds(av.startSeq, av.endSeq);\r
183     g.drawImage(image, 0, 0, this);\r
184   }\r
185 \r
186   /**\r
187    * local copy of av.getCharHeight set at top of drawIds\r
188    */\r
189   private int avcharHeight;\r
190   void drawIds(int starty, int endy)\r
191   {\r
192     // hardwired italic IDs in applet currently\r
193     Font italic = new Font(av.getFont().getName(), Font.ITALIC, av\r
194             .getFont().getSize());\r
195     // temp variable for speed\r
196     avcharHeight = av.getCharHeight();\r
197 \r
198     gg.setFont(italic);\r
199 \r
200     Color currentColor = Color.white;\r
201     Color currentTextColor = Color.black;\r
202 \r
203     final boolean doHiddenCheck = av.isDisplayReferenceSeq()\r
204             || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows()\r
205             && av.getShowHiddenMarkers();\r
206 \r
207     if (av.getWrapAlignment())\r
208     {\r
209       int maxwidth = av.getAlignment().getWidth();\r
210       int alheight = av.getAlignment().getHeight();\r
211 \r
212       if (av.hasHiddenColumns())\r
213       {\r
214         maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;\r
215       }\r
216 \r
217       int annotationHeight = 0;\r
218       AnnotationLabels labels = null;\r
219 \r
220       if (av.isShowAnnotation())\r
221       {\r
222         AnnotationPanel ap = new AnnotationPanel(av);\r
223         annotationHeight = ap.adjustPanelHeight();\r
224         labels = new AnnotationLabels(av);\r
225       }\r
226       int hgap = avcharHeight;\r
227       if (av.getScaleAboveWrapped())\r
228       {\r
229         hgap += avcharHeight;\r
230       }\r
231 \r
232       int cHeight = alheight * avcharHeight + hgap + annotationHeight;\r
233 \r
234       int rowSize = av.getEndRes() - av.getStartRes();\r
235       // Draw the rest of the panels\r
236       for (int ypos = hgap, row = av.startRes; (ypos <= getSize().height)\r
237               && (row < maxwidth); ypos += cHeight, row += rowSize)\r
238       {\r
239         for (int i = starty; i < alheight; i++)\r
240         {\r
241 \r
242           SequenceI s = av.getAlignment().getSequenceAt(i);\r
243           gg.setFont(italic);\r
244           if (doHiddenCheck)\r
245           {\r
246             setHiddenFont(s);\r
247           }\r
248           drawIdString(gg, hiddenRows, s, i, 0, ypos);\r
249         }\r
250 \r
251         if (labels != null)\r
252         {\r
253           gg.translate(0, ypos + (alheight * avcharHeight));\r
254           labels.drawComponent(gg, getSize().width);\r
255           gg.translate(0, -ypos - (alheight * avcharHeight));\r
256         }\r
257 \r
258       }\r
259     }\r
260     else\r
261     {\r
262       // Now draw the id strings\r
263       SequenceI seq;\r
264       for (int i = starty; i < endy; i++)\r
265       {\r
266 \r
267         seq = av.getAlignment().getSequenceAt(i);\r
268         if (seq == null)\r
269         {\r
270           continue;\r
271         }\r
272         gg.setFont(italic);\r
273         // boolean isrep=false;\r
274         if (doHiddenCheck)\r
275         {\r
276           // isrep =\r
277           setHiddenFont(seq);\r
278         }\r
279 \r
280         // Selected sequence colours\r
281         if ((searchResults != null) && searchResults.contains(seq))\r
282         {\r
283           currentColor = Color.black;\r
284           currentTextColor = Color.white;\r
285         }\r
286         else if ((av.getSelectionGroup() != null)\r
287                 && av.getSelectionGroup().getSequences(null).contains(seq))\r
288         {\r
289           currentColor = Color.lightGray;\r
290           currentTextColor = Color.black;\r
291         }\r
292         else\r
293         {\r
294           currentColor = av.getSequenceColour(seq);\r
295           currentTextColor = Color.black;\r
296         }\r
297 \r
298         gg.setColor(currentColor);\r
299         // TODO: isrep could be used to highlight the representative in a\r
300         // different way\r
301         gg.fillRect(0, (i - starty) * avcharHeight, getSize().width,\r
302                 avcharHeight);\r
303         gg.setColor(currentTextColor);\r
304 \r
305         gg.drawString(seq.getDisplayId(av.getShowJVSuffix()), 0,\r
306                 (((i - starty) * avcharHeight) + avcharHeight)\r
307                         - (avcharHeight / 5));\r
308 \r
309         if (hiddenRows)\r
310         {\r
311           drawMarker(i, starty, 0);\r
312         }\r
313       }\r
314     }\r
315   }\r
316 \r
317   public void setHighlighted(List<SequenceI> list)\r
318   {\r
319     searchResults = list;\r
320     repaint();\r
321   }\r
322 \r
323   void drawMarker(int i, int starty, int yoffset)\r
324   {\r
325     SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;\r
326     // Use this method here instead of calling hiddenSeq adjust\r
327     // 3 times.\r
328     int hSize = hseqs.length;\r
329 \r
330     int hiddenIndex = i;\r
331     int lastIndex = i - 1;\r
332     int nextIndex = i + 1;\r
333 \r
334     for (int j = 0; j < hSize; j++)\r
335     {\r
336       if (hseqs[j] != null)\r
337       {\r
338         if (j - 1 < hiddenIndex)\r
339         {\r
340           hiddenIndex++;\r
341         }\r
342         if (j - 1 < lastIndex)\r
343         {\r
344           lastIndex++;\r
345         }\r
346         if (j - 1 < nextIndex)\r
347         {\r
348           nextIndex++;\r
349         }\r
350       }\r
351     }\r
352 \r
353     boolean below = (hiddenIndex > lastIndex + 1);\r
354     boolean above = (nextIndex > hiddenIndex + 1);\r
355 \r
356     gg.setColor(Color.blue);\r
357     if (below)\r
358     {\r
359       gg.fillPolygon(new int[]\r
360       { getSize().width - avcharHeight, getSize().width - avcharHeight,\r
361           getSize().width }, new int[]\r
362       { (i - starty) * avcharHeight + yoffset,\r
363           (i - starty) * avcharHeight + yoffset + avcharHeight / 4,\r
364           (i - starty) * avcharHeight + yoffset }, 3);\r
365     }\r
366     if (above)\r
367     {\r
368       gg.fillPolygon(new int[]\r
369       { getSize().width - avcharHeight, getSize().width - avcharHeight,\r
370           getSize().width }, new int[]\r
371       { (i - starty + 1) * avcharHeight + yoffset,\r
372           (i - starty + 1) * avcharHeight + yoffset - avcharHeight / 4,\r
373           (i - starty + 1) * avcharHeight + yoffset }, 3);\r
374 \r
375     }\r
376   }\r
377 \r
378   boolean setHiddenFont(SequenceI seq)\r
379   {\r
380     Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()\r
381             .getSize());\r
382 \r
383     if (av.isHiddenRepSequence(seq))\r
384     {\r
385       gg.setFont(bold);\r
386       return true;\r
387     }\r
388     return false;\r
389   }\r
390 }\r