update author list in license for (JAL-826)
[jalview.git] / src / jalview / gui / IdCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import javax.swing.*;
23
24 import jalview.datamodel.*;
25
26 /**
27  * DOCUMENT ME!
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class IdCanvas extends JPanel
33 {
34   protected AlignViewport av;
35
36   protected boolean showScores = true;
37
38   protected int maxIdLength = -1;
39
40   protected String maxIdStr = null;
41
42   BufferedImage image;
43
44   Graphics2D gg;
45
46   int imgHeight = 0;
47
48   boolean fastPaint = false;
49
50   java.util.Vector searchResults;
51
52   FontMetrics fm;
53
54   AnnotationLabels labels = null;
55
56   AnnotationPanel ap;
57
58   Font idfont;
59
60   /**
61    * Creates a new IdCanvas object.
62    * 
63    * @param av
64    *          DOCUMENT ME!
65    */
66   public IdCanvas(AlignViewport av)
67   {
68     setLayout(new BorderLayout());
69     this.av = av;
70     PaintRefresher.Register(this, av.getSequenceSetId());
71   }
72
73   /**
74    * DOCUMENT ME!
75    * 
76    * @param gg
77    *          DOCUMENT ME!
78    * @param s
79    *          DOCUMENT ME!
80    * @param i
81    *          DOCUMENT ME!
82    * @param starty
83    *          DOCUMENT ME!
84    * @param ypos
85    *          DOCUMENT ME!
86    */
87   public void drawIdString(Graphics2D gg, SequenceI s, int i, int starty,
88           int ypos)
89   {
90     int xPos = 0;
91     int panelWidth = getWidth();
92     int charHeight = av.charHeight;
93
94     if ((searchResults != null) && searchResults.contains(s))
95     {
96       gg.setColor(Color.black);
97       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
98               charHeight);
99       gg.setColor(Color.white);
100     }
101     else if ((av.getSelectionGroup() != null)
102             && av.getSelectionGroup().getSequences(null).contains(s))
103     {
104       gg.setColor(Color.lightGray);
105       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
106               charHeight);
107       gg.setColor(Color.white);
108     }
109     else
110     {
111       gg.setColor(av.getSequenceColour(s));
112       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
113               charHeight);
114       gg.setColor(Color.black);
115     }
116
117     if (av.rightAlignIds)
118     {
119       xPos = panelWidth
120               - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
121     }
122
123     gg.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
124             (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
125
126     if (av.hasHiddenRows && av.showHiddenMarkers)
127     {
128       drawMarker(i, starty, ypos);
129     }
130
131   }
132
133   /**
134    * DOCUMENT ME!
135    * 
136    * @param vertical
137    *          DOCUMENT ME!
138    */
139   public void fastPaint(int vertical)
140   {
141     if (gg == null)
142     {
143       repaint();
144
145       return;
146     }
147
148     gg.copyArea(0, 0, getWidth(), imgHeight, 0, -vertical * av.charHeight);
149
150     int ss = av.startSeq;
151     int es = av.endSeq;
152     int transY = 0;
153
154     if (vertical > 0) // scroll down
155     {
156       ss = es - vertical;
157
158       if (ss < av.startSeq)
159       { // ie scrolling too fast, more than a page at a time
160         ss = av.startSeq;
161       }
162       else
163       {
164         transY = imgHeight - (vertical * av.charHeight);
165       }
166     }
167     else if (vertical < 0)
168     {
169       es = ss - vertical;
170
171       if (es > av.endSeq)
172       {
173         es = av.endSeq;
174       }
175     }
176
177     gg.translate(0, transY);
178
179     drawIds(ss, es);
180
181     gg.translate(0, -transY);
182
183     fastPaint = true;
184     repaint();
185   }
186
187   /**
188    * DOCUMENT ME!
189    * 
190    * @param g
191    *          DOCUMENT ME!
192    */
193   public void paintComponent(Graphics g)
194   {
195     g.setColor(Color.white);
196     g.fillRect(0, 0, getWidth(), getHeight());
197
198     if (fastPaint)
199     {
200       fastPaint = false;
201       g.drawImage(image, 0, 0, this);
202
203       return;
204     }
205
206     int oldHeight = imgHeight;
207
208     imgHeight = getHeight();
209     imgHeight -= (imgHeight % av.charHeight);
210
211     if (imgHeight < 1)
212     {
213       return;
214     }
215
216     if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
217     {
218       image = new BufferedImage(getWidth(), imgHeight,
219               BufferedImage.TYPE_INT_RGB);
220     }
221
222     gg = (Graphics2D) image.getGraphics();
223
224     // Fill in the background
225     gg.setColor(Color.white);
226     gg.fillRect(0, 0, getWidth(), imgHeight);
227
228     drawIds(av.getStartSeq(), av.endSeq);
229
230     g.drawImage(image, 0, 0, this);
231   }
232
233   /**
234    * DOCUMENT ME!
235    * 
236    * @param starty
237    *          DOCUMENT ME!
238    * @param endy
239    *          DOCUMENT ME!
240    */
241   void drawIds(int starty, int endy)
242   {
243     if (av.seqNameItalics)
244     {
245       idfont = new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
246               .getSize());
247     }
248     else
249     {
250       idfont = av.getFont();
251     }
252
253     gg.setFont(idfont);
254     fm = gg.getFontMetrics();
255
256     if (av.antiAlias)
257     {
258       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
259               RenderingHints.VALUE_ANTIALIAS_ON);
260     }
261
262     Color currentColor = Color.white;
263     Color currentTextColor = Color.black;
264
265     if (av.getWrapAlignment())
266     {
267       int maxwidth = av.alignment.getWidth();
268       int alheight = av.alignment.getHeight();
269
270       if (av.hasHiddenColumns)
271       {
272         maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
273       }
274
275       int annotationHeight = 0;
276
277       if (av.showAnnotation)
278       {
279         if (ap == null)
280         {
281           ap = new AnnotationPanel(av);
282         }
283
284         annotationHeight = ap.adjustPanelHeight();
285         if (labels == null)
286         {
287           labels = new AnnotationLabels(av);
288         }
289       }
290
291       int hgap = av.charHeight;
292       if (av.scaleAboveWrapped)
293       {
294         hgap += av.charHeight;
295       }
296
297       int cHeight = alheight * av.charHeight + hgap + annotationHeight;
298
299       int rowSize = av.getEndRes() - av.getStartRes();
300
301       // Draw the rest of the panels
302       for (int ypos = hgap, row = av.startRes; (ypos <= getHeight())
303               && (row < maxwidth); ypos += cHeight, row += rowSize)
304       {
305         for (int i = starty; i < alheight; i++)
306         {
307           SequenceI s = av.alignment.getSequenceAt(i);
308           if (av.hasHiddenRows)
309           {
310             setHiddenFont(s);
311           }
312           else
313           {
314             gg.setFont(idfont);
315           }
316
317           drawIdString(gg, s, i, 0, ypos);
318         }
319
320         if (labels != null && av.showAnnotation)
321         {
322           gg.translate(0, ypos + (alheight * av.charHeight));
323           labels.drawComponent(gg, getWidth());
324           gg.translate(0, -ypos - (alheight * av.charHeight));
325         }
326       }
327     }
328     else
329     {
330       // No need to hang on to labels if we're not wrapped
331       labels = null;
332
333       // Now draw the id strings
334       int panelWidth = getWidth();
335       int xPos = 0;
336
337       SequenceI sequence;
338       // Now draw the id strings
339       for (int i = starty; i < endy; i++)
340       {
341         sequence = av.alignment.getSequenceAt(i);
342
343         if (sequence == null)
344         {
345           continue;
346         }
347
348         if (av.hasHiddenRows)
349         {
350           setHiddenFont(sequence);
351         }
352
353         // Selected sequence colours
354         if ((searchResults != null) && searchResults.contains(sequence))
355         {
356           currentColor = Color.black;
357           currentTextColor = Color.white;
358         }
359         else if ((av.getSelectionGroup() != null)
360                 && av.getSelectionGroup().getSequences(null)
361                         .contains(sequence))
362         {
363           currentColor = Color.lightGray;
364           currentTextColor = Color.black;
365         }
366         else
367         {
368           currentColor = av.getSequenceColour(sequence);
369           currentTextColor = Color.black;
370         }
371
372         gg.setColor(currentColor);
373
374         gg.fillRect(0, (i - starty) * av.charHeight, getWidth(),
375                 av.charHeight);
376
377         gg.setColor(currentTextColor);
378
379         String string = sequence.getDisplayId(av.getShowJVSuffix());
380
381         if (av.rightAlignIds)
382         {
383           xPos = panelWidth - fm.stringWidth(string) - 4;
384         }
385
386         gg.drawString(string, xPos,
387                 (((i - starty) * av.charHeight) + av.charHeight)
388                         - (av.charHeight / 5));
389
390         if (av.hasHiddenRows && av.showHiddenMarkers)
391         {
392           drawMarker(i, starty, 0);
393         }
394
395       }
396
397     }
398   }
399
400   void drawMarker(int i, int starty, int yoffset)
401   {
402
403     SequenceI[] hseqs = av.alignment.getHiddenSequences().hiddenSequences;
404     // Use this method here instead of calling hiddenSeq adjust
405     // 3 times.
406     int hSize = hseqs.length;
407
408     int hiddenIndex = i;
409     int lastIndex = i - 1;
410     int nextIndex = i + 1;
411
412     for (int j = 0; j < hSize; j++)
413     {
414       if (hseqs[j] != null)
415       {
416         if (j - 1 < hiddenIndex)
417         {
418           hiddenIndex++;
419         }
420         if (j - 1 < lastIndex)
421         {
422           lastIndex++;
423         }
424         if (j - 1 < nextIndex)
425         {
426           nextIndex++;
427         }
428       }
429     }
430
431     boolean below = (hiddenIndex > lastIndex + 1);
432     boolean above = (nextIndex > hiddenIndex + 1);
433
434     gg.setColor(Color.blue);
435     if (below)
436     {
437       gg.fillPolygon(
438               new int[]
439               { getWidth() - av.charHeight, getWidth() - av.charHeight,
440                   getWidth() }, new int[]
441               {
442                   (i - starty) * av.charHeight + yoffset,
443                   (i - starty) * av.charHeight + yoffset + av.charHeight
444                           / 4, (i - starty) * av.charHeight + yoffset }, 3);
445     }
446     if (above)
447     {
448       gg.fillPolygon(
449               new int[]
450               { getWidth() - av.charHeight, getWidth() - av.charHeight,
451                   getWidth() }, new int[]
452               {
453                   (i - starty + 1) * av.charHeight + yoffset,
454                   (i - starty + 1) * av.charHeight + yoffset
455                           - av.charHeight / 4,
456                   (i - starty + 1) * av.charHeight + yoffset }, 3);
457
458     }
459   }
460
461   void setHiddenFont(SequenceI seq)
462   {
463     Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
464             .getSize());
465
466     if (av.hiddenRepSequences != null
467             && av.hiddenRepSequences.containsKey(seq))
468     {
469       gg.setFont(bold);
470     }
471     else
472     {
473       gg.setFont(idfont);
474     }
475   }
476
477   /**
478    * DOCUMENT ME!
479    * 
480    * @param found
481    *          DOCUMENT ME!
482    */
483   public void setHighlighted(java.util.Vector found)
484   {
485     searchResults = found;
486     repaint();
487   }
488 }