JAL-2600 Add in IdCanvas and checks for 0 size images
[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, boolean isresize)
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 (isresize)
175     {
176       if (vertical != 0)
177       {
178         transY = imgHeight - ((vertical + 1) * av.getCharHeight());
179         ss = es - vertical - ss;
180       }
181     }
182     else
183     {
184       if (vertical > 0) // scroll down
185       {
186         ss = es - vertical;
187
188         if (ss < ranges.getStartSeq())
189         { // ie scrolling too fast, more than a page at a time
190           ss = ranges.getStartSeq();
191         }
192         else
193         {
194           transY = imgHeight - ((vertical + 1) * av.getCharHeight());
195         }
196       }
197       else if (vertical < 0) // scroll up
198       {
199         es = ss - vertical;
200
201         if (es > ranges.getEndSeq())
202         {
203           es = ranges.getEndSeq();
204         }
205       }
206     }
207
208     gg.translate(0, transY);
209
210     drawIds(ss, es);
211
212     gg.translate(0, -transY);
213
214     fastPaint = true;
215     repaint();
216   }
217
218   /**
219    * DOCUMENT ME!
220    * 
221    * @param g
222    *          DOCUMENT ME!
223    */
224   @Override
225   public void paintComponent(Graphics g)
226   {
227     g.setColor(Color.white);
228     g.fillRect(0, 0, getWidth(), getHeight());
229
230     if (fastPaint)
231     {
232       fastPaint = false;
233       g.drawImage(image, 0, 0, this);
234
235       return;
236     }
237
238     int oldHeight = imgHeight;
239
240     imgHeight = getHeight();
241     imgHeight -= (imgHeight % av.getCharHeight());
242
243     if (imgHeight < 1)
244     {
245       return;
246     }
247
248     if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
249     {
250       image = new BufferedImage(getWidth(), imgHeight,
251               BufferedImage.TYPE_INT_RGB);
252     }
253
254     gg = (Graphics2D) image.getGraphics();
255
256     // Fill in the background
257     gg.setColor(Color.white);
258     gg.fillRect(0, 0, getWidth(), imgHeight);
259
260     drawIds(av.getRanges().getStartSeq(), av.getRanges().getEndSeq());
261
262     g.drawImage(image, 0, 0, this);
263   }
264
265   /**
266    * DOCUMENT ME!
267    * 
268    * @param starty
269    *          DOCUMENT ME!
270    * @param endy
271    *          DOCUMENT ME!
272    */
273   void drawIds(int starty, int endy)
274   {
275     if (av.isSeqNameItalics())
276     {
277       setIdfont(new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
278               .getSize()));
279     }
280     else
281     {
282       setIdfont(av.getFont());
283     }
284
285     gg.setFont(getIdfont());
286     fm = gg.getFontMetrics();
287
288     if (av.antiAlias)
289     {
290       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
291               RenderingHints.VALUE_ANTIALIAS_ON);
292     }
293
294     Color currentColor = Color.white;
295     Color currentTextColor = Color.black;
296
297     final boolean doHiddenCheck = av.isDisplayReferenceSeq()
298             || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows();
299
300     if (av.getWrapAlignment())
301     {
302       int maxwidth = av.getAlignment().getWidth();
303       int alheight = av.getAlignment().getHeight();
304
305       if (av.hasHiddenColumns())
306       {
307         maxwidth = av.getAlignment().getHiddenColumns()
308                 .findColumnPosition(maxwidth) - 1;
309       }
310
311       int annotationHeight = 0;
312
313       if (av.isShowAnnotation())
314       {
315         if (ap == null)
316         {
317           ap = new AnnotationPanel(av);
318         }
319
320         annotationHeight = ap.adjustPanelHeight();
321         if (labels == null)
322         {
323           labels = new AnnotationLabels(av);
324         }
325       }
326
327       int hgap = av.getCharHeight();
328       if (av.getScaleAboveWrapped())
329       {
330         hgap += av.getCharHeight();
331       }
332
333       int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
334
335       int rowSize = av.getRanges().getEndRes()
336               - av.getRanges().getStartRes();
337
338       // Draw the rest of the panels
339       for (int ypos = hgap, row = av.getRanges().getStartRes(); (ypos <= getHeight())
340               && (row < maxwidth); ypos += cHeight, row += rowSize)
341       {
342         for (int i = starty; i < alheight; i++)
343         {
344           SequenceI s = av.getAlignment().getSequenceAt(i);
345           if (doHiddenCheck)
346           {
347             setHiddenFont(s);
348           }
349           else
350           {
351             gg.setFont(getIdfont());
352           }
353
354           drawIdString(gg, hiddenRows, s, i, 0, ypos);
355         }
356
357         if (labels != null && av.isShowAnnotation())
358         {
359           gg.translate(0, ypos + (alheight * av.getCharHeight()));
360           labels.drawComponent(gg, getWidth());
361           gg.translate(0, -ypos - (alheight * av.getCharHeight()));
362         }
363       }
364     }
365     else
366     {
367       // No need to hang on to labels if we're not wrapped
368       labels = null;
369
370       // Now draw the id strings
371       int panelWidth = getWidth();
372       int xPos = 0;
373
374       SequenceI sequence;
375       // Now draw the id strings
376       for (int i = starty; i <= endy; i++)
377       {
378         sequence = av.getAlignment().getSequenceAt(i);
379
380         if (sequence == null)
381         {
382           continue;
383         }
384
385         if (doHiddenCheck)
386         {
387           setHiddenFont(sequence);
388         }
389
390         // Selected sequence colours
391         if ((searchResults != null) && searchResults.contains(sequence))
392         {
393           currentColor = Color.black;
394           currentTextColor = Color.white;
395         }
396         else if ((av.getSelectionGroup() != null)
397                 && av.getSelectionGroup().getSequences(null)
398                         .contains(sequence))
399         {
400           currentColor = Color.lightGray;
401           currentTextColor = Color.black;
402         }
403         else
404         {
405           currentColor = av.getSequenceColour(sequence);
406           currentTextColor = Color.black;
407         }
408
409         gg.setColor(currentColor);
410
411         gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
412                 av.getCharHeight());
413
414         gg.setColor(currentTextColor);
415
416         String string = sequence.getDisplayId(av.getShowJVSuffix());
417
418         if (av.isRightAlignIds())
419         {
420           xPos = panelWidth - fm.stringWidth(string) - 4;
421         }
422
423         gg.drawString(string, xPos,
424                 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
425                         - (av.getCharHeight() / 5));
426
427         if (hiddenRows)
428         {
429           drawMarker(i, starty, 0);
430         }
431
432       }
433
434     }
435   }
436
437   void drawMarker(int i, int starty, int yoffset)
438   {
439
440     SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
441     // Use this method here instead of calling hiddenSeq adjust
442     // 3 times.
443     int hSize = hseqs.length;
444
445     int hiddenIndex = i;
446     int lastIndex = i - 1;
447     int nextIndex = i + 1;
448
449     for (int j = 0; j < hSize; j++)
450     {
451       if (hseqs[j] != null)
452       {
453         if (j - 1 < hiddenIndex)
454         {
455           hiddenIndex++;
456         }
457         if (j - 1 < lastIndex)
458         {
459           lastIndex++;
460         }
461         if (j - 1 < nextIndex)
462         {
463           nextIndex++;
464         }
465       }
466     }
467
468     boolean below = (hiddenIndex > lastIndex + 1);
469     boolean above = (nextIndex > hiddenIndex + 1);
470
471     gg.setColor(Color.blue);
472     if (below)
473     {
474       gg.fillPolygon(
475               new int[] { getWidth() - av.getCharHeight(),
476                   getWidth() - av.getCharHeight(), getWidth() },
477               new int[] {
478                   (i - starty) * av.getCharHeight() + yoffset,
479                   (i - starty) * av.getCharHeight() + yoffset
480                           + av.getCharHeight() / 4,
481                   (i - starty) * av.getCharHeight() + yoffset }, 3);
482     }
483     if (above)
484     {
485       gg.fillPolygon(
486               new int[] { getWidth() - av.getCharHeight(),
487                   getWidth() - av.getCharHeight(), getWidth() },
488               new int[] {
489                   (i - starty + 1) * av.getCharHeight() + yoffset,
490                   (i - starty + 1) * av.getCharHeight() + yoffset
491                           - av.getCharHeight() / 4,
492                   (i - starty + 1) * av.getCharHeight() + yoffset }, 3);
493
494     }
495   }
496
497   void setHiddenFont(SequenceI seq)
498   {
499     Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
500             .getSize());
501
502     if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
503     {
504       gg.setFont(bold);
505     }
506     else
507     {
508       gg.setFont(getIdfont());
509     }
510   }
511
512   /**
513    * DOCUMENT ME!
514    * 
515    * @param list
516    *          DOCUMENT ME!
517    */
518   public void setHighlighted(List<SequenceI> list)
519   {
520     searchResults = list;
521     repaint();
522   }
523
524   public Font getIdfont()
525   {
526     return idfont;
527   }
528
529   public void setIdfont(Font idfont)
530   {
531     this.idfont = idfont;
532   }
533
534   @Override
535   public void propertyChange(PropertyChangeEvent evt)
536   {
537     // Respond to viewport range changes (e.g. alignment panel was scrolled)
538     if (evt.getPropertyName().equals("startseq"))
539     {
540       fastPaint((int) evt.getNewValue() - (int) evt.getOldValue(), false);
541     }
542     else if (evt.getPropertyName().equals("endseq"))
543     {
544       fastPaint((int) evt.getNewValue() - (int) evt.getOldValue(), true);
545     }
546   }
547 }