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