321edef3b1bec6cfc21f9fa0bcdb277f0e757d85
[jalview.git] / src / jalview / appletgui / 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.appletgui;\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     String sequence  = seq.getSequence();\r
126     for (int i = start; i <= end; i++)\r
127     {\r
128         graphics.setColor(Color.black);\r
129         if(i<sequence.length())\r
130           s = sequence.charAt(i);\r
131         else\r
132           s = ' ';\r
133 \r
134         if(!renderGaps && jalview.util.Comparison.isGap(s))\r
135           continue;\r
136 \r
137 \r
138         if (inCurrentSequenceGroup(i))\r
139         {\r
140           if(!currentSequenceGroup.getDisplayText())\r
141             continue;\r
142 \r
143             if (currentSequenceGroup.getColourText())\r
144             {\r
145               getBoxColour(currentSequenceGroup.cs, seq, i);\r
146               graphics.setColor(resBoxColour.darker());\r
147             }\r
148         }\r
149         else\r
150         {\r
151           if(!av.getShowText())\r
152             continue;\r
153 \r
154           if(av.getColourText())\r
155            {\r
156              getBoxColour(av.getGlobalColourScheme(), seq, i);\r
157              if(av.getShowBoxes())\r
158               graphics.setColor(resBoxColour.darker());\r
159             else\r
160               graphics.setColor(resBoxColour);\r
161            }\r
162         }\r
163 \r
164           charOffset =  (width - fm.charWidth(s))/2;\r
165           graphics.drawString(String.valueOf(s),\r
166                               charOffset + x1 + width * (i - start),\r
167                               y1 + height - pady);\r
168         }\r
169 \r
170 \r
171   }\r
172 \r
173   boolean inCurrentSequenceGroup(int res)\r
174   {\r
175     if(allGroups ==null)\r
176       return false;\r
177 \r
178     for(int i=0; i<allGroups.length; i++)\r
179       if(allGroups[i].getStartRes()<=res && allGroups[i].getEndRes()>=res)\r
180       {\r
181          currentSequenceGroup = allGroups[i];\r
182          return true;\r
183       }\r
184 \r
185     return false;\r
186   }\r
187 \r
188   public void drawHighlightedText(SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
189   {\r
190     int pady = height/5;\r
191     int charOffset=0;\r
192     graphics.setColor(Color.black);\r
193     graphics.fillRect(x1,y1,width*(end-start+1),height);\r
194     graphics.setColor(Color.white);\r
195 \r
196     char s='~';\r
197     // Need to find the sequence position here.\r
198     for (int i = start; i <= end; i++)\r
199     {\r
200        if(i<seq.getLength())\r
201           s = seq.getSequence().charAt(i);\r
202 \r
203      charOffset =  (width - fm.charWidth(s))/2;\r
204       graphics.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
205     }\r
206   }\r
207 \r
208 \r
209 }\r