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