tooltip is not shown for Annotation elements with zero length description
[jalview.git] / src / jalview / gui / AnnotationPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.awt.font.LineMetrics;
24 import java.awt.geom.AffineTransform;
25 import java.awt.image.*;
26 import java.util.Hashtable;
27
28 import javax.swing.*;
29
30 import jalview.analysis.AAFrequency;
31 import jalview.datamodel.*;
32 import jalview.schemes.ColourSchemeI;
33
34 /**
35  * DOCUMENT ME!
36  * 
37  * @author $author$
38  * @version $Revision$
39  */
40 public class AnnotationPanel extends JPanel implements MouseListener,
41         MouseMotionListener, ActionListener, AdjustmentListener
42 {
43   final String HELIX = "Helix";
44
45   final String SHEET = "Sheet";
46
47   final String LABEL = "Label";
48
49   final String REMOVE = "Remove Annotation";
50
51   final String COLOUR = "Colour";
52
53   final Color HELIX_COLOUR = Color.red.darker();
54
55   final Color SHEET_COLOUR = Color.green.darker().darker();
56
57   /** DOCUMENT ME!! */
58   AlignViewport av;
59
60   AlignmentPanel ap;
61
62   int activeRow = -1;
63
64   BufferedImage image;
65
66   BufferedImage fadedImage;
67
68   Graphics2D gg;
69
70   FontMetrics fm;
71
72   int imgWidth = 0;
73
74   boolean fastPaint = false;
75
76   // Used For mouse Dragging and resizing graphs
77   int graphStretch = -1;
78
79   int graphStretchY = -1;
80
81   int min; // used by mouseDragged to see if user
82
83   int max; // used by mouseDragged to see if user
84
85   boolean mouseDragging = false;
86
87   boolean MAC = false;
88
89   /**
90    * Creates a new AnnotationPanel object.
91    * 
92    * @param ap
93    *                DOCUMENT ME!
94    */
95   public AnnotationPanel(AlignmentPanel ap)
96   {
97
98     MAC = new jalview.util.Platform().isAMac();
99
100     ToolTipManager.sharedInstance().registerComponent(this);
101     ToolTipManager.sharedInstance().setInitialDelay(0);
102     ToolTipManager.sharedInstance().setDismissDelay(10000);
103     this.ap = ap;
104     av = ap.av;
105     this.setLayout(null);
106     addMouseListener(this);
107     addMouseMotionListener(this);
108     ap.annotationScroller.getVerticalScrollBar()
109             .addAdjustmentListener(this);
110   }
111
112   public AnnotationPanel(AlignViewport av)
113   {
114     this.av = av;
115   }
116
117   /**
118    * DOCUMENT ME!
119    * 
120    * @param evt
121    *                DOCUMENT ME!
122    */
123   public void adjustmentValueChanged(AdjustmentEvent evt)
124   {
125     ap.alabels.setScrollOffset(-evt.getValue());
126   }
127
128   /**
129    * Calculates the height of the annotation displayed in the annotation panel. 
130    * Callers should normally call the ap.adjustAnnotationHeight method to ensure all
131    * annotation associated components are updated correctly.
132    *  
133    */
134   public int adjustPanelHeight()
135   {
136     // setHeight of panels
137     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
138     int height = 0;
139
140     if (aa != null)
141     {
142       for (int i = 0; i < aa.length; i++)
143       {
144         if (aa[i]==null)
145         {
146           System.err.println("Null annotation row: ignoring.");
147           continue;
148         }
149         if (!aa[i].visible)
150         {
151           continue;
152         }
153
154         aa[i].height = 0;
155
156         if (aa[i].hasText)
157         {
158           aa[i].height += av.charHeight;
159         }
160
161         if (aa[i].hasIcons)
162         {
163           aa[i].height += 16;
164         }
165
166         if (aa[i].graph > 0)
167         {
168           aa[i].height += aa[i].graphHeight;
169         }
170
171         if (aa[i].height == 0)
172         {
173           aa[i].height = 20;
174         }
175
176         height += aa[i].height;
177       }
178     }
179     else
180     {
181       height = 20;
182     }
183
184     this.setPreferredSize(new Dimension(1, height));
185     if (ap!=null)
186     {
187       // revalidate only when the alignment panel is fully constructed
188       ap.validate();
189     }
190
191     return height;
192   }
193
194   /**
195    * DOCUMENT ME!
196    * 
197    * @param evt
198    *                DOCUMENT ME!
199    */
200   public void actionPerformed(ActionEvent evt)
201   {
202     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
203     Annotation[] anot = aa[activeRow].annotations;
204
205     if (anot.length < av.getColumnSelection().getMax())
206     {
207       Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
208       System.arraycopy(anot, 0, temp, 0, anot.length);
209       anot = temp;
210       aa[activeRow].annotations = anot;
211     }
212
213     if (evt.getActionCommand().equals(REMOVE))
214     {
215       for (int i = 0; i < av.getColumnSelection().size(); i++)
216       {
217         anot[av.getColumnSelection().columnAt(i)] = null;
218       }
219     }
220     else if (evt.getActionCommand().equals(LABEL))
221     {
222       String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
223       String label = JOptionPane.showInputDialog(this, "Enter label",
224               exMesg);
225
226       if (label == null)
227       {
228         return;
229       }
230
231       if ((label.length() > 0) && !aa[activeRow].hasText)
232       {
233         aa[activeRow].hasText = true;
234       }
235
236       for (int i = 0; i < av.getColumnSelection().size(); i++)
237       {
238         int index = av.getColumnSelection().columnAt(i);
239
240         if (!av.colSel.isVisible(index))
241           continue;
242
243         if (anot[index] == null)
244         {
245           anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
246                                                             // null exceptions
247                                                             // aren't raised
248                                                             // elsewhere.
249         }
250         else
251         {
252           anot[index].displayCharacter = label;
253         }
254       }
255     }
256     else if (evt.getActionCommand().equals(COLOUR))
257     {
258       Color col = JColorChooser.showDialog(this,
259               "Choose foreground colour", Color.black);
260
261       for (int i = 0; i < av.getColumnSelection().size(); i++)
262       {
263         int index = av.getColumnSelection().columnAt(i);
264
265         if (!av.colSel.isVisible(index))
266           continue;
267
268         if (anot[index] == null)
269         {
270           anot[index] = new Annotation("", "", ' ', 0);
271         }
272
273         anot[index].colour = col;
274       }
275     }
276     else
277     // HELIX OR SHEET
278     {
279       char type = 0;
280       String symbol = "\u03B1";
281
282       if (evt.getActionCommand().equals(HELIX))
283       {
284         type = 'H';
285       }
286       else if (evt.getActionCommand().equals(SHEET))
287       {
288         type = 'E';
289         symbol = "\u03B2";
290       }
291
292       if (!aa[activeRow].hasIcons)
293       {
294         aa[activeRow].hasIcons = true;
295       }
296
297       String label = JOptionPane.showInputDialog(
298               "Enter a label for the structure?", symbol);
299
300       if (label == null)
301       {
302         return;
303       }
304
305       if ((label.length() > 0) && !aa[activeRow].hasText)
306       {
307         aa[activeRow].hasText = true;
308       }
309
310       for (int i = 0; i < av.getColumnSelection().size(); i++)
311       {
312         int index = av.getColumnSelection().columnAt(i);
313
314         if (!av.colSel.isVisible(index))
315           continue;
316
317         if (anot[index] == null)
318         {
319           anot[index] = new Annotation(label, "", type, 0);
320         }
321
322         anot[index].secondaryStructure = type;
323         anot[index].displayCharacter = label;
324       }
325     }
326
327     adjustPanelHeight();
328     repaint();
329
330     return;
331   }
332
333   private String collectAnnotVals(Annotation[] anot,
334           ColumnSelection columnSelection, String label2)
335   {
336     String collatedInput = "";
337     String last = "";
338     for (int i = 0; i < columnSelection.size(); i++)
339     {
340       int index = columnSelection.columnAt(i);
341       // always check for current display state - just in case
342       if (!av.colSel.isVisible(index))
343         continue;
344       String tlabel = null;
345       if (anot[index] != null)
346       {
347         if (label2.equals(HELIX) || label2.equals(SHEET)
348                 || label2.equals(LABEL))
349         {
350           tlabel = anot[index].description;
351           if (tlabel == null || tlabel.length()<1)
352           {
353             if (label2.equals(HELIX) || label2.equals(SHEET))
354             {
355               tlabel = "" + anot[index].secondaryStructure;
356             }
357             else
358             {
359               tlabel = "" + anot[index].displayCharacter;
360             }
361           }
362         }
363         if (tlabel != null && !tlabel.equals(last))
364         {
365           if (last.length() > 0)
366           {
367             collatedInput += " ";
368           }
369           collatedInput += tlabel;
370         }
371       }
372     }
373     return collatedInput;
374   }
375
376   /**
377    * DOCUMENT ME!
378    * 
379    * @param evt
380    *                DOCUMENT ME!
381    */
382   public void mousePressed(MouseEvent evt)
383   {
384
385     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
386     if (aa == null)
387     {
388       return;
389     }
390
391     int height = 0;
392     activeRow = -1;
393
394     for (int i = 0; i < aa.length; i++)
395     {
396       if (aa[i].visible)
397       {
398         height += aa[i].height;
399       }
400
401       if (evt.getY() < height)
402       {
403         if (aa[i].editable)
404         {
405           activeRow = i;
406         }
407         else if (aa[i].graph > 0)
408         {
409           // Stretch Graph
410           graphStretch = i;
411           graphStretchY = evt.getY();
412         }
413
414         break;
415       }
416     }
417
418     if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
419     {
420       if (av.getColumnSelection() == null)
421       {
422         return;
423       }
424
425       JPopupMenu pop = new JPopupMenu("Structure type");
426       JMenuItem item = new JMenuItem(HELIX);
427       item.addActionListener(this);
428       pop.add(item);
429       item = new JMenuItem(SHEET);
430       item.addActionListener(this);
431       pop.add(item);
432       item = new JMenuItem(LABEL);
433       item.addActionListener(this);
434       pop.add(item);
435       item = new JMenuItem(COLOUR);
436       item.addActionListener(this);
437       pop.add(item);
438       item = new JMenuItem(REMOVE);
439       item.addActionListener(this);
440       pop.add(item);
441       pop.show(this, evt.getX(), evt.getY());
442
443       return;
444     }
445
446     if (aa == null)
447     {
448       return;
449     }
450
451     ap.scalePanel.mousePressed(evt);
452
453   }
454
455   /**
456    * DOCUMENT ME!
457    * 
458    * @param evt
459    *                DOCUMENT ME!
460    */
461   public void mouseReleased(MouseEvent evt)
462   {
463     graphStretch = -1;
464     graphStretchY = -1;
465     mouseDragging = false;
466     ap.scalePanel.mouseReleased(evt);
467   }
468
469   /**
470    * DOCUMENT ME!
471    * 
472    * @param evt
473    *                DOCUMENT ME!
474    */
475   public void mouseEntered(MouseEvent evt)
476   {
477     ap.scalePanel.mouseEntered(evt);
478   }
479
480   /**
481    * DOCUMENT ME!
482    * 
483    * @param evt
484    *                DOCUMENT ME!
485    */
486   public void mouseExited(MouseEvent evt)
487   {
488     ap.scalePanel.mouseExited(evt);
489   }
490
491   /**
492    * DOCUMENT ME!
493    * 
494    * @param evt
495    *                DOCUMENT ME!
496    */
497   public void mouseDragged(MouseEvent evt)
498   {
499     if (graphStretch > -1)
500     {
501       av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
502               - evt.getY();
503       if (av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight < 0)
504       {
505         av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight = 0;
506       }
507       graphStretchY = evt.getY();
508       adjustPanelHeight();
509       ap.paintAlignment(true);
510     }
511     else
512     {
513       ap.scalePanel.mouseDragged(evt);
514     }
515   }
516
517   /**
518    * DOCUMENT ME!
519    * 
520    * @param evt
521    *                DOCUMENT ME!
522    */
523   public void mouseMoved(MouseEvent evt)
524   {
525     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
526
527     if (aa == null)
528     {
529       this.setToolTipText(null);
530       return;
531     }
532
533     int row = -1;
534     int height = 0;
535
536     for (int i = 0; i < aa.length; i++)
537     {
538       if (aa[i].visible)
539       {
540         height += aa[i].height;
541       }
542
543       if (evt.getY() < height)
544       {
545         row = i;
546
547         break;
548       }
549     }
550
551     if (row == -1)
552     {
553       this.setToolTipText(null);
554       return;
555     }
556
557     int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
558
559     if (av.hasHiddenColumns)
560     {
561       res = av.getColumnSelection().adjustForHiddenColumns(res);
562     }
563
564     if (row > -1 && aa[row].annotations != null 
565             && res < (int) aa[row].annotations.length)
566     {
567       if (aa[row].graphGroup > -1)
568       {
569         StringBuffer tip = new StringBuffer("<html>");
570         for (int gg = 0; gg < aa.length; gg++)
571         {
572           if (aa[gg].graphGroup == aa[row].graphGroup
573                   && aa[gg].annotations[res] != null)
574           {
575             tip.append(aa[gg].label + " "
576                     + aa[gg].annotations[res].description + "<br>");
577           }
578         }
579         if (tip.length() != 6)
580         {
581           tip.setLength(tip.length() - 4);
582           this.setToolTipText(tip.toString() + "</html>");
583         }
584       }
585       else if (aa[row].annotations[res] != null
586               && aa[row].annotations[res].description != null && aa[row].annotations[res].description.length()>0)
587       {
588         this.setToolTipText(aa[row].annotations[res].description);
589       }
590       else
591       {
592         // clear the tooltip.
593         this.setToolTipText(null);
594       }
595
596       if (aa[row].annotations[res] != null)
597       {
598         StringBuffer text = new StringBuffer("Sequence position "
599                 + (res + 1));
600
601         if (aa[row].annotations[res].description != null)
602         {
603           text.append("  " + aa[row].annotations[res].description);
604         }
605
606         ap.alignFrame.statusBar.setText(text.toString());
607       }
608     }
609     else
610     {
611       this.setToolTipText(null);
612     }
613   }
614
615   /**
616    * DOCUMENT ME!
617    * 
618    * @param evt
619    *                DOCUMENT ME!
620    */
621   public void mouseClicked(MouseEvent evt)
622   {
623   }
624
625   /**
626    * DOCUMENT ME!
627    * 
628    * @param g
629    *                DOCUMENT ME!
630    */
631   public void paintComponent(Graphics g)
632   {
633     g.setColor(Color.white);
634     g.fillRect(0, 0, getWidth(), getHeight());
635
636     if (image != null)
637     {
638       if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
639               || (getVisibleRect().height != g.getClipBounds().height))
640       {
641         g.drawImage(image, 0, 0, this);
642         fastPaint = false;
643         return;
644       }
645     }
646     imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
647     if (imgWidth < 1)
648       return;
649     if (image == null || imgWidth != image.getWidth()
650             || image.getHeight(this) != getHeight())
651     {
652       image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
653               BufferedImage.TYPE_INT_RGB);
654       gg = (Graphics2D) image.getGraphics();
655
656       if (av.antiAlias)
657       {
658         gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
659                 RenderingHints.VALUE_ANTIALIAS_ON);
660       }
661
662       gg.setFont(av.getFont());
663       fm = gg.getFontMetrics();
664       gg.setColor(Color.white);
665       gg.fillRect(0, 0, imgWidth, image.getHeight());
666     }
667
668     drawComponent(gg, av.startRes, av.endRes + 1);
669     g.drawImage(image, 0, 0, this);
670   }
671
672   /**
673    * non-Thread safe repaint
674    * 
675    * @param horizontal
676    *                repaint with horizontal shift in alignment
677    */
678   public void fastPaint(int horizontal)
679   {
680
681     if ((horizontal == 0) || gg == null
682             || av.alignment.getAlignmentAnnotation() == null
683             || av.alignment.getAlignmentAnnotation().length < 1
684             || av.updatingConsensus || av.updatingConservation)
685     {
686       repaint();
687       return;
688     }
689     gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
690
691     int sr = av.startRes;
692     int er = av.endRes + 1;
693     int transX = 0;
694
695     if (horizontal > 0) // scrollbar pulled right, image to the left
696     {
697       transX = (er - sr - horizontal) * av.charWidth;
698       sr = er - horizontal;
699     }
700     else if (horizontal < 0)
701     {
702       er = sr - horizontal;
703     }
704
705     gg.translate(transX, 0);
706
707     drawComponent(gg, sr, er);
708
709     gg.translate(-transX, 0);
710
711     fastPaint = true;
712     repaint();
713
714   }
715
716   /**
717    * DOCUMENT ME!
718    * 
719    * @param g
720    *                DOCUMENT ME!
721    * @param startRes
722    *                DOCUMENT ME!
723    * @param endRes
724    *                DOCUMENT ME!
725    */
726   public void drawComponent(Graphics g, int startRes, int endRes)
727   {
728     if (av.updatingConsensus || av.updatingConservation)
729     {
730       if (image == null)
731       {
732         return;
733       }
734       // We'll keep a record of the old image,
735       // and draw a faded image until the calculation
736       // has completed
737       if (fadedImage == null || fadedImage.getWidth() != imgWidth
738               || fadedImage.getHeight() != image.getHeight())
739       {
740         fadedImage = new BufferedImage(imgWidth, image.getHeight(),
741                 BufferedImage.TYPE_INT_RGB);
742
743         Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
744
745         fadedG.setColor(Color.white);
746         fadedG.fillRect(0, 0, imgWidth, image.getHeight());
747
748         fadedG.setComposite(AlphaComposite.getInstance(
749                 AlphaComposite.SRC_OVER, .3f));
750         fadedG.drawImage(image, 0, 0, this);
751
752       }
753
754     }
755     else
756     {
757       fadedImage = null;
758     }
759
760     g.setColor(Color.white);
761     g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
762
763     g.setFont(av.getFont());
764     if (fm == null)
765     {
766       fm = g.getFontMetrics();
767     }
768
769     if ((av.alignment.getAlignmentAnnotation() == null)
770             || (av.alignment.getAlignmentAnnotation().length < 1))
771     {
772       g.setColor(Color.white);
773       g.fillRect(0, 0, getWidth(), getHeight());
774       g.setColor(Color.black);
775       if (av.validCharWidth)
776       {
777         g.drawString("Alignment has no annotations", 20, 15);
778       }
779
780       return;
781     }
782
783     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
784
785     int x = 0, y = 0;
786     int column = 0;
787     char lastSS;
788     int lastSSX;
789     int iconOffset = 0;
790     boolean validRes = false;
791     boolean validEnd = false;
792     boolean labelAllCols = false;
793     boolean centreColLabels,centreColLabelsDef = av.getCentreColumnLabels();
794     boolean scaleColLabel=false;
795     boolean[] graphGroupDrawn = new boolean[aa.length];
796     int charOffset = 0; // offset for a label
797     float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a column.
798     Font ofont = g.getFont();
799     // \u03B2 \u03B1
800     for (int i = 0; i < aa.length; i++)
801     {
802       AlignmentAnnotation row = aa[i];
803
804       if (!row.visible)
805       {
806         continue;
807       }
808       centreColLabels = row.centreColLabels || centreColLabelsDef;
809       labelAllCols = row.showAllColLabels;
810       scaleColLabel = row.scaleColLabel;
811       lastSS = ' ';
812       lastSSX = 0;
813
814       if (row.graph > 0)
815       {
816         if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
817         {
818           continue;
819         }
820
821         // this is so that we draw the characters below the graph
822         y += row.height;
823
824         if (row.hasText)
825         {
826           iconOffset = av.charHeight - fm.getDescent();
827           y -= av.charHeight;
828         }
829       }
830       else if (row.hasText)
831       {
832         iconOffset = av.charHeight - fm.getDescent();
833
834       }
835       else
836       {
837         iconOffset = 0;
838       }
839
840       if (av.updatingConsensus && aa[i] == av.consensus)
841       {
842         y += av.charHeight;
843
844         g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0, y
845                 - row.height, imgWidth, y, this);
846         g.setColor(Color.black);
847         // g.drawString("Calculating Consensus....",20, y-row.height/2);
848
849         continue;
850       }
851       else if (av.updatingConservation
852               && aa[i].label.equals("Conservation"))
853       {
854
855         y += av.charHeight;
856         g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0, y
857                 - row.height, imgWidth, y, this);
858
859         g.setColor(Color.black);
860         // g.drawString("Calculating Conservation.....",20, y-row.height/2);
861
862         continue;
863       }
864       else if (av.updatingConservation && aa[i].label.equals("Quality"))
865       {
866
867         y += av.charHeight;
868         g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0, y
869                 - row.height, imgWidth, y, this);
870         g.setColor(Color.black);
871         // / g.drawString("Calculating Quality....",20, y-row.height/2);
872
873         continue;
874       }
875
876       x = 0;
877       while (x < endRes - startRes)
878       {
879         if (av.hasHiddenColumns)
880         {
881           column = av.getColumnSelection().adjustForHiddenColumns(
882                   startRes + x);
883           if (column > row.annotations.length - 1)
884           {
885             break;
886           }
887         }
888         else
889         {
890           column = startRes + x;
891         }
892
893         if ((row.annotations == null) || (row.annotations.length <= column)
894                 || (row.annotations[column] == null))
895         {
896           validRes = false;
897         }
898         else
899         {
900           validRes = true;
901         }
902
903         if (activeRow == i)
904         {
905           g.setColor(Color.red);
906
907           if (av.getColumnSelection() != null)
908           {
909             for (int n = 0; n < av.getColumnSelection().size(); n++)
910             {
911               int v = av.getColumnSelection().columnAt(n);
912
913               if (v == column)
914               {
915                 g
916                         .fillRect(x * av.charWidth, y, av.charWidth,
917                                 av.charHeight);
918               }
919             }
920           }
921         }
922
923         if (av.validCharWidth && validRes
924                 && row.annotations[column].displayCharacter != null
925                 && (row.annotations[column].displayCharacter.length() > 0))
926         {
927           
928           if (centreColLabels || scaleColLabel)
929           {
930             fmWidth = (float) fm
931             .charsWidth(row.annotations[column].displayCharacter
932                     .toCharArray(), 0,
933                     row.annotations[column].displayCharacter.length());
934
935             if ( scaleColLabel ) { 
936               // justify the label and scale to fit in column
937               if (fmWidth > av.charWidth) {
938                 // scale only if the current font isn't already small enough
939                 fmScaling = av.charWidth;
940                 fmScaling /= fmWidth;
941                 g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(fmScaling, 1.0)));
942                 // and update the label's width to reflect the scaling.
943                 fmWidth = av.charWidth;
944               }
945             }
946           } else {
947             fmWidth = (float) fm
948             .charWidth(row.annotations[column].displayCharacter.charAt(0));
949           }
950           charOffset =  (int) ((av.charWidth - fmWidth) / 2f);
951           
952           if (row.annotations[column].colour == null)
953             g.setColor(Color.black);
954           else
955             g.setColor(row.annotations[column].colour);
956
957           if (column == 0 || row.graph > 0)
958           {
959             g.drawString(row.annotations[column].displayCharacter,
960                     (x * av.charWidth) + charOffset, y + iconOffset);
961           }
962           else if (row.annotations[column - 1] == null
963                   || (labelAllCols||!row.annotations[column].displayCharacter
964                           .equals(row.annotations[column - 1].displayCharacter) || (row.annotations[column].displayCharacter
965                           .length() < 2 && row.annotations[column].secondaryStructure == ' ')))
966           {
967             g.drawString(row.annotations[column].displayCharacter, x
968                     * av.charWidth + charOffset, y + iconOffset);
969           }
970           g.setFont(ofont);
971         }
972
973         if (row.hasIcons)
974         {
975           if (!validRes
976                   || (row.annotations[column].secondaryStructure != lastSS))
977           {
978             switch (lastSS)
979             {
980             case 'H':
981               g.setColor(HELIX_COLOUR);
982               if (MAC)
983               {
984                 // Off by 1 offset when drawing rects and ovals
985                 // to offscreen image on the MAC
986                 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
987                         (x * av.charWidth) - lastSSX, 7, 8, 8);
988                 break;
989               }
990
991               int sCol = (lastSSX / av.charWidth) + startRes;
992               int x1 = lastSSX;
993               int x2 = (x * av.charWidth);
994
995               if (sCol == 0
996                       || row.annotations[sCol - 1] == null
997                       || row.annotations[sCol - 1].secondaryStructure != 'H')
998               {
999                 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90,
1000                         180);
1001                 x1 += av.charWidth / 2;
1002               }
1003
1004               if (!validRes || row.annotations[column] == null
1005                       || row.annotations[column].secondaryStructure != 'H')
1006               {
1007                 g.fillArc((x * av.charWidth) - av.charWidth, y + 4
1008                         + iconOffset, av.charWidth, 8, 270, 180);
1009                 x2 -= av.charWidth / 2;
1010               }
1011
1012               g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1013               break;
1014
1015             case 'E':
1016               g.setColor(SHEET_COLOUR);
1017               g.fillRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
1018                       - lastSSX - 4, 7);
1019               g.fillPolygon(new int[]
1020               { (x * av.charWidth) - 4, (x * av.charWidth) - 4,
1021                   (x * av.charWidth) }, new int[]
1022               { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
1023                       3);
1024
1025               break;
1026
1027             default:
1028               g.setColor(Color.gray);
1029               g.fillRect(lastSSX, y + 6 + iconOffset, (x * av.charWidth)
1030                       - lastSSX, 2);
1031
1032               break;
1033             }
1034
1035             if (validRes)
1036             {
1037               lastSS = row.annotations[column].secondaryStructure;
1038             }
1039             else
1040             {
1041               lastSS = ' ';
1042             }
1043
1044             lastSSX = (x * av.charWidth);
1045           }
1046         }
1047
1048         column++;
1049         x++;
1050       }
1051
1052       if (column >= row.annotations.length)
1053       {
1054         column = row.annotations.length - 1;
1055         validEnd = false;
1056       }
1057       else
1058       {
1059         validEnd = true;
1060       }
1061
1062       // x ++;
1063
1064       if (row.hasIcons)
1065       {
1066         switch (lastSS)
1067         {
1068         case 'H':
1069           g.setColor(HELIX_COLOUR);
1070           if (MAC)
1071           {
1072             // Off by 1 offset when drawing rects and ovals
1073             // to offscreen image on the MAC
1074             g.fillRoundRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
1075                     - lastSSX, 7, 8, 8);
1076             break;
1077           }
1078
1079           int sCol = (lastSSX / av.charWidth) + startRes;
1080           int x1 = lastSSX;
1081           int x2 = (x * av.charWidth);
1082
1083           if (sCol == 0 || row.annotations[sCol - 1] == null
1084                   || row.annotations[sCol - 1].secondaryStructure != 'H')
1085           {
1086             g
1087                     .fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8,
1088                             90, 180);
1089             x1 += av.charWidth / 2;
1090           }
1091
1092           if (row.annotations[column] == null
1093                   || row.annotations[column].secondaryStructure != 'H')
1094           {
1095             g.fillArc((x * av.charWidth) - av.charWidth,
1096                     y + 4 + iconOffset, av.charWidth, 8, 270, 180);
1097             x2 -= av.charWidth / 2;
1098           }
1099
1100           g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1101
1102           break;
1103
1104         case 'E':
1105           g.setColor(SHEET_COLOUR);
1106
1107           if (!validEnd || row.annotations[endRes] == null
1108                   || row.annotations[endRes].secondaryStructure != 'E')
1109           {
1110             g.fillRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
1111                     - lastSSX - 4, 7);
1112             g.fillPolygon(new int[]
1113             { (x * av.charWidth) - 4, (x * av.charWidth) - 4,
1114                 (x * av.charWidth) }, new int[]
1115             { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset }, 3);
1116           }
1117           else
1118           {
1119             g.fillRect(lastSSX, y + 4 + iconOffset, (x + 1) * av.charWidth
1120                     - lastSSX, 7);
1121           }
1122           break;
1123
1124         default:
1125           g.setColor(Color.gray);
1126           g.fillRect(lastSSX, y + 6 + iconOffset, (x * av.charWidth)
1127                   - lastSSX, 2);
1128
1129           break;
1130         }
1131       }
1132
1133       if (row.graph > 0 && row.graphHeight > 0)
1134       {
1135         if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1136         {
1137           if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
1138           {
1139             float groupmax = -999999, groupmin = 9999999;
1140             for (int gg = 0; gg < aa.length; gg++)
1141             {
1142               if (aa[gg].graphGroup != row.graphGroup)
1143               {
1144                 continue;
1145               }
1146
1147               if (aa[gg] != row)
1148               {
1149                 aa[gg].visible = false;
1150               }
1151
1152               if (aa[gg].graphMax > groupmax)
1153               {
1154                 groupmax = aa[gg].graphMax;
1155               }
1156               if (aa[gg].graphMin < groupmin)
1157               {
1158                 groupmin = aa[gg].graphMin;
1159               }
1160             }
1161
1162             for (int gg = 0; gg < aa.length; gg++)
1163             {
1164               if (aa[gg].graphGroup == row.graphGroup)
1165               {
1166                 drawLineGraph(g, aa[gg], startRes, endRes, y, groupmin,
1167                         groupmax, row.graphHeight);
1168               }
1169             }
1170
1171             graphGroupDrawn[row.graphGroup] = true;
1172           }
1173           else
1174           {
1175             drawLineGraph(g, row, startRes, endRes, y, row.graphMin,
1176                     row.graphMax, row.graphHeight);
1177           }
1178         }
1179         else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1180         {
1181           drawBarGraph(g, row, startRes, endRes, row.graphMin,
1182                   row.graphMax, y);
1183         }
1184       }
1185
1186       if (row.graph > 0 && row.hasText)
1187       {
1188         y += av.charHeight;
1189       }
1190
1191       if (row.graph == 0)
1192       {
1193         y += aa[i].height;
1194       }
1195     }
1196   }
1197
1198   public void drawLineGraph(Graphics g, AlignmentAnnotation aa, int sRes,
1199           int eRes, int y, float min, float max, int graphHeight)
1200   {
1201     if (sRes > aa.annotations.length)
1202     {
1203       return;
1204     }
1205
1206     int x = 0;
1207
1208     // Adjustment for fastpaint to left
1209     if (eRes < av.endRes)
1210     {
1211       eRes++;
1212     }
1213
1214     eRes = Math.min(eRes, aa.annotations.length);
1215
1216     if (sRes == 0)
1217     {
1218       x++;
1219     }
1220
1221     int y1 = y, y2 = y;
1222     float range = max - min;
1223
1224     // //Draw origin
1225     if (min < 0)
1226     {
1227       y2 = y - (int) ((0 - min / range) * graphHeight);
1228     }
1229
1230     g.setColor(Color.gray);
1231     g.drawLine(x - av.charWidth, y2, (eRes - sRes + 1) * av.charWidth, y2);
1232
1233     eRes = Math.min(eRes, aa.annotations.length);
1234
1235     int column;
1236     int aaMax = aa.annotations.length - 1;
1237
1238     while (x < eRes - sRes)
1239     {
1240       column = sRes + x;
1241       if (av.hasHiddenColumns)
1242       {
1243         column = av.getColumnSelection().adjustForHiddenColumns(column);
1244       }
1245
1246       if (column > aaMax)
1247       {
1248         break;
1249       }
1250
1251       if (aa.annotations[column] == null
1252               || aa.annotations[column - 1] == null)
1253       {
1254         x++;
1255         continue;
1256       }
1257
1258       if (aa.annotations[column].colour == null)
1259         g.setColor(Color.black);
1260       else
1261         g.setColor(aa.annotations[column].colour);
1262
1263       y1 = y
1264               - (int) (((aa.annotations[column - 1].value - min) / range) * graphHeight);
1265       y2 = y
1266               - (int) (((aa.annotations[column].value - min) / range) * graphHeight);
1267
1268       g.drawLine(x * av.charWidth - av.charWidth / 2, y1, x * av.charWidth
1269               + av.charWidth / 2, y2);
1270       x++;
1271     }
1272
1273     if (aa.threshold != null)
1274     {
1275       g.setColor(aa.threshold.colour);
1276       Graphics2D g2 = (Graphics2D) g;
1277       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1278               BasicStroke.JOIN_ROUND, 3f, new float[]
1279               { 5f, 3f }, 0f));
1280
1281       y2 = (int) (y - ((aa.threshold.value - min) / range) * graphHeight);
1282       g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1283       g2.setStroke(new BasicStroke());
1284     }
1285   }
1286
1287   public void drawBarGraph(Graphics g, AlignmentAnnotation aa, int sRes,
1288           int eRes, float min, float max, int y)
1289   {
1290     ColourSchemeI profcolour = av.getGlobalColourScheme();
1291     if (profcolour==null)
1292     {
1293       profcolour = new jalview.schemes.ZappoColourScheme();
1294     }
1295     if (sRes > aa.annotations.length)
1296     {
1297       return;
1298     }
1299     Font ofont = g.getFont();
1300     eRes = Math.min(eRes, aa.annotations.length);
1301
1302     int x = 0, y1 = y, y2 = y;
1303
1304     float range = max - min;
1305
1306     if (min < 0)
1307     {
1308       y2 = y - (int) ((0 - min / (range)) * aa.graphHeight);
1309     }
1310
1311     g.setColor(Color.gray);
1312
1313     g.drawLine(x, y2, (eRes - sRes) * av.charWidth, y2);
1314
1315     int column;
1316     int aaMax = aa.annotations.length - 1;
1317     boolean renderHistogram = true, renderProfile = true;
1318     if (aa.autoCalculated && aa.label.startsWith("Consensus")) {
1319       // TODO: generalise this to have render styles for consensus/profile data 
1320       if (aa.groupRef!=null)
1321       {
1322         renderHistogram = aa.groupRef.isShowConsensusHistogram();
1323         renderProfile = aa.groupRef.isShowSequenceLogo();
1324       } else {
1325         renderHistogram = av.isShowConsensusHistogram();
1326         renderProfile = av.isShowSequenceLogo();
1327       }
1328     }
1329     while (x < eRes - sRes)
1330     {
1331       column = sRes + x;
1332       if (av.hasHiddenColumns)
1333       {
1334         column = av.getColumnSelection().adjustForHiddenColumns(column);
1335       }
1336
1337       if (column > aaMax)
1338       {
1339         break;
1340       }
1341
1342       if (aa.annotations[column] == null)
1343       {
1344         x++;
1345         continue;
1346       }
1347       if (aa.annotations[column].colour == null)
1348         g.setColor(Color.black);
1349       else
1350         g.setColor(aa.annotations[column].colour);
1351
1352       y1 = y
1353               - (int) (((aa.annotations[column].value - min) / (range)) * aa.graphHeight);
1354
1355       if (renderHistogram)
1356       {      
1357         if (y1 - y2 > 0)
1358       {
1359         g.fillRect(x * av.charWidth, y2, av.charWidth, y1 - y2);
1360       }
1361       else
1362       {
1363         g.fillRect(x * av.charWidth, y1, av.charWidth, y2 - y1);
1364       }
1365       }
1366       // draw profile if available
1367       if (aa.annotations[column].value!=0 && renderProfile)
1368       {
1369         int profl[] = getProfileFor(aa,column);
1370         int ht = y1,htn=y2-y1;//aa.graphHeight;
1371           float wdth;
1372           double ht2=0;
1373           char[] dc = new char[1];
1374           LineMetrics lm;
1375           for (int c=1;profl!=null && c<profl[0];)
1376           {
1377             dc[0] = (char) profl[c++];
1378             wdth = av.charWidth;
1379             wdth/=(float) fm
1380                     .charsWidth(dc,0,1);
1381             
1382             if (c>2)
1383             {
1384               ht+=(int)ht2;
1385             }
1386             {
1387               //if (aa.annotations[column].value==0) {
1388               //  g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(wdth, (ht2=(aa.graphHeight*0.1/av.charHeight)))));
1389               //  ht = y2-(int)ht2;
1390               //} else {
1391                 g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(wdth, (ht2=(htn*((double)profl[c++])/100.0))/av.charHeight)));
1392                 lm = g.getFontMetrics().getLineMetrics(dc,0,1, g);
1393                 //htn -=ht2;
1394               //}
1395               g.setColor(profcolour.findColour(dc[0])); // (av.globalColourScheme!=null) ? );// try to get a colourscheme for the group(aa.groupRef.cs==null) ? av.textColour2 : cs.findColour(dc));
1396               g.drawChars(dc,0,1,x*av.charWidth, (int) (ht+lm.getHeight()));
1397               // ht+=g.getFontMetrics().getAscent()-g.getFontMetrics().getDescent();
1398             }
1399           }
1400           g.setFont(ofont);
1401       }
1402       x++;
1403     }
1404     if (aa.threshold != null)
1405     {
1406       g.setColor(aa.threshold.colour);
1407       Graphics2D g2 = (Graphics2D) g;
1408       g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1409               BasicStroke.JOIN_ROUND, 3f, new float[]
1410               { 5f, 3f }, 0f));
1411
1412       y2 = (int) (y - ((aa.threshold.value - min) / range) * aa.graphHeight);
1413       g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1414       g2.setStroke(new BasicStroke());
1415     }
1416   }
1417
1418   private int[] getProfileFor(AlignmentAnnotation aa, int column)
1419   {
1420     if (aa.autoCalculated && aa.label.startsWith("Consensus")) {
1421     if (aa.groupRef!=null && aa.groupRef.consensusData!=null && aa.groupRef.isShowSequenceLogo()) {
1422       return AAFrequency.extractProfile(aa.groupRef.consensusData[column],aa.groupRef.getIgnoreGapsConsensus());
1423     }
1424     // TODO extend annotation row to enable dynamic and static profile data to be stored  
1425     if (aa.groupRef==null && aa.sequenceRef==null && av.isShowSequenceLogo())
1426     {
1427       return AAFrequency.extractProfile(av.hconsensus[column],av.getIgnoreGapsConsensus());
1428     }
1429     }
1430     return null;
1431   }
1432
1433   // used by overview window
1434   public void drawGraph(Graphics g, AlignmentAnnotation aa, int width,
1435           int y, int sRes, int eRes)
1436   {
1437     eRes = Math.min(eRes, aa.annotations.length);
1438     g.setColor(Color.white);
1439     g.fillRect(0, 0, width, y);
1440     g.setColor(new Color(0, 0, 180));
1441
1442     int x = 0, height;
1443
1444     for (int j = sRes; j < eRes; j++)
1445     {
1446       if (aa.annotations[j] != null)
1447       {
1448         if (aa.annotations[j].colour == null)
1449           g.setColor(Color.black);
1450         else
1451           g.setColor(aa.annotations[j].colour);
1452
1453         height = (int) ((aa.annotations[j].value / aa.graphMax) * y);
1454         if (height > y)
1455         {
1456           height = y;
1457         }
1458
1459         g.fillRect(x, y - height, av.charWidth, height);
1460       }
1461       x += av.charWidth;
1462     }
1463   }
1464
1465 }