JAL-2778 Changed local repaint to alignment repaint for fastpaints
[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     /*
159      * for now, not attempting fast paint of wrapped ids...
160      */
161     if (gg == null || av.getWrapAlignment())
162     {
163       repaint();
164
165       return;
166     }
167
168     ViewportRanges ranges = av.getRanges();
169
170     gg.copyArea(0, 0, getWidth(), imgHeight, 0,
171             -vertical * av.getCharHeight());
172
173     int ss = ranges.getStartSeq();
174     int es = ranges.getEndSeq();
175     int transY = 0;
176
177     if (vertical > 0) // scroll down
178     {
179       ss = es - vertical;
180
181       if (ss < ranges.getStartSeq())
182       { // ie scrolling too fast, more than a page at a time
183         ss = ranges.getStartSeq();
184       }
185       else
186       {
187         transY = imgHeight - ((vertical + 1) * av.getCharHeight());
188       }
189     }
190     else if (vertical < 0) // scroll up
191     {
192       es = ss - vertical;
193
194       if (es > ranges.getEndSeq())
195       {
196         es = ranges.getEndSeq();
197       }
198     }
199
200     gg.translate(0, transY);
201
202     drawIds(ss, es);
203
204     gg.translate(0, -transY);
205
206     fastPaint = true;
207
208     av.getAlignPanel().alignFrame.repaint();
209   }
210
211   /**
212    * DOCUMENT ME!
213    * 
214    * @param g
215    *          DOCUMENT ME!
216    */
217   @Override
218   public void paintComponent(Graphics g)
219   {
220     super.paintComponent(g);
221
222     g.setColor(Color.white);
223     g.fillRect(0, 0, getWidth(), getHeight());
224     
225     if (fastPaint)
226     {
227       fastPaint = false;
228       g.drawImage(image, 0, 0, this);
229     
230       return;
231     }
232     
233     int oldHeight = imgHeight;
234     
235     imgHeight = getHeight();
236     imgHeight -= (imgHeight % av.getCharHeight());
237     
238     if (imgHeight < 1)
239     {
240       return;
241     }
242     
243     if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
244     {
245       image = new BufferedImage(getWidth(), imgHeight,
246               BufferedImage.TYPE_INT_RGB);
247     }
248     
249     gg = (Graphics2D) image.getGraphics();
250     
251     // Fill in the background
252     gg.setColor(Color.white);
253     gg.fillRect(0, 0, getWidth(), imgHeight);
254     
255     drawIds(av.getRanges().getStartSeq(), av.getRanges().getEndSeq());
256     
257     g.drawImage(image, 0, 0, this);
258   }
259
260   /**
261    * DOCUMENT ME!
262    * 
263    * @param starty
264    *          DOCUMENT ME!
265    * @param endy
266    *          DOCUMENT ME!
267    */
268   void drawIds(int starty, int endy)
269   {
270     if (av.isSeqNameItalics())
271     {
272       setIdfont(new Font(av.getFont().getName(), Font.ITALIC,
273               av.getFont().getSize()));
274     }
275     else
276     {
277       setIdfont(av.getFont());
278     }
279
280     gg.setFont(getIdfont());
281     fm = gg.getFontMetrics();
282
283     if (av.antiAlias)
284     {
285       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
286               RenderingHints.VALUE_ANTIALIAS_ON);
287     }
288
289     Color currentColor = Color.white;
290     Color currentTextColor = Color.black;
291
292     boolean hasHiddenRows = av.hasHiddenRows();
293
294     if (av.getWrapAlignment())
295     {
296       drawIdsWrapped(starty, hasHiddenRows);
297       return;
298     }
299
300     // No need to hang on to labels if we're not wrapped
301     labels = null;
302
303     // Now draw the id strings
304     int panelWidth = getWidth();
305     int xPos = 0;
306
307     SequenceI sequence;
308     // Now draw the id strings
309     for (int i = starty; i <= endy; i++)
310     {
311       sequence = av.getAlignment().getSequenceAt(i);
312
313       if (sequence == null)
314       {
315         continue;
316       }
317
318       if (hasHiddenRows || av.isDisplayReferenceSeq())
319       {
320         setHiddenFont(sequence);
321       }
322
323       // Selected sequence colours
324       if ((searchResults != null) && searchResults.contains(sequence))
325       {
326         currentColor = Color.black;
327         currentTextColor = Color.white;
328       }
329       else if ((av.getSelectionGroup() != null) && av.getSelectionGroup()
330               .getSequences(null).contains(sequence))
331       {
332         currentColor = Color.lightGray;
333         currentTextColor = Color.black;
334       }
335       else
336       {
337         currentColor = av.getSequenceColour(sequence);
338         currentTextColor = Color.black;
339       }
340
341       gg.setColor(currentColor);
342
343       gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
344               av.getCharHeight());
345
346       gg.setColor(currentTextColor);
347
348       String string = sequence.getDisplayId(av.getShowJVSuffix());
349
350       if (av.isRightAlignIds())
351       {
352         xPos = panelWidth - fm.stringWidth(string) - 4;
353       }
354
355       gg.drawString(string, xPos,
356               (((i - starty) * av.getCharHeight()) + av.getCharHeight())
357                       - (av.getCharHeight() / 5));
358
359       if (hasHiddenRows)
360       {
361         drawMarker(i, starty, 0);
362       }
363     }
364   }
365
366   /**
367    * Draws sequence ids in wrapped mode
368    * 
369    * @param starty
370    * @param hasHiddenRows
371    */
372   protected void drawIdsWrapped(int starty, boolean hasHiddenRows)
373   {
374     int maxwidth = av.getAlignment().getWidth();
375     int alheight = av.getAlignment().getHeight();
376
377     if (av.hasHiddenColumns())
378     {
379       maxwidth = av.getAlignment().getHiddenColumns()
380               .findColumnPosition(maxwidth) - 1;
381     }
382
383     int annotationHeight = 0;
384
385     if (av.isShowAnnotation())
386     {
387       if (ap == null)
388       {
389         ap = new AnnotationPanel(av);
390       }
391
392       annotationHeight = ap.adjustPanelHeight();
393       if (labels == null)
394       {
395         labels = new AnnotationLabels(av);
396       }
397     }
398
399     int hgap = av.getCharHeight();
400     if (av.getScaleAboveWrapped())
401     {
402       hgap += av.getCharHeight();
403     }
404
405     int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
406
407     ViewportRanges ranges = av.getRanges();
408
409     int rowSize = ranges.getViewportWidth();
410
411     /*
412      * draw repeating sequence ids until out of sequence data or
413      * out of visible space, whichever comes first
414      */
415     int ypos = hgap;
416     int row = ranges.getStartRes();
417     while ((ypos <= getHeight()) && (row < maxwidth))
418     {
419       for (int i = starty; i < alheight; i++)
420       {
421         SequenceI s = av.getAlignment().getSequenceAt(i);
422         if (hasHiddenRows || av.isDisplayReferenceSeq())
423         {
424           setHiddenFont(s);
425         }
426         else
427         {
428           gg.setFont(getIdfont());
429         }
430
431         drawIdString(gg, hasHiddenRows, s, i, 0, ypos);
432       }
433
434       if (labels != null && av.isShowAnnotation())
435       {
436         gg.translate(0, ypos + (alheight * av.getCharHeight()));
437         labels.drawComponent(gg, getWidth());
438         gg.translate(0, -ypos - (alheight * av.getCharHeight()));
439       }
440
441       ypos += cHeight;
442       row += rowSize;
443     }
444   }
445
446   void drawMarker(int i, int starty, int yoffset)
447   {
448
449     SequenceI[] hseqs = av.getAlignment()
450             .getHiddenSequences().hiddenSequences;
451     // Use this method here instead of calling hiddenSeq adjust
452     // 3 times.
453     int hSize = hseqs.length;
454
455     int hiddenIndex = i;
456     int lastIndex = i - 1;
457     int nextIndex = i + 1;
458
459     for (int j = 0; j < hSize; j++)
460     {
461       if (hseqs[j] != null)
462       {
463         if (j - 1 < hiddenIndex)
464         {
465           hiddenIndex++;
466         }
467         if (j - 1 < lastIndex)
468         {
469           lastIndex++;
470         }
471         if (j - 1 < nextIndex)
472         {
473           nextIndex++;
474         }
475       }
476     }
477
478     boolean below = (hiddenIndex > lastIndex + 1);
479     boolean above = (nextIndex > hiddenIndex + 1);
480
481     gg.setColor(Color.blue);
482     if (below)
483     {
484       gg.fillPolygon(
485               new int[]
486               { getWidth() - av.getCharHeight(),
487                   getWidth() - av.getCharHeight(), getWidth() },
488               new int[]
489               { (i - starty) * av.getCharHeight() + yoffset,
490                   (i - starty) * av.getCharHeight() + yoffset
491                           + av.getCharHeight() / 4,
492                   (i - starty) * av.getCharHeight() + yoffset },
493               3);
494     }
495     if (above)
496     {
497       gg.fillPolygon(
498               new int[]
499               { getWidth() - av.getCharHeight(),
500                   getWidth() - av.getCharHeight(), getWidth() },
501               new int[]
502               { (i - starty + 1) * av.getCharHeight() + yoffset,
503                   (i - starty + 1) * av.getCharHeight() + yoffset
504                           - av.getCharHeight() / 4,
505                   (i - starty + 1) * av.getCharHeight() + yoffset },
506               3);
507
508     }
509   }
510
511   void setHiddenFont(SequenceI seq)
512   {
513     Font bold = new Font(av.getFont().getName(), Font.BOLD,
514             av.getFont().getSize());
515
516     if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
517     {
518       gg.setFont(bold);
519     }
520     else
521     {
522       gg.setFont(getIdfont());
523     }
524   }
525
526   /**
527    * DOCUMENT ME!
528    * 
529    * @param list
530    *          DOCUMENT ME!
531    */
532   public void setHighlighted(List<SequenceI> list)
533   {
534     searchResults = list;
535     repaint();
536   }
537
538   public Font getIdfont()
539   {
540     return idfont;
541   }
542
543   public void setIdfont(Font idfont)
544   {
545     this.idfont = idfont;
546   }
547
548   /**
549    * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
550    * scrolling and resizing change viewport ranges. Scrolling changes both start
551    * and end points, but resize only changes end values. Here we only want to
552    * fastpaint on a scroll, with resize using a normal paint, so scroll events
553    * are identified as changes to the horizontal or vertical start value.
554    * <p>
555    * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
556    * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
557    * provided, but it generates a change of "startres" which does require an
558    * update here.
559    */
560   @Override
561   public void propertyChange(PropertyChangeEvent evt)
562   {
563     String propertyName = evt.getPropertyName();
564     if (propertyName.equals(ViewportRanges.STARTSEQ)
565             || (av.getWrapAlignment()
566                     && propertyName.equals(ViewportRanges.STARTRES)))
567     {
568       fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
569     }
570     else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
571     {
572       fastPaint(((int[]) evt.getNewValue())[1]
573               - ((int[]) evt.getOldValue())[1]);
574     }
575   }
576 }