Jalview 2.6 source licence
[jalview.git] / src / jalview / appletgui / AnnotationPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
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     if (needValidating)
330     {
331       ap.validate();
332       needValidating=false;
333     }
334     ap.scalePanel.mouseReleased(evt);
335   }
336
337   public void mouseClicked(MouseEvent evt)
338   {
339   }
340   boolean needValidating=false;
341   public void mouseDragged(MouseEvent evt)
342   {
343     if (graphStretch > -1)
344     {
345       av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
346               - evt.getY();
347       if (av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight < 0)
348       {
349         av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight = 0;
350       }
351       graphStretchY = evt.getY();
352       adjustPanelHeight();
353       needValidating=true;
354       ap.paintAlignment(true);
355     }
356     else
357     {
358       ap.scalePanel.mouseDragged(evt);
359     }
360   }
361
362   public void mouseMoved(MouseEvent evt)
363   {
364     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
365     if (aa == null)
366     {
367       return;
368     }
369
370     int row = -1;
371     int height = 0;
372     for (int i = 0; i < aa.length; i++)
373     {
374
375       if (aa[i].visible)
376       {
377         height += aa[i].height;
378       }
379
380       if (evt.getY() < height)
381       {
382         row = i;
383         break;
384       }
385     }
386
387     int res = evt.getX() / av.getCharWidth() + av.getStartRes();
388
389     if (av.hasHiddenColumns)
390     {
391       res = av.getColumnSelection().adjustForHiddenColumns(res);
392     }
393
394     if (row > -1 && res < aa[row].annotations.length
395             && aa[row].annotations[res] != null)
396     {
397       StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
398       if (aa[row].annotations[res].description != null)
399       {
400         text.append("  " + aa[row].annotations[res].description);
401       }
402       ap.alignFrame.statusBar.setText(text.toString());
403     }
404   }
405
406   public void mouseEntered(MouseEvent evt)
407   {
408     ap.scalePanel.mouseEntered(evt);
409   }
410
411   public void mouseExited(MouseEvent evt)
412   {
413     ap.scalePanel.mouseExited(evt);
414   }
415
416   public int adjustPanelHeight()
417   {
418     // setHeight of panels
419     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
420     int height = 0;
421
422     if (aa != null)
423     {
424       for (int i = 0; i < aa.length; i++)
425       {
426         if (!aa[i].visible)
427         {
428           continue;
429         }
430
431         aa[i].height = 0;
432
433         if (aa[i].hasText)
434         {
435           aa[i].height += av.charHeight;
436         }
437
438         if (aa[i].hasIcons)
439         {
440           aa[i].height += 16;
441         }
442
443         if (aa[i].graph > 0)
444         {
445           aa[i].height += aa[i].graphHeight;
446         }
447
448         if (aa[i].height == 0)
449         {
450           aa[i].height = 20;
451         }
452
453         height += aa[i].height;
454       }
455     }
456     if (height==0)
457     {
458       height = 20;
459     }
460     this.setSize(getSize().width, height);
461     repaint();
462
463     return height;
464
465   }
466
467   public void addEditableColumn(int i)
468   {
469     if (activeRow == -1)
470     {
471       AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
472       if (aa == null)
473       {
474         return;
475       }
476
477       for (int j = 0; j < aa.length; j++)
478       {
479         if (aa[j].editable)
480         {
481           activeRow = j;
482           break;
483         }
484       }
485     }
486
487     if (activeRes == null)
488     {
489       activeRes = new Vector();
490       activeRes.addElement(String.valueOf(i));
491       return;
492     }
493
494     activeRes.addElement(String.valueOf(i));
495   }
496
497   public void update(Graphics g)
498   {
499     paint(g);
500   }
501
502   public void paint(Graphics g)
503   {
504
505     imgWidth = getSize().width;
506     // (av.endRes - av.startRes + 1) * av.charWidth;
507
508     if (image == null || imgWidth != image.getWidth(this))
509     {
510       image = createImage(imgWidth, ap.annotationPanel.getSize().height);
511       gg = image.getGraphics();
512       gg.setFont(av.getFont());
513       fm = gg.getFontMetrics();
514       fastPaint = false;
515     }
516
517     if (fastPaint)
518     {
519       g.drawImage(image, 0, 0, this);
520       fastPaint = false;
521       return;
522     }
523
524     gg.setColor(Color.white);
525     gg.fillRect(0, 0, getSize().width, getSize().height);
526     drawComponent(gg, av.startRes, av.endRes + 1);
527
528     g.drawImage(image, 0, 0, this);
529   }
530
531   public void fastPaint(int horizontal)
532   {
533     if (horizontal == 0 || av.alignment.getAlignmentAnnotation() == null
534             || av.alignment.getAlignmentAnnotation().length < 1)
535     {
536       repaint();
537       return;
538     }
539
540     gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
541             * av.charWidth, 0);
542     int sr = av.startRes, er = av.endRes + 1, transX = 0;
543
544     if (horizontal > 0) // scrollbar pulled right, image to the left
545     {
546       transX = (er - sr - horizontal) * av.charWidth;
547       sr = er - horizontal;
548     }
549     else if (horizontal < 0)
550     {
551       er = sr - horizontal;
552     }
553
554     gg.translate(transX, 0);
555
556     drawComponent(gg, sr, er);
557
558     gg.translate(-transX, 0);
559
560     fastPaint = true;
561     repaint();
562   }
563
564   /**
565    * DOCUMENT ME!
566    * 
567    * @param g
568    *          DOCUMENT ME!
569    * @param startRes
570    *          DOCUMENT ME!
571    * @param endRes
572    *          DOCUMENT ME!
573    */
574   public void drawComponent(Graphics g, int startRes, int endRes)
575   {
576     Font ofont = av.getFont();
577     g.setFont(ofont);
578
579     g.setColor(Color.white);
580     g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
581
582     if (fm == null)
583     {
584       fm = g.getFontMetrics();
585     }
586
587     if ((av.alignment.getAlignmentAnnotation() == null)
588             || (av.alignment.getAlignmentAnnotation().length < 1))
589     {
590       g.setColor(Color.white);
591       g.fillRect(0, 0, getSize().width, getSize().height);
592       g.setColor(Color.black);
593       if (av.validCharWidth)
594       {
595         g.drawString("Alignment has no annotations", 20, 15);
596       }
597
598       return;
599     }
600
601     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
602
603     int x = 0;
604     int y = 0;
605     int column = 0;
606     char lastSS;
607     int lastSSX;
608     int iconOffset = av.charHeight / 2;
609     boolean validRes = false;
610     boolean validEnd = false;
611     boolean labelAllCols = false;
612     boolean centreColLabels, centreColLabelsDef = av
613             .getCentreColumnLabels();
614     boolean scaleColLabel = false;
615     boolean[] graphGroupDrawn = new boolean[aa.length];
616     int charOffset = 0; // offset for a label
617     float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a
618                                    // column.
619     // \u03B2 \u03B1
620     for (int i = 0; i < aa.length; i++)
621     {
622       AlignmentAnnotation row = aa[i];
623
624       if (!row.visible)
625       {
626         continue;
627       }
628       centreColLabels = row.centreColLabels || centreColLabelsDef;
629       labelAllCols = row.showAllColLabels;
630       scaleColLabel = row.scaleColLabel;
631       lastSS = ' ';
632       lastSSX = 0;
633
634       if (row.graph > 0)
635       {
636         if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
637         {
638           continue;
639         }
640
641         // this is so that we draw the characters below the graph
642         y += row.height;
643
644         if (row.hasText)
645         {
646           iconOffset = av.charHeight - fm.getDescent();
647           y -= av.charHeight;
648         }
649       }
650       // TODO: else is the logic used in application, applet had no 'else'
651       else if (row.hasText)
652       {
653         iconOffset = av.charHeight - fm.getDescent();
654
655       }
656       else
657       {
658         iconOffset = 0;
659       }
660
661       x = 0;
662       while (x < endRes - startRes)
663       {
664         if (av.hasHiddenColumns)
665         {
666           column = av.getColumnSelection().adjustForHiddenColumns(
667                   startRes + x);
668           if (column > row.annotations.length - 1)
669           {
670             break;
671           }
672         }
673         else
674         {
675           column = startRes + x;
676         }
677
678         if ((row.annotations.length <= column)
679                 || (row.annotations[column] == null))
680         {
681           validRes = false;
682         }
683         else
684         {
685           validRes = true;
686         }
687
688         if (activeRow == i)
689         {
690           g.setColor(Color.red);
691
692           if (av.getColumnSelection() != null)
693           {
694             for (int n = 0; n < av.getColumnSelection().size(); n++)
695             {
696               int v = av.getColumnSelection().columnAt(n);
697
698               if (v == column)
699               {
700                 g
701                         .fillRect(x * av.charWidth, y, av.charWidth,
702                                 av.charHeight);
703               }
704             }
705           }
706         }
707
708         if (av.validCharWidth
709                 && validRes
710                 && (row.annotations[column].displayCharacter != null && row.annotations[column].displayCharacter
711                         .length() > 0))
712         {
713
714           if (centreColLabels || scaleColLabel)
715           {
716             fmWidth = (float) fm.charsWidth(
717                     row.annotations[column].displayCharacter.toCharArray(),
718                     0, row.annotations[column].displayCharacter.length());
719
720             if (scaleColLabel)
721             {
722               // justify the label and scale to fit in column
723               if (fmWidth > av.charWidth)
724               {
725                 // scale only if the current font isn't already small enough
726                 fmScaling = av.charWidth;
727                 fmScaling /= fmWidth;
728                 // not 1.1 // g.setFont(new
729                 // Font(ofont,AffineTransform.getScaleInstance(fmScaling,
730                 // 1.0)));
731                 // and update the label's width to reflect the scaling.
732                 fmWidth = av.charWidth;
733               }
734             }
735           }
736           else
737           {
738             fmWidth = (float) fm
739                     .charWidth(row.annotations[column].displayCharacter
740                             .charAt(0));
741           }
742           charOffset = (int) ((av.charWidth - fmWidth) / 2f);
743
744           if (row.annotations[column].colour == null)
745             g.setColor(Color.black);
746           else
747             g.setColor(row.annotations[column].colour);
748
749           if (column == 0 || row.graph > 0)
750           {
751             g.drawString(row.annotations[column].displayCharacter,
752                     (x * av.charWidth) + charOffset, y + iconOffset + 3); // +
753                                                                           // 3?
754           }
755           else if (row.annotations[column - 1] == null
756                   || (labelAllCols
757                           || !row.annotations[column].displayCharacter
758                                   .equals(row.annotations[column - 1].displayCharacter) || (row.annotations[column].displayCharacter
759                           .length() < 2 && row.annotations[column].secondaryStructure == ' ')))
760           {
761             g.drawString(row.annotations[column].displayCharacter,
762                     (x * av.charWidth) + charOffset, y + iconOffset + 3); // +3?
763           }
764           g.setFont(ofont);
765         }
766
767         if (row.hasIcons)
768         {
769           if (!validRes
770                   || (row.annotations[column].secondaryStructure != lastSS))
771           {
772             switch (lastSS)
773             {
774             case 'H':
775               g.setColor(HELIX_COLOUR);
776               if (MAC)
777               {
778                 // Off by 1 offset when drawing rects and ovals
779                 // to offscreen image on the MAC
780                 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
781                         (x * av.charWidth) - lastSSX, 7, 8, 8);
782                 break;
783               }
784
785               int sCol = (lastSSX / av.charWidth) + startRes;
786               int x1 = lastSSX;
787               int x2 = (x * av.charWidth);
788
789               if (sCol == 0
790                       || row.annotations[sCol - 1] == null
791                       || row.annotations[sCol - 1].secondaryStructure != 'H')
792               {
793                 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90,
794                         180);
795                 x1 += av.charWidth / 2;
796               }
797
798               if (!validRes || row.annotations[column] == null
799                       || row.annotations[column].secondaryStructure != 'H')
800               {
801                 g.fillArc((x * av.charWidth) - av.charWidth, y + 4
802                         + iconOffset, av.charWidth, 8, 270, 180);
803                 x2 -= av.charWidth / 2;
804               }
805
806               g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
807               break;
808
809             case 'E':
810               g.setColor(SHEET_COLOUR);
811               g.fillRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
812                       - lastSSX - 4, 7);
813               g.fillPolygon(new int[]
814               { (x * av.charWidth) - 4, (x * av.charWidth) - 4,
815                   (x * av.charWidth) }, new int[]
816               { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
817                       3);
818
819               break;
820
821             default:
822               g.setColor(Color.gray);
823               g.fillRect(lastSSX, y + 6 + iconOffset, (x * av.charWidth)
824                       - lastSSX, 2);
825
826               break;
827             }
828
829             if (validRes)
830             {
831               lastSS = row.annotations[column].secondaryStructure;
832             }
833             else
834             {
835               lastSS = ' ';
836             }
837
838             lastSSX = (x * av.charWidth);
839           }
840         }
841
842         column++;
843         x++;
844       }
845
846       if (column >= row.annotations.length)
847       {
848         column = row.annotations.length - 1;
849         validEnd = false;
850       }
851       else
852       {
853         validEnd = true;
854       }
855
856       // x ++;
857
858       if (row.hasIcons)
859       {
860         switch (lastSS)
861         {
862         case 'H':
863           g.setColor(HELIX_COLOUR);
864           if (MAC)
865           {
866             // Off by 1 offset when drawing rects and ovals
867             // to offscreen image on the MAC
868             g.fillRoundRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
869                     - lastSSX, 7, 8, 8);
870             break;
871           }
872
873           int sCol = (lastSSX / av.charWidth) + startRes;
874           int x1 = lastSSX;
875           int x2 = (x * av.charWidth);
876
877           if (sCol == 0 || row.annotations[sCol - 1] == null
878                   || row.annotations[sCol - 1].secondaryStructure != 'H')
879           {
880             g
881                     .fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8,
882                             90, 180);
883             x1 += av.charWidth / 2;
884           }
885
886           if (row.annotations[column] == null
887                   || row.annotations[column].secondaryStructure != 'H')
888           {
889             g.fillArc((x * av.charWidth) - av.charWidth,
890                     y + 4 + iconOffset, av.charWidth, 8, 270, 180);
891             x2 -= av.charWidth / 2;
892           }
893
894           g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
895
896           break;
897
898         case 'E':
899           g.setColor(SHEET_COLOUR);
900
901           if (!validEnd || row.annotations[endRes] == null
902                   || row.annotations[endRes].secondaryStructure != 'E')
903           {
904             g.fillRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
905                     - lastSSX - 4, 7);
906             g.fillPolygon(new int[]
907             { (x * av.charWidth) - 4, (x * av.charWidth) - 4,
908                 (x * av.charWidth) }, new int[]
909             { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset }, 3);
910           }
911           else
912           {
913             g.fillRect(lastSSX, y + 4 + iconOffset, x * av.charWidth
914                     - lastSSX, 7);
915           }
916           break;
917
918         default:
919           g.setColor(Color.gray);
920           if (!av.wrapAlignment || endRes == av.endRes)
921           {
922             g.fillRect(lastSSX, y + 6 + iconOffset, (x * av.charWidth)
923                     - lastSSX, 2);
924           }
925
926           break;
927         }
928       }
929
930       if (row.graph > 0 && row.graphHeight > 0)
931       {
932         if (row.graph == AlignmentAnnotation.LINE_GRAPH)
933         {
934           if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
935           {
936             float groupmax = -999999, groupmin = 9999999;
937             for (int gg = 0; gg < aa.length; gg++)
938             {
939               if (aa[gg].graphGroup != row.graphGroup)
940               {
941                 continue;
942               }
943
944               if (aa[gg] != row)
945               {
946                 aa[gg].visible = false;
947               }
948
949               if (aa[gg].graphMax > groupmax)
950               {
951                 groupmax = aa[gg].graphMax;
952               }
953               if (aa[gg].graphMin < groupmin)
954               {
955                 groupmin = aa[gg].graphMin;
956               }
957             }
958
959             for (int gg = 0; gg < aa.length; gg++)
960             {
961               if (aa[gg].graphGroup == row.graphGroup)
962               {
963                 drawLineGraph(g, aa[gg], startRes, endRes, y, groupmin,
964                         groupmax, row.graphHeight);
965               }
966             }
967
968             graphGroupDrawn[row.graphGroup] = true;
969           }
970           else
971           {
972             drawLineGraph(g, row, startRes, endRes, y, row.graphMin,
973                     row.graphMax, row.graphHeight);
974           }
975         }
976         else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
977         {
978           drawBarGraph(g, row, startRes, endRes, row.graphMin,
979                   row.graphMax, y);
980         }
981       }
982
983       if (row.graph > 0 && row.hasText)
984       {
985         y += av.charHeight;
986       }
987
988       if (row.graph == 0)
989       {
990         y += aa[i].height;
991       }
992     }
993   }
994
995   public void drawLineGraph(Graphics g, AlignmentAnnotation aa, int sRes,
996           int eRes, int y, float min, float max, int graphHeight)
997   {
998     if (sRes > aa.annotations.length)
999     {
1000       return;
1001     }
1002
1003     int x = 0;
1004
1005     // Adjustment for fastpaint to left
1006     if (eRes < av.endRes)
1007     {
1008       eRes++;
1009     }
1010
1011     eRes = Math.min(eRes, aa.annotations.length);
1012
1013     int y1 = y, y2 = y;
1014     float range = max - min;
1015
1016     // //Draw origin
1017     if (min < 0)
1018     {
1019       y2 = y - (int) ((0 - min / range) * graphHeight);
1020     }
1021
1022     g.setColor(Color.gray);
1023     g.drawLine(x - av.charWidth, y2, (eRes - sRes) * av.charWidth, y2);
1024
1025     eRes = Math.min(eRes, aa.annotations.length);
1026
1027     int column;
1028     int aaMax = aa.annotations.length - 1;
1029
1030     while (x < eRes - sRes)
1031     {
1032       column = sRes + x;
1033       if (av.hasHiddenColumns)
1034       {
1035         column = av.getColumnSelection().adjustForHiddenColumns(column);
1036       }
1037
1038       if (column > aaMax)
1039       {
1040         break;
1041       }
1042
1043       if (aa.annotations[column] == null) // || coaa.annotations[column - 1] ==
1044       // null)
1045       {
1046         x++;
1047         continue;
1048       }
1049
1050       if (aa.annotations[column].colour == null)
1051         g.setColor(Color.black);
1052       else
1053         g.setColor(aa.annotations[column].colour);
1054       if (column == 0 || aa.annotations[column - 1] == null)
1055       {
1056         y1 = y
1057                 - (int) (((aa.annotations[column].value - min) / range) * graphHeight);
1058       }
1059       else
1060       {
1061         y1 = y
1062                 - (int) (((aa.annotations[column - 1].value - min) / range) * graphHeight);
1063       }
1064       y2 = y
1065               - (int) (((aa.annotations[column].value - min) / range) * graphHeight);
1066
1067       g.drawLine(x * av.charWidth - av.charWidth / 2, y1, x * av.charWidth
1068               + av.charWidth / 2, y2);
1069       x++;
1070     }
1071
1072     if (aa.threshold != null)
1073     {
1074       g.setColor(aa.threshold.colour);
1075
1076       y2 = (int) (y - ((aa.threshold.value - min) / range) * graphHeight);
1077       g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1078     }
1079   }
1080
1081   public void drawBarGraph(Graphics g, AlignmentAnnotation aa, int sRes,
1082           int eRes, float min, float max, int y)
1083   {
1084     ColourSchemeI profcolour = av.getGlobalColourScheme();
1085     if (profcolour == null)
1086     {
1087       profcolour = new jalview.schemes.ZappoColourScheme();
1088     }
1089     if (sRes > aa.annotations.length)
1090     {
1091       return;
1092     }
1093     Font ofont = g.getFont();
1094     eRes = Math.min(eRes, aa.annotations.length);
1095
1096     int x = 0, y1 = y, y2 = y;
1097
1098     float range = max - min;
1099
1100     if (min < 0)
1101     {
1102       y2 = y - (int) ((0 - min / (range)) * aa.graphHeight);
1103     }
1104
1105     g.setColor(Color.gray);
1106
1107     g.drawLine(x, y2, (eRes - sRes) * av.charWidth, y2);
1108
1109     int column;
1110     int aaMax = aa.annotations.length - 1;
1111     boolean renderHistogram = true, renderProfile = false;
1112     /*
1113      * Logos are disabled for 2.5 release : Bug # 0060064 if (aa.autoCalculated
1114      * && aa.label.startsWith("Consensus")) { // TODO: generalise this to have
1115      * render styles for consensus/profile data if (aa.groupRef!=null) {
1116      * renderHistogram = aa.groupRef.isShowConsensusHistogram(); renderProfile =
1117      * aa.groupRef.isShowSequenceLogo(); } else { renderHistogram =
1118      * av.isShowConsensusHistogram(); renderProfile = av.isShowSequenceLogo(); }
1119      * }
1120      */
1121     while (x < eRes - sRes)
1122     {
1123       column = sRes + x;
1124       if (av.hasHiddenColumns)
1125       {
1126         column = av.getColumnSelection().adjustForHiddenColumns(column);
1127       }
1128
1129       if (column > aaMax)
1130       {
1131         break;
1132       }
1133
1134       if (aa.annotations[column] == null)
1135       {
1136         x++;
1137         continue;
1138       }
1139
1140       if (aa.annotations[column].colour == null)
1141         g.setColor(Color.black);
1142       else
1143         g.setColor(aa.annotations[column].colour);
1144
1145       y1 = y
1146               - (int) (((aa.annotations[column].value - min) / (range)) * aa.graphHeight);
1147
1148       if (renderHistogram)
1149       {
1150         if (y1 - y2 > 0)
1151         {
1152           g.fillRect(x * av.charWidth, y2, av.charWidth, y1 - y2);
1153         }
1154         else
1155         {
1156           g.fillRect(x * av.charWidth, y1, av.charWidth, y2 - y1);
1157         }
1158       }
1159       // draw profile if available
1160       // Disabled for 2.5 release: see bug #0060064
1161       /**
1162        * if (aa.annotations[column].value!=0 && renderProfile) { int profl[] =
1163        * getProfileFor(aa,column); int ht = y1; //,htn=y2-y1;//aa.graphHeight;
1164        * float wdth; double ht2=0; char[] dc = new char[1]; // LineMetrics lm;
1165        * for (int c=1;profl!=null && c<profl[0];) { dc[0] = (char) profl[c++];
1166        * wdth = av.charWidth; wdth/=(float) fm .charsWidth(dc,0,1);
1167        * 
1168        * if (c>2) { ht+=(int)ht2; } { // not java 1.1 compatible: Bug # 0060064
1169        * g.setFont(ofont.deriveFont(AffineTransform.getScaleInstance(wdth,
1170        * (ht2=(htn*((double)profl[c++])/100.0))/av.charHeight))); lm =
1171        * g.getFontMetrics().getLineMetrics(dc,0,1, g);
1172        * g.setColor(profcolour.findColour(dc[0]));
1173        * g.drawChars(dc,0,1,x*av.charWidth, (int) (ht+lm.getHeight())); } }
1174        * g.setFont(ofont); }
1175        **/
1176       x++;
1177
1178     }
1179     if (aa.threshold != null)
1180     {
1181       g.setColor(aa.threshold.colour);
1182       y2 = (int) (y - ((aa.threshold.value - min) / range) * aa.graphHeight);
1183       g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1184     }
1185   }
1186
1187   /*
1188    * Disabled for 2.5 release - see bug #0060064 private int[]
1189    * getProfileFor(AlignmentAnnotation aa, int column) { // if
1190    * (aa.autoCalculated && aa.label.startsWith("Consensus")) { if
1191    * (aa.groupRef!=null && aa.groupRef.consensusData!=null) { // &&
1192    * aa.groupRef.isShowSequenceLogo()) { return
1193    * AAFrequency.extractProfile(aa.groupRef
1194    * .consensusData[column],aa.groupRef.getIgnoreGapsConsensus()); } // TODO
1195    * extend annotation row to enable dynamic and static profile data to be
1196    * stored if (aa.groupRef==null && aa.sequenceRef==null) // &&
1197    * av.isShowSequenceLogo()) { return
1198    * AAFrequency.extractProfile(av.hconsensus[column
1199    * ],av.getIgnoreGapsConsensus()); } // } return null; }
1200    */
1201
1202   // used by overview window
1203   public void drawGraph(Graphics g, AlignmentAnnotation aa, int width,
1204           int y, int sRes, int eRes)
1205   {
1206     eRes = Math.min(eRes, aa.annotations.length);
1207     g.setColor(Color.white);
1208     g.fillRect(0, 0, width, y);
1209     g.setColor(new Color(0, 0, 180));
1210
1211     int x = 0, height;
1212
1213     for (int j = sRes; j < eRes; j++)
1214     {
1215       if (aa.annotations[j].colour == null)
1216         g.setColor(Color.black);
1217       else
1218         g.setColor(aa.annotations[j].colour);
1219
1220       height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
1221       if (height > y)
1222       {
1223         height = y;
1224       }
1225       g.fillRect(x, y - height, av.charWidth, height);
1226       x += av.charWidth;
1227     }
1228   }
1229 }