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