7bcc9870c49f7b72443df1b75e3f1e91f5b7c32d
[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().sequences.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     String string = s.getDisplayId(av.getShowDBPrefix(), av.getShowJVSuffix());\r
78 \r
79     gg.drawString(string, 0,\r
80                   ((i - starty) * charHeight) + ypos +\r
81                   charHeight - (charHeight / 5));\r
82 \r
83   }\r
84 \r
85   public void fastPaint(int vertical)\r
86   {\r
87     if (gg == null)\r
88     {\r
89       repaint();\r
90       return;\r
91     }\r
92 \r
93     gg.copyArea(0, 0, getSize().width, imgHeight, 0, -vertical * av.charHeight);\r
94 \r
95     int ss = av.startSeq, es = av.endSeq, transY = 0;\r
96     if (vertical > 0) // scroll down\r
97     {\r
98       ss = es - vertical;\r
99       if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time\r
100       {\r
101         ss = av.startSeq;\r
102       }\r
103       else\r
104       {\r
105         transY = imgHeight - vertical * av.charHeight;\r
106       }\r
107     }\r
108     else if (vertical < 0)\r
109     {\r
110       es = ss - vertical;\r
111       if (es > av.endSeq)\r
112       {\r
113         es = av.endSeq;\r
114       }\r
115     }\r
116 \r
117     gg.translate(0, transY);\r
118 \r
119     drawIds(ss, es);\r
120 \r
121     gg.translate(0, -transY);\r
122 \r
123     fastPaint = true;\r
124     repaint();\r
125   }\r
126 \r
127   public void update(Graphics g)\r
128   {\r
129     paint(g);\r
130   }\r
131 \r
132   public void paint(Graphics g)\r
133   {\r
134     if (getSize().height < 0 || getSize().width < 0)\r
135     {\r
136       return;\r
137     }\r
138     if (fastPaint)\r
139     {\r
140       fastPaint = false;\r
141       g.drawImage(image, 0, 0, this);\r
142       return;\r
143     }\r
144 \r
145     imgHeight = getSize().height;\r
146     imgHeight -= imgHeight % av.charHeight;\r
147 \r
148     if (imgHeight < 1)\r
149     {\r
150       return;\r
151     }\r
152 \r
153     if (image == null || imgHeight != image.getHeight(this))\r
154     {\r
155       image = createImage(getSize().width, imgHeight);\r
156       gg = image.getGraphics();\r
157       gg.setFont(av.getFont());\r
158     }\r
159 \r
160     //Fill in the background\r
161     gg.setColor(Color.white);\r
162     Font italic = new Font(av.getFont().getName(), Font.ITALIC,\r
163                            av.getFont().getSize());\r
164     gg.setFont(italic);\r
165 \r
166     gg.fillRect(0, 0, getSize().width, getSize().height);\r
167     drawIds(av.getStartSeq(), av.endSeq);\r
168     g.drawImage(image, 0, 0, this);\r
169   }\r
170 \r
171   void drawIds(int starty, int endy)\r
172   {\r
173     Color currentColor = Color.white;\r
174     Color currentTextColor = Color.black;\r
175     Font italic = new Font(av.getFont().getName(), Font.ITALIC,\r
176                              av.getFont().getSize());\r
177 \r
178     if (av.getWrapAlignment())\r
179     {\r
180       int annotationHeight = 0;\r
181       AnnotationLabels labels = null;\r
182 \r
183       if(av.showAnnotation)\r
184       {\r
185         AnnotationPanel ap = new AnnotationPanel(av);\r
186         annotationHeight = ap.adjustPanelHeight();\r
187         labels = new AnnotationLabels(av);\r
188       }\r
189 \r
190       int hgap = av.charHeight;\r
191       if (av.scaleAboveWrapped)\r
192         hgap += av.charHeight;\r
193 \r
194       int cHeight = av.getAlignment().getHeight() * av.charHeight\r
195           + hgap\r
196           + annotationHeight;\r
197 \r
198         int rowSize = av.getEndRes() - av.getStartRes();\r
199 \r
200         // Draw the rest of the panels\r
201         for (int ypos = hgap, row = av.startRes;\r
202                 (ypos <= getSize().height) && (row < av.alignment.getWidth());\r
203                 ypos += cHeight, row += rowSize)\r
204         {\r
205             for (int i = starty; i < av.alignment.getHeight(); i++)\r
206             {\r
207                 SequenceI s = av.alignment.getSequenceAt(i);\r
208                 drawIdString(gg, s, i, 0, ypos);\r
209             }\r
210 \r
211             if(labels!=null)\r
212             {\r
213               gg.setFont(av.getFont());\r
214               gg.translate(0, ypos+(av.getAlignment().getHeight() * av.charHeight));\r
215               labels.drawComponent(gg, getSize().width);\r
216               gg.translate(0, -ypos-(av.getAlignment().getHeight() * av.charHeight));\r
217               gg.setFont(italic);\r
218             }\r
219         }\r
220 \r
221     }\r
222     else\r
223     {\r
224 \r
225       //Now draw the id strings\r
226       for (int i = starty; i < endy; i++)\r
227       {\r
228         // Selected sequence colours\r
229 \r
230         if (searchResults != null &&\r
231             searchResults.contains(av.alignment.getSequenceAt(i)))\r
232         {\r
233           gg.setColor(Color.black);\r
234           currentColor = Color.black;\r
235           currentTextColor = Color.white;\r
236         }\r
237         else if (av.getSelectionGroup() != null\r
238                  &&\r
239                  av.getSelectionGroup().sequences.contains(av.alignment.\r
240             getSequenceAt(i)))\r
241         {\r
242           currentColor = Color.lightGray;\r
243           currentTextColor = Color.black;\r
244         }\r
245         else\r
246         {\r
247           currentColor = av.alignment.getSequenceAt(i).getColor();\r
248           currentTextColor = Color.black;\r
249         }\r
250 \r
251         gg.setColor(currentColor);\r
252 \r
253         gg.fillRect(0,\r
254                     ((i - starty) * av.charHeight),\r
255                     getSize().width,\r
256                     av.charHeight);\r
257 \r
258         gg.setColor(currentTextColor);\r
259         String string = av.alignment.getSequenceAt(i)\r
260             .getDisplayId(av.getShowDBPrefix(), av.getShowJVSuffix());\r
261 \r
262         gg.drawString(string, 0,\r
263                       ((i - starty) * av.charHeight) +\r
264                       av.charHeight - (av.charHeight / 5));\r
265       }\r
266 \r
267       // add a border\r
268       gg.setColor(Color.white);\r
269       gg.fillRect(getSize().width - 4, 0, 4, getSize().height);\r
270     }\r
271 \r
272   }\r
273 \r
274   public void setHighlighted(java.util.Vector found)\r
275   {\r
276     searchResults = found;\r
277     repaint();\r
278   }\r
279 }\r