Formatting changes
[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.analysis.*;\r
25 import jalview.datamodel.*;\r
26 \r
27 public class IdCanvas\r
28     extends Panel\r
29 {\r
30   protected AlignViewport av;\r
31 \r
32   protected boolean showScores = true;\r
33 \r
34   protected int maxIdLength = -1;\r
35   protected String maxIdStr = null;\r
36   Image image;\r
37   Graphics gg;\r
38   int imgHeight = 0;\r
39   boolean fastPaint = false;\r
40 \r
41   java.util.Vector searchResults;\r
42 \r
43   public IdCanvas(AlignViewport av)\r
44   {\r
45     setLayout(null);\r
46     this.av = av;\r
47     PaintRefresher.Register(this, av.alignment);\r
48   }\r
49 \r
50   public void drawIdString(Graphics gg, SequenceI s, int i, int starty,\r
51                            int ypos)\r
52   {\r
53     int charHeight = av.getCharHeight();\r
54 \r
55     if (searchResults != null && searchResults.contains(s))\r
56     {\r
57       gg.setColor(Color.black);\r
58       gg.fillRect(0, ((i - starty) * charHeight) + ypos,\r
59                   getSize().width, charHeight);\r
60       gg.setColor(Color.white);\r
61     }\r
62     else if (av.getSelectionGroup() != null &&\r
63              av.getSelectionGroup().sequences.contains(s))\r
64     {\r
65       gg.setColor(Color.lightGray);\r
66       gg.fillRect(0, ((i - starty) * charHeight) + ypos,\r
67                   getSize().width, charHeight);\r
68       gg.setColor(Color.white);\r
69     }\r
70     else\r
71     {\r
72       gg.setColor(s.getColor());\r
73       gg.fillRect(0, ((i - starty) * charHeight) + ypos,\r
74                   getSize().width, charHeight);\r
75       gg.setColor(Color.black);\r
76     }\r
77 \r
78     String string = s.getName();\r
79     if (av.getShowFullId())\r
80     {\r
81       string = s.getDisplayId();\r
82     }\r
83 \r
84     gg.drawString(string, 0,\r
85                   ((i - starty) * charHeight) + ypos +\r
86                   charHeight - (charHeight / 5));\r
87 \r
88   }\r
89 \r
90   public void fastPaint(int vertical)\r
91   {\r
92     if (gg == null)\r
93     {\r
94       repaint();\r
95       return;\r
96     }\r
97 \r
98     gg.copyArea(0, 0, getSize().width, imgHeight, 0, -vertical * av.charHeight);\r
99 \r
100     int ss = av.startSeq, es = av.endSeq, transY = 0;\r
101     if (vertical > 0) // scroll down\r
102     {\r
103       ss = es - vertical;\r
104       if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time\r
105       {\r
106         ss = av.startSeq;\r
107       }\r
108       else\r
109       {\r
110         transY = imgHeight - vertical * av.charHeight;\r
111       }\r
112     }\r
113     else if (vertical < 0)\r
114     {\r
115       es = ss - vertical;\r
116       if (es > av.endSeq)\r
117       {\r
118         es = av.endSeq;\r
119       }\r
120     }\r
121 \r
122     gg.translate(0, transY);\r
123 \r
124     drawIds(ss, es);\r
125 \r
126     gg.translate(0, -transY);\r
127 \r
128     fastPaint = true;\r
129     repaint();\r
130   }\r
131 \r
132   public void update(Graphics g)\r
133   {\r
134     paint(g);\r
135   }\r
136 \r
137   public void paint(Graphics g)\r
138   {\r
139     if (getSize().height < 0 || getSize().width < 0)\r
140     {\r
141       return;\r
142     }\r
143     if (fastPaint)\r
144     {\r
145       fastPaint = false;\r
146       g.drawImage(image, 0, 0, this);\r
147       return;\r
148     }\r
149 \r
150     imgHeight = getSize().height;\r
151     imgHeight -= imgHeight % av.charHeight;\r
152 \r
153     if (imgHeight < 1)\r
154     {\r
155       return;\r
156     }\r
157 \r
158     if (image == null || imgHeight != image.getHeight(this))\r
159     {\r
160       image = createImage(getSize().width, imgHeight);\r
161       gg = image.getGraphics();\r
162       gg.setFont(av.getFont());\r
163     }\r
164 \r
165     //Fill in the background\r
166     gg.setColor(Color.white);\r
167     Font italic = new Font(av.getFont().getName(), Font.ITALIC,\r
168                            av.getFont().getSize());\r
169     gg.setFont(italic);\r
170 \r
171     gg.fillRect(0, 0, getSize().width, getSize().height);\r
172     drawIds(av.getStartSeq(), av.endSeq);\r
173     g.drawImage(image, 0, 0, this);\r
174   }\r
175 \r
176   void drawIds(int starty, int endy)\r
177   {\r
178     Color currentColor = Color.white;\r
179     Color currentTextColor = Color.black;\r
180 \r
181     if (av.getWrapAlignment())\r
182     {\r
183 \r
184       int rowSize = av.getEndRes() - av.getStartRes();\r
185       // Draw the rest of the panels\r
186 \r
187       for (int ypos = 2 * av.charHeight, row = av.startRes;\r
188            ypos <= getSize().height && row < av.alignment.getWidth();\r
189            ypos += av.chunkHeight, row += rowSize)\r
190       {\r
191         for (int i = starty; i < av.alignment.getHeight(); i++)\r
192         {\r
193           SequenceI s = av.alignment.getSequenceAt(i);\r
194           drawIdString(gg, s, i, 0, ypos);\r
195         }\r
196       }\r
197 \r
198     }\r
199     else\r
200     {\r
201 \r
202       //Now draw the id strings\r
203       for (int i = starty; i < endy; i++)\r
204       {\r
205         // Selected sequence colours\r
206 \r
207         if (searchResults != null &&\r
208             searchResults.contains(av.alignment.getSequenceAt(i)))\r
209         {\r
210           gg.setColor(Color.black);\r
211           currentColor = Color.black;\r
212           currentTextColor = Color.white;\r
213         }\r
214         else if (av.getSelectionGroup() != null\r
215                  &&\r
216                  av.getSelectionGroup().sequences.contains(av.alignment.\r
217             getSequenceAt(i)))\r
218         {\r
219           currentColor = Color.lightGray;\r
220           currentTextColor = Color.black;\r
221         }\r
222         else\r
223         {\r
224           currentColor = av.alignment.getSequenceAt(i).getColor();\r
225           currentTextColor = Color.black;\r
226         }\r
227 \r
228         gg.setColor(currentColor);\r
229 \r
230         gg.fillRect(0,\r
231                     ((i - starty) * av.charHeight),\r
232                     getSize().width,\r
233                     av.charHeight);\r
234 \r
235         gg.setColor(currentTextColor);\r
236         String string = av.alignment.getSequenceAt(i).getName();\r
237         if (av.getShowFullId())\r
238         {\r
239           string = av.alignment.getSequenceAt(i).getDisplayId();\r
240         }\r
241         gg.drawString(string, 0,\r
242                       ((i - starty) * av.charHeight) +\r
243                       av.charHeight - (av.charHeight / 5));\r
244       }\r
245 \r
246       // add a border\r
247       gg.setColor(Color.white);\r
248       gg.fillRect(getSize().width - 4, 0, 4, getSize().height);\r
249     }\r
250 \r
251   }\r
252 \r
253   public void setHighlighted(java.util.Vector found)\r
254   {\r
255     searchResults = found;\r
256     repaint();\r
257   }\r
258 }\r