8f8ed35a1bc4a72aa3f6bb127e982cbe13dc603b
[jalview.git] / src / jalview / appletgui / IdCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.appletgui;
22
23 import jalview.datamodel.SequenceI;
24
25 import java.awt.Color;
26 import java.awt.Font;
27 import java.awt.Graphics;
28 import java.awt.Image;
29 import java.awt.Panel;
30 import java.util.List;
31
32 public class IdCanvas extends Panel
33 {
34   protected AlignViewport av;
35
36   protected boolean showScores = true;
37
38   protected int maxIdLength = -1;
39
40   protected String maxIdStr = null;
41
42   Image image;
43
44   Graphics gg;
45
46   int imgHeight = 0;
47
48   boolean fastPaint = false;
49
50   List<SequenceI> searchResults;
51
52   public IdCanvas(AlignViewport av)
53   {
54     setLayout(null);
55     this.av = av;
56     PaintRefresher.Register(this, av.getSequenceSetId());
57   }
58
59   public void drawIdString(Graphics gg, boolean hiddenRows, SequenceI s,
60           int i, int starty, int ypos)
61   {
62     int charHeight = av.getCharHeight();
63
64     if (searchResults != null && searchResults.contains(s))
65     {
66       gg.setColor(Color.black);
67       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getSize().width,
68               charHeight);
69       gg.setColor(Color.white);
70     }
71     else if (av.getSelectionGroup() != null
72             && av.getSelectionGroup().getSequences(null).contains(s))
73     {
74       gg.setColor(Color.lightGray);
75       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getSize().width,
76               charHeight);
77       gg.setColor(Color.white);
78     }
79     else
80     {
81       gg.setColor(av.getSequenceColour(s));
82       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getSize().width,
83               charHeight);
84       gg.setColor(Color.black);
85     }
86
87     gg.drawString(s.getDisplayId(av.getShowJVSuffix()), 0,
88             ((i - starty) * charHeight) + ypos + charHeight
89                     - (charHeight / 5));
90
91     if (hiddenRows)
92     {
93       drawMarker(i, starty, ypos);
94     }
95
96   }
97
98   public void fastPaint(int vertical)
99   {
100     if (gg == null)
101     {
102       repaint();
103       return;
104     }
105
106     gg.copyArea(0, 0, getSize().width, imgHeight, 0,
107             -vertical * av.getCharHeight());
108
109     int ss = av.getStartSeq(), es = av.getEndSeq(), transY = 0;
110     if (vertical > 0) // scroll down
111     {
112       ss = es - vertical;
113       if (ss < av.getStartSeq()) // ie scrolling too fast, more than a page at a
114                                  // time
115       {
116         ss = av.getStartSeq();
117       }
118       else
119       {
120         transY = imgHeight - vertical * av.getCharHeight();
121       }
122     }
123     else if (vertical < 0)
124     {
125       es = ss - vertical;
126       if (es > av.getEndSeq())
127       {
128         es = av.getEndSeq();
129       }
130     }
131
132     gg.translate(0, transY);
133
134     drawIds(ss, es);
135
136     gg.translate(0, -transY);
137
138     fastPaint = true;
139     repaint();
140   }
141
142   @Override
143   public void update(Graphics g)
144   {
145     paint(g);
146   }
147
148   @Override
149   public void paint(Graphics g)
150   {
151     if (getSize().height < 0 || getSize().width < 0)
152     {
153       return;
154     }
155     if (fastPaint)
156     {
157       fastPaint = false;
158       g.drawImage(image, 0, 0, this);
159       return;
160     }
161
162     imgHeight = getSize().height;
163     imgHeight -= imgHeight % av.getCharHeight();
164
165     if (imgHeight < 1)
166     {
167       return;
168     }
169
170     if (image == null || imgHeight != image.getHeight(this))
171     {
172       image = createImage(getSize().width, imgHeight);
173       gg = image.getGraphics();
174       gg.setFont(av.getFont());
175     }
176
177     // Fill in the background
178     gg.setColor(Color.white);
179     Font italic = new Font(av.getFont().getName(), Font.ITALIC, av
180             .getFont().getSize());
181     gg.setFont(italic);
182
183     gg.fillRect(0, 0, getSize().width, getSize().height);
184     drawIds(av.getStartSeq(), av.getEndSeq());
185     g.drawImage(image, 0, 0, this);
186   }
187
188   /**
189    * local copy of av.getCharHeight set at top of drawIds
190    */
191   private int avcharHeight;
192
193   void drawIds(int starty, int endy)
194   {
195     // hardwired italic IDs in applet currently
196     Font italic = new Font(av.getFont().getName(), Font.ITALIC, av
197             .getFont().getSize());
198     // temp variable for speed
199     avcharHeight = av.getCharHeight();
200
201     gg.setFont(italic);
202
203     Color currentColor = Color.white;
204     Color currentTextColor = Color.black;
205
206     final boolean doHiddenCheck = av.isDisplayReferenceSeq()
207             || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows()
208             && av.getShowHiddenMarkers();
209
210     if (av.getWrapAlignment())
211     {
212       int maxwidth = av.getAlignment().getWidth();
213       int alheight = av.getAlignment().getHeight();
214
215       if (av.hasHiddenColumns())
216       {
217         maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
218       }
219
220       int annotationHeight = 0;
221       AnnotationLabels labels = null;
222
223       if (av.isShowAnnotation())
224       {
225         AnnotationPanel ap = new AnnotationPanel(av);
226         annotationHeight = ap.adjustPanelHeight();
227         labels = new AnnotationLabels(av);
228       }
229       int hgap = avcharHeight;
230       if (av.getScaleAboveWrapped())
231       {
232         hgap += avcharHeight;
233       }
234
235       int cHeight = alheight * avcharHeight + hgap + annotationHeight;
236
237       int rowSize = av.getEndRes() - av.getStartRes();
238       // Draw the rest of the panels
239       for (int ypos = hgap, row = av.getStartRes(); (ypos <= getSize().height)
240               && (row < maxwidth); ypos += cHeight, row += rowSize)
241       {
242         for (int i = starty; i < alheight; i++)
243         {
244
245           SequenceI s = av.getAlignment().getSequenceAt(i);
246           gg.setFont(italic);
247           if (doHiddenCheck)
248           {
249             setHiddenFont(s);
250           }
251           drawIdString(gg, hiddenRows, s, i, 0, ypos);
252         }
253
254         if (labels != null)
255         {
256           gg.translate(0, ypos + (alheight * avcharHeight));
257           labels.drawComponent(gg, getSize().width);
258           gg.translate(0, -ypos - (alheight * avcharHeight));
259         }
260
261       }
262     }
263     else
264     {
265       // Now draw the id strings
266       SequenceI seq;
267       for (int i = starty; i < endy; i++)
268       {
269
270         seq = av.getAlignment().getSequenceAt(i);
271         if (seq == null)
272         {
273           continue;
274         }
275         gg.setFont(italic);
276         // boolean isrep=false;
277         if (doHiddenCheck)
278         {
279           // isrep =
280           setHiddenFont(seq);
281         }
282
283         // Selected sequence colours
284         if ((searchResults != null) && searchResults.contains(seq))
285         {
286           currentColor = Color.black;
287           currentTextColor = Color.white;
288         }
289         else if ((av.getSelectionGroup() != null)
290                 && av.getSelectionGroup().getSequences(null).contains(seq))
291         {
292           currentColor = Color.lightGray;
293           currentTextColor = Color.black;
294         }
295         else
296         {
297           currentColor = av.getSequenceColour(seq);
298           currentTextColor = Color.black;
299         }
300
301         gg.setColor(currentColor);
302         // TODO: isrep could be used to highlight the representative in a
303         // different way
304         gg.fillRect(0, (i - starty) * avcharHeight, getSize().width,
305                 avcharHeight);
306         gg.setColor(currentTextColor);
307
308         gg.drawString(seq.getDisplayId(av.getShowJVSuffix()), 0,
309                 (((i - starty) * avcharHeight) + avcharHeight)
310                         - (avcharHeight / 5));
311
312         if (hiddenRows)
313         {
314           drawMarker(i, starty, 0);
315         }
316       }
317     }
318   }
319
320   public void setHighlighted(List<SequenceI> list)
321   {
322     searchResults = list;
323     repaint();
324   }
325
326   void drawMarker(int i, int starty, int yoffset)
327   {
328     SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
329     // Use this method here instead of calling hiddenSeq adjust
330     // 3 times.
331     int hSize = hseqs.length;
332
333     int hiddenIndex = i;
334     int lastIndex = i - 1;
335     int nextIndex = i + 1;
336
337     for (int j = 0; j < hSize; j++)
338     {
339       if (hseqs[j] != null)
340       {
341         if (j - 1 < hiddenIndex)
342         {
343           hiddenIndex++;
344         }
345         if (j - 1 < lastIndex)
346         {
347           lastIndex++;
348         }
349         if (j - 1 < nextIndex)
350         {
351           nextIndex++;
352         }
353       }
354     }
355
356     boolean below = (hiddenIndex > lastIndex + 1);
357     boolean above = (nextIndex > hiddenIndex + 1);
358
359     gg.setColor(Color.blue);
360     if (below)
361     {
362       gg.fillPolygon(new int[] { getSize().width - avcharHeight,
363           getSize().width - avcharHeight, getSize().width }, new int[] {
364           (i - starty) * avcharHeight + yoffset,
365           (i - starty) * avcharHeight + yoffset + avcharHeight / 4,
366           (i - starty) * avcharHeight + yoffset }, 3);
367     }
368     if (above)
369     {
370       gg.fillPolygon(new int[] { getSize().width - avcharHeight,
371           getSize().width - avcharHeight, getSize().width }, new int[] {
372           (i - starty + 1) * avcharHeight + yoffset,
373           (i - starty + 1) * avcharHeight + yoffset - avcharHeight / 4,
374           (i - starty + 1) * avcharHeight + yoffset }, 3);
375
376     }
377   }
378
379   boolean setHiddenFont(SequenceI seq)
380   {
381     Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
382             .getSize());
383
384     if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
385     {
386       gg.setFont(bold);
387       return true;
388     }
389     return false;
390   }
391 }