Looks to viewportfor global settings
[jalview.git] / src / jalview / gui / SequenceRenderer.java
1 package jalview.gui;\r
2 \r
3 import jalview.datamodel.*;\r
4 import jalview.schemes.*;\r
5 import java.awt.*;\r
6 import java.util.*;\r
7 \r
8 public class SequenceRenderer implements RendererI\r
9 {\r
10   AlignViewport av;\r
11   FontMetrics fm;\r
12   boolean renderGaps = true;\r
13   SequenceGroup currentSequenceGroup = null;\r
14   Color color;\r
15 \r
16   public SequenceRenderer(AlignViewport av)\r
17   {\r
18     this.av = av;\r
19   }\r
20 \r
21 \r
22   public void renderGaps(boolean b)\r
23   {\r
24     renderGaps = b;\r
25   }\r
26 \r
27   public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
28   {\r
29    Color c = Color.white;\r
30     try{\r
31        c = cs.findColour(seq, seq.getSequence(i, i + 1), i, null);\r
32     }catch(Exception ex){}\r
33 \r
34     return c;\r
35   }\r
36 \r
37   public void drawSequence(Graphics g,SequenceI seq,SequenceGroup sg, int start, int end, int x1, int y1, int width, int height, Vector pid, int seqnum)\r
38   {\r
39     currentSequenceGroup = sg;\r
40 \r
41     drawBoxes(g, seq, start, end, x1, y1, (int) width, height, pid);\r
42 \r
43     fm = g.getFontMetrics();\r
44     drawText(g,seq,start,end,x1,y1,(int)width,height);\r
45 \r
46   }\r
47 \r
48   public void drawBoxes(Graphics g, SequenceI seq,int start, int end, int x1, int y1, int width, int height,Vector freq) {\r
49     int i      = start;\r
50     int length = seq.getLength();\r
51 \r
52     Color currentColor = Color.WHITE;\r
53 \r
54     int curStart = x1;\r
55     int curWidth = width;\r
56 \r
57   //  int threshold = 80;\r
58 \r
59     while (i <= end && i < length)\r
60     {\r
61       color = color.white;\r
62 \r
63       if(inCurrentSequenceGroup(i))\r
64       {\r
65         if( currentSequenceGroup.getDisplayBoxes())\r
66              color = getResidueBoxColour(currentSequenceGroup.cs, seq, i);\r
67       }\r
68       else if(av.getShowBoxes())\r
69            color = getResidueBoxColour(av.getGlobalColourScheme(), seq, i);\r
70 \r
71 \r
72     //  Hashtable hash  = (Hashtable)freq.elementAt(i-start);\r
73     //  String    s     = (String)hash.get("maxResidue");\r
74     //  int       count = ((Integer)hash.get("maxCount")).intValue();\r
75   //    int       max   = ((Integer)hash.get("size")).intValue();\r
76    //   int       nongap = ((Integer)hash.get("nongap")).intValue();\r
77    //   float     frac  = (float)(count*1.0/(1.0*nongap));\r
78 \r
79       //System.out.println("Frac/count/nongap " + frac + " " + count + " " + nongap);\r
80     /*  if (!seq.getSequence().substring(i,i+1).equals(s) ||\r
81           s.equals("-") ||\r
82           s.equals(".") ||\r
83           s.equals(" "))\r
84       {\r
85         c = Color.white;\r
86       } else {\r
87         if (frac > 0.9) {\r
88           c = Color.red;\r
89         } else if (frac > 0.8) {\r
90           c = Color.orange;\r
91         } else if (frac > 0.7) {\r
92           c = Color.pink;\r
93         } else if (frac > 0.5) {\r
94           c = Color.yellow;\r
95         } else if (frac> 0.3) {\r
96             c = Color.lightGray;\r
97         }\r
98       }*/\r
99 \r
100       if (color != currentColor || color != null)\r
101       {\r
102         g.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
103 \r
104         currentColor = color;\r
105         g.setColor(color);\r
106 \r
107         curStart = i;\r
108         curWidth = width;\r
109       }\r
110       else\r
111         curWidth += width;\r
112 \r
113       i++;\r
114     }\r
115     g.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
116   }\r
117 \r
118   public void drawText(Graphics g, 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     g.setColor(Color.black);\r
123 \r
124     char s;\r
125     // Need to find the sequence position here.\r
126     for (int i = start; i <= end; i++)\r
127     {\r
128         if(i<seq.getLength())\r
129           s = seq.getSequence().charAt(i);\r
130         else\r
131           s = ' ';\r
132 \r
133         if(!renderGaps && (s=='-' || s=='.' || s==' '))\r
134           continue;\r
135 \r
136         g.setColor(Color.black);\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             g.setColor(getResidueBoxColour(currentSequenceGroup.cs, seq, i).darker());\r
145         }\r
146         else\r
147         {\r
148           if(!av.getShowText())\r
149             continue;\r
150 \r
151           if(av.getColourText())\r
152             g.setColor(getResidueBoxColour(av.getGlobalColourScheme(), seq, i).darker());\r
153         }\r
154 \r
155       charOffset =  (width - fm.charWidth(s))/2;\r
156       g.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
157     }\r
158 \r
159 \r
160   }\r
161 \r
162   boolean inCurrentSequenceGroup(int res)\r
163   {\r
164     if(currentSequenceGroup==null)\r
165       return false;\r
166 \r
167     return (currentSequenceGroup.getStartRes()<=res && currentSequenceGroup.getEndRes()>=res)?true:false;\r
168   }\r
169 \r
170   public void drawHighlightedText(Graphics g, SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
171   {\r
172     int pady = height/5;\r
173     int charOffset=0;\r
174     g.setColor(Color.BLACK);\r
175     g.fillRect(x1,y1,width*(end-start+1),height);\r
176     g.setColor(Color.white);\r
177 \r
178     char s='~';\r
179     // Need to find the sequence position here.\r
180     for (int i = start; i <= end; i++)\r
181     {\r
182        if(i<seq.getLength())\r
183           s = seq.getSequence().charAt(i);\r
184 \r
185       charOffset =  (width - fm.charWidth(s))/2;\r
186       g.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
187     }\r
188   }\r
189 \r
190 \r
191 }\r