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