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