JAL-247 pass idwidth parameter to IdCanvas.drawMarker
[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   AnnotationPanel ap;
67
68   private Font idfont;
69
70   /**
71    * Creates a new IdCanvas object.
72    * 
73    * @param av
74    *          DOCUMENT ME!
75    */
76   public IdCanvas(AlignViewport av)
77   {
78     setLayout(new BorderLayout());
79     this.av = av;
80     PaintRefresher.Register(this, av.getSequenceSetId());
81     av.getRanges().addPropertyChangeListener(this);
82     }
83
84   /**
85    * DOCUMENT ME!
86    * 
87    * @param g
88    *                     DOCUMENT ME!
89    * @param hiddenRows
90    *                     true - check and display hidden row marker if need be
91    * @param s
92    *                     DOCUMENT ME!
93    * @param i
94    *                     DOCUMENT ME!
95    * @param starty
96    *                     DOCUMENT ME!
97    * @param ypos
98    *                     DOCUMENT ME!
99    * @param idWidth
100    */
101   public void drawIdString(Graphics2D g, boolean hiddenRows, SequenceI s,
102           int i, int starty, int ypos, int idWidth)
103   {
104     int xPos = 0;
105     int panelWidth = getWidth();
106     int charHeight = av.getCharHeight();
107
108     if ((searchResults != null) && searchResults.contains(s))
109     {
110       g.setColor(Color.black);
111       g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
112               charHeight);
113       g.setColor(Color.white);
114     }
115     else if ((av.getSelectionGroup() != null)
116             && av.getSelectionGroup().getSequences(null).contains(s))
117     {
118       g.setColor(Color.lightGray);
119       g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
120               charHeight);
121       g.setColor(Color.white);
122     }
123     else
124     {
125       g.setColor(av.getSequenceColour(s));
126       g.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
127               charHeight);
128       g.setColor(Color.black);
129     }
130
131     if (av.isRightAlignIds())
132     {
133       FontMetrics fm = g.getFontMetrics();
134       xPos = panelWidth
135               - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
136     }
137
138     g.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
139             (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
140
141     if (hiddenRows)
142     {
143       drawMarker(g, av, i, starty, ypos, idWidth);
144     }
145
146   }
147
148   /**
149    * DOCUMENT ME!
150    * 
151    * @param vertical
152    *          DOCUMENT ME!
153    */
154   public void fastPaint(int vertical)
155   {
156     /*
157      * for now, not attempting fast paint of wrapped ids...
158      */
159     if (gg == null || av.getWrapAlignment())
160     {
161       repaint();
162
163       return;
164     }
165
166     ViewportRanges ranges = av.getRanges();
167
168     gg.copyArea(0, 0, getWidth(), imgHeight, 0,
169             -vertical * av.getCharHeight());
170
171     int ss = ranges.getStartSeq();
172     int es = ranges.getEndSeq();
173     int transY = 0;
174
175     if (vertical > 0) // scroll down
176     {
177       ss = es - vertical;
178
179       if (ss < ranges.getStartSeq())
180       { // ie scrolling too fast, more than a page at a time
181         ss = ranges.getStartSeq();
182       }
183       else
184       {
185         transY = imgHeight - ((vertical + 1) * av.getCharHeight());
186       }
187     }
188     else if (vertical < 0) // scroll up
189     {
190       es = ss - vertical;
191
192       if (es > ranges.getEndSeq())
193       {
194         es = ranges.getEndSeq();
195       }
196     }
197
198     gg.translate(0, transY);
199
200     drawIds(gg, av, ss, es, searchResults, getWidth());
201
202     gg.translate(0, -transY);
203
204     fastPaint = true;
205
206     // Call repaint on alignment panel so that repaints from other alignment
207     // panel components can be aggregated. Otherwise performance of the overview
208     // window and others may be adversely affected.
209     av.getAlignPanel().repaint();
210   }
211
212   /**
213    * DOCUMENT ME!
214    * 
215    * @param g
216    *          DOCUMENT ME!
217    */
218   @Override
219   public void paintComponent(Graphics g)
220   {
221     super.paintComponent(g);
222
223     g.setColor(Color.white);
224     g.fillRect(0, 0, getWidth(), getHeight());
225     
226     if (fastPaint)
227     {
228       fastPaint = false;
229       g.drawImage(image, 0, 0, this);
230     
231       return;
232     }
233     
234     int oldHeight = imgHeight;
235     
236     imgHeight = getHeight();
237     imgHeight -= (imgHeight % av.getCharHeight());
238     
239     if (imgHeight < 1)
240     {
241       return;
242     }
243     
244     if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
245     {
246         image = new BufferedImage(getWidth(), imgHeight,
247                 BufferedImage.TYPE_INT_RGB);
248     }
249     
250     gg = (Graphics2D) image.getGraphics();
251     
252     // Fill in the background
253     gg.setColor(Color.white);
254     gg.fillRect(0, 0, getWidth(), imgHeight);
255     
256     drawIds(gg, av, av.getRanges().getStartSeq(), av.getRanges().getEndSeq(), searchResults, getWidth());
257     
258     g.drawImage(image, 0, 0, this);
259   }
260
261   /**
262    * Draws sequence ids from sequence index startSeq to endSeq (inclusive), with
263    * the font and other display settings configured on the viewport. Ids of
264    * sequences included in the selection are coloured grey, otherwise the current
265    * id colour for the sequence id is used.
266    * 
267    * @param g
268    * @param alignViewport
269    * @param startSeq
270    * @param endSeq
271    * @param selection
272    * @param idWidth
273    */
274   void drawIds(Graphics2D g, AlignViewport alignViewport, final int startSeq,
275           final int endSeq, List<SequenceI> selection, int idWidth)
276   {
277     Font font = alignViewport.getFont();
278     if (alignViewport.isSeqNameItalics())
279     {
280       setIdfont(new Font(font.getName(), Font.ITALIC,
281               font.getSize()));
282     }
283     else
284     {
285       setIdfont(font);
286     }
287
288     g.setFont(getIdfont());
289     FontMetrics fm = g.getFontMetrics();
290
291     if (alignViewport.antiAlias)
292     {
293       g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
294               RenderingHints.VALUE_ANTIALIAS_ON);
295     }
296
297     Color currentColor = Color.white;
298     Color currentTextColor = Color.black;
299
300     boolean hasHiddenRows = alignViewport.hasHiddenRows();
301
302     if (alignViewport.getWrapAlignment())
303     {
304       drawIdsWrapped(g, alignViewport, startSeq, getHeight(), getWidth());
305       return;
306     }
307
308     // Now draw the id strings
309     int panelWidth = getWidth();
310     int xPos = 0;
311
312     // Now draw the id strings
313     for (int i = startSeq; i <= endSeq; i++)
314     {
315       SequenceI sequence = alignViewport.getAlignment().getSequenceAt(i);
316
317       if (sequence == null)
318       {
319         continue;
320       }
321
322       if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
323       {
324         g.setFont(getHiddenFont(sequence, alignViewport));
325       }
326
327       // Selected sequence colours
328       if (selection != null && selection.contains(sequence))
329       {
330         currentColor = Color.black;
331         currentTextColor = Color.white;
332       }
333       else if ((alignViewport.getSelectionGroup() != null) && alignViewport
334               .getSelectionGroup().getSequences(null).contains(sequence))
335       {
336         currentColor = Color.lightGray;
337         currentTextColor = Color.black;
338       }
339       else
340       {
341         currentColor = alignViewport.getSequenceColour(sequence);
342         currentTextColor = Color.black;
343       }
344
345       g.setColor(currentColor);
346
347       int charHeight = alignViewport.getCharHeight();
348       g.fillRect(0, (i - startSeq) * charHeight,
349               getWidth(), charHeight);
350
351       g.setColor(currentTextColor);
352
353       String string = sequence
354               .getDisplayId(alignViewport.getShowJVSuffix());
355
356       if (alignViewport.isRightAlignIds())
357       {
358         xPos = panelWidth - fm.stringWidth(string) - 4;
359       }
360
361       g.drawString(string, xPos, (((i - startSeq) * charHeight) + charHeight)
362               - (charHeight / 5));
363
364       if (hasHiddenRows)
365       {
366         drawMarker(g, alignViewport, i, startSeq, 0, idWidth);
367       }
368     }
369   }
370
371   /**
372    * Draws sequence ids, and annotation labels if annotations are shown, in
373    * wrapped mode
374    * 
375    * @param g
376    * @param alignViewport
377    * @param startSeq
378    * @param idWidth
379    */
380   void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
381           int startSeq, int pageHeight, int idWidth)
382   {
383     int alignmentWidth = alignViewport.getAlignment().getWidth();
384     final int alheight = alignViewport.getAlignment().getHeight();
385
386     /*
387      * assumption: SeqCanvas.calculateWrappedGeometry has been called
388      */
389     SeqCanvas seqCanvas = alignViewport.getAlignPanel()
390             .getSeqPanel().seqCanvas;
391
392     final int charHeight = alignViewport.getCharHeight();
393
394     AnnotationLabels labels = null;
395     if (alignViewport.isShowAnnotation())
396     {
397       labels = new AnnotationLabels(alignViewport);
398     }
399
400     ViewportRanges ranges = alignViewport.getRanges();
401
402     int rowSize = ranges.getViewportWidth();
403
404     /*
405      * draw repeating sequence ids until out of sequence data or
406      * out of visible space, whichever comes first
407      */
408     boolean hasHiddenRows = alignViewport.hasHiddenRows();
409     int ypos = seqCanvas.wrappedSpaceAboveAlignment;
410     int rowStartRes = ranges.getStartRes();
411     while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
412     {
413       for (int i = startSeq; i < alheight; i++)
414       {
415         SequenceI s = alignViewport.getAlignment().getSequenceAt(i);
416         if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
417         {
418           g.setFont(getHiddenFont(s, alignViewport));
419         }
420         else
421         {
422           g.setFont(getIdfont());
423         }
424         drawIdString(g, hasHiddenRows, s, i, 0, ypos, idWidth);
425       }
426
427       if (labels != null && alignViewport.isShowAnnotation())
428       {
429         g.translate(0, ypos + (alheight * charHeight));
430         labels.drawComponent(g, getWidth());
431         g.translate(0, -ypos - (alheight * charHeight));
432       }
433
434       ypos += seqCanvas.wrappedRepeatHeightPx;
435       rowStartRes += rowSize;
436     }
437   }
438
439   /**
440    * Draws a marker (a blue right-pointing triangle) between sequences to indicate
441    * hidden sequences.
442    * 
443    * @param g
444    * @param alignViewport
445    * @param seqIndex
446    * @param starty
447    * @param yoffset
448    * @param idWidth
449    */
450   void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex, int starty, int yoffset, int idWidth)
451   {
452     SequenceI[] hseqs = alignViewport.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 = seqIndex;
459     int lastIndex = seqIndex - 1;
460     int nextIndex = seqIndex + 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     /*
482      * are we below or above the hidden sequences?
483      */
484     boolean below = (hiddenIndex > lastIndex + 1);
485     boolean above = (nextIndex > hiddenIndex + 1);
486
487     g.setColor(Color.blue);
488     int charHeight = av.getCharHeight();
489
490     /*
491      * vertices of the triangle, below or above hidden seqs
492      */
493     int[] xPoints = new int[] { idWidth - charHeight, idWidth - charHeight,
494         idWidth };
495     int yShift = seqIndex - starty;
496
497     if (below)
498     {
499       int[] yPoints = new int[] { yShift * charHeight + yoffset,
500           yShift * charHeight + yoffset + charHeight / 4,
501           yShift * charHeight + yoffset };
502       g.fillPolygon(xPoints, yPoints, 3);
503     }
504     if (above)
505     {
506       yShift++;
507       int[] yPoints = new int[] { yShift * charHeight + yoffset,
508           yShift * charHeight + yoffset - charHeight / 4,
509           yShift * charHeight + yoffset };
510       g.fillPolygon(xPoints, yPoints, 3);
511     }
512   }
513
514   /**
515    * Answers the standard sequence id font, or a bold font if the sequence is
516    * set as reference or a hidden group representative
517    * 
518    * @param seq
519    * @param alignViewport
520    * @return
521    */
522   private Font getHiddenFont(SequenceI seq, AlignViewport alignViewport)
523   {
524     if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
525     {
526       return new Font(av.getFont().getName(), Font.BOLD,
527               av.getFont().getSize());
528     }
529     return getIdfont();
530   }
531
532   /**
533    * DOCUMENT ME!
534    * 
535    * @param list
536    *          DOCUMENT ME!
537    */
538   public void setHighlighted(List<SequenceI> list)
539   {
540     searchResults = list;
541     repaint();
542   }
543
544   public Font getIdfont()
545   {
546     return idfont;
547   }
548
549   public void setIdfont(Font idfont)
550   {
551     this.idfont = idfont;
552   }
553
554   /**
555    * Respond to viewport range changes (e.g. alignment panel was scrolled). Both
556    * scrolling and resizing change viewport ranges. Scrolling changes both start
557    * and end points, but resize only changes end values. Here we only want to
558    * fastpaint on a scroll, with resize using a normal paint, so scroll events
559    * are identified as changes to the horizontal or vertical start value.
560    * <p>
561    * In unwrapped mode, only responds to a vertical scroll, as horizontal scroll
562    * leaves sequence ids unchanged. In wrapped mode, only vertical scroll is
563    * provided, but it generates a change of "startres" which does require an
564    * update here.
565    */
566   @Override
567   public void propertyChange(PropertyChangeEvent evt)
568   {
569     String propertyName = evt.getPropertyName();
570     if (propertyName.equals(ViewportRanges.STARTSEQ)
571             || (av.getWrapAlignment()
572                     && propertyName.equals(ViewportRanges.STARTRES)))
573     {
574       fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
575     }
576     else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
577     {
578       fastPaint(((int[]) evt.getNewValue())[1]
579               - ((int[]) evt.getOldValue())[1]);
580     }
581     else if (propertyName.equals(ViewportRanges.MOVE_VIEWPORT))
582     {
583       repaint();
584     }
585   }
586 }