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