JAL-2854 compute visible annotation panel height and provide it directly to via Scrol...
[jalview.git] / src / jalview / gui / AnnotationPanel.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.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.HiddenColumns;
27 import jalview.datamodel.SequenceI;
28 import jalview.renderer.AnnotationRenderer;
29 import jalview.renderer.AwtRenderPanelI;
30 import jalview.schemes.ResidueProperties;
31 import jalview.util.Comparison;
32 import jalview.util.MessageManager;
33 import jalview.util.Platform;
34 import jalview.viewmodel.ViewportListenerI;
35 import jalview.viewmodel.ViewportRanges;
36
37 import java.awt.AlphaComposite;
38 import java.awt.Color;
39 import java.awt.Dimension;
40 import java.awt.FontMetrics;
41 import java.awt.Graphics;
42 import java.awt.Graphics2D;
43 import java.awt.Image;
44 import java.awt.Rectangle;
45 import java.awt.RenderingHints;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.event.AdjustmentEvent;
49 import java.awt.event.AdjustmentListener;
50 import java.awt.event.MouseEvent;
51 import java.awt.event.MouseListener;
52 import java.awt.event.MouseMotionListener;
53 import java.awt.event.MouseWheelEvent;
54 import java.awt.event.MouseWheelListener;
55 import java.awt.image.BufferedImage;
56 import java.beans.PropertyChangeEvent;
57 import java.util.ArrayList;
58 import java.util.Collections;
59 import java.util.List;
60
61 import javax.swing.JColorChooser;
62 import javax.swing.JMenuItem;
63 import javax.swing.JPanel;
64 import javax.swing.JPopupMenu;
65 import javax.swing.Scrollable;
66 import javax.swing.ToolTipManager;
67
68 /**
69  * AnnotationPanel displays visible portion of annotation rows below unwrapped
70  * alignment
71  * 
72  * @author $author$
73  * @version $Revision$
74  */
75 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
76         MouseListener, MouseWheelListener, MouseMotionListener,
77         ActionListener, AdjustmentListener, Scrollable, ViewportListenerI
78 {
79   String HELIX = MessageManager.getString("label.helix");
80
81   String SHEET = MessageManager.getString("label.sheet");
82
83   /**
84    * For RNA secondary structure "stems" aka helices
85    */
86   String STEM = MessageManager.getString("label.rna_helix");
87
88   String LABEL = MessageManager.getString("label.label");
89
90   String REMOVE = MessageManager.getString("label.remove_annotation");
91
92   String COLOUR = MessageManager.getString("action.colour");
93
94   public final Color HELIX_COLOUR = Color.red.darker();
95
96   public final Color SHEET_COLOUR = Color.green.darker().darker();
97
98   public final Color STEM_COLOUR = Color.blue.darker();
99
100   /** DOCUMENT ME!! */
101   public AlignViewport av;
102
103   AlignmentPanel ap;
104
105   public int activeRow = -1;
106
107   public BufferedImage image;
108
109   public volatile BufferedImage fadedImage;
110
111   Graphics2D gg;
112
113   public FontMetrics fm;
114
115   public int imgWidth = 0;
116
117   boolean fastPaint = false;
118
119   // Used For mouse Dragging and resizing graphs
120   int graphStretch = -1;
121
122   int graphStretchY = -1;
123
124   int min; // used by mouseDragged to see if user
125
126   int max; // used by mouseDragged to see if user
127
128   boolean mouseDragging = false;
129
130   // for editing cursor
131   int cursorX = 0;
132
133   int cursorY = 0;
134
135   public final AnnotationRenderer renderer;
136
137   private MouseWheelListener[] _mwl;
138
139   /**
140    * Creates a new AnnotationPanel object.
141    * 
142    * @param ap
143    *          DOCUMENT ME!
144    */
145   public AnnotationPanel(AlignmentPanel ap)
146   {
147     ToolTipManager.sharedInstance().registerComponent(this);
148     ToolTipManager.sharedInstance().setInitialDelay(0);
149     ToolTipManager.sharedInstance().setDismissDelay(10000);
150     this.ap = ap;
151     av = ap.av;
152     this.setLayout(null);
153     addMouseListener(this);
154     addMouseMotionListener(this);
155     ap.annotationScroller.getVerticalScrollBar()
156             .addAdjustmentListener(this);
157     // save any wheel listeners on the scroller, so we can propagate scroll
158     // events to them.
159     _mwl = ap.annotationScroller.getMouseWheelListeners();
160     // and then set our own listener to consume all mousewheel events
161     ap.annotationScroller.addMouseWheelListener(this);
162     renderer = new AnnotationRenderer();
163
164     av.getRanges().addPropertyChangeListener(this);
165   }
166
167   public AnnotationPanel(AlignViewport av)
168   {
169     this.av = av;
170     renderer = new AnnotationRenderer();
171   }
172
173   @Override
174   public void mouseWheelMoved(MouseWheelEvent e)
175   {
176     if (e.isShiftDown())
177     {
178       e.consume();
179       double wheelRotation = e.getPreciseWheelRotation();
180       if (wheelRotation > 0)
181       {
182         av.getRanges().scrollRight(true);
183       }
184       else if (wheelRotation < 0)
185       {
186         av.getRanges().scrollRight(false);
187       }
188     }
189     else
190     {
191       // TODO: find the correct way to let the event bubble up to
192       // ap.annotationScroller
193       for (MouseWheelListener mwl : _mwl)
194       {
195         if (mwl != null)
196         {
197           mwl.mouseWheelMoved(e);
198         }
199         if (e.isConsumed())
200         {
201           break;
202         }
203       }
204     }
205   }
206
207   @Override
208   public Dimension getPreferredScrollableViewportSize()
209   {
210     Dimension ps = getPreferredSize();
211     return new Dimension(ps.width, adjustForAlignFrame(false, ps.height));
212   }
213
214   @Override
215   public int getScrollableBlockIncrement(Rectangle visibleRect,
216           int orientation, int direction)
217   {
218     return 30;
219   }
220
221   @Override
222   public boolean getScrollableTracksViewportHeight()
223   {
224     return false;
225   }
226
227   @Override
228   public boolean getScrollableTracksViewportWidth()
229   {
230     return true;
231   }
232
233   @Override
234   public int getScrollableUnitIncrement(Rectangle visibleRect,
235           int orientation, int direction)
236   {
237     return 30;
238   }
239
240   /*
241    * (non-Javadoc)
242    * 
243    * @see
244    * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
245    * .AdjustmentEvent)
246    */
247   @Override
248   public void adjustmentValueChanged(AdjustmentEvent evt)
249   {
250     // update annotation label display
251     ap.getAlabels().setScrollOffset(-evt.getValue());
252   }
253
254   /**
255    * Calculates the height of the annotation displayed in the annotation panel.
256    * Callers should normally call the ap.adjustAnnotationHeight method to ensure
257    * all annotation associated components are updated correctly.
258    * 
259    */
260   public int adjustPanelHeight()
261   {
262     int height = av.calcPanelHeight();
263     this.setPreferredSize(new Dimension(1, height));
264     if (ap != null)
265     {
266       // revalidate only when the alignment panel is fully constructed
267       ap.validate();
268     }
269
270     return height;
271   }
272
273   /**
274    * DOCUMENT ME!
275    * 
276    * @param evt
277    *          DOCUMENT ME!
278    */
279   @Override
280   public void actionPerformed(ActionEvent evt)
281   {
282     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
283     if (aa == null)
284     {
285       return;
286     }
287     Annotation[] anot = aa[activeRow].annotations;
288
289     if (anot.length < av.getColumnSelection().getMax())
290     {
291       Annotation[] temp = new Annotation[av.getColumnSelection().getMax()
292               + 2];
293       System.arraycopy(anot, 0, temp, 0, anot.length);
294       anot = temp;
295       aa[activeRow].annotations = anot;
296     }
297
298     String action = evt.getActionCommand();
299     if (action.equals(REMOVE))
300     {
301       for (int index : av.getColumnSelection().getSelected())
302       {
303         if (av.getAlignment().getHiddenColumns().isVisible(index))
304         {
305           anot[index] = null;
306         }
307       }
308     }
309     else if (action.equals(LABEL))
310     {
311       String exMesg = collectAnnotVals(anot, LABEL);
312       String label = JvOptionPane.showInputDialog(this,
313               MessageManager.getString("label.enter_label"), exMesg);
314
315       if (label == null)
316       {
317         return;
318       }
319
320       if ((label.length() > 0) && !aa[activeRow].hasText)
321       {
322         aa[activeRow].hasText = true;
323       }
324
325       for (int index : av.getColumnSelection().getSelected())
326       {
327         if (!av.getAlignment().getHiddenColumns().isVisible(index))
328         {
329           continue;
330         }
331
332         if (anot[index] == null)
333         {
334           anot[index] = new Annotation(label, "", ' ', 0);
335         }
336         else
337         {
338           anot[index].displayCharacter = label;
339         }
340       }
341     }
342     else if (action.equals(COLOUR))
343     {
344       Color col = JColorChooser.showDialog(this,
345               MessageManager.getString("label.select_foreground_colour"),
346               Color.black);
347
348       for (int index : av.getColumnSelection().getSelected())
349       {
350         if (!av.getAlignment().getHiddenColumns().isVisible(index))
351         {
352           continue;
353         }
354
355         if (anot[index] == null)
356         {
357           anot[index] = new Annotation("", "", ' ', 0);
358         }
359
360         anot[index].colour = col;
361       }
362     }
363     else
364     // HELIX, SHEET or STEM
365     {
366       char type = 0;
367       String symbol = "\u03B1"; // alpha
368
369       if (action.equals(HELIX))
370       {
371         type = 'H';
372       }
373       else if (action.equals(SHEET))
374       {
375         type = 'E';
376         symbol = "\u03B2"; // beta
377       }
378
379       // Added by LML to color stems
380       else if (action.equals(STEM))
381       {
382         type = 'S';
383         int column = av.getColumnSelection().getSelectedRanges().get(0)[0];
384         symbol = aa[activeRow].getDefaultRnaHelixSymbol(column);
385       }
386
387       if (!aa[activeRow].hasIcons)
388       {
389         aa[activeRow].hasIcons = true;
390       }
391
392       String label = JvOptionPane.showInputDialog(MessageManager
393               .getString("label.enter_label_for_the_structure"), symbol);
394
395       if (label == null)
396       {
397         return;
398       }
399
400       if ((label.length() > 0) && !aa[activeRow].hasText)
401       {
402         aa[activeRow].hasText = true;
403         if (action.equals(STEM))
404         {
405           aa[activeRow].showAllColLabels = true;
406         }
407       }
408       for (int index : av.getColumnSelection().getSelected())
409       {
410         if (!av.getAlignment().getHiddenColumns().isVisible(index))
411         {
412           continue;
413         }
414
415         if (anot[index] == null)
416         {
417           anot[index] = new Annotation(label, "", type, 0);
418         }
419
420         anot[index].secondaryStructure = type != 'S' ? type
421                 : label.length() == 0 ? ' ' : label.charAt(0);
422         anot[index].displayCharacter = label;
423
424       }
425     }
426
427     av.getAlignment().validateAnnotation(aa[activeRow]);
428     ap.alignmentChanged();
429     ap.alignFrame.setMenusForViewport();
430     adjustPanelHeight();
431     repaint();
432
433     return;
434   }
435
436   /**
437    * Returns any existing annotation concatenated as a string. For each
438    * annotation, takes the description, if any, else the secondary structure
439    * character (if type is HELIX, SHEET or STEM), else the display character (if
440    * type is LABEL).
441    * 
442    * @param anots
443    * @param type
444    * @return
445    */
446   private String collectAnnotVals(Annotation[] anots, String type)
447   {
448     // TODO is this method wanted? why? 'last' is not used
449
450     StringBuilder collatedInput = new StringBuilder(64);
451     String last = "";
452     ColumnSelection viscols = av.getColumnSelection();
453     HiddenColumns hidden = av.getAlignment().getHiddenColumns();
454
455     /*
456      * the selection list (read-only view) is in selection order, not
457      * column order; make a copy so we can sort it
458      */
459     List<Integer> selected = new ArrayList<>(viscols.getSelected());
460     Collections.sort(selected);
461     for (int index : selected)
462     {
463       // always check for current display state - just in case
464       if (!hidden.isVisible(index))
465       {
466         continue;
467       }
468       String tlabel = null;
469       if (anots[index] != null)
470       { // LML added stem code
471         if (type.equals(HELIX) || type.equals(SHEET) || type.equals(STEM)
472                 || type.equals(LABEL))
473         {
474           tlabel = anots[index].description;
475           if (tlabel == null || tlabel.length() < 1)
476           {
477             if (type.equals(HELIX) || type.equals(SHEET)
478                     || type.equals(STEM))
479             {
480               tlabel = "" + anots[index].secondaryStructure;
481             }
482             else
483             {
484               tlabel = "" + anots[index].displayCharacter;
485             }
486           }
487         }
488         if (tlabel != null && !tlabel.equals(last))
489         {
490           if (last.length() > 0)
491           {
492             collatedInput.append(" ");
493           }
494           collatedInput.append(tlabel);
495         }
496       }
497     }
498     return collatedInput.toString();
499   }
500
501   /**
502    * DOCUMENT ME!
503    * 
504    * @param evt
505    *          DOCUMENT ME!
506    */
507   @Override
508   public void mousePressed(MouseEvent evt)
509   {
510
511     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
512     if (aa == null)
513     {
514       return;
515     }
516
517     int height = 0;
518     activeRow = -1;
519
520     final int y = evt.getY();
521     for (int i = 0; i < aa.length; i++)
522     {
523       if (aa[i].visible)
524       {
525         height += aa[i].height;
526       }
527
528       if (y < height)
529       {
530         if (aa[i].editable)
531         {
532           activeRow = i;
533         }
534         else if (aa[i].graph > 0)
535         {
536           // Stretch Graph
537           graphStretch = i;
538           graphStretchY = y;
539         }
540
541         break;
542       }
543     }
544
545     /*
546      * isPopupTrigger fires in mousePressed on Mac,
547      * not until mouseRelease on Windows
548      */
549     if (evt.isPopupTrigger() && activeRow != -1)
550     {
551       showPopupMenu(y, evt.getX());
552       return;
553     }
554
555     ap.getScalePanel().mousePressed(evt);
556   }
557
558   /**
559    * Construct and display a context menu at the right-click position
560    * 
561    * @param y
562    * @param x
563    */
564   void showPopupMenu(final int y, int x)
565   {
566     if (av.getColumnSelection() == null
567             || av.getColumnSelection().isEmpty())
568     {
569       return;
570     }
571
572     JPopupMenu pop = new JPopupMenu(
573             MessageManager.getString("label.structure_type"));
574     JMenuItem item;
575     /*
576      * Just display the needed structure options
577      */
578     if (av.getAlignment().isNucleotide())
579     {
580       item = new JMenuItem(STEM);
581       item.addActionListener(this);
582       pop.add(item);
583     }
584     else
585     {
586       item = new JMenuItem(HELIX);
587       item.addActionListener(this);
588       pop.add(item);
589       item = new JMenuItem(SHEET);
590       item.addActionListener(this);
591       pop.add(item);
592     }
593     item = new JMenuItem(LABEL);
594     item.addActionListener(this);
595     pop.add(item);
596     item = new JMenuItem(COLOUR);
597     item.addActionListener(this);
598     pop.add(item);
599     item = new JMenuItem(REMOVE);
600     item.addActionListener(this);
601     pop.add(item);
602     pop.show(this, x, y);
603   }
604
605   /**
606    * DOCUMENT ME!
607    * 
608    * @param evt
609    *          DOCUMENT ME!
610    */
611   @Override
612   public void mouseReleased(MouseEvent evt)
613   {
614     graphStretch = -1;
615     graphStretchY = -1;
616     mouseDragging = false;
617     ap.getScalePanel().mouseReleased(evt);
618
619     /*
620      * isPopupTrigger is set in mouseReleased on Windows
621      * (in mousePressed on Mac)
622      */
623     if (evt.isPopupTrigger() && activeRow != -1)
624     {
625       showPopupMenu(evt.getY(), evt.getX());
626     }
627
628   }
629
630   /**
631    * DOCUMENT ME!
632    * 
633    * @param evt
634    *          DOCUMENT ME!
635    */
636   @Override
637   public void mouseEntered(MouseEvent evt)
638   {
639     ap.getScalePanel().mouseEntered(evt);
640   }
641
642   /**
643    * DOCUMENT ME!
644    * 
645    * @param evt
646    *          DOCUMENT ME!
647    */
648   @Override
649   public void mouseExited(MouseEvent evt)
650   {
651     ap.getScalePanel().mouseExited(evt);
652   }
653
654   /**
655    * DOCUMENT ME!
656    * 
657    * @param evt
658    *          DOCUMENT ME!
659    */
660   @Override
661   public void mouseDragged(MouseEvent evt)
662   {
663     if (graphStretch > -1)
664     {
665       av.getAlignment()
666               .getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
667                       - evt.getY();
668       if (av.getAlignment()
669               .getAlignmentAnnotation()[graphStretch].graphHeight < 0)
670       {
671         av.getAlignment()
672                 .getAlignmentAnnotation()[graphStretch].graphHeight = 0;
673       }
674       graphStretchY = evt.getY();
675       adjustPanelHeight();
676       ap.paintAlignment(false, false);
677     }
678     else
679     {
680       ap.getScalePanel().mouseDragged(evt);
681     }
682   }
683
684   /**
685    * Constructs the tooltip, and constructs and displays a status message, for
686    * the current mouse position
687    * 
688    * @param evt
689    */
690   @Override
691   public void mouseMoved(MouseEvent evt)
692   {
693     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
694
695     if (aa == null)
696     {
697       this.setToolTipText(null);
698       return;
699     }
700
701     int row = -1;
702     int height = 0;
703
704     for (int i = 0; i < aa.length; i++)
705     {
706       if (aa[i].visible)
707       {
708         height += aa[i].height;
709       }
710
711       if (evt.getY() < height)
712       {
713         row = i;
714         break;
715       }
716     }
717
718     if (row == -1)
719     {
720       this.setToolTipText(null);
721       return;
722     }
723
724     int column = (evt.getX() / av.getCharWidth())
725             + av.getRanges().getStartRes();
726
727     if (av.hasHiddenColumns())
728     {
729       column = av.getAlignment().getHiddenColumns()
730               .visibleToAbsoluteColumn(column);
731     }
732
733     AlignmentAnnotation ann = aa[row];
734     if (row > -1 && ann.annotations != null
735             && column < ann.annotations.length)
736     {
737       buildToolTip(ann, column, aa);
738       setStatusMessage(column, ann);
739     }
740     else
741     {
742       this.setToolTipText(null);
743       ap.alignFrame.statusBar.setText(" ");
744     }
745   }
746
747   /**
748    * Builds a tooltip for the annotation at the current mouse position.
749    * 
750    * @param ann
751    * @param column
752    * @param anns
753    */
754   void buildToolTip(AlignmentAnnotation ann, int column,
755           AlignmentAnnotation[] anns)
756   {
757     if (ann.graphGroup > -1)
758     {
759       StringBuilder tip = new StringBuilder(32);
760       tip.append("<html>");
761       for (int i = 0; i < anns.length; i++)
762       {
763         if (anns[i].graphGroup == ann.graphGroup
764                 && anns[i].annotations[column] != null)
765         {
766           tip.append(anns[i].label);
767           String description = anns[i].annotations[column].description;
768           if (description != null && description.length() > 0)
769           {
770             tip.append(" ").append(description);
771           }
772           tip.append("<br>");
773         }
774       }
775       if (tip.length() != 6)
776       {
777         tip.setLength(tip.length() - 4);
778         this.setToolTipText(tip.toString() + "</html>");
779       }
780     }
781     else if (ann.annotations[column] != null)
782     {
783       String description = ann.annotations[column].description;
784       if (description != null && description.length() > 0)
785       {
786         this.setToolTipText(JvSwingUtils.wrapTooltip(true, description));
787       }
788       else
789       {
790         this.setToolTipText(null); // no tooltip if null or empty description
791       }
792     }
793     else
794     {
795       // clear the tooltip.
796       this.setToolTipText(null);
797     }
798   }
799
800   /**
801    * Constructs and displays the status bar message
802    * 
803    * @param column
804    * @param ann
805    */
806   void setStatusMessage(int column, AlignmentAnnotation ann)
807   {
808     /*
809      * show alignment column and annotation description if any
810      */
811     StringBuilder text = new StringBuilder(32);
812     text.append(MessageManager.getString("label.column")).append(" ")
813             .append(column + 1);
814
815     if (ann.annotations[column] != null)
816     {
817       String description = ann.annotations[column].description;
818       if (description != null && description.trim().length() > 0)
819       {
820         text.append("  ").append(description);
821       }
822     }
823
824     /*
825      * if the annotation is sequence-specific, show the sequence number
826      * in the alignment, and (if not a gap) the residue and position
827      */
828     SequenceI seqref = ann.sequenceRef;
829     if (seqref != null)
830     {
831       int seqIndex = av.getAlignment().findIndex(seqref);
832       if (seqIndex != -1)
833       {
834         text.append(", ").append(MessageManager.getString("label.sequence"))
835                 .append(" ").append(seqIndex + 1);
836         char residue = seqref.getCharAt(column);
837         if (!Comparison.isGap(residue))
838         {
839           text.append(" ");
840           String name;
841           if (av.getAlignment().isNucleotide())
842           {
843             name = ResidueProperties.nucleotideName
844                     .get(String.valueOf(residue));
845             text.append(" Nucleotide: ")
846                     .append(name != null ? name : residue);
847           }
848           else
849           {
850             name = 'X' == residue ? "X"
851                     : ('*' == residue ? "STOP"
852                             : ResidueProperties.aa2Triplet
853                                     .get(String.valueOf(residue)));
854             text.append(" Residue: ").append(name != null ? name : residue);
855           }
856           int residuePos = seqref.findPosition(column);
857           text.append(" (").append(residuePos).append(")");
858         }
859       }
860     }
861
862     ap.alignFrame.statusBar.setText(text.toString());
863   }
864
865   /**
866    * DOCUMENT ME!
867    * 
868    * @param evt
869    *          DOCUMENT ME!
870    */
871   @Override
872   public void mouseClicked(MouseEvent evt)
873   {
874     // if (activeRow != -1)
875     // {
876     // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
877     // AlignmentAnnotation anot = aa[activeRow];
878     // }
879   }
880
881   // TODO mouseClicked-content and drawCursor are quite experimental!
882   public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
883           int y1)
884   {
885     int pady = av.getCharHeight() / 5;
886     int charOffset = 0;
887     graphics.setColor(Color.black);
888     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
889
890     if (av.validCharWidth)
891     {
892       graphics.setColor(Color.white);
893
894       char s = seq.getCharAt(res);
895
896       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
897       graphics.drawString(String.valueOf(s), charOffset + x1,
898               (y1 + av.getCharHeight()) - pady);
899     }
900
901   }
902
903   private volatile boolean imageFresh = false;
904
905   /**
906    * DOCUMENT ME!
907    * 
908    * @param g
909    *          DOCUMENT ME!
910    */
911   @Override
912   public void paintComponent(Graphics g)
913   {
914     super.paintComponent(g);
915
916     g.setColor(Color.white);
917     g.fillRect(0, 0, getWidth(), getHeight());
918
919     if (image != null)
920     {
921       if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
922               || (getVisibleRect().height != g.getClipBounds().height))
923       {
924         g.drawImage(image, 0, 0, this);
925         fastPaint = false;
926         return;
927       }
928     }
929     imgWidth = (av.getRanges().getEndRes() - av.getRanges().getStartRes()
930             + 1) * av.getCharWidth();
931     if (imgWidth < 1)
932     {
933       return;
934     }
935     if (image == null || imgWidth != image.getWidth(this)
936             || image.getHeight(this) != getHeight())
937     {
938       try
939       {
940         image = new BufferedImage(imgWidth,
941                 ap.getAnnotationPanel().getHeight(),
942                 BufferedImage.TYPE_INT_RGB);
943       } catch (OutOfMemoryError oom)
944       {
945         try
946         {
947           System.gc();
948         } catch (Exception x)
949         {
950         }
951         ;
952         new OOMWarning(
953                 "Couldn't allocate memory to redraw screen. Please restart Jalview",
954                 oom);
955         return;
956       }
957       gg = (Graphics2D) image.getGraphics();
958
959       if (av.antiAlias)
960       {
961         gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
962                 RenderingHints.VALUE_ANTIALIAS_ON);
963       }
964
965       gg.setFont(av.getFont());
966       fm = gg.getFontMetrics();
967       gg.setColor(Color.white);
968       gg.fillRect(0, 0, imgWidth, image.getHeight());
969       imageFresh = true;
970     }
971     
972     drawComponent(gg, av.getRanges().getStartRes(),
973             av.getRanges().getEndRes() + 1);
974     imageFresh = false;
975     g.drawImage(image, 0, 0, this);
976   }
977
978   /**
979    * set true to enable redraw timing debug output on stderr
980    */
981   private final boolean debugRedraw = false;
982
983   /**
984    * non-Thread safe repaint
985    * 
986    * @param horizontal
987    *          repaint with horizontal shift in alignment
988    */
989   public void fastPaint(int horizontal)
990   {
991     if ((horizontal == 0) || gg == null
992             || av.getAlignment().getAlignmentAnnotation() == null
993             || av.getAlignment().getAlignmentAnnotation().length < 1
994             || av.isCalcInProgress())
995     {
996       repaint();
997       return;
998     }
999
1000     int sr = av.getRanges().getStartRes();
1001     int er = av.getRanges().getEndRes() + 1;
1002     int transX = 0;
1003
1004     gg.copyArea(0, 0, imgWidth, getHeight(),
1005             -horizontal * av.getCharWidth(), 0);
1006
1007     if (horizontal > 0) // scrollbar pulled right, image to the left
1008     {
1009       transX = (er - sr - horizontal) * av.getCharWidth();
1010       sr = er - horizontal;
1011     }
1012     else if (horizontal < 0)
1013     {
1014       er = sr - horizontal;
1015     }
1016
1017     gg.translate(transX, 0);
1018
1019     drawComponent(gg, sr, er);
1020
1021     gg.translate(-transX, 0);
1022
1023     fastPaint = true;
1024
1025     // Call repaint on alignment panel so that repaints from other alignment
1026     // panel components can be aggregated. Otherwise performance of the overview
1027     // window and others may be adversely affected.
1028     av.getAlignPanel().repaint();
1029   }
1030
1031   private volatile boolean lastImageGood = false;
1032
1033   /**
1034    * DOCUMENT ME!
1035    * 
1036    * @param g
1037    *          DOCUMENT ME!
1038    * @param startRes
1039    *          DOCUMENT ME!
1040    * @param endRes
1041    *          DOCUMENT ME!
1042    */
1043   public void drawComponent(Graphics g, int startRes, int endRes)
1044   {
1045     BufferedImage oldFaded = fadedImage;
1046     if (av.isCalcInProgress())
1047     {
1048       if (image == null)
1049       {
1050         lastImageGood = false;
1051         return;
1052       }
1053       // We'll keep a record of the old image,
1054       // and draw a faded image until the calculation
1055       // has completed
1056       if (lastImageGood
1057               && (fadedImage == null || fadedImage.getWidth() != imgWidth
1058                       || fadedImage.getHeight() != image.getHeight()))
1059       {
1060         // System.err.println("redraw faded image ("+(fadedImage==null ?
1061         // "null image" : "") + " lastGood="+lastImageGood+")");
1062         fadedImage = new BufferedImage(imgWidth, image.getHeight(),
1063                 BufferedImage.TYPE_INT_RGB);
1064
1065         Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
1066
1067         fadedG.setColor(Color.white);
1068         fadedG.fillRect(0, 0, imgWidth, image.getHeight());
1069
1070         fadedG.setComposite(
1071                 AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .3f));
1072         fadedG.drawImage(image, 0, 0, this);
1073
1074       }
1075       // make sure we don't overwrite the last good faded image until all
1076       // calculations have finished
1077       lastImageGood = false;
1078
1079     }
1080     else
1081     {
1082       if (fadedImage != null)
1083       {
1084         oldFaded = fadedImage;
1085       }
1086       fadedImage = null;
1087     }
1088
1089     g.setColor(Color.white);
1090     g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(), getHeight());
1091
1092     g.setFont(av.getFont());
1093     if (fm == null)
1094     {
1095       fm = g.getFontMetrics();
1096     }
1097
1098     if ((av.getAlignment().getAlignmentAnnotation() == null)
1099             || (av.getAlignment().getAlignmentAnnotation().length < 1))
1100     {
1101       g.setColor(Color.white);
1102       g.fillRect(0, 0, getWidth(), getHeight());
1103       g.setColor(Color.black);
1104       if (av.validCharWidth)
1105       {
1106         g.drawString(MessageManager
1107                 .getString("label.alignment_has_no_annotations"), 20, 15);
1108       }
1109
1110       return;
1111     }
1112     lastImageGood = renderer.drawComponent(this, av, g, activeRow, startRes,
1113             endRes);
1114     if (!lastImageGood && fadedImage == null)
1115     {
1116       fadedImage = oldFaded;
1117     }
1118   }
1119
1120   @Override
1121   public FontMetrics getFontMetrics()
1122   {
1123     return fm;
1124   }
1125
1126   @Override
1127   public Image getFadedImage()
1128   {
1129     return fadedImage;
1130   }
1131
1132   @Override
1133   public int getFadedImageWidth()
1134   {
1135     return imgWidth;
1136   }
1137
1138   private int[] bounds = new int[2];
1139
1140   @Override
1141   public int[] getVisibleVRange()
1142   {
1143     if (ap != null && ap.getAlabels() != null)
1144     {
1145       int sOffset = -ap.getAlabels().getScrollOffset();
1146       int visHeight = sOffset + ap.annotationSpaceFillerHolder.getHeight();
1147       bounds[0] = sOffset;
1148       bounds[1] = visHeight;
1149       return bounds;
1150     }
1151     else
1152     {
1153       return null;
1154     }
1155   }
1156
1157   /**
1158    * Try to ensure any references held are nulled
1159    */
1160   public void dispose()
1161   {
1162     av = null;
1163     ap = null;
1164     image = null;
1165     fadedImage = null;
1166     gg = null;
1167     _mwl = null;
1168
1169     /*
1170      * I created the renderer so I will dispose of it
1171      */
1172     if (renderer != null)
1173     {
1174       renderer.dispose();
1175     }
1176   }
1177
1178   @Override
1179   public void propertyChange(PropertyChangeEvent evt)
1180   {
1181     // Respond to viewport range changes (e.g. alignment panel was scrolled)
1182     // Both scrolling and resizing change viewport ranges: scrolling changes
1183     // both start and end points, but resize only changes end values.
1184     // Here we only want to fastpaint on a scroll, with resize using a normal
1185     // paint, so scroll events are identified as changes to the horizontal or
1186     // vertical start value.
1187     if (evt.getPropertyName().equals(ViewportRanges.STARTRES))
1188     {
1189       fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
1190     }
1191     else if (evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ))
1192     {
1193       fastPaint(((int[]) evt.getNewValue())[0]
1194               - ((int[]) evt.getOldValue())[0]);
1195     }
1196     else if (evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
1197     {
1198       repaint();
1199     }
1200   }
1201
1202   /**
1203    * computes the visible height of the annotation panel
1204    * 
1205    * @param adjustPanelHeight
1206    *          - when false, just adjust existing height according to other
1207    *          windows
1208    * @param annotationHeight
1209    * @return height to use for the ScrollerPreferredVisibleSize
1210    */
1211   public int adjustForAlignFrame(boolean adjustPanelHeight,
1212           int annotationHeight)
1213   {
1214     /*
1215      * Estimate available height in the AlignFrame for alignment +
1216      * annotations. Deduct an estimate for title bar, menu bar, scale panel,
1217      * hscroll, status bar, insets. 
1218      */
1219     int stuff = (ap.getViewName() != null ? 30 : 0)
1220             + (Platform.isAMac() ? 120 : 140);
1221     int availableHeight = ap.alignFrame.getHeight() - stuff;
1222     int rowHeight = av.getCharHeight();
1223
1224     if (adjustPanelHeight)
1225     {
1226       int alignmentHeight = rowHeight * av.getAlignment().getHeight();
1227
1228       /*
1229        * If not enough vertical space, maximize annotation height while keeping
1230        * at least two rows of alignment visible
1231        */
1232       if (annotationHeight + alignmentHeight > availableHeight)
1233       {
1234         annotationHeight = Math.min(annotationHeight,
1235                 availableHeight - 2 * rowHeight);
1236       }
1237     }
1238     else
1239     {
1240       // maintain same window layout whilst updating sliders
1241       annotationHeight = Math.min(ap.annotationScroller.getSize().height,
1242               availableHeight - 2 * rowHeight);
1243     }
1244     return annotationHeight;
1245   }
1246 }