GPL license added
[jalview.git] / src / jalview / gui / IdCanvas.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 java.awt.*;\r
23 import java.awt.Graphics2D.*;\r
24 import java.awt.image.*;\r
25 import javax.swing.*;\r
26 import jalview.datamodel.*;\r
27 import jalview.analysis.*;\r
28 public class IdCanvas extends JPanel\r
29 {\r
30   protected AlignViewport av;\r
31 \r
32   protected boolean showScores  = true;\r
33 \r
34   protected int     maxIdLength = -1;\r
35   protected String  maxIdStr    = null;\r
36   BufferedImage image;\r
37   Graphics2D gg;\r
38   int imgHeight=0;\r
39   boolean fastPaint = false;\r
40 \r
41   java.util.Vector searchResults;\r
42 \r
43   public IdCanvas(AlignViewport av)\r
44   {\r
45     setLayout(new BorderLayout());\r
46     this.av         = av;\r
47     PaintRefresher.Register(this);\r
48   }\r
49 \r
50   public void drawIdString(Graphics2D gg,SequenceI s,int i, int starty, int ypos) {\r
51       int charHeight = av.getCharHeight();\r
52 \r
53       if(searchResults!=null && searchResults.contains(s))\r
54       {\r
55         gg.setColor(Color.black);\r
56         gg.fillRect(0,AlignmentUtil.getPixelHeight(starty,i,charHeight)+ ypos,getWidth(),charHeight);\r
57         gg.setColor(Color.white);\r
58       }\r
59       else if (av.getSelectionGroup()!=null && av.getSelectionGroup().sequences.contains(s)) {\r
60           gg.setColor(Color.lightGray);\r
61           gg.fillRect(0,AlignmentUtil.getPixelHeight(starty,i,charHeight)+ ypos,getWidth(),charHeight);\r
62           gg.setColor(Color.white);\r
63       } else {\r
64           gg.setColor(s.getColor());\r
65           gg.fillRect(0,AlignmentUtil.getPixelHeight(starty,i,charHeight)+ ypos,getWidth(),charHeight);\r
66           gg.setColor(Color.black);\r
67       }\r
68 \r
69       String string = s.getName();\r
70       if(av.getShowFullId())\r
71         string = s.getDisplayId();\r
72 \r
73       gg.drawString(string,0,AlignmentUtil.getPixelHeight(starty,i,charHeight) + ypos + charHeight-   (charHeight/5));\r
74 \r
75   }\r
76 \r
77   public void fastPaint(int vertical)\r
78   {\r
79     if(gg==null)\r
80     {  repaint(); return;}\r
81 \r
82     gg.copyArea( 0,0, getWidth(), imgHeight, 0, -vertical*av.charHeight );\r
83 \r
84     int ss=av.startSeq, es=av.endSeq, transY = 0;\r
85     if (vertical > 0) // scroll down\r
86      {\r
87        ss = es - vertical;\r
88        if(ss<av.startSeq) // ie scrolling too fast, more than a page at a time\r
89          ss = av.startSeq;\r
90        else\r
91          transY = imgHeight - vertical * av.charHeight;\r
92      }\r
93      else if (vertical < 0)\r
94      {\r
95        es = ss - vertical;\r
96        if(es > av.endSeq)\r
97          es = av.endSeq;\r
98      }\r
99 \r
100 \r
101 \r
102         gg.translate(0, transY);\r
103 \r
104         drawIds(ss, es);\r
105 \r
106         gg.translate( 0, -transY );\r
107 \r
108 \r
109       fastPaint = true;\r
110       repaint();\r
111   }\r
112 \r
113   public void paintComponent(Graphics g)\r
114   {\r
115     g.setColor(Color.white);\r
116     g.fillRect(0, 0, getWidth(), getHeight());\r
117 \r
118     if (fastPaint)\r
119     {\r
120       fastPaint = false;\r
121       g.drawImage(image, 0, 0, this);\r
122       return;\r
123     }\r
124 \r
125     imgHeight = getHeight();\r
126     imgHeight -= imgHeight % av.charHeight;\r
127 \r
128     if (imgHeight<1)\r
129       return;\r
130 \r
131     image = new BufferedImage(getWidth(), imgHeight, BufferedImage.TYPE_INT_RGB);\r
132     gg = (Graphics2D) image.getGraphics();\r
133     //Fill in the background\r
134     gg.setColor(Color.white);\r
135     gg.fillRect(0, 0, getWidth(), imgHeight);\r
136     gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
137                         RenderingHints.VALUE_ANTIALIAS_ON);\r
138     Font italic = new Font(av.getFont().getName(), Font.ITALIC,\r
139                            av.getFont().getSize());\r
140     gg.setFont(italic);\r
141 \r
142     drawIds( av.getStartSeq(), av.endSeq);\r
143 \r
144     g.drawImage(image, 0, 0, this);\r
145   }\r
146 \r
147   void drawIds(int starty, int endy)\r
148   {\r
149     Color currentColor     = Color.white;\r
150     Color currentTextColor = Color.black;\r
151 \r
152     if (av.getWrapAlignment())\r
153     {\r
154 \r
155           int rowSize =  av.getEndRes() - av.getStartRes();\r
156           // Draw the rest of the panels\r
157 \r
158           for(int ypos=2*av.charHeight, row=av.startRes;\r
159               ypos <= getHeight() && row<av.alignment.getWidth();\r
160               ypos += av.chunkHeight, row+=rowSize )\r
161           {\r
162             for (int i = starty; i < av.alignment.getHeight(); i++)\r
163             {\r
164               SequenceI s = av.alignment.getSequenceAt(i);\r
165               drawIdString(gg, s, i, 0, ypos);\r
166             }\r
167           }\r
168 \r
169 \r
170     } else\r
171     {\r
172 \r
173       //Now draw the id strings\r
174       for (int i = starty; i < endy; i++)\r
175       {\r
176         // Selected sequence colours\r
177 \r
178         if(searchResults!=null && searchResults.contains(av.alignment.getSequenceAt(i)))\r
179         {\r
180           gg.setColor(Color.black);\r
181           currentColor = Color.black;\r
182           currentTextColor = Color.white;\r
183       }\r
184       else if (av.getSelectionGroup()!= null\r
185             && av.getSelectionGroup().sequences.contains(av.alignment.getSequenceAt(i)))\r
186         {\r
187           currentColor = Color.lightGray;\r
188           currentTextColor = Color.black;\r
189         }\r
190         else\r
191         {\r
192           currentColor = av.alignment.getSequenceAt(i).getColor();\r
193           currentTextColor = Color.black;\r
194         }\r
195 \r
196         gg.setColor(currentColor);\r
197 \r
198         gg.fillRect(0,\r
199                     AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
200                     getWidth(),\r
201                     av.charHeight);\r
202 \r
203         gg.setColor(currentTextColor);\r
204         String string = av.alignment.getSequenceAt(i).getName();\r
205         if(av.getShowFullId())\r
206           string = av.alignment.getSequenceAt(i).getDisplayId();\r
207         gg.drawString(string, 0,\r
208                       AlignmentUtil.getPixelHeight(starty, i, av.charHeight) +\r
209                       av.charHeight - (av.charHeight / 5));\r
210       }\r
211 \r
212       // add a border\r
213       gg.setColor(Color.white);\r
214       gg.fillRect(getWidth()-4,0,4,getHeight());\r
215     }\r
216 \r
217   }\r
218 \r
219   public void setHighlighted(java.util.Vector found)\r
220   {\r
221     searchResults = found;\r
222     repaint();\r
223   }\r
224 }\r