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