2c987546870243fee869ff5d431000d171a3851a
[jalview.git] / src / jalview / gui / SequenceRenderer.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.gui;\r
21 \r
22 import jalview.datamodel.*;\r
23 import jalview.schemes.*;\r
24 import java.awt.*;\r
25 \r
26 public class SequenceRenderer\r
27 {\r
28   AlignViewport av;\r
29   FontMetrics fm;\r
30   boolean renderGaps = true;\r
31   SequenceGroup currentSequenceGroup = null;\r
32   SequenceGroup [] allGroups = null;\r
33   Color resBoxColour;\r
34   Graphics graphics;\r
35 \r
36   public SequenceRenderer(AlignViewport av)\r
37   {\r
38     this.av = av;\r
39   }\r
40 \r
41 \r
42   public void renderGaps(boolean b)\r
43   {\r
44     renderGaps = b;\r
45   }\r
46 \r
47   public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
48   {\r
49     getBoxColour(cs, seq, i);\r
50     return resBoxColour;\r
51   }\r
52 \r
53   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
54   {\r
55    if (cs != null)\r
56        resBoxColour = cs.findColour(seq.getSequence(i, i + 1), i);\r
57    else\r
58        resBoxColour = Color.white;\r
59   }\r
60 \r
61   public void drawSequence(Graphics g,SequenceI seq,SequenceGroup [] sg, int start, int end, int x1, int y1, int width, int height)\r
62   {\r
63     allGroups = sg;\r
64 \r
65     graphics = g;\r
66 \r
67     drawBoxes(seq, start, end, x1, y1, (int) width, height);\r
68 \r
69     fm = g.getFontMetrics();\r
70     drawText(seq,start,end,x1,y1,(int)width,height);\r
71 \r
72   }\r
73 \r
74   public void drawBoxes(SequenceI seq,int start, int end, int x1, int y1, int width, int height) {\r
75     int i      = start;\r
76     int length = seq.getLength();\r
77 \r
78     int curStart = -1;\r
79     int curWidth = width;\r
80 \r
81     Color tempColour = null;\r
82     while (i <= end && i < length)\r
83     {\r
84       if(inCurrentSequenceGroup(i))\r
85       {\r
86         if( currentSequenceGroup.getDisplayBoxes())\r
87              getBoxColour(currentSequenceGroup.cs, seq, i);\r
88         else\r
89              resBoxColour = Color.white;\r
90       }\r
91       else if(av.getShowBoxes())\r
92             getBoxColour(av.getGlobalColourScheme(), seq, i);\r
93       else\r
94         resBoxColour = Color.white;\r
95 \r
96 \r
97       if (resBoxColour != tempColour)\r
98       {\r
99         if(tempColour!=null)\r
100           graphics.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
101         graphics.setColor(resBoxColour);\r
102 \r
103         curStart = i;\r
104         curWidth = width;\r
105         tempColour = resBoxColour;\r
106 \r
107       }\r
108       else\r
109         curWidth += width;\r
110 \r
111       i++;\r
112     }\r
113 \r
114 \r
115      graphics.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
116   }\r
117 \r
118   public void drawText(SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
119   {\r
120     int pady = height/5;\r
121     int charOffset=0;\r
122     char s;\r
123     // Need to find the sequence position here.\r
124 \r
125 \r
126     String sequence  = seq.getSequence();\r
127     for (int i = start; i <= end; i++)\r
128     {\r
129         graphics.setColor(Color.black);\r
130 \r
131         if(i<sequence.length())\r
132           s = sequence.charAt(i);\r
133         else\r
134           s = ' ';\r
135 \r
136         if(!renderGaps && jalview.util.Comparison.isGap(s))\r
137           continue;\r
138 \r
139 \r
140         if (inCurrentSequenceGroup(i))\r
141         {\r
142           if(!currentSequenceGroup.getDisplayText())\r
143             continue;\r
144 \r
145             if (currentSequenceGroup.getColourText())\r
146             {\r
147               getBoxColour(currentSequenceGroup.cs, seq, i);\r
148               graphics.setColor(resBoxColour.darker());\r
149             }\r
150         }\r
151         else\r
152         {\r
153           if(!av.getShowText())\r
154             continue;\r
155 \r
156           if(av.getColourText())\r
157            {\r
158              getBoxColour(av.getGlobalColourScheme(), seq, i);\r
159              if(av.getShowBoxes())\r
160               graphics.setColor(resBoxColour.darker());\r
161             else\r
162               graphics.setColor(resBoxColour);\r
163            }\r
164         }\r
165 \r
166           charOffset =  (width - fm.charWidth(s))/2;\r
167           graphics.drawString(String.valueOf(s),\r
168                               charOffset + x1 + width * (i - start),\r
169                               y1 + height - pady);\r
170         }\r
171 \r
172 \r
173   }\r
174 \r
175   boolean inCurrentSequenceGroup(int res)\r
176   {\r
177     if(allGroups ==null)\r
178       return false;\r
179 \r
180     for(int i=0; i<allGroups.length; i++)\r
181       if(allGroups[i].getStartRes()<=res && allGroups[i].getEndRes()>=res)\r
182       {\r
183          currentSequenceGroup = allGroups[i];\r
184          return true;\r
185       }\r
186 \r
187     return false;\r
188   }\r
189 \r
190   public void drawHighlightedText(SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
191   {\r
192     int pady = height/5;\r
193     int charOffset=0;\r
194     graphics.setColor(Color.BLACK);\r
195     graphics.fillRect(x1,y1,width*(end-start+1),height);\r
196     graphics.setColor(Color.white);\r
197 \r
198     char s='~';\r
199     // Need to find the sequence position here.\r
200     for (int i = start; i <= end; i++)\r
201     {\r
202        if(i<seq.getLength())\r
203           s = seq.getSequence().charAt(i);\r
204 \r
205      charOffset =  (width - fm.charWidth(s))/2;\r
206       graphics.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
207     }\r
208   }\r
209 \r
210 \r
211 }\r