Allow colouring of ids from tree selected groups
[jalview.git] / src / jalview / gui / IdCanvas.java
1 package jalview.gui;\r
2 \r
3 import java.awt.*;\r
4 import javax.swing.*;\r
5 import jalview.datamodel.*;\r
6 import jalview.analysis.*;\r
7 public class IdCanvas extends JPanel\r
8 {\r
9   protected Image    img;\r
10   protected Graphics gg;\r
11 \r
12   protected int      imgWidth;\r
13   protected int      imgHeight;\r
14 \r
15   protected AlignViewport av;\r
16 \r
17   public boolean paintFlag   = false;\r
18   protected boolean showScores  = true;\r
19 \r
20   protected int     maxIdLength = -1;\r
21   protected String  maxIdStr    = null;\r
22 \r
23   public IdCanvas(AlignViewport av)\r
24   {\r
25     setLayout(new BorderLayout());\r
26     this.av         = av;\r
27     PaintRefresher.Register(this);\r
28   }\r
29 \r
30   public void drawIdString(Graphics g,SequenceI ds,int i, int starty, int ypos) {\r
31       int charHeight = av.getCharHeight();\r
32 \r
33       if (av.getSelection().contains(ds)) {\r
34           gg.setColor(Color.gray);\r
35           gg.fillRect(0,AlignmentUtil.getPixelHeight(starty,i,charHeight)+ ypos,getWidth(),charHeight);\r
36           gg.setColor(Color.white);\r
37       } else {\r
38           gg.setColor(ds.getColor());\r
39           gg.fillRect(0,AlignmentUtil.getPixelHeight(starty,i,charHeight)+ ypos,getWidth(),charHeight);\r
40           gg.setColor(Color.black);\r
41       }\r
42 \r
43       String string = ds.getName() + "/" + ds.getStart() + "-" + ds.getEnd();\r
44 \r
45       gg.drawString(string,0,AlignmentUtil.getPixelHeight(starty,i,charHeight) + ypos + charHeight/2);\r
46 \r
47   }\r
48 \r
49   public void paintComponent(Graphics g) {\r
50     AlignmentI da         = av.getAlignment();\r
51     int        charWidth  = (int)av.getCharWidth();\r
52     int        charHeight = av.getCharHeight();\r
53     Font       f          = av.getFont();\r
54 \r
55     if (img == null ||\r
56         imgWidth != getWidth()   ||\r
57         imgHeight != getHeight() ||\r
58         paintFlag == true) {\r
59 \r
60       imgWidth = getWidth();\r
61       imgHeight = getHeight();\r
62 \r
63       if (imgWidth <= 0 )  {\r
64         imgWidth  = 700;\r
65       }\r
66       if (imgHeight <= 0 ) {\r
67         imgHeight = 500;\r
68       }\r
69 \r
70       img = createImage(imgWidth,imgHeight);\r
71 \r
72       gg = img.getGraphics();\r
73       gg.setColor(Color.white);\r
74       gg.fillRect(0,0,imgWidth,imgHeight);\r
75 \r
76       gg.setFont(f);\r
77 \r
78       paintFlag = false;\r
79 \r
80     }\r
81 \r
82 \r
83     //Fill in the background\r
84     gg.setColor(Color.white);\r
85     gg.fillRect(0,0,imgWidth,imgHeight);\r
86 \r
87     Color currentColor     = Color.white;\r
88     Color currentTextColor = Color.black;\r
89 \r
90     //Which ids are we printing\r
91     int starty = av.getStartSeq();\r
92     int endy   = av.getEndSeq();\r
93 \r
94 \r
95 \r
96     if (av.getWrapAlignment()) {\r
97         starty = starty%av.getChunkHeight();\r
98 \r
99         int ypos     = 0;\r
100         int rowstart = starty;\r
101 \r
102         if (starty == 0) {\r
103             ypos = 2*charHeight;\r
104         } else if (starty == 1) {\r
105             starty = 0;\r
106             ypos = charHeight;\r
107         }\r
108 \r
109         endy   = starty + da.getHeight();\r
110 \r
111         if (endy > da.getHeight()) {\r
112             endy = da.getHeight();\r
113         }\r
114 \r
115         for (int i = starty; i < endy; i++) {\r
116           SequenceI s = da.getSequenceAt(i);\r
117           drawIdString(gg,s,i,starty,ypos);\r
118         }\r
119         if (rowstart == 0) {\r
120             ypos   = ypos +  av.getChunkHeight();\r
121         } else if (rowstart == 1) {\r
122             ypos   = ypos +  av.getChunkHeight();\r
123         } else {\r
124             ypos   = ypos +  av.getChunkHeight() - rowstart*charHeight;\r
125         }\r
126 \r
127         starty = 0;\r
128 \r
129         int chunkwidth = av.getChunkWidth();\r
130         int startx = (int)(av.getEndSeq()/chunkwidth)*chunkwidth;\r
131         int endx   = startx + chunkwidth;\r
132 \r
133 \r
134         while (ypos <= getHeight() && endx < da.getWidth()) {\r
135 \r
136             for (int i = starty; i < endy; i++) {\r
137               SequenceI s = da.getSequenceAt(i);\r
138               drawIdString(gg,s,i,starty,ypos);\r
139             }\r
140 \r
141             ypos   += av.getChunkHeight();\r
142             startx += chunkwidth;\r
143             endx = startx + chunkwidth;\r
144 \r
145             if (endx > da.getWidth()) {\r
146                 endx = da.getWidth();\r
147             }\r
148 \r
149             starty = 0;\r
150 \r
151             if (endy > da.getHeight()) {\r
152                 endy = da.getHeight();\r
153             }\r
154 \r
155         }\r
156     } else {\r
157 \r
158     //Now draw the id strings\r
159     for (int i = starty; i < endy; i++)\r
160     {\r
161 \r
162       // Selected sequence colours\r
163 \r
164       if (av.getSelection().contains(da.getSequenceAt(i))) {\r
165         if (currentColor != Color.gray) {\r
166           currentColor     = Color.gray;\r
167           currentTextColor = Color.black;\r
168         }\r
169       }\r
170       else if (da.getSequenceAt(i).getColor() != null)\r
171       {\r
172         currentColor = da.getSequenceAt(i).getColor();\r
173         currentTextColor = Color.black;\r
174       }\r
175 \r
176       gg.setColor(currentColor);\r
177 \r
178       if (currentColor != Color.BLACK)\r
179       {\r
180         gg.fillRect(0,\r
181                     AlignmentUtil.getPixelHeight(starty,i,charHeight),\r
182                     getWidth(),\r
183                     charHeight);\r
184       }\r
185 \r
186       gg.setColor(currentTextColor);\r
187 \r
188       String string = da.getSequenceAt(i).getDisplayId();\r
189 \r
190       gg.drawString(string,0,AlignmentUtil.getPixelHeight(starty,i,charHeight) +  charHeight-   (charHeight/5));\r
191     }\r
192     }\r
193 \r
194     g.drawImage(img,0,0,this);\r
195   }\r
196 \r
197 \r
198   public Dimension getLabelWidth()\r
199   {\r
200 \r
201    FontMetrics fm = this.getGraphics().getFontMetrics(av.font);\r
202    AlignmentI al = av.getAlignment();\r
203 \r
204    int i   = 0;\r
205    int idWidth = 0;\r
206 \r
207    while (i < al.getHeight() && al.getSequenceAt(i) != null)\r
208    {\r
209      SequenceI s   = al.getSequenceAt(i);\r
210      String str   = s.getDisplayId();\r
211      if (fm.stringWidth(str) > idWidth)\r
212        idWidth = fm.stringWidth(str);\r
213      i++;\r
214    }\r
215 \r
216    return new Dimension(idWidth + 10,getHeight());\r
217  }\r
218 \r
219  public Dimension getPreferredSize()\r
220  {\r
221    return getLabelWidth();\r
222  }\r
223 \r
224 \r
225 }\r