patch re JAL-554
[jalview.git] / src / jalview / appletgui / AnnotationPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.appletgui;
19
20 import java.util.*;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import jalview.datamodel.*;
25 import jalview.schemes.ColourSchemeI;
26
27 public class AnnotationPanel extends Panel implements AdjustmentListener,
28         ActionListener, MouseListener, MouseMotionListener
29 {
30   AlignViewport av;
31
32   AlignmentPanel ap;
33
34   int activeRow = -1;
35
36   Vector activeRes;
37
38   static String HELIX = "Helix";
39
40   static String SHEET = "Sheet";
41
42   static String LABEL = "Label";
43
44   static String REMOVE = "Remove Annotation";
45
46   static String COLOUR = "Colour";
47
48   static Color HELIX_COLOUR = Color.red.darker();
49
50   static Color SHEET_COLOUR = Color.green.darker().darker();
51
52   Image image;
53
54   Graphics gg;
55
56   FontMetrics fm;
57
58   int imgWidth = 0;
59
60   boolean fastPaint = false;
61
62   // Used For mouse Dragging and resizing graphs
63   int graphStretch = -1;
64
65   int graphStretchY = -1;
66
67   boolean mouseDragging = false;
68
69   public static int GRAPH_HEIGHT = 40;
70
71   boolean MAC = false;
72
73   public AnnotationPanel(AlignmentPanel ap)
74   {
75     MAC = new jalview.util.Platform().isAMac();
76     this.ap = ap;
77     av = ap.av;
78     setLayout(null);
79     adjustPanelHeight();
80
81     addMouseMotionListener(this);
82
83     addMouseListener(this);
84
85     // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
86   }
87
88   public AnnotationPanel(AlignViewport av)
89   {
90     this.av = av;
91   }
92
93   public void adjustmentValueChanged(AdjustmentEvent evt)
94   {
95     ap.alabels.setScrollOffset(-evt.getValue());
96   }
97
98   /**
99    * DOCUMENT ME!
100    * 
101    * @param evt
102    *          DOCUMENT ME!
103    */
104   public void actionPerformed(ActionEvent evt)
105   {
106     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
107     if (aa==null)
108     {
109       return;
110     }
111     Annotation[] anot = aa[activeRow].annotations;
112
113     if (anot.length < av.getColumnSelection().getMax())
114     {
115       Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
116       System.arraycopy(anot, 0, temp, 0, anot.length);
117       anot = temp;
118       aa[activeRow].annotations = anot;
119     }
120
121     String label = "";
122     if (av.colSel != null && av.colSel.size() > 0
123             && anot[av.colSel.getMin()] != null)
124       label = anot[av.getColumnSelection().getMin()].displayCharacter;
125
126     if (evt.getActionCommand().equals(REMOVE))
127     {
128       for (int i = 0; i < av.getColumnSelection().size(); i++)
129       {
130         anot[av.getColumnSelection().columnAt(i)] = null;
131       }
132     }
133     else if (evt.getActionCommand().equals(LABEL))
134     {
135       label = enterLabel(label, "Enter Label");
136
137       if (label == null)
138       {
139         return;
140       }
141
142       if ((label.length() > 0) && !aa[activeRow].hasText)
143       {
144         aa[activeRow].hasText = true;
145       }
146
147       for (int i = 0; i < av.getColumnSelection().size(); i++)
148       {
149         int index = av.getColumnSelection().columnAt(i);
150
151         if (!av.colSel.isVisible(index))
152           continue;
153
154         if (anot[index] == null)
155         {
156           anot[index] = new Annotation(label, "", ' ', 0);
157         }
158
159         anot[index].displayCharacter = label;
160       }
161     }
162     else if (evt.getActionCommand().equals(COLOUR))
163     {
164       UserDefinedColours udc = new UserDefinedColours(this, Color.black,
165               ap.alignFrame);
166
167       Color col = udc.getColor();
168
169       for (int i = 0; i < av.getColumnSelection().size(); i++)
170       {
171         int index = av.getColumnSelection().columnAt(i);
172
173         if (!av.colSel.isVisible(index))
174           continue;
175
176         if (anot[index] == null)
177         {
178           anot[index] = new Annotation("", "", ' ', 0);
179         }
180
181         anot[index].colour = col;
182       }
183     }
184     else
185     // HELIX OR SHEET
186     {
187       char type = 0;
188       String symbol = "\u03B1";
189
190       if (evt.getActionCommand().equals(HELIX))
191       {
192         type = 'H';
193       }
194       else if (evt.getActionCommand().equals(SHEET))
195       {
196         type = 'E';
197         symbol = "\u03B2";
198       }
199
200       if (!aa[activeRow].hasIcons)
201       {
202         aa[activeRow].hasIcons = true;
203       }
204
205       label = enterLabel(symbol, "Enter Label");
206
207       if (label == null)
208       {
209         return;
210       }
211
212       if ((label.length() > 0) && !aa[activeRow].hasText)
213       {
214         aa[activeRow].hasText = true;
215       }
216
217       for (int i = 0; i < av.getColumnSelection().size(); i++)
218       {
219         int index = av.getColumnSelection().columnAt(i);
220
221         if (!av.colSel.isVisible(index))
222           continue;
223
224         if (anot[index] == null)
225         {
226           anot[index] = new Annotation(label, "", type, 0);
227         }
228
229         anot[index].secondaryStructure = type;
230         anot[index].displayCharacter = label;
231       }
232     }
233
234     adjustPanelHeight();
235     repaint();
236
237     return;
238   }
239
240   String enterLabel(String text, String label)
241   {
242     EditNameDialog dialog = new EditNameDialog(text, null, label, null,
243             ap.alignFrame, "Enter Label", 400, 200, true);
244
245     if (dialog.accept)
246       return dialog.getName();
247     else
248       return null;
249   }
250
251   public void mousePressed(MouseEvent evt)
252   {
253     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
254     if (aa == null)
255     {
256       return;
257     }
258
259     int height = 0;
260     activeRow = -1;
261
262     for (int i = 0; i < aa.length; i++)
263     {
264       if (aa[i].visible)
265       {
266         height += aa[i].height;
267       }
268
269       if (evt.getY() < height)
270       {
271         if (aa[i].editable)
272         {
273           activeRow = i;
274         }
275         else if (aa[i].graph > 0)
276         {
277           // Stretch Graph
278           graphStretch = i;
279           graphStretchY = evt.getY();
280         }
281
282         break;
283       }
284     }
285
286     if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
287             && activeRow != -1)
288     {
289       if (av.getColumnSelection() == null)
290       {
291         return;
292       }
293
294       PopupMenu pop = new PopupMenu("Structure type");
295       MenuItem item = new MenuItem(HELIX);
296       item.addActionListener(this);
297       pop.add(item);
298       item = new MenuItem(SHEET);
299       item.addActionListener(this);
300       pop.add(item);
301       item = new MenuItem(LABEL);
302       item.addActionListener(this);
303       pop.add(item);
304       item = new MenuItem(COLOUR);
305       item.addActionListener(this);
306       pop.add(item);
307       item = new MenuItem(REMOVE);
308       item.addActionListener(this);
309       pop.add(item);
310       ap.alignFrame.add(pop);
311       pop.show(this, evt.getX(), evt.getY());
312
313       return;
314     }
315
316     if (aa == null)
317     {
318       return;
319     }
320
321     ap.scalePanel.mousePressed(evt);
322   }
323
324   public void mouseReleased(MouseEvent evt)
325   {
326     graphStretch = -1;
327     graphStretchY = -1;
328     mouseDragging = false;
329     ap.scalePanel.mouseReleased(evt);
330   }
331
332   public void mouseClicked(MouseEvent evt)
333   {
334   }
335
336   public void mouseDragged(MouseEvent evt)
337   {
338     if (graphStretch > -1)
339     {
340       av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
341               - evt.getY();
342       if (av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight < 0)
343       {
344         av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight = 0;
345       }
346       graphStretchY = evt.getY();
347       adjustPanelHeight();
348       ap.paintAlignment(true);
349     }
350     else
351     {
352       ap.scalePanel.mouseDragged(evt);
353     }
354   }
355
356   public void mouseMoved(MouseEvent evt)
357   {
358     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
359     if (aa == null)
360     {
361       return;
362     }
363
364     int row = -1;
365     int height = 0;
366     for (int i = 0; i < aa.length; i++)
367     {
368
369       if (aa[i].visible)
370       {
371         height += aa[i].height;
372       }
373
374       if (evt.getY() < height)
375       {
376         row = i;
377         break;
378       }
379     }
380
381     int res = evt.getX() / av.getCharWidth() + av.getStartRes();
382
383     if (av.hasHiddenColumns)
384     {
385       res = av.getColumnSelection().adjustForHiddenColumns(res);
386     }
387
388     if (row > -1 && res < aa[row].annotations.length
389             && aa[row].annotations[res] != null)
390     {
391       StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
392       if (aa[row].annotations[res].description != null)
393       {
394         text.append("  " + aa[row].annotations[res].description);
395       }
396       ap.alignFrame.statusBar.setText(text.toString());
397     }
398   }
399
400   public void mouseEntered(MouseEvent evt)
401   {
402     ap.scalePanel.mouseEntered(evt);
403   }
404
405   public void mouseExited(MouseEvent evt)
406   {
407     ap.scalePanel.mouseExited(evt);
408   }
409
410   public int adjustPanelHeight()
411   {
412     // setHeight of panels
413     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
414     int height = 0;
415
416     if (aa != null)
417     {
418       for (int i = 0; i < aa.length; i++)
419       {
420         if (!aa[i].visible)
421         {
422           continue;
423         }
424
425         aa[i].height = 0;
426
427         if (aa[i].hasText)
428         {
429           aa[i].height += av.charHeight;
430         }
431
432         if (aa[i].hasIcons)
433         {
434           aa[i].height += 16;
435         }
436
437         if (aa[i].graph > 0)
438         {
439           aa[i].height += aa[i].graphHeight;
440         }
441
442         if (aa[i].height == 0)
443         {
444           aa[i].height = 20;
445         }
446
447         height += aa[i].height;
448       }
449     }
450     else
451     {
452       height = 20;
453     }
454
455     this.setSize(getSize().width, height);
456
457     repaint();
458
459     return height;
460
461   }
462
463   public void addEditableColumn(int i)
464   {
465     if (activeRow == -1)
466     {
467       AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
468       if (aa == null)
469       {
470         return;
471       }
472
473       for (int j = 0; j < aa.length; j++)
474       {
475         if (aa[j].editable)
476         {
477           activeRow = j;
478           break;
479         }
480       }
481     }
482
483     if (activeRes == null)
484     {
485       activeRes = new Vector();
486       activeRes.addElement(String.valueOf(i));
487       return;
488     }
489
490     activeRes.addElement(String.valueOf(i));
491   }
492
493   public void update(Graphics g)
494   {
495     paint(g);
496   }
497
498   public void paint(Graphics g)
499   {
500
501     imgWidth = getSize().width;
502     // (av.endRes - av.startRes + 1) * av.charWidth;
503
504     if (image == null || imgWidth != image.getWidth(this))
505     {
506       image = createImage(imgWidth, ap.annotationPanel.getSize().height);
507       gg = image.getGraphics();
508       gg.setFont(av.getFont());
509       fm = gg.getFontMetrics();
510       fastPaint = false;
511     }
512
513     if (fastPaint)
514     {
515       g.drawImage(image, 0, 0, this);
516       fastPaint = false;
517       return;
518     }
519
520     gg.setColor(Color.white);
521     gg.fillRect(0, 0, getSize().width, getSize().height);
522     drawComponent(gg, av.startRes, av.endRes + 1);
523
524     g.drawImage(image, 0, 0, this);
525   }
526
527   public void fastPaint(int horizontal)
528   {
529     if (horizontal == 0 || av.alignment.getAlignmentAnnotation() == null
530             || av.alignment.getAlignmentAnnotation().length < 1)
531     {
532       repaint();
533       return;
534     }
535
536     gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
537             * av.charWidth, 0);
538     int sr = av.startRes, er = av.endRes + 1, transX = 0;
539
540     if (horizontal > 0) // scrollbar pulled right, image to the left
541     {
542       transX = (er - sr - horizontal) * av.charWidth;
543       sr = er - horizontal;
544     }
545     else if (horizontal < 0)
546     {
547       er = sr - horizontal;
548     }
549
550     gg.translate(transX, 0);
551
552     drawComponent(gg, sr, er);
553
554     gg.translate(-transX, 0);
555
556     fastPaint = true;
557     repaint();
558   }
559
560   /**
561    * DOCUMENT ME!
562    * 
563    * @param g
564    *          DOCUMENT ME!
565    * @param startRes
566    *          DOCUMENT ME!
567    * @param endRes
568    *          DOCUMENT ME!
569    */
570   public void drawComponent(Graphics g, int startRes, int endRes)
571   {
572     Font ofont = av.getFont();
573     g.setFont(ofont);
574
575     g.setColor(Color.white);
576     g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
577
578     if (fm == null)
579     {
580       fm = g.getFontMetrics();
581     }
582
583     if ((av.alignment.getAlignmentAnnotation() == null)
584             || (av.alignment.getAlignmentAnnotation().length < 1))
585     {
586       g.setColor(Color.white);
587       g.fillRect(0, 0, getSize().width, getSize().height);
588       g.setColor(Color.black);
589       if (av.validCharWidth)
590       {
591         g.drawString("Alignment has no annotations", 20, 15);
592       }
593
594       return;
595     }
596
597     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
598
599     int x = 0;
600     int y = 0;
601     int column = 0;
602     char lastSS;
603     int lastSSX;
604     int iconOffset = av.charHeight / 2;
605     boolean validRes = false;
606     boolean validEnd = false;
607     boolean labelAllCols = false;
608     boolean centreColLabels, centreColLabelsDef = av
609             .getCentreColumnLabels();
610     boolean scaleColLabel = false;
611     boolean[] graphGroupDrawn = new boolean[aa.length];
612     int charOffset = 0; // offset for a label
613     float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a
614                                    // column.
615     // \u03B2 \u03B1
616     for (int i = 0; i < aa.length; i++)
617     {
618       AlignmentAnnotation row = aa[i];
619
620       if (!row.visible)
621       {
622         continue;
623       }
624       centreColLabels = row.centreColLabels || centreColLabelsDef;
625       labelAllCols = row.showAllColLabels;
626       scaleColLabel = row.scaleColLabel;
627       lastSS = ' ';
628       lastSSX = 0;
629
630       if (row.graph > 0)
631       {
632         if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
633         {
634           continue;
635         }
636
637         // this is so that we draw the characters below the graph
638         y += row.height;
639
640         if (row.hasText)
641         {
642           iconOffset = av.charHeight - fm.getDescent();
643           y -= av.charHeight;
644         }
645       }
646       // TODO: else is the logic used in application, applet had no 'else'
647       else if (row.hasText)
648       {
649         iconOffset = av.charHeight - fm.getDescent();
650
651       }
652       else
653       {
654         iconOffset = 0;
655       }
656
657       x = 0;
658       while (x < endRes - startRes)
659       {
660         if (av.hasHiddenColumns)
661         {
662           column = av.getColumnSelection().adjustForHiddenColumns(
663                   startRes + x);
664           if (column > row.annotations.length - 1)
665           {
666             break;
667           }
668         }
669         else
670         {
671           column = startRes + x;
672         }
673
674         if ((row.annotations.length <= column)
675                 || (row.annotations[column] == null))
676         {
677           validRes = false;
678         }
679         else
680         {
681           validRes = true;
682         }
683
684         if (activeRow == i)
685         {
686           g.setColor(Color.red);
687
688           if (av.getColumnSelection() != null)
689           {
690             for (int n = 0; n < av.getColumnSelection().size(); n++)
691             {
692               int v = av.getColumnSelection().columnAt(n);
693
694               if (v == column)
695               {
696                 g
697                         .fillRect(x * av.charWidth, y, av.charWidth,
698                                 av.charHeight);
699               }
700             }
701           }
702         }
703
704         if (av.validCharWidth
705                 && validRes
706                 && (row.annotations[column].displayCharacter != null && row.annotations[column].displayCharacter
707                         .length() > 0))
708         {
709
710           if (centreColLabels || scaleColLabel)
711           {
712             fmWidth = (float) fm.charsWidth(
713                     row.annotations[column].displayCharacter.toCharArray(),
714                     0, row.annotations[column].displayCharacter.length());
715
716             if (scaleColLabel)
717             {
718               // justify the label and scale to fit in column
719               if (fmWidth > av.charWidth)
720               {
721                 // scale only if the current font isn't already small enough
722                 fmScaling = av.charWidth;
723                 fmScaling /= fmWidth;
724                 // not 1.1 // g.setFont(new
725                 // Font(ofont,AffineTransform.getScaleInstance(fmScaling,
726                 // 1.0)));
727                 // and update the label's width to reflect the scaling.
728                 fmWidth = av.charWidth;
729               }
730             }
731           }
732           else
733           {
734             fmWidth = (float) fm
735                     .charWidth(row.annotations[column].displayCharacter
736                             .charAt(0));
737           }
738           charOffset = (int) ((av.charWidth - fmWidth) / 2f);
739
740           if (row.annotations[column].colour == null)
741             g.setColor(Color.black);
742           else
743             g.setColor(row.annotations[column].colour);
744
745           if (column == 0 || row.graph > 0)
746           {
747             g.drawString(row.annotations[column].displayCharacter,
748                     (x * av.charWidth) + charOffset, y + iconOffset + 3); // +
749                                                                           // 3?
750           }
751           else if (row.annotations[column - 1] == null
752                   || (labelAllCols
753                           || !row.annotations[column].displayCharacter
754                                   .equals(row.annotations[column - 1].displayCharacter) || (row.annotations[column].displayCharacter
755                           .length() < 2 && row.annotations[column].secondaryStructure == ' ')))
756           {
757             g.drawString(row.annotations[column].displayCharacter,
758                     (x * av.charWidth) + charOffset, y + iconOffset + 3); // +3?
759           }
760           g.setFont(ofont);
761         }
762
763         if (row.hasIcons)
764         {
765           if (!validRes
766                   || (row.annotations[column].secondaryStructure != lastSS))
767           {
768             switch (lastSS)
769             {
770             case 'H':
771               g.setColor(HELIX_COLOUR);
772               if (MAC)
773               {
774                 // Off by 1 offset when drawing rects and ovals
775                 // to offscreen image on the MAC
776                 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
777                         (x * av.charWidth) - lastSSX, 7, 8, 8);
778                 break;
779               }
780
781               int sCol = (lastSSX / av.charWidth) + startRes;
782               int x1 = lastSSX;
783               int x2 = (x * av.charWidth);
784
785               if (sCol == 0
786                       || row.annotations[sCol - 1] == null
787                       || row.annotations[sCol - 1].secondaryStructure != 'H')
788               {
789                 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90,
790                         180);
791                 x1 += av.charWidth / 2;
792               }
793
794               if (!validRes || row.annotations[column] == null
795                       || row.annotations[column].secondaryStructure != 'H')
796               {
797                 g.fillArc((x * av.charWidth) - av.charWidth, y + 4
798                         + iconOffset, av.charWidth, 8, 270, 180);
799                 x2 -= av.charWidth / 2;
800               }
801
802               g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
803               break;
804
805             case 'E':
806               g.setColor(SHEET_COLOUR);
807               g.fillRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
808                       - lastSSX - 4, 7);
809               g.fillPolygon(new int[]
810               { (x * av.charWidth) - 4, (x * av.charWidth) - 4,
811                   (x * av.charWidth) }, new int[]
812               { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
813                       3);
814
815               break;
816
817             default:
818               g.setColor(Color.gray);
819               g.fillRect(lastSSX, y + 6 + iconOffset, (x * av.charWidth)
820                       - lastSSX, 2);
821
822               break;
823             }
824
825             if (validRes)
826             {
827               lastSS = row.annotations[column].secondaryStructure;
828             }
829             else
830             {
831               lastSS = ' ';
832             }
833
834             lastSSX = (x * av.charWidth);
835           }
836         }
837
838         column++;
839         x++;
840       }
841
842       if (column >= row.annotations.length)
843       {
844         column = row.annotations.length - 1;
845         validEnd = false;
846       }
847       else
848       {
849         validEnd = true;
850       }
851
852       // x ++;
853
854       if (row.hasIcons)
855       {
856         switch (lastSS)
857         {
858         case 'H':
859           g.setColor(HELIX_COLOUR);
860           if (MAC)
861           {
862             // Off by 1 offset when drawing rects and ovals
863             // to offscreen image on the MAC
864             g.fillRoundRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
865                     - lastSSX, 7, 8, 8);
866             break;
867           }
868
869           int sCol = (lastSSX / av.charWidth) + startRes;
870           int x1 = lastSSX;
871           int x2 = (x * av.charWidth);
872
873           if (sCol == 0 || row.annotations[sCol - 1] == null
874                   || row.annotations[sCol - 1].secondaryStructure != 'H')
875           {
876             g
877                     .fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8,
878                             90, 180);
879             x1 += av.charWidth / 2;
880           }
881
882           if (row.annotations[column] == null
883                   || row.annotations[column].secondaryStructure != 'H')
884           {
885             g.fillArc((x * av.charWidth) - av.charWidth,
886                     y + 4 + iconOffset, av.charWidth, 8, 270, 180);
887             x2 -= av.charWidth / 2;
888           }
889
890           g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
891
892           break;
893
894         case 'E':
895           g.setColor(SHEET_COLOUR);
896
897           if (!validEnd || row.annotations[endRes] == null
898                   || row.annotations[endRes].secondaryStructure != 'E')
899           {
900             g.fillRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
901                     - lastSSX - 4, 7);
902             g.fillPolygon(new int[]
903             { (x * av.charWidth) - 4, (x * av.charWidth) - 4,
904                 (x * av.charWidth) }, new int[]
905             { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset }, 3);
906           }
907           else
908           {
909             g.fillRect(lastSSX, y + 4 + iconOffset, x * av.charWidth
910                     - lastSSX, 7);
911           }
912           break;
913
914         default:
915           g.setColor(Color.gray);
916           if (!av.wrapAlignment || endRes == av.endRes)
917           {
918             g.fillRect(lastSSX, y + 6 + iconOffset, (x * av.charWidth)
919                     - lastSSX, 2);
920           }
921
922           break;
923         }
924       }
925
926       if (row.graph > 0 && row.graphHeight > 0)
927       {
928         if (row.graph == AlignmentAnnotation.LINE_GRAPH)
929         {
930           if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
931           {
932             float groupmax = -999999, groupmin = 9999999;
933             for (int gg = 0; gg < aa.length; gg++)
934             {
935               if (aa[gg].graphGroup != row.graphGroup)
936               {
937                 continue;
938               }
939
940               if (aa[gg] != row)
941               {
942                 aa[gg].visible = false;
943               }
944
945               if (aa[gg].graphMax > groupmax)
946               {
947                 groupmax = aa[gg].graphMax;
948               }
949               if (aa[gg].graphMin < groupmin)
950               {
951                 groupmin = aa[gg].graphMin;
952               }
953             }
954
955             for (int gg = 0; gg < aa.length; gg++)
956             {
957               if (aa[gg].graphGroup == row.graphGroup)
958               {
959                 drawLineGraph(g, aa[gg], startRes, endRes, y, groupmin,
960                         groupmax, row.graphHeight);
961               }
962             }
963
964             graphGroupDrawn[row.graphGroup] = true;
965           }
966           else
967           {
968             drawLineGraph(g, row, startRes, endRes, y, row.graphMin,
969                     row.graphMax, row.graphHeight);
970           }
971         }
972         else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
973         {
974           drawBarGraph(g, row, startRes, endRes, row.graphMin,
975                   row.graphMax, y);
976         }
977       }
978
979       if (row.graph > 0 && row.hasText)
980       {
981         y += av.charHeight;
982       }
983
984       if (row.graph == 0)
985       {
986         y += aa[i].height;
987       }
988     }
989   }
990
991   public void drawLineGraph(Graphics g, AlignmentAnnotation aa, int sRes,
992           int eRes, int y, float min, float max, int graphHeight)
993   {
994     if (sRes > aa.annotations.length)
995     {
996       return;
997     }
998
999     int x = 0;
1000
1001     // Adjustment for fastpaint to left
1002     if (eRes < av.endRes)
1003     {
1004       eRes++;
1005     }
1006
1007     eRes = Math.min(eRes, aa.annotations.length);
1008
1009     int y1 = y, y2 = y;
1010     float range = max - min;
1011
1012     // //Draw origin
1013     if (min < 0)
1014     {
1015       y2 = y - (int) ((0 - min / range) * graphHeight);
1016     }
1017
1018     g.setColor(Color.gray);
1019     g.drawLine(x - av.charWidth, y2, (eRes - sRes) * av.charWidth, y2);
1020
1021     eRes = Math.min(eRes, aa.annotations.length);
1022
1023     int column;
1024     int aaMax = aa.annotations.length - 1;
1025
1026     while (x < eRes - sRes)
1027     {
1028       column = sRes + x;
1029       if (av.hasHiddenColumns)
1030       {
1031         column = av.getColumnSelection().adjustForHiddenColumns(column);
1032       }
1033
1034       if (column > aaMax)
1035       {
1036         break;
1037       }
1038
1039       if (aa.annotations[column] == null) // || coaa.annotations[column - 1] ==
1040       // null)
1041       {
1042         x++;
1043         continue;
1044       }
1045
1046       if (aa.annotations[column].colour == null)
1047         g.setColor(Color.black);
1048       else
1049         g.setColor(aa.annotations[column].colour);
1050       if (column == 0 || aa.annotations[column - 1] == null)
1051       {
1052         y1 = y
1053                 - (int) (((aa.annotations[column].value - min) / range) * graphHeight);
1054       }
1055       else
1056       {
1057         y1 = y
1058                 - (int) (((aa.annotations[column - 1].value - min) / range) * graphHeight);
1059       }
1060       y2 = y
1061               - (int) (((aa.annotations[column].value - min) / range) * graphHeight);
1062
1063       g.drawLine(x * av.charWidth - av.charWidth / 2, y1, x * av.charWidth
1064               + av.charWidth / 2, y2);
1065       x++;
1066     }
1067
1068     if (aa.threshold != null)
1069     {
1070       g.setColor(aa.threshold.colour);
1071
1072       y2 = (int) (y - ((aa.threshold.value - min) / range) * graphHeight);
1073       g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1074     }
1075   }
1076
1077   public void drawBarGraph(Graphics g, AlignmentAnnotation aa, int sRes,
1078           int eRes, float min, float max, int y)
1079   {
1080     ColourSchemeI profcolour = av.getGlobalColourScheme();
1081     if (profcolour == null)
1082     {
1083       profcolour = new jalview.schemes.ZappoColourScheme();
1084     }
1085     if (sRes > aa.annotations.length)
1086     {
1087       return;
1088     }
1089     Font ofont = g.getFont();
1090     eRes = Math.min(eRes, aa.annotations.length);
1091
1092     int x = 0, y1 = y, y2 = y;
1093
1094     float range = max - min;
1095
1096     if (min < 0)
1097     {
1098       y2 = y - (int) ((0 - min / (range)) * aa.graphHeight);
1099     }
1100
1101     g.setColor(Color.gray);
1102
1103     g.drawLine(x, y2, (eRes - sRes) * av.charWidth, y2);
1104
1105     int column;
1106     int aaMax = aa.annotations.length - 1;
1107     boolean renderHistogram = true, renderProfile = false;
1108     /*
1109      * Logos are disabled for 2.5 release : Bug # 0060064 if (aa.autoCalculated
1110      * && aa.label.startsWith("Consensus")) { // TODO: generalise this to have
1111      * render styles for consensus/profile data if (aa.groupRef!=null) {
1112      * renderHistogram = aa.groupRef.isShowConsensusHistogram(); renderProfile =
1113      * aa.groupRef.isShowSequenceLogo(); } else { renderHistogram =
1114      * av.isShowConsensusHistogram(); renderProfile = av.isShowSequenceLogo(); }
1115      * }
1116      */
1117     while (x < eRes - sRes)
1118     {
1119       column = sRes + x;
1120       if (av.hasHiddenColumns)
1121       {
1122         column = av.getColumnSelection().adjustForHiddenColumns(column);
1123       }
1124
1125       if (column > aaMax)
1126       {
1127         break;
1128       }
1129
1130       if (aa.annotations[column] == null)
1131       {
1132         x++;
1133         continue;
1134       }
1135
1136       if (aa.annotations[column].colour == null)
1137         g.setColor(Color.black);
1138       else
1139         g.setColor(aa.annotations[column].colour);
1140
1141       y1 = y
1142               - (int) (((aa.annotations[column].value - min) / (range)) * aa.graphHeight);
1143
1144       if (renderHistogram)
1145       {
1146         if (y1 - y2 > 0)
1147         {
1148           g.fillRect(x * av.charWidth, y2, av.charWidth, y1 - y2);
1149         }
1150         else
1151         {
1152           g.fillRect(x * av.charWidth, y1, av.charWidth, y2 - y1);
1153         }
1154       }
1155       // draw profile if available
1156       // Disabled for 2.5 release: see bug #0060064
1157       /**
1158        * if (aa.annotations[column].value!=0 && renderProfile) { int profl[] =
1159        * getProfileFor(aa,column); int ht = y1; //,htn=y2-y1;//aa.graphHeight;
1160        * float wdth; double ht2=0; char[] dc = new char[1]; // LineMetrics lm;
1161        * for (int c=1;profl!=null && c<profl[0];) { dc[0] = (char) profl[c++];
1162        * wdth = av.charWidth; wdth/=(float) fm .charsWidth(dc,0,1);
1163        * 
1164        * if (c>2) { ht+=(int)ht2; } { // not java 1.1 compatible: Bug # 0060064
1165        * g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(wdth,
1166        * (ht2=(htn*((double)profl[c++])/100.0))/av.charHeight))); lm =
1167        * g.getFontMetrics().getLineMetrics(dc,0,1, g);
1168        * g.setColor(profcolour.findColour(dc[0]));
1169        * g.drawChars(dc,0,1,x*av.charWidth, (int) (ht+lm.getHeight())); } }
1170        * g.setFont(ofont); }
1171        **/
1172       x++;
1173
1174     }
1175     if (aa.threshold != null)
1176     {
1177       g.setColor(aa.threshold.colour);
1178       y2 = (int) (y - ((aa.threshold.value - min) / range) * aa.graphHeight);
1179       g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1180     }
1181   }
1182
1183   /*
1184    * Disabled for 2.5 release - see bug #0060064 private int[]
1185    * getProfileFor(AlignmentAnnotation aa, int column) { // if
1186    * (aa.autoCalculated && aa.label.startsWith("Consensus")) { if
1187    * (aa.groupRef!=null && aa.groupRef.consensusData!=null) { // &&
1188    * aa.groupRef.isShowSequenceLogo()) { return
1189    * AAFrequency.extractProfile(aa.groupRef
1190    * .consensusData[column],aa.groupRef.getIgnoreGapsConsensus()); } // TODO
1191    * extend annotation row to enable dynamic and static profile data to be
1192    * stored if (aa.groupRef==null && aa.sequenceRef==null) // &&
1193    * av.isShowSequenceLogo()) { return
1194    * AAFrequency.extractProfile(av.hconsensus[column
1195    * ],av.getIgnoreGapsConsensus()); } // } return null; }
1196    */
1197
1198   // used by overview window
1199   public void drawGraph(Graphics g, AlignmentAnnotation aa, int width,
1200           int y, int sRes, int eRes)
1201   {
1202     eRes = Math.min(eRes, aa.annotations.length);
1203     g.setColor(Color.white);
1204     g.fillRect(0, 0, width, y);
1205     g.setColor(new Color(0, 0, 180));
1206
1207     int x = 0, height;
1208
1209     for (int j = sRes; j < eRes; j++)
1210     {
1211       if (aa.annotations[j].colour == null)
1212         g.setColor(Color.black);
1213       else
1214         g.setColor(aa.annotations[j].colour);
1215
1216       height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
1217       if (height > y)
1218       {
1219         height = y;
1220       }
1221       g.fillRect(x, y - height, av.charWidth, height);
1222       x += av.charWidth;
1223     }
1224   }
1225 }