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