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