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