GPL license added
[jalview.git] / src / jalview / gui / FeatureRenderer.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 import java.util.*;\r
26 \r
27 public class FeatureRenderer\r
28 {\r
29   AlignViewport av;\r
30 \r
31   SequenceGroup currentSequenceGroup = null;\r
32   SequenceGroup [] allGroups = null;\r
33   Color resBoxColour;\r
34   Graphics graphics;\r
35 \r
36   public FeatureRenderer(AlignViewport av)\r
37   {\r
38     this.av = av;\r
39   }\r
40 \r
41 \r
42   public void drawSequence(Graphics g,SequenceI seq,SequenceGroup [] sg, int start, int end, int x1, int y1, int width, int height)\r
43   {\r
44     Vector features = seq.getSequenceFeatures();\r
45     Enumeration e = features.elements();\r
46     while( e.hasMoreElements() )\r
47     {\r
48       SequenceFeature sf = (SequenceFeature)e.nextElement();\r
49       if(sf.getStart()>seq.getEnd())\r
50         continue;\r
51 \r
52       int fstart = seq.findIndex(sf.getStart())-1;\r
53       int fend = seq.findIndex(sf.getEnd())-1;\r
54 \r
55       if(   (fstart<=end && fend>=start)  )\r
56       {\r
57         if(fstart<0) // fix for if the feature we have starts before the sequence start,\r
58           fstart = 0;// but the feature end is still valid!!\r
59 \r
60         if(fstart==fend)\r
61         {\r
62           g.setColor(Color.red);\r
63           g.fillRoundRect( (fstart - start) * width, y1, width, height, 4,4);\r
64           g.setColor(Color.white);\r
65 \r
66           char s = seq.getSequence().charAt(fstart);\r
67           FontMetrics fm = g.getFontMetrics();\r
68           int charOffset =  (width - fm.charWidth(s))/2;\r
69           int pady = height/5;\r
70           g.drawString(String.valueOf(s), charOffset + x1 + width * (fstart - start), y1 + height - pady);\r
71 \r
72         }\r
73         else\r
74         {\r
75           for (int i = fstart; i <= fend; i++)\r
76           {\r
77             char s = seq.getSequence().charAt(i);\r
78             if( jalview.util.Comparison.isGap(s) )\r
79               continue;\r
80 \r
81             g.setColor(Color.blue);\r
82             g.fillRect( (i-start) * width, y1, width, height);\r
83 \r
84             g.setColor(Color.white);\r
85 \r
86             FontMetrics fm = g.getFontMetrics();\r
87             int charOffset = (width - fm.charWidth(s)) / 2;\r
88             int pady = height / 5;\r
89             g.drawString(String.valueOf(s),\r
90                          charOffset + x1 + width * (i-start),\r
91                          y1 + height - pady);\r
92           }\r
93         }\r
94       }\r
95 \r
96     }\r
97    }\r
98 \r
99 }\r