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