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