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