Preparing for groupselect mode
[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     if(currentSequenceGroup!=null && currentSequenceGroup.getDisplayBoxes())\r
58     {\r
59       curStart = currentSequenceGroup.getStartRes();\r
60       curWidth = currentSequenceGroup.getEndRes();\r
61     }\r
62 \r
63   //  int threshold = 80;\r
64 \r
65     while (i <= end && i < length)\r
66     {\r
67       if(inCurrentSequenceGroup(i))\r
68       {\r
69         color = getResidueBoxColour(currentSequenceGroup.cs, seq, i);\r
70       }\r
71       else\r
72         color = getResidueBoxColour(av.getGlobalColourScheme(), seq, i);\r
73 \r
74     //  Hashtable hash  = (Hashtable)freq.elementAt(i-start);\r
75     //  String    s     = (String)hash.get("maxResidue");\r
76     //  int       count = ((Integer)hash.get("maxCount")).intValue();\r
77   //    int       max   = ((Integer)hash.get("size")).intValue();\r
78    //   int       nongap = ((Integer)hash.get("nongap")).intValue();\r
79    //   float     frac  = (float)(count*1.0/(1.0*nongap));\r
80 \r
81       //System.out.println("Frac/count/nongap " + frac + " " + count + " " + nongap);\r
82     /*  if (!seq.getSequence().substring(i,i+1).equals(s) ||\r
83           s.equals("-") ||\r
84           s.equals(".") ||\r
85           s.equals(" "))\r
86       {\r
87         c = Color.white;\r
88       } else {\r
89         if (frac > 0.9) {\r
90           c = Color.red;\r
91         } else if (frac > 0.8) {\r
92           c = Color.orange;\r
93         } else if (frac > 0.7) {\r
94           c = Color.pink;\r
95         } else if (frac > 0.5) {\r
96           c = Color.yellow;\r
97         } else if (frac> 0.3) {\r
98             c = Color.lightGray;\r
99         }\r
100       }*/\r
101 \r
102       if (color != currentColor || color != null)\r
103       {\r
104         g.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
105 \r
106         currentColor = color;\r
107         g.setColor(color);\r
108 \r
109         curStart = i;\r
110         curWidth = width;\r
111       }\r
112       else\r
113         curWidth += width;\r
114 \r
115       i++;\r
116     }\r
117     g.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
118   }\r
119 \r
120   public void drawText(Graphics g, SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
121   {\r
122     int pady = height/5;\r
123     int charOffset=0;\r
124     g.setColor(Color.black);\r
125 \r
126     char s;\r
127     // Need to find the sequence position here.\r
128     for (int i = start; i <= end; i++)\r
129     {\r
130         if(i<seq.getLength())\r
131           s = seq.getSequence().charAt(i);\r
132         else\r
133           s = ' ';\r
134 \r
135         if(!renderGaps && (s=='-' || s=='.' || s==' '))\r
136           continue;\r
137 \r
138 \r
139         if (inCurrentSequenceGroup(i))\r
140         {\r
141           if(!currentSequenceGroup.getDisplayText())\r
142             continue;\r
143 \r
144           if(currentSequenceGroup.getColourText())\r
145             g.setColor(getResidueBoxColour(currentSequenceGroup.cs, seq, i).darker());\r
146         }\r
147         else\r
148           g.setColor(Color.black);\r
149 \r
150       charOffset =  (width - fm.charWidth(s))/2;\r
151       g.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
152     }\r
153 \r
154 \r
155   }\r
156 \r
157   boolean inCurrentSequenceGroup(int res)\r
158   {\r
159     if(currentSequenceGroup==null)\r
160       return false;\r
161 \r
162     return (currentSequenceGroup.getStartRes()<=res && currentSequenceGroup.getEndRes()>=res)?true:false;\r
163   }\r
164 \r
165   public void drawHighlightedText(Graphics g, SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
166   {\r
167     int pady = height/5;\r
168     int charOffset=0;\r
169     g.setColor(Color.BLACK);\r
170     g.fillRect(x1,y1,width*(end-start+1),height);\r
171     g.setColor(Color.white);\r
172 \r
173     char s='~';\r
174     // Need to find the sequence position here.\r
175     for (int i = start; i <= end; i++)\r
176     {\r
177        if(i<seq.getLength())\r
178           s = seq.getSequence().charAt(i);\r
179 \r
180       charOffset =  (width - fm.charWidth(s))/2;\r
181       g.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
182     }\r
183   }\r
184 \r
185 \r
186 }\r