5b589bbd54f175517f4873057ee03953ccc6acd4
[jalview.git] / src / jalview / gui / IdCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import java.util.List;
23
24 import javax.swing.*;
25
26 import jalview.datamodel.*;
27
28 /**
29  * DOCUMENT ME!
30  * 
31  * @author $author$
32  * @version $Revision$
33  */
34 public class IdCanvas extends JPanel
35 {
36   protected AlignViewport av;
37
38   protected boolean showScores = true;
39
40   protected int maxIdLength = -1;
41
42   protected String maxIdStr = null;
43
44   BufferedImage image;
45
46   Graphics2D gg;
47
48   int imgHeight = 0;
49
50   boolean fastPaint = false;
51
52   List<SequenceI> searchResults;
53
54   FontMetrics fm;
55
56   AnnotationLabels labels = null;
57
58   AnnotationPanel ap;
59
60   Font idfont;
61
62   /**
63    * Creates a new IdCanvas object.
64    * 
65    * @param av
66    *          DOCUMENT ME!
67    */
68   public IdCanvas(AlignViewport av)
69   {
70     setLayout(new BorderLayout());
71     this.av = av;
72     PaintRefresher.Register(this, av.getSequenceSetId());
73   }
74
75   /**
76    * DOCUMENT ME!
77    * 
78    * @param gg
79    *          DOCUMENT ME!
80    * @param s
81    *          DOCUMENT ME!
82    * @param i
83    *          DOCUMENT ME!
84    * @param starty
85    *          DOCUMENT ME!
86    * @param ypos
87    *          DOCUMENT ME!
88    */
89   public void drawIdString(Graphics2D gg, SequenceI s, int i, int starty,
90           int ypos)
91   {
92     int xPos = 0;
93     int panelWidth = getWidth();
94     int charHeight = av.charHeight;
95
96     if ((searchResults != null) && searchResults.contains(s))
97     {
98       gg.setColor(Color.black);
99       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
100               charHeight);
101       gg.setColor(Color.white);
102     }
103     else if ((av.getSelectionGroup() != null)
104             && av.getSelectionGroup().getSequences(null).contains(s))
105     {
106       gg.setColor(Color.lightGray);
107       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
108               charHeight);
109       gg.setColor(Color.white);
110     }
111     else
112     {
113       gg.setColor(av.getSequenceColour(s));
114       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
115               charHeight);
116       gg.setColor(Color.black);
117     }
118
119     if (av.rightAlignIds)
120     {
121       xPos = panelWidth
122               - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
123     }
124
125     gg.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
126             (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
127
128     if (av.hasHiddenRows() && av.showHiddenMarkers)
129     {
130       drawMarker(i, starty, ypos);
131     }
132
133   }
134
135   /**
136    * DOCUMENT ME!
137    * 
138    * @param vertical
139    *          DOCUMENT ME!
140    */
141   public void fastPaint(int vertical)
142   {
143     if (gg == null)
144     {
145       repaint();
146
147       return;
148     }
149
150     gg.copyArea(0, 0, getWidth(), imgHeight, 0, -vertical * av.charHeight);
151
152     int ss = av.startSeq;
153     int es = av.endSeq;
154     int transY = 0;
155
156     if (vertical > 0) // scroll down
157     {
158       ss = es - vertical;
159
160       if (ss < av.startSeq)
161       { // ie scrolling too fast, more than a page at a time
162         ss = av.startSeq;
163       }
164       else
165       {
166         transY = imgHeight - (vertical * av.charHeight);
167       }
168     }
169     else if (vertical < 0)
170     {
171       es = ss - vertical;
172
173       if (es > av.endSeq)
174       {
175         es = av.endSeq;
176       }
177     }
178
179     gg.translate(0, transY);
180
181     drawIds(ss, es);
182
183     gg.translate(0, -transY);
184
185     fastPaint = true;
186     repaint();
187   }
188
189   /**
190    * DOCUMENT ME!
191    * 
192    * @param g
193    *          DOCUMENT ME!
194    */
195   public void paintComponent(Graphics g)
196   {
197     g.setColor(Color.white);
198     g.fillRect(0, 0, getWidth(), getHeight());
199
200     if (fastPaint)
201     {
202       fastPaint = false;
203       g.drawImage(image, 0, 0, this);
204
205       return;
206     }
207
208     int oldHeight = imgHeight;
209
210     imgHeight = getHeight();
211     imgHeight -= (imgHeight % av.charHeight);
212
213     if (imgHeight < 1)
214     {
215       return;
216     }
217
218     if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
219     {
220       image = new BufferedImage(getWidth(), imgHeight,
221               BufferedImage.TYPE_INT_RGB);
222     }
223
224     gg = (Graphics2D) image.getGraphics();
225
226     // Fill in the background
227     gg.setColor(Color.white);
228     gg.fillRect(0, 0, getWidth(), imgHeight);
229
230     drawIds(av.getStartSeq(), av.endSeq);
231
232     g.drawImage(image, 0, 0, this);
233   }
234
235   /**
236    * DOCUMENT ME!
237    * 
238    * @param starty
239    *          DOCUMENT ME!
240    * @param endy
241    *          DOCUMENT ME!
242    */
243   void drawIds(int starty, int endy)
244   {
245     if (av.seqNameItalics)
246     {
247       idfont = new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
248               .getSize());
249     }
250     else
251     {
252       idfont = av.getFont();
253     }
254
255     gg.setFont(idfont);
256     fm = gg.getFontMetrics();
257
258     if (av.antiAlias)
259     {
260       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
261               RenderingHints.VALUE_ANTIALIAS_ON);
262     }
263
264     Color currentColor = Color.white;
265     Color currentTextColor = Color.black;
266
267     if (av.getWrapAlignment())
268     {
269       int maxwidth = av.getAlignment().getWidth();
270       int alheight = av.getAlignment().getHeight();
271
272       if (av.hasHiddenColumns())
273       {
274         maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
275       }
276
277       int annotationHeight = 0;
278
279       if (av.showAnnotation)
280       {
281         if (ap == null)
282         {
283           ap = new AnnotationPanel(av);
284         }
285
286         annotationHeight = ap.adjustPanelHeight();
287         if (labels == null)
288         {
289           labels = new AnnotationLabels(av);
290         }
291       }
292
293       int hgap = av.charHeight;
294       if (av.scaleAboveWrapped)
295       {
296         hgap += av.charHeight;
297       }
298
299       int cHeight = alheight * av.charHeight + hgap + annotationHeight;
300
301       int rowSize = av.getEndRes() - av.getStartRes();
302
303       // Draw the rest of the panels
304       for (int ypos = hgap, row = av.startRes; (ypos <= getHeight())
305               && (row < maxwidth); ypos += cHeight, row += rowSize)
306       {
307         for (int i = starty; i < alheight; i++)
308         {
309           SequenceI s = av.getAlignment().getSequenceAt(i);
310           if (av.hasHiddenRows())
311           {
312             setHiddenFont(s);
313           }
314           else
315           {
316             gg.setFont(idfont);
317           }
318
319           drawIdString(gg, s, i, 0, ypos);
320         }
321
322         if (labels != null && av.showAnnotation)
323         {
324           gg.translate(0, ypos + (alheight * av.charHeight));
325           labels.drawComponent(gg, getWidth());
326           gg.translate(0, -ypos - (alheight * av.charHeight));
327         }
328       }
329     }
330     else
331     {
332       // No need to hang on to labels if we're not wrapped
333       labels = null;
334
335       // Now draw the id strings
336       int panelWidth = getWidth();
337       int xPos = 0;
338
339       SequenceI sequence;
340       // Now draw the id strings
341       for (int i = starty; i < endy; i++)
342       {
343         sequence = av.getAlignment().getSequenceAt(i);
344
345         if (sequence == null)
346         {
347           continue;
348         }
349
350         if (av.hasHiddenRows())
351         {
352           setHiddenFont(sequence);
353         }
354
355         // Selected sequence colours
356         if ((searchResults != null) && searchResults.contains(sequence))
357         {
358           currentColor = Color.black;
359           currentTextColor = Color.white;
360         }
361         else if ((av.getSelectionGroup() != null)
362                 && av.getSelectionGroup().getSequences(null)
363                         .contains(sequence))
364         {
365           currentColor = Color.lightGray;
366           currentTextColor = Color.black;
367         }
368         else
369         {
370           currentColor = av.getSequenceColour(sequence);
371           currentTextColor = Color.black;
372         }
373
374         gg.setColor(currentColor);
375
376         gg.fillRect(0, (i - starty) * av.charHeight, getWidth(),
377                 av.charHeight);
378
379         gg.setColor(currentTextColor);
380
381         String string = sequence.getDisplayId(av.getShowJVSuffix());
382
383         if (av.rightAlignIds)
384         {
385           xPos = panelWidth - fm.stringWidth(string) - 4;
386         }
387
388         gg.drawString(string, xPos,
389                 (((i - starty) * av.charHeight) + av.charHeight)
390                         - (av.charHeight / 5));
391
392         if (av.hasHiddenRows() && av.showHiddenMarkers)
393         {
394           drawMarker(i, starty, 0);
395         }
396
397       }
398
399     }
400   }
401
402   void drawMarker(int i, int starty, int yoffset)
403   {
404
405     SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
406     // Use this method here instead of calling hiddenSeq adjust
407     // 3 times.
408     int hSize = hseqs.length;
409
410     int hiddenIndex = i;
411     int lastIndex = i - 1;
412     int nextIndex = i + 1;
413
414     for (int j = 0; j < hSize; j++)
415     {
416       if (hseqs[j] != null)
417       {
418         if (j - 1 < hiddenIndex)
419         {
420           hiddenIndex++;
421         }
422         if (j - 1 < lastIndex)
423         {
424           lastIndex++;
425         }
426         if (j - 1 < nextIndex)
427         {
428           nextIndex++;
429         }
430       }
431     }
432
433     boolean below = (hiddenIndex > lastIndex + 1);
434     boolean above = (nextIndex > hiddenIndex + 1);
435
436     gg.setColor(Color.blue);
437     if (below)
438     {
439       gg.fillPolygon(
440               new int[]
441               { getWidth() - av.charHeight, getWidth() - av.charHeight,
442                   getWidth() }, new int[]
443               {
444                   (i - starty) * av.charHeight + yoffset,
445                   (i - starty) * av.charHeight + yoffset + av.charHeight
446                           / 4, (i - starty) * av.charHeight + yoffset }, 3);
447     }
448     if (above)
449     {
450       gg.fillPolygon(
451               new int[]
452               { getWidth() - av.charHeight, getWidth() - av.charHeight,
453                   getWidth() }, new int[]
454               {
455                   (i - starty + 1) * av.charHeight + yoffset,
456                   (i - starty + 1) * av.charHeight + yoffset
457                           - av.charHeight / 4,
458                   (i - starty + 1) * av.charHeight + yoffset }, 3);
459
460     }
461   }
462
463   void setHiddenFont(SequenceI seq)
464   {
465     Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
466             .getSize());
467
468     if (av.isHiddenRepSequence(seq))
469     {
470       gg.setFont(bold);
471     }
472     else
473     {
474       gg.setFont(idfont);
475     }
476   }
477
478   /**
479    * DOCUMENT ME!
480    * 
481    * @param list
482    *          DOCUMENT ME!
483    */
484   public void setHighlighted(List<SequenceI> list)
485   {
486     searchResults = list;
487     repaint();
488   }
489 }