Merge branch 'Release_2_8_0b1_Branch' into try_r20b1_merge
[jalview.git] / src / jalview / appletgui / AnnotationPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
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 import jalview.renderer.AnnotationRenderer;
28 import jalview.renderer.AwtRenderPanelI;
29 import jalview.util.MessageManager;
30
31 public class AnnotationPanel extends Panel implements AwtRenderPanelI,
32         AdjustmentListener, ActionListener, MouseListener,
33         MouseMotionListener
34 {
35   AlignViewport av;
36
37   AlignmentPanel ap;
38
39   int activeRow = -1;
40
41   Vector activeRes;
42
43   final String HELIX = "Helix";
44
45   final String SHEET = "Sheet";
46
47   /**
48    * For RNA secondary structure "stems" aka helices
49    */
50   final String STEM = "RNA Helix";
51
52   final String LABEL = "Label";
53
54   final String REMOVE = "Remove Annotation";
55
56   final String COLOUR = "Colour";
57
58   final Color HELIX_COLOUR = Color.red.darker();
59
60   final Color SHEET_COLOUR = Color.green.darker().darker();
61
62   Image image;
63
64   Graphics gg;
65
66   FontMetrics fm;
67
68   int imgWidth = 0;
69
70   boolean fastPaint = false;
71
72   // Used For mouse Dragging and resizing graphs
73   int graphStretch = -1;
74
75   int graphStretchY = -1;
76
77   boolean mouseDragging = false;
78
79   public static int GRAPH_HEIGHT = 40;
80
81   boolean MAC = false;
82
83   public final AnnotationRenderer renderer;
84
85   public AnnotationPanel(AlignmentPanel ap)
86   {
87     MAC = new jalview.util.Platform().isAMac();
88     this.ap = ap;
89     av = ap.av;
90     setLayout(null);
91     int height = adjustPanelHeight();
92     ap.apvscroll.setValues(0, getSize().height, 0, height);
93
94     addMouseMotionListener(this);
95
96     addMouseListener(this);
97
98     // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
99     renderer = new AnnotationRenderer();
100   }
101
102   public AnnotationPanel(AlignViewport av)
103   {
104     this.av = av;
105     renderer = new AnnotationRenderer();
106   }
107
108   @Override
109   public void adjustmentValueChanged(AdjustmentEvent evt)
110   {
111   }
112
113   /**
114    * DOCUMENT ME!
115    * 
116    * @param evt
117    *          DOCUMENT ME!
118    */
119   @Override
120   public void actionPerformed(ActionEvent evt)
121   {
122     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
123     if (aa == null)
124     {
125       return;
126     }
127     Annotation[] anot = aa[activeRow].annotations;
128
129     if (anot.length < av.getColumnSelection().getMax())
130     {
131       Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
132       System.arraycopy(anot, 0, temp, 0, anot.length);
133       anot = temp;
134       aa[activeRow].annotations = anot;
135     }
136
137     String label = "";
138     if (av.getColumnSelection() != null
139             && av.getColumnSelection().size() > 0
140             && anot[av.getColumnSelection().getMin()] != null)
141       label = anot[av.getColumnSelection().getMin()].displayCharacter;
142
143     if (evt.getActionCommand().equals(REMOVE))
144     {
145       for (int i = 0; i < av.getColumnSelection().size(); i++)
146       {
147         anot[av.getColumnSelection().columnAt(i)] = null;
148       }
149     }
150     else if (evt.getActionCommand().equals(LABEL))
151     {
152       label = enterLabel(label, "Enter Label");
153
154       if (label == null)
155       {
156         return;
157       }
158
159       if ((label.length() > 0) && !aa[activeRow].hasText)
160       {
161         aa[activeRow].hasText = true;
162       }
163
164       for (int i = 0; i < av.getColumnSelection().size(); i++)
165       {
166         int index = av.getColumnSelection().columnAt(i);
167
168         if (!av.getColumnSelection().isVisible(index))
169           continue;
170
171         if (anot[index] == null)
172         {
173           anot[index] = new Annotation(label, "", ' ', 0);
174         }
175
176         anot[index].displayCharacter = label;
177       }
178     }
179     else if (evt.getActionCommand().equals(COLOUR))
180     {
181       UserDefinedColours udc = new UserDefinedColours(this, Color.black,
182               ap.alignFrame);
183
184       Color col = udc.getColor();
185
186       for (int i = 0; i < av.getColumnSelection().size(); i++)
187       {
188         int index = av.getColumnSelection().columnAt(i);
189
190         if (!av.getColumnSelection().isVisible(index))
191           continue;
192
193         if (anot[index] == null)
194         {
195           anot[index] = new Annotation("", "", ' ', 0);
196         }
197
198         anot[index].colour = col;
199       }
200     }
201     else
202     // HELIX OR SHEET
203     {
204       char type = 0;
205       String symbol = "\u03B1";
206
207       if (evt.getActionCommand().equals(HELIX))
208       {
209         type = 'H';
210       }
211       else if (evt.getActionCommand().equals(SHEET))
212       {
213         type = 'E';
214         symbol = "\u03B2";
215       }
216
217       // Added by LML to color stems
218       else if (evt.getActionCommand().equals(STEM))
219       {
220         type = 'S';
221         symbol = "\u03C3";
222       }
223
224       if (!aa[activeRow].hasIcons)
225       {
226         aa[activeRow].hasIcons = true;
227       }
228
229       label = enterLabel(symbol, "Enter Label");
230
231       if (label == null)
232       {
233         return;
234       }
235
236       if ((label.length() > 0) && !aa[activeRow].hasText)
237       {
238         aa[activeRow].hasText = true;
239       }
240
241       for (int i = 0; i < av.getColumnSelection().size(); i++)
242       {
243         int index = av.getColumnSelection().columnAt(i);
244
245         if (!av.getColumnSelection().isVisible(index))
246           continue;
247
248         if (anot[index] == null)
249         {
250           anot[index] = new Annotation(label, "", type, 0);
251         }
252
253         anot[index].secondaryStructure = type;
254         anot[index].displayCharacter = label;
255       }
256     }
257
258     av.getAlignment().validateAnnotation(aa[activeRow]);
259
260     ap.alignmentChanged();
261     adjustPanelHeight();
262     repaint();
263
264     return;
265   }
266
267   String enterLabel(String text, String label)
268   {
269     EditNameDialog dialog = new EditNameDialog(text, null, label, null,
270             ap.alignFrame, "Enter Label", 400, 200, true);
271
272     if (dialog.accept)
273       return dialog.getName();
274     else
275       return null;
276   }
277
278   @Override
279   public void mousePressed(MouseEvent evt)
280   {
281     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
282     if (aa == null)
283     {
284       return;
285     }
286
287     int height = -scrollOffset;
288     activeRow = -1;
289
290     for (int i = 0; i < aa.length; i++)
291     {
292       if (aa[i].visible)
293       {
294         height += aa[i].height;
295       }
296
297       if (evt.getY() < height)
298       {
299         if (aa[i].editable)
300         {
301           activeRow = i;
302         }
303         else if (aa[i].graph > 0)
304         {
305           // Stretch Graph
306           graphStretch = i;
307           graphStretchY = evt.getY();
308         }
309
310         break;
311       }
312     }
313
314     if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
315             && activeRow != -1)
316     {
317       if (av.getColumnSelection() == null)
318       {
319         return;
320       }
321
322       PopupMenu pop = new PopupMenu(MessageManager.getString("label.structure_type"));
323       MenuItem item;
324       /*
325        * Just display the needed structure options
326        */
327       if (av.getAlignment().isNucleotide() == true)
328       {
329         item = new MenuItem(STEM);
330         item.addActionListener(this);
331         pop.add(item);
332       }
333       else
334       {
335         item = new MenuItem(HELIX);
336         item.addActionListener(this);
337         pop.add(item);
338         item = new MenuItem(SHEET);
339         item.addActionListener(this);
340         pop.add(item);
341       }
342       item = new MenuItem(LABEL);
343       item.addActionListener(this);
344       pop.add(item);
345       item = new MenuItem(COLOUR);
346       item.addActionListener(this);
347       pop.add(item);
348       item = new MenuItem(REMOVE);
349       item.addActionListener(this);
350       pop.add(item);
351       ap.alignFrame.add(pop);
352       pop.show(this, evt.getX(), evt.getY());
353
354       return;
355     }
356
357     if (aa == null)
358     {
359       return;
360     }
361
362     ap.scalePanel.mousePressed(evt);
363   }
364
365   @Override
366   public void mouseReleased(MouseEvent evt)
367   {
368     graphStretch = -1;
369     graphStretchY = -1;
370     mouseDragging = false;
371     if (needValidating)
372     {
373       ap.validate();
374       needValidating = false;
375     }
376     ap.scalePanel.mouseReleased(evt);
377   }
378
379   @Override
380   public void mouseClicked(MouseEvent evt)
381   {
382   }
383
384   boolean needValidating = false;
385
386   @Override
387   public void mouseDragged(MouseEvent evt)
388   {
389     if (graphStretch > -1)
390     {
391       av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
392               - evt.getY();
393       if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
394       {
395         av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
396       }
397       graphStretchY = evt.getY();
398       av.calcPanelHeight();
399       needValidating = true;
400       ap.paintAlignment(true);
401     }
402     else
403     {
404       ap.scalePanel.mouseDragged(evt);
405     }
406   }
407
408   @Override
409   public void mouseMoved(MouseEvent evt)
410   {
411     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
412     if (aa == null)
413     {
414       return;
415     }
416
417     int row = -1;
418     int height = -scrollOffset;
419     for (int i = 0; i < aa.length; i++)
420     {
421
422       if (aa[i].visible)
423       {
424         height += aa[i].height;
425       }
426
427       if (evt.getY() < height)
428       {
429         row = i;
430         break;
431       }
432     }
433
434     int res = evt.getX() / av.getCharWidth() + av.getStartRes();
435
436     if (av.hasHiddenColumns())
437     {
438       res = av.getColumnSelection().adjustForHiddenColumns(res);
439     }
440
441     if (row > -1 && res < aa[row].annotations.length
442             && aa[row].annotations[res] != null)
443     {
444       StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
445       if (aa[row].annotations[res].description != null)
446       {
447         text.append("  " + aa[row].annotations[res].description);
448       }
449       ap.alignFrame.statusBar.setText(text.toString());
450     }
451   }
452
453   @Override
454   public void mouseEntered(MouseEvent evt)
455   {
456     ap.scalePanel.mouseEntered(evt);
457   }
458
459   @Override
460   public void mouseExited(MouseEvent evt)
461   {
462     ap.scalePanel.mouseExited(evt);
463   }
464
465   public int adjustPanelHeight()
466   {
467     return adjustPanelHeight(true);
468   }
469
470   public int adjustPanelHeight(boolean repaint)
471   {
472     int height = av.calcPanelHeight();
473     this.setSize(new Dimension(getSize().width, height));
474     if (repaint)
475     {
476       repaint();
477     }
478     return height;
479   }
480
481   /**
482    * calculate the height for visible annotation, revalidating bounds where
483    * necessary ABSTRACT GUI METHOD
484    * 
485    * @return total height of annotation
486    */
487
488   public void addEditableColumn(int i)
489   {
490     if (activeRow == -1)
491     {
492       AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
493       if (aa == null)
494       {
495         return;
496       }
497
498       for (int j = 0; j < aa.length; j++)
499       {
500         if (aa[j].editable)
501         {
502           activeRow = j;
503           break;
504         }
505       }
506     }
507
508     if (activeRes == null)
509     {
510       activeRes = new Vector();
511       activeRes.addElement(String.valueOf(i));
512       return;
513     }
514
515     activeRes.addElement(String.valueOf(i));
516   }
517
518   @Override
519   public void update(Graphics g)
520   {
521     paint(g);
522   }
523
524   @Override
525   public void paint(Graphics g)
526   {
527     Dimension d = getSize();
528     imgWidth = d.width;
529     // (av.endRes - av.startRes + 1) * av.charWidth;
530     if (imgWidth < 1 || d.height < 1)
531     {
532       return;
533     }
534     if (image == null || imgWidth != image.getWidth(this)
535             || d.height != image.getHeight(this))
536     {
537       image = createImage(imgWidth, d.height);
538       gg = image.getGraphics();
539       gg.setFont(av.getFont());
540       fm = gg.getFontMetrics();
541       fastPaint = false;
542     }
543
544     if (fastPaint)
545     {
546       g.drawImage(image, 0, 0, this);
547       fastPaint = false;
548       return;
549     }
550
551     gg.setColor(Color.white);
552     gg.fillRect(0, 0, getSize().width, getSize().height);
553     drawComponent(gg, av.startRes, av.endRes + 1);
554
555     g.drawImage(image, 0, 0, this);
556   }
557
558   public void fastPaint(int horizontal)
559   {
560     if (horizontal == 0
561             || av.getAlignment().getAlignmentAnnotation() == null
562             || av.getAlignment().getAlignmentAnnotation().length < 1)
563     {
564       repaint();
565       return;
566     }
567
568     gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
569             * av.charWidth, 0);
570     int sr = av.startRes, er = av.endRes + 1, transX = 0;
571
572     if (horizontal > 0) // scrollbar pulled right, image to the left
573     {
574       transX = (er - sr - horizontal) * av.charWidth;
575       sr = er - horizontal;
576     }
577     else if (horizontal < 0)
578     {
579       er = sr - horizontal;
580     }
581
582     gg.translate(transX, 0);
583
584     drawComponent(gg, sr, er);
585
586     gg.translate(-transX, 0);
587
588     fastPaint = true;
589     repaint();
590   }
591
592   /**
593    * DOCUMENT ME!
594    * 
595    * @param g
596    *          DOCUMENT ME!
597    * @param startRes
598    *          DOCUMENT ME!
599    * @param endRes
600    *          DOCUMENT ME!
601    */
602   public void drawComponent(Graphics g, int startRes, int endRes)
603   {
604     Font ofont = av.getFont();
605     g.setFont(ofont);
606
607     g.setColor(Color.white);
608     g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
609
610     if (fm == null)
611     {
612       fm = g.getFontMetrics();
613     }
614
615     if ((av.getAlignment().getAlignmentAnnotation() == null)
616             || (av.getAlignment().getAlignmentAnnotation().length < 1))
617     {
618       g.setColor(Color.white);
619       g.fillRect(0, 0, getSize().width, getSize().height);
620       g.setColor(Color.black);
621       if (av.validCharWidth)
622       {
623         g.drawString(MessageManager.getString("label.alignment_has_no_annotations"), 20, 15);
624       }
625
626       return;
627     }
628     g.translate(0, -scrollOffset);
629     renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
630     g.translate(0, +scrollOffset);
631   }
632
633   int scrollOffset = 0;
634
635   public void setScrollOffset(int value)
636   {
637     scrollOffset = value;
638     repaint();
639   }
640
641   @Override
642   public FontMetrics getFontMetrics()
643   {
644     return fm;
645   }
646
647   @Override
648   public Image getFadedImage()
649   {
650     return image;
651   }
652
653   @Override
654   public int getFadedImageWidth()
655   {
656     return imgWidth;
657   }
658   private int[] bounds = new int[2];
659   @Override
660   public int[] getVisibleVRange()
661   {
662     if (ap!=null && ap.alabels!=null)
663     {
664     int sOffset=-ap.alabels.scrollOffset;
665     int visHeight = sOffset+ap.annotationPanelHolder.getHeight();
666     bounds[0] = sOffset; bounds[1]=visHeight;
667     return bounds;
668     } else return null;
669   }
670 }