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