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