77d6b512564d3801cdc973b0fe4d1a29e2e90ad5
[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.SequenceI;
27 import jalview.renderer.AnnotationRenderer;
28 import jalview.renderer.AwtRenderPanelI;
29 import jalview.util.MessageManager;
30
31 import java.awt.AlphaComposite;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.FontMetrics;
35 import java.awt.Graphics;
36 import java.awt.Graphics2D;
37 import java.awt.Image;
38 import java.awt.Rectangle;
39 import java.awt.RenderingHints;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.AdjustmentEvent;
43 import java.awt.event.AdjustmentListener;
44 import java.awt.event.MouseEvent;
45 import java.awt.event.MouseListener;
46 import java.awt.event.MouseMotionListener;
47 import java.awt.event.MouseWheelEvent;
48 import java.awt.event.MouseWheelListener;
49 import java.awt.image.BufferedImage;
50
51 import javax.swing.JColorChooser;
52 import javax.swing.JMenuItem;
53 import javax.swing.JOptionPane;
54 import javax.swing.JPanel;
55 import javax.swing.JPopupMenu;
56 import javax.swing.Scrollable;
57 import javax.swing.SwingUtilities;
58 import javax.swing.ToolTipManager;
59
60 /**
61  * AnnotationPanel displays visible portion of annotation rows below unwrapped
62  * alignment
63  * 
64  * @author $author$
65  * @version $Revision$
66  */
67 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
68         MouseListener, MouseWheelListener, MouseMotionListener,
69         ActionListener, AdjustmentListener, Scrollable
70 {
71   String HELIX = MessageManager.getString("label.helix");
72
73   String SHEET = MessageManager.getString("label.sheet");
74
75   /**
76    * For RNA secondary structure "stems" aka helices
77    */
78   String STEM = MessageManager.getString("label.rna_helix");
79
80   String LABEL = MessageManager.getString("label.label");
81
82   String REMOVE = MessageManager.getString("label.remove_annotation");
83
84   String COLOUR = MessageManager.getString("action.colour");
85
86   public final Color HELIX_COLOUR = Color.red.darker();
87
88   public final Color SHEET_COLOUR = Color.green.darker().darker();
89
90   public final Color STEM_COLOUR = Color.blue.darker();
91
92   /** DOCUMENT ME!! */
93   public AlignViewport av;
94
95   AlignmentPanel ap;
96
97   public int activeRow = -1;
98
99   public BufferedImage image;
100
101   public volatile BufferedImage fadedImage;
102
103   Graphics2D gg;
104
105   public FontMetrics fm;
106
107   public int imgWidth = 0;
108
109   boolean fastPaint = false;
110
111   // Used For mouse Dragging and resizing graphs
112   int graphStretch = -1;
113
114   int graphStretchY = -1;
115
116   int min; // used by mouseDragged to see if user
117
118   int max; // used by mouseDragged to see if user
119
120   boolean mouseDragging = false;
121
122   boolean MAC = false;
123
124   // for editing cursor
125   int cursorX = 0;
126
127   int cursorY = 0;
128
129   public final AnnotationRenderer renderer;
130
131   private MouseWheelListener[] _mwl;
132
133   /**
134    * Creates a new AnnotationPanel object.
135    * 
136    * @param ap
137    *          DOCUMENT ME!
138    */
139   public AnnotationPanel(AlignmentPanel ap)
140   {
141
142     MAC = jalview.util.Platform.isAMac();
143
144     ToolTipManager.sharedInstance().registerComponent(this);
145     ToolTipManager.sharedInstance().setInitialDelay(0);
146     ToolTipManager.sharedInstance().setDismissDelay(10000);
147     this.ap = ap;
148     av = ap.av;
149     this.setLayout(null);
150     addMouseListener(this);
151     addMouseMotionListener(this);
152     ap.annotationScroller.getVerticalScrollBar()
153             .addAdjustmentListener(this);
154     // save any wheel listeners on the scroller, so we can propagate scroll
155     // events to them.
156     _mwl = ap.annotationScroller.getMouseWheelListeners();
157     // and then set our own listener to consume all mousewheel events
158     ap.annotationScroller.addMouseWheelListener(this);
159     renderer = new AnnotationRenderer();
160   }
161
162   public AnnotationPanel(AlignViewport av)
163   {
164     this.av = av;
165     renderer = new AnnotationRenderer();
166   }
167
168   @Override
169   public void mouseWheelMoved(MouseWheelEvent e)
170   {
171     if (e.isShiftDown())
172     {
173       e.consume();
174       if (e.getWheelRotation() > 0)
175       {
176         ap.scrollRight(true);
177       }
178       else
179       {
180         ap.scrollRight(false);
181       }
182     }
183     else
184     {
185       // TODO: find the correct way to let the event bubble up to
186       // ap.annotationScroller
187       for (MouseWheelListener mwl : _mwl)
188       {
189         if (mwl != null)
190         {
191           mwl.mouseWheelMoved(e);
192         }
193         if (e.isConsumed())
194         {
195           break;
196         }
197       }
198     }
199   }
200
201   @Override
202   public Dimension getPreferredScrollableViewportSize()
203   {
204     return getPreferredSize();
205   }
206
207   @Override
208   public int getScrollableBlockIncrement(Rectangle visibleRect,
209           int orientation, int direction)
210   {
211     return 30;
212   }
213
214   @Override
215   public boolean getScrollableTracksViewportHeight()
216   {
217     return false;
218   }
219
220   @Override
221   public boolean getScrollableTracksViewportWidth()
222   {
223     return true;
224   }
225
226   @Override
227   public int getScrollableUnitIncrement(Rectangle visibleRect,
228           int orientation, int direction)
229   {
230     return 30;
231   }
232
233   /*
234    * (non-Javadoc)
235    * 
236    * @see
237    * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
238    * .AdjustmentEvent)
239    */
240   @Override
241   public void adjustmentValueChanged(AdjustmentEvent evt)
242   {
243     // update annotation label display
244     ap.getAlabels().setScrollOffset(-evt.getValue());
245   }
246
247   /**
248    * Calculates the height of the annotation displayed in the annotation panel.
249    * Callers should normally call the ap.adjustAnnotationHeight method to ensure
250    * all annotation associated components are updated correctly.
251    * 
252    */
253   public int adjustPanelHeight()
254   {
255     int height = av.calcPanelHeight();
256     this.setPreferredSize(new Dimension(1, height));
257     if (ap != null)
258     {
259       // revalidate only when the alignment panel is fully constructed
260       ap.validate();
261     }
262
263     return height;
264   }
265
266   /**
267    * DOCUMENT ME!
268    * 
269    * @param evt
270    *          DOCUMENT ME!
271    */
272   @Override
273   public void actionPerformed(ActionEvent evt)
274   {
275     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
276     if (aa == null)
277     {
278       return;
279     }
280     Annotation[] anot = aa[activeRow].annotations;
281
282     if (anot.length < av.getColumnSelection().getMax())
283     {
284       Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
285       System.arraycopy(anot, 0, temp, 0, anot.length);
286       anot = temp;
287       aa[activeRow].annotations = anot;
288     }
289
290     if (evt.getActionCommand().equals(REMOVE))
291     {
292       for (int i = 0; i < av.getColumnSelection().size(); i++)
293       {
294         anot[av.getColumnSelection().columnAt(i)] = null;
295       }
296     }
297     else if (evt.getActionCommand().equals(LABEL))
298     {
299       String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
300       String label = JOptionPane.showInputDialog(this,
301               MessageManager.getString("label.enter_label"), exMesg);
302
303       if (label == null)
304       {
305         return;
306       }
307
308       if ((label.length() > 0) && !aa[activeRow].hasText)
309       {
310         aa[activeRow].hasText = true;
311       }
312
313       for (int i = 0; i < av.getColumnSelection().size(); i++)
314       {
315         int index = av.getColumnSelection().columnAt(i);
316
317         if (!av.getColumnSelection().isVisible(index))
318         {
319           continue;
320         }
321
322         if (anot[index] == null)
323         {
324           anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
325           // null exceptions
326           // aren't raised
327           // elsewhere.
328         }
329         else
330         {
331           anot[index].displayCharacter = label;
332         }
333       }
334     }
335     else if (evt.getActionCommand().equals(COLOUR))
336     {
337       Color col = JColorChooser.showDialog(this,
338               MessageManager.getString("label.select_foreground_colour"),
339               Color.black);
340
341       for (int i = 0; i < av.getColumnSelection().size(); i++)
342       {
343         int index = av.getColumnSelection().columnAt(i);
344
345         if (!av.getColumnSelection().isVisible(index))
346         {
347           continue;
348         }
349
350         if (anot[index] == null)
351         {
352           anot[index] = new Annotation("", "", ' ', 0);
353         }
354
355         anot[index].colour = col;
356       }
357     }
358     else
359     // HELIX OR SHEET
360     {
361       char type = 0;
362       String symbol = "\u03B1";
363
364       if (evt.getActionCommand().equals(HELIX))
365       {
366         type = 'H';
367       }
368       else if (evt.getActionCommand().equals(SHEET))
369       {
370         type = 'E';
371         symbol = "\u03B2";
372       }
373
374       // Added by LML to color stems
375       else if (evt.getActionCommand().equals(STEM))
376       {
377         type = 'S';
378         symbol = "\u03C3";
379       }
380
381       if (!aa[activeRow].hasIcons)
382       {
383         aa[activeRow].hasIcons = true;
384       }
385
386       String label = JOptionPane.showInputDialog(MessageManager
387               .getString("label.enter_label_for_the_structure"), symbol);
388
389       if (label == null)
390       {
391         return;
392       }
393
394       if ((label.length() > 0) && !aa[activeRow].hasText)
395       {
396         aa[activeRow].hasText = true;
397         if (evt.getActionCommand().equals(STEM))
398         {
399           aa[activeRow].showAllColLabels = true;
400         }
401       }
402       for (int i = 0; i < av.getColumnSelection().size(); i++)
403       {
404         int index = av.getColumnSelection().columnAt(i);
405
406         if (!av.getColumnSelection().isVisible(index))
407         {
408           continue;
409         }
410
411         if (anot[index] == null)
412         {
413           anot[index] = new Annotation(label, "", type, 0);
414         }
415
416         anot[index].secondaryStructure = type != 'S' ? type : label
417                 .length() == 0 ? ' ' : label.charAt(0);
418         anot[index].displayCharacter = label;
419
420       }
421     }
422     av.getAlignment().validateAnnotation(aa[activeRow]);
423     ap.alignmentChanged();
424
425     adjustPanelHeight();
426     repaint();
427
428     return;
429   }
430
431   private String collectAnnotVals(Annotation[] anot,
432           ColumnSelection columnSelection, String label2)
433   {
434     String collatedInput = "";
435     String last = "";
436     ColumnSelection viscols = av.getColumnSelection();
437     // TODO: refactor and save av.getColumnSelection for efficiency
438     for (int i = 0; i < columnSelection.size(); i++)
439     {
440       int index = columnSelection.columnAt(i);
441       // always check for current display state - just in case
442       if (!viscols.isVisible(index))
443       {
444         continue;
445       }
446       String tlabel = null;
447       if (anot[index] != null)
448       { // LML added stem code
449         if (label2.equals(HELIX) || label2.equals(SHEET)
450                 || label2.equals(STEM) || label2.equals(LABEL))
451         {
452           tlabel = anot[index].description;
453           if (tlabel == null || tlabel.length() < 1)
454           {
455             if (label2.equals(HELIX) || label2.equals(SHEET)
456                     || label2.equals(STEM))
457             {
458               tlabel = "" + anot[index].secondaryStructure;
459             }
460             else
461             {
462               tlabel = "" + anot[index].displayCharacter;
463             }
464           }
465         }
466         if (tlabel != null && !tlabel.equals(last))
467         {
468           if (last.length() > 0)
469           {
470             collatedInput += " ";
471           }
472           collatedInput += tlabel;
473         }
474       }
475     }
476     return collatedInput;
477   }
478
479   /**
480    * DOCUMENT ME!
481    * 
482    * @param evt
483    *          DOCUMENT ME!
484    */
485   @Override
486   public void mousePressed(MouseEvent evt)
487   {
488
489     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
490     if (aa == null)
491     {
492       return;
493     }
494
495     int height = 0;
496     activeRow = -1;
497
498     for (int i = 0; i < aa.length; i++)
499     {
500       if (aa[i].visible)
501       {
502         height += aa[i].height;
503       }
504
505       if (evt.getY() < height)
506       {
507         if (aa[i].editable)
508         {
509           activeRow = i;
510         }
511         else if (aa[i].graph > 0)
512         {
513           // Stretch Graph
514           graphStretch = i;
515           graphStretchY = evt.getY();
516         }
517
518         break;
519       }
520     }
521
522     if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
523     {
524       if (av.getColumnSelection() == null)
525       {
526         return;
527       }
528
529       JPopupMenu pop = new JPopupMenu(
530               MessageManager.getString("label.structure_type"));
531       JMenuItem item;
532       /*
533        * Just display the needed structure options
534        */
535       if (av.getAlignment().isNucleotide() == true)
536       {
537         item = new JMenuItem(STEM);
538         item.addActionListener(this);
539         pop.add(item);
540       }
541       else
542       {
543         item = new JMenuItem(HELIX);
544         item.addActionListener(this);
545         pop.add(item);
546         item = new JMenuItem(SHEET);
547         item.addActionListener(this);
548         pop.add(item);
549       }
550       item = new JMenuItem(LABEL);
551       item.addActionListener(this);
552       pop.add(item);
553       item = new JMenuItem(COLOUR);
554       item.addActionListener(this);
555       pop.add(item);
556       item = new JMenuItem(REMOVE);
557       item.addActionListener(this);
558       pop.add(item);
559       pop.show(this, evt.getX(), evt.getY());
560
561       return;
562     }
563
564     if (aa == null)
565     {
566       return;
567     }
568
569     ap.getScalePanel().mousePressed(evt);
570
571   }
572
573   /**
574    * DOCUMENT ME!
575    * 
576    * @param evt
577    *          DOCUMENT ME!
578    */
579   @Override
580   public void mouseReleased(MouseEvent evt)
581   {
582     graphStretch = -1;
583     graphStretchY = -1;
584     mouseDragging = false;
585     ap.getScalePanel().mouseReleased(evt);
586   }
587
588   /**
589    * DOCUMENT ME!
590    * 
591    * @param evt
592    *          DOCUMENT ME!
593    */
594   @Override
595   public void mouseEntered(MouseEvent evt)
596   {
597     ap.getScalePanel().mouseEntered(evt);
598   }
599
600   /**
601    * DOCUMENT ME!
602    * 
603    * @param evt
604    *          DOCUMENT ME!
605    */
606   @Override
607   public void mouseExited(MouseEvent evt)
608   {
609     ap.getScalePanel().mouseExited(evt);
610   }
611
612   /**
613    * DOCUMENT ME!
614    * 
615    * @param evt
616    *          DOCUMENT ME!
617    */
618   @Override
619   public void mouseDragged(MouseEvent evt)
620   {
621     if (graphStretch > -1)
622     {
623       av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
624               - evt.getY();
625       if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
626       {
627         av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
628       }
629       graphStretchY = evt.getY();
630       adjustPanelHeight();
631       ap.paintAlignment(true);
632     }
633     else
634     {
635       ap.getScalePanel().mouseDragged(evt);
636     }
637   }
638
639   /**
640    * DOCUMENT ME!
641    * 
642    * @param evt
643    *          DOCUMENT ME!
644    */
645   @Override
646   public void mouseMoved(MouseEvent evt)
647   {
648     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
649
650     if (aa == null)
651     {
652       this.setToolTipText(null);
653       return;
654     }
655
656     int row = -1;
657     int height = 0;
658
659     for (int i = 0; i < aa.length; i++)
660     {
661       if (aa[i].visible)
662       {
663         height += aa[i].height;
664       }
665
666       if (evt.getY() < height)
667       {
668         row = i;
669
670         break;
671       }
672     }
673
674     if (row == -1)
675     {
676       this.setToolTipText(null);
677       return;
678     }
679
680     int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
681
682     if (av.hasHiddenColumns())
683     {
684       res = av.getColumnSelection().adjustForHiddenColumns(res);
685     }
686
687     if (row > -1 && aa[row].annotations != null
688             && res < aa[row].annotations.length)
689     {
690       if (aa[row].graphGroup > -1)
691       {
692         StringBuffer tip = new StringBuffer("<html>");
693         for (int gg = 0; gg < aa.length; gg++)
694         {
695           if (aa[gg].graphGroup == aa[row].graphGroup
696                   && aa[gg].annotations[res] != null)
697           {
698             tip.append(aa[gg].label + " "
699                     + aa[gg].annotations[res].description + "<br>");
700           }
701         }
702         if (tip.length() != 6)
703         {
704           tip.setLength(tip.length() - 4);
705           this.setToolTipText(tip.toString() + "</html>");
706         }
707       }
708       else if (aa[row].annotations[res] != null
709               && aa[row].annotations[res].description != null
710               && aa[row].annotations[res].description.length() > 0)
711       {
712         this.setToolTipText(JvSwingUtils.wrapTooltip(true,
713                 aa[row].annotations[res].description));
714       }
715       else
716       {
717         // clear the tooltip.
718         this.setToolTipText(null);
719       }
720
721       if (aa[row].annotations[res] != null)
722       {
723         StringBuffer text = new StringBuffer("Sequence position "
724                 + (res + 1));
725
726         if (aa[row].annotations[res].description != null)
727         {
728           text.append("  " + aa[row].annotations[res].description);
729         }
730
731         ap.alignFrame.statusBar.setText(text.toString());
732       }
733     }
734     else
735     {
736       this.setToolTipText(null);
737     }
738   }
739
740   /**
741    * DOCUMENT ME!
742    * 
743    * @param evt
744    *          DOCUMENT ME!
745    */
746   @Override
747   public void mouseClicked(MouseEvent evt)
748   {
749     // if (activeRow != -1)
750     // {
751     // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
752     // AlignmentAnnotation anot = aa[activeRow];
753     // }
754   }
755
756   // TODO mouseClicked-content and drawCursor are quite experimental!
757   public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
758           int y1)
759   {
760     int pady = av.getCharHeight() / 5;
761     int charOffset = 0;
762     graphics.setColor(Color.black);
763     graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
764
765     if (av.validCharWidth)
766     {
767       graphics.setColor(Color.white);
768
769       char s = seq.getCharAt(res);
770
771       charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
772       graphics.drawString(String.valueOf(s), charOffset + x1,
773               (y1 + av.getCharHeight()) - pady);
774     }
775
776   }
777
778   private volatile boolean imageFresh = false;
779
780   /**
781    * DOCUMENT ME!
782    * 
783    * @param g
784    *          DOCUMENT ME!
785    */
786   @Override
787   public void paintComponent(Graphics g)
788   {
789     g.setColor(Color.white);
790     g.fillRect(0, 0, getWidth(), getHeight());
791
792     if (image != null)
793     {
794       if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
795               || (getVisibleRect().height != g.getClipBounds().height))
796       {
797         g.drawImage(image, 0, 0, this);
798         fastPaint = false;
799         return;
800       }
801     }
802     imgWidth = (av.endRes - av.startRes + 1) * av.getCharWidth();
803     if (imgWidth < 1)
804     {
805       return;
806     }
807     if (image == null || imgWidth != image.getWidth(this)
808             || image.getHeight(this) != getHeight())
809     {
810       try
811       {
812         image = new BufferedImage(imgWidth, ap.getAnnotationPanel()
813                 .getHeight(), BufferedImage.TYPE_INT_RGB);
814       } catch (OutOfMemoryError oom)
815       {
816         try
817         {
818           System.gc();
819         } catch (Exception x)
820         {
821         }
822         ;
823         new OOMWarning(
824                 "Couldn't allocate memory to redraw screen. Please restart Jalview",
825                 oom);
826         return;
827       }
828       gg = (Graphics2D) image.getGraphics();
829
830       if (av.antiAlias)
831       {
832         gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
833                 RenderingHints.VALUE_ANTIALIAS_ON);
834       }
835
836       gg.setFont(av.getFont());
837       fm = gg.getFontMetrics();
838       gg.setColor(Color.white);
839       gg.fillRect(0, 0, imgWidth, image.getHeight());
840       imageFresh = true;
841     }
842
843     drawComponent(gg, av.startRes, av.endRes + 1);
844     imageFresh = false;
845     g.drawImage(image, 0, 0, this);
846   }
847
848   /**
849    * set true to enable redraw timing debug output on stderr
850    */
851   private final boolean debugRedraw = false;
852
853   /**
854    * non-Thread safe repaint
855    * 
856    * @param horizontal
857    *          repaint with horizontal shift in alignment
858    */
859   public void fastPaint(int horizontal)
860   {
861     if ((horizontal == 0) || gg == null
862             || av.getAlignment().getAlignmentAnnotation() == null
863             || av.getAlignment().getAlignmentAnnotation().length < 1
864             || av.isCalcInProgress())
865     {
866       repaint();
867       return;
868     }
869     long stime = System.currentTimeMillis();
870     gg.copyArea(0, 0, imgWidth, getHeight(),
871             -horizontal * av.getCharWidth(), 0);
872     long mtime = System.currentTimeMillis();
873     int sr = av.startRes;
874     int er = av.endRes + 1;
875     int transX = 0;
876
877     if (horizontal > 0) // scrollbar pulled right, image to the left
878     {
879       transX = (er - sr - horizontal) * av.getCharWidth();
880       sr = er - horizontal;
881     }
882     else if (horizontal < 0)
883     {
884       er = sr - horizontal;
885     }
886
887     gg.translate(transX, 0);
888
889     drawComponent(gg, sr, er);
890
891     gg.translate(-transX, 0);
892     long dtime = System.currentTimeMillis();
893     fastPaint = true;
894     repaint();
895     long rtime = System.currentTimeMillis();
896     if (debugRedraw)
897     {
898       System.err.println("Scroll:\t" + horizontal + "\tCopyArea:\t"
899               + (mtime - stime) + "\tDraw component:\t" + (dtime - mtime)
900               + "\tRepaint call:\t" + (rtime - dtime));
901     }
902
903   }
904
905   private volatile boolean lastImageGood = false;
906
907   /**
908    * DOCUMENT ME!
909    * 
910    * @param g
911    *          DOCUMENT ME!
912    * @param startRes
913    *          DOCUMENT ME!
914    * @param endRes
915    *          DOCUMENT ME!
916    */
917   public void drawComponent(Graphics g, int startRes, int endRes)
918   {
919     BufferedImage oldFaded = fadedImage;
920     if (av.isCalcInProgress())
921     {
922       if (image == null)
923       {
924         lastImageGood = false;
925         return;
926       }
927       // We'll keep a record of the old image,
928       // and draw a faded image until the calculation
929       // has completed
930       if (lastImageGood
931               && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
932                       .getHeight() != image.getHeight()))
933       {
934         // System.err.println("redraw faded image ("+(fadedImage==null ?
935         // "null image" : "") + " lastGood="+lastImageGood+")");
936         fadedImage = new BufferedImage(imgWidth, image.getHeight(),
937                 BufferedImage.TYPE_INT_RGB);
938
939         Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
940
941         fadedG.setColor(Color.white);
942         fadedG.fillRect(0, 0, imgWidth, image.getHeight());
943
944         fadedG.setComposite(AlphaComposite.getInstance(
945                 AlphaComposite.SRC_OVER, .3f));
946         fadedG.drawImage(image, 0, 0, this);
947
948       }
949       // make sure we don't overwrite the last good faded image until all
950       // calculations have finished
951       lastImageGood = false;
952
953     }
954     else
955     {
956       if (fadedImage != null)
957       {
958         oldFaded = fadedImage;
959       }
960       fadedImage = null;
961     }
962
963     g.setColor(Color.white);
964     g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(), getHeight());
965
966     g.setFont(av.getFont());
967     if (fm == null)
968     {
969       fm = g.getFontMetrics();
970     }
971
972     if ((av.getAlignment().getAlignmentAnnotation() == null)
973             || (av.getAlignment().getAlignmentAnnotation().length < 1))
974     {
975       g.setColor(Color.white);
976       g.fillRect(0, 0, getWidth(), getHeight());
977       g.setColor(Color.black);
978       if (av.validCharWidth)
979       {
980         g.drawString(MessageManager
981                 .getString("label.alignment_has_no_annotations"), 20, 15);
982       }
983
984       return;
985     }
986     lastImageGood = renderer.drawComponent(this, av, g, activeRow,
987             startRes, endRes);
988     if (!lastImageGood && fadedImage == null)
989     {
990       fadedImage = oldFaded;
991     }
992   }
993
994   @Override
995   public FontMetrics getFontMetrics()
996   {
997     return fm;
998   }
999
1000   @Override
1001   public Image getFadedImage()
1002   {
1003     return fadedImage;
1004   }
1005
1006   @Override
1007   public int getFadedImageWidth()
1008   {
1009     return imgWidth;
1010   }
1011
1012   private int[] bounds = new int[2];
1013
1014   @Override
1015   public int[] getVisibleVRange()
1016   {
1017     if (ap != null && ap.getAlabels() != null)
1018     {
1019       int sOffset = -ap.getAlabels().getScrollOffset();
1020       int visHeight = sOffset + ap.annotationSpaceFillerHolder.getHeight();
1021       bounds[0] = sOffset;
1022       bounds[1] = visHeight;
1023       return bounds;
1024     }
1025     else
1026     {
1027       return null;
1028     }
1029   }
1030 }