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