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