2cb5bcdcdf1cf724ad97853162c568fc0c5f0669
[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.util.ColorUtils;
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(ColorUtils.getColor(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   @Override
209   public void paintComponent(Graphics g)
210   {
211     g.setColor(Color.white);
212     g.fillRect(0, 0, getWidth(), getHeight());
213
214     if (fastPaint)
215     {
216       fastPaint = false;
217       g.drawImage(image, 0, 0, this);
218
219       return;
220     }
221
222     int oldHeight = imgHeight;
223
224     imgHeight = getHeight();
225     imgHeight -= (imgHeight % av.getCharHeight());
226
227     if (imgHeight < 1)
228     {
229       return;
230     }
231
232     if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
233     {
234       image = new BufferedImage(getWidth(), imgHeight,
235               BufferedImage.TYPE_INT_RGB);
236     }
237
238     gg = (Graphics2D) image.getGraphics();
239
240     // Fill in the background
241     gg.setColor(Color.white);
242     gg.fillRect(0, 0, getWidth(), imgHeight);
243
244     drawIds(av.getStartSeq(), av.endSeq);
245
246     g.drawImage(image, 0, 0, this);
247   }
248
249   /**
250    * DOCUMENT ME!
251    * 
252    * @param starty
253    *          DOCUMENT ME!
254    * @param endy
255    *          DOCUMENT ME!
256    */
257   void drawIds(int starty, int endy)
258   {
259     if (av.isSeqNameItalics())
260     {
261       setIdfont(new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
262               .getSize()));
263     }
264     else
265     {
266       setIdfont(av.getFont());
267     }
268
269     gg.setFont(getIdfont());
270     fm = gg.getFontMetrics();
271
272     if (av.antiAlias)
273     {
274       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
275               RenderingHints.VALUE_ANTIALIAS_ON);
276     }
277
278     Color currentColor = Color.white;
279     Color currentTextColor = Color.black;
280
281     final boolean doHiddenCheck = av.isDisplayReferenceSeq()
282             || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows();
283
284     if (av.getWrapAlignment())
285     {
286       int maxwidth = av.getAlignment().getWidth();
287       int alheight = av.getAlignment().getHeight();
288
289       if (av.hasHiddenColumns())
290       {
291         maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
292       }
293
294       int annotationHeight = 0;
295
296       if (av.isShowAnnotation())
297       {
298         if (ap == null)
299         {
300           ap = new AnnotationPanel(av);
301         }
302
303         annotationHeight = ap.adjustPanelHeight();
304         if (labels == null)
305         {
306           labels = new AnnotationLabels(av);
307         }
308       }
309
310       int hgap = av.getCharHeight();
311       if (av.getScaleAboveWrapped())
312       {
313         hgap += av.getCharHeight();
314       }
315
316       int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
317
318       int rowSize = av.getEndRes() - av.getStartRes();
319
320       // Draw the rest of the panels
321       for (int ypos = hgap, row = av.startRes; (ypos <= getHeight())
322               && (row < maxwidth); ypos += cHeight, row += rowSize)
323       {
324         for (int i = starty; i < alheight; i++)
325         {
326           SequenceI s = av.getAlignment().getSequenceAt(i);
327           if (doHiddenCheck)
328           {
329             setHiddenFont(s);
330           }
331           else
332           {
333             gg.setFont(getIdfont());
334           }
335
336           drawIdString(gg, hiddenRows, s, i, 0, ypos);
337         }
338
339         if (labels != null && av.isShowAnnotation())
340         {
341           gg.translate(0, ypos + (alheight * av.getCharHeight()));
342           labels.drawComponent(gg, getWidth());
343           gg.translate(0, -ypos - (alheight * av.getCharHeight()));
344         }
345       }
346     }
347     else
348     {
349       // No need to hang on to labels if we're not wrapped
350       labels = null;
351
352       // Now draw the id strings
353       int panelWidth = getWidth();
354       int xPos = 0;
355
356       SequenceI sequence;
357       // Now draw the id strings
358       for (int i = starty; i < endy; i++)
359       {
360         sequence = av.getAlignment().getSequenceAt(i);
361
362         if (sequence == null)
363         {
364           continue;
365         }
366
367         if (doHiddenCheck)
368         {
369           setHiddenFont(sequence);
370         }
371
372         // Selected sequence colours
373         if ((searchResults != null) && searchResults.contains(sequence))
374         {
375           currentColor = Color.black;
376           currentTextColor = Color.white;
377         }
378         else if ((av.getSelectionGroup() != null)
379                 && av.getSelectionGroup().getSequences(null)
380                         .contains(sequence))
381         {
382           currentColor = Color.lightGray;
383           currentTextColor = Color.black;
384         }
385         else
386         {
387           currentColor = ColorUtils
388                   .getColor(av.getSequenceColour(sequence));
389           currentTextColor = Color.black;
390         }
391
392         gg.setColor(currentColor);
393
394         gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
395                 av.getCharHeight());
396
397         gg.setColor(currentTextColor);
398
399         String string = sequence.getDisplayId(av.getShowJVSuffix());
400
401         if (av.isRightAlignIds())
402         {
403           xPos = panelWidth - fm.stringWidth(string) - 4;
404         }
405
406         gg.drawString(string, xPos,
407                 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
408                         - (av.getCharHeight() / 5));
409
410         if (hiddenRows)
411         {
412           drawMarker(i, starty, 0);
413         }
414
415       }
416
417     }
418   }
419
420   void drawMarker(int i, int starty, int yoffset)
421   {
422
423     SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
424     // Use this method here instead of calling hiddenSeq adjust
425     // 3 times.
426     int hSize = hseqs.length;
427
428     int hiddenIndex = i;
429     int lastIndex = i - 1;
430     int nextIndex = i + 1;
431
432     for (int j = 0; j < hSize; j++)
433     {
434       if (hseqs[j] != null)
435       {
436         if (j - 1 < hiddenIndex)
437         {
438           hiddenIndex++;
439         }
440         if (j - 1 < lastIndex)
441         {
442           lastIndex++;
443         }
444         if (j - 1 < nextIndex)
445         {
446           nextIndex++;
447         }
448       }
449     }
450
451     boolean below = (hiddenIndex > lastIndex + 1);
452     boolean above = (nextIndex > hiddenIndex + 1);
453
454     gg.setColor(Color.blue);
455     if (below)
456     {
457       gg.fillPolygon(
458               new int[] { getWidth() - av.getCharHeight(),
459                   getWidth() - av.getCharHeight(), getWidth() },
460               new int[] {
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[] { getWidth() - av.getCharHeight(),
470                   getWidth() - av.getCharHeight(), getWidth() },
471               new int[] {
472                   (i - starty + 1) * av.getCharHeight() + yoffset,
473                   (i - starty + 1) * av.getCharHeight() + yoffset
474                           - av.getCharHeight() / 4,
475                   (i - starty + 1) * av.getCharHeight() + yoffset }, 3);
476
477     }
478   }
479
480   void setHiddenFont(SequenceI seq)
481   {
482     Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
483             .getSize());
484
485     if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
486     {
487       gg.setFont(bold);
488     }
489     else
490     {
491       gg.setFont(getIdfont());
492     }
493   }
494
495   /**
496    * DOCUMENT ME!
497    * 
498    * @param list
499    *          DOCUMENT ME!
500    */
501   public void setHighlighted(List<SequenceI> list)
502   {
503     searchResults = list;
504     repaint();
505   }
506
507   public Font getIdfont()
508   {
509     return idfont;
510   }
511
512   public void setIdfont(Font idfont)
513   {
514     this.idfont = idfont;
515   }
516 }