Formatted source
[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);\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 \r
97       return;\r
98     }\r
99 \r
100     gg.copyArea(0, 0, getWidth(), imgHeight, 0, -vertical * av.charHeight);\r
101 \r
102     int ss = av.startSeq;\r
103     int es = av.endSeq;\r
104     int transY = 0;\r
105 \r
106     if (vertical > 0) // scroll down\r
107     {\r
108       ss = es - vertical;\r
109 \r
110       if (ss < av.startSeq)\r
111       { // ie scrolling too fast, more than a page at a time\r
112         ss = av.startSeq;\r
113       }\r
114       else\r
115       {\r
116         transY = imgHeight - (vertical * av.charHeight);\r
117       }\r
118     }\r
119     else if (vertical < 0)\r
120     {\r
121       es = ss - vertical;\r
122 \r
123       if (es > av.endSeq)\r
124       {\r
125         es = av.endSeq;\r
126       }\r
127     }\r
128 \r
129     gg.translate(0, transY);\r
130 \r
131     drawIds(ss, es);\r
132 \r
133     gg.translate(0, -transY);\r
134 \r
135     fastPaint = true;\r
136     repaint();\r
137   }\r
138 \r
139   public void paintComponent(Graphics g)\r
140   {\r
141     g.setColor(Color.white);\r
142     g.fillRect(0, 0, getWidth(), getHeight());\r
143 \r
144     if (fastPaint)\r
145     {\r
146       fastPaint = false;\r
147       g.drawImage(image, 0, 0, this);\r
148 \r
149       return;\r
150     }\r
151 \r
152     imgHeight = getHeight();\r
153     imgHeight -= (imgHeight % av.charHeight);\r
154 \r
155     if (imgHeight < 1)\r
156     {\r
157       return;\r
158     }\r
159 \r
160     image = new BufferedImage(getWidth(), imgHeight,\r
161                               BufferedImage.TYPE_INT_RGB);\r
162     gg = (Graphics2D) image.getGraphics();\r
163 \r
164     //Fill in the background\r
165     gg.setColor(Color.white);\r
166     gg.fillRect(0, 0, getWidth(), imgHeight);\r
167     gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
168                         RenderingHints.VALUE_ANTIALIAS_ON);\r
169 \r
170     Font italic = new Font(av.getFont().getName(), Font.ITALIC,\r
171                            av.getFont().getSize());\r
172     gg.setFont(italic);\r
173 \r
174     drawIds(av.getStartSeq(), av.endSeq);\r
175 \r
176     g.drawImage(image, 0, 0, this);\r
177   }\r
178 \r
179   void drawIds(int starty, int endy)\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