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