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