Show scores
[jalview.git] / src / jalview / gui / AnnotationLabels.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.util.*;
22
23 import java.awt.*;
24 import java.awt.datatransfer.*;
25 import java.awt.event.*;
26 import java.awt.image.*;
27 import javax.swing.*;
28
29 import jalview.datamodel.*;
30 import jalview.io.*;
31
32 /**
33  * DOCUMENT ME!
34  *
35  * @author $author$
36  * @version $Revision$
37  */
38 public class AnnotationLabels
39     extends JPanel implements MouseListener,
40     MouseMotionListener, ActionListener
41 {
42   static String ADDNEW = "Add New Row";
43   static String EDITNAME = "Edit Label/Description";
44   static String HIDE = "Hide This Row";
45   static String DELETE = "Delete This Row";
46   static String SHOWALL = "Show All Hidden Rows";
47   static String OUTPUT_TEXT = "Export Annotation";
48   static String COPYCONS_SEQ = "Copy Consensus Sequence";
49   boolean resizePanel = false;
50   Image image;
51   AlignmentPanel ap;
52   AlignViewport av;
53   boolean resizing = false;
54   MouseEvent dragEvent;
55   int oldY;
56   int selectedRow;
57   int scrollOffset = 0;
58   Font font = new Font("Arial", Font.PLAIN, 11);
59
60   /**
61    * Creates a new AnnotationLabels object.
62    *
63    * @param ap DOCUMENT ME!
64    */
65   public AnnotationLabels(AlignmentPanel ap)
66   {
67     this.ap = ap;
68     av = ap.av;
69     ToolTipManager.sharedInstance().registerComponent(this);
70
71     java.net.URL url = getClass().getResource("/images/idwidth.gif");
72     Image temp = null;
73
74     if (url != null)
75     {
76       temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
77     }
78
79     try
80     {
81       MediaTracker mt = new MediaTracker(this);
82       mt.addImage(temp, 0);
83       mt.waitForID(0);
84     }
85     catch (Exception ex)
86     {
87     }
88
89     BufferedImage bi = new BufferedImage(temp.getHeight(this),
90                                          temp.getWidth(this),
91                                          BufferedImage.TYPE_INT_RGB);
92     Graphics2D g = (Graphics2D) bi.getGraphics();
93     g.rotate(Math.toRadians(90));
94     g.drawImage(temp, 0, -bi.getWidth(this), this);
95     image = (Image) bi;
96
97     addMouseListener(this);
98     addMouseMotionListener(this);
99   }
100
101   public AnnotationLabels(AlignViewport av)
102   {
103     this.av = av;
104   }
105
106   /**
107    * DOCUMENT ME!
108    *
109    * @param y DOCUMENT ME!
110    */
111   public void setScrollOffset(int y)
112   {
113     scrollOffset = y;
114     repaint();
115   }
116
117   void getSelectedRow(int y)
118   {
119     int height = 0;
120     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
121
122     if (aa != null)
123     {
124       for (int i = 0; i < aa.length; i++)
125       {
126         if (!aa[i].visible)
127         {
128           continue;
129         }
130
131         height += aa[i].height;
132
133         if (y < height)
134         {
135           selectedRow = i;
136
137           break;
138         }
139       }
140     }
141   }
142
143   /**
144    * DOCUMENT ME!
145    *
146    * @param evt DOCUMENT ME!
147    */
148   public void actionPerformed(ActionEvent evt)
149   {
150     int dif = 0;
151     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
152
153     if (evt.getActionCommand().equals(ADDNEW))
154     {
155       AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
156           null,
157           new Annotation[ap.av.alignment.getWidth()]);
158
159       if (!editLabelDescription(newAnnotation))
160       {
161         return;
162       }
163
164       ap.av.alignment.addAnnotation(newAnnotation);
165       ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
166       if (aa != null)
167       {
168         dif = aa[aa.length - 1].height;
169       }
170     }
171     else if (evt.getActionCommand().equals(EDITNAME))
172     {
173       editLabelDescription(aa[selectedRow]);
174       repaint();
175     }
176     else if (evt.getActionCommand().equals(HIDE))
177     {
178       aa[selectedRow].visible = false;
179
180       if (aa[selectedRow].label.equals("Quality"))
181       {
182         ap.av.quality = null;
183       }
184
185       dif = aa[selectedRow].height * -1;
186     }
187     else if (evt.getActionCommand().equals(DELETE))
188     {
189       ap.av.alignment.deleteAnnotation(aa[selectedRow]);
190       dif = aa[selectedRow].height * -1;
191     }
192     else if (evt.getActionCommand().equals(SHOWALL))
193     {
194       for (int i = 0; i < aa.length; i++)
195       {
196         if (!aa[i].visible && aa[i].annotations!=null)
197         {
198           dif += aa[i].height;
199           aa[i].visible = true;
200         }
201       }
202     }
203     else if (evt.getActionCommand().equals(OUTPUT_TEXT))
204     {
205       new AnnotationExporter().exportAnnotations(
206           ap,
207           new AlignmentAnnotation[]
208           {aa[selectedRow]},
209           null, null
210           );
211     }
212     else if (evt.getActionCommand().equals(COPYCONS_SEQ))
213     {
214       SequenceI cons = av.getConsensusSeq();
215       if (cons != null)
216       {
217         copy_annotseqtoclipboard(cons);
218       }
219
220     }
221
222     ap.annotationPanel.adjustPanelHeight();
223     ap.annotationScroller.validate();
224     ap.paintAlignment(true);
225   }
226
227   /**
228    * DOCUMENT ME!
229    *
230    * @param e DOCUMENT ME!
231    */
232   boolean editLabelDescription(AlignmentAnnotation annotation)
233   {
234     EditNameDialog dialog = new EditNameDialog(annotation.label,
235                                                annotation.description,
236                                                "       Annotation Name ",
237                                                "Annotation Description ",
238                                                "Edit Annotation Name/Description");
239
240     if (!dialog.accept)
241     {
242       return false;
243     }
244
245     annotation.label = dialog.getName();
246
247     String text = dialog.getDescription();
248     if (text!=null && text.length() == 0)
249     {
250       text = null;
251     }
252     annotation.description = text;
253
254     return true;
255   }
256
257   /**
258    * DOCUMENT ME!
259    *
260    * @param evt DOCUMENT ME!
261    */
262   public void mousePressed(MouseEvent evt)
263   {
264     getSelectedRow(evt.getY() - scrollOffset);
265     oldY = evt.getY();
266   }
267
268   /**
269    * DOCUMENT ME!
270    *
271    * @param evt DOCUMENT ME!
272    */
273   public void mouseReleased(MouseEvent evt)
274   {
275     int start = selectedRow;
276     getSelectedRow(evt.getY() - scrollOffset);
277     int end = selectedRow;
278
279     if (start != end)
280     {
281       //Swap these annotations
282       AlignmentAnnotation startAA = ap.av.alignment.getAlignmentAnnotation()[
283           start];
284       AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
285
286       ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
287       ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
288     }
289
290     resizePanel = false;
291     dragEvent = null;
292     repaint();
293     ap.annotationPanel.repaint();
294   }
295
296   /**
297    * DOCUMENT ME!
298    *
299    * @param evt DOCUMENT ME!
300    */
301   public void mouseEntered(MouseEvent evt)
302   {
303     if (evt.getY() < 10)
304     {
305       resizePanel = true;
306       repaint();
307     }
308   }
309
310   /**
311    * DOCUMENT ME!
312    *
313    * @param evt DOCUMENT ME!
314    */
315   public void mouseExited(MouseEvent evt)
316   {
317     if (dragEvent == null)
318     {
319       resizePanel = false;
320       repaint();
321     }
322   }
323
324   /**
325    * DOCUMENT ME!
326    *
327    * @param evt DOCUMENT ME!
328    */
329   public void mouseDragged(MouseEvent evt)
330   {
331     dragEvent = evt;
332
333     if (resizePanel)
334     {
335       Dimension d = ap.annotationScroller.getPreferredSize();
336       int dif = evt.getY() - oldY;
337
338       dif /= ap.av.charHeight;
339       dif *= ap.av.charHeight;
340
341       if ( (d.height - dif) > 20)
342       {
343         ap.annotationScroller.setPreferredSize(new Dimension(d.width,
344             d.height - dif));
345         d = ap.annotationSpaceFillerHolder.getPreferredSize();
346         ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
347             d.width, d.height - dif));
348         ap.paintAlignment(true);
349       }
350
351       ap.addNotify();
352     }
353     else
354     {
355       repaint();
356     }
357   }
358
359   /**
360    * DOCUMENT ME!
361    *
362    * @param evt DOCUMENT ME!
363    */
364   public void mouseMoved(MouseEvent evt)
365   {
366     resizePanel = evt.getY() < 10;
367
368     getSelectedRow(evt.getY() - scrollOffset);
369
370
371     if (selectedRow > -1
372         && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
373     {
374       AlignmentAnnotation aa = ap.av.alignment.
375           getAlignmentAnnotation()[selectedRow];
376
377       StringBuffer desc = new StringBuffer("<html>");
378
379       if (aa.description != null && !aa.description.equals("New description"))
380       {
381         desc.append(aa.description+"<br>");
382       }
383       if(!Float.isNaN(aa.score))
384       {
385         desc.append("Score: "+aa.score);
386       }
387
388       if(desc.length()!=6)
389       {
390         desc.append("</html>");
391         this.setToolTipText(desc.toString());
392       }
393       else
394         this.setToolTipText(null);
395     }
396
397   }
398
399   /**
400    * DOCUMENT ME!
401    *
402    * @param evt DOCUMENT ME!
403    */
404   public void mouseClicked(MouseEvent evt)
405   {
406     if (!SwingUtilities.isRightMouseButton(evt))
407     {
408       return;
409     }
410
411     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
412
413     if ( (aa == null) || (aa.length == 0))
414     {
415       JPopupMenu pop = new JPopupMenu("Annotations");
416       JMenuItem item = new JMenuItem(ADDNEW);
417       item.addActionListener(this);
418       pop.add(item);
419       pop.show(this, evt.getX(), evt.getY());
420
421       return;
422     }
423
424     JPopupMenu pop = new JPopupMenu("Annotations");
425     JMenuItem item = new JMenuItem(ADDNEW);
426     item.addActionListener(this);
427     pop.add(item);
428     item = new JMenuItem(EDITNAME);
429     item.addActionListener(this);
430     pop.add(item);
431     item = new JMenuItem(HIDE);
432     item.addActionListener(this);
433     pop.add(item);
434     item = new JMenuItem(DELETE);
435     item.addActionListener(this);
436     pop.add(item);
437     item = new JMenuItem(SHOWALL);
438     item.addActionListener(this);
439     pop.add(item);
440     item = new JMenuItem(OUTPUT_TEXT);
441     item.addActionListener(this);
442     pop.add(item);
443     // annotation object should be typed
444     if (aa[selectedRow] == ap.av.consensus)
445     {
446       pop.addSeparator();
447       final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
448           "Ignore Gaps In Consensus",
449           ap.av.getIgnoreGapsConsensus());
450       cbmi.addActionListener(new ActionListener()
451       {
452         public void actionPerformed(ActionEvent e)
453         {
454           ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
455         }
456       });
457       pop.add(cbmi);
458       final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
459       consclipbrd.addActionListener(this);
460       pop.add(consclipbrd);
461     }
462
463     pop.show(this, evt.getX(), evt.getY());
464   }
465
466   /**
467    * do a single sequence copy to jalview and the system clipboard
468    *
469    * @param sq sequence to be copied to clipboard
470    */
471   protected void copy_annotseqtoclipboard(SequenceI sq)
472   {
473     SequenceI[] seqs = new SequenceI[]
474         {
475         sq};
476     String[] omitHidden = null;
477     SequenceI[] dseqs = new SequenceI[]
478         {
479         sq.getDatasetSequence()};
480     if (dseqs[0] == null)
481     {
482       dseqs[0] = new Sequence(sq);
483       dseqs[0].setSequence(
484           jalview.analysis.AlignSeq.extractGaps(
485               jalview.util.Comparison.GapChars,
486               sq.getSequenceAsString()));
487
488       sq.setDatasetSequence(dseqs[0]);
489     }
490     Alignment ds = new Alignment(dseqs);
491     if (av.hasHiddenColumns)
492     {
493       omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
494           sq.getLength(), seqs);
495     }
496
497     String output = new FormatAdapter().formatSequences(
498         "Fasta",
499         seqs,
500         omitHidden);
501
502     Toolkit.getDefaultToolkit().getSystemClipboard()
503         .setContents(new StringSelection(output), Desktop.instance);
504
505     Vector hiddenColumns = null;
506     if (av.hasHiddenColumns)
507     {
508       hiddenColumns = new Vector();
509       for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
510       {
511         int[] region = (int[])
512             av.getColumnSelection().getHiddenColumns().elementAt(i);
513
514         hiddenColumns.addElement(new int[]
515                                  {region[0],
516                                  region[1]});
517       }
518     }
519
520     Desktop.jalviewClipboard = new Object[]
521         {
522         seqs,
523         ds, // what is the dataset of a consensus sequence ? need to flag sequence as special.
524         hiddenColumns};
525   }
526
527   /**
528    * DOCUMENT ME!
529    *
530    * @param g1 DOCUMENT ME!
531    */
532   public void paintComponent(Graphics g)
533   {
534
535     int width = getWidth();
536     if (width == 0)
537     {
538       width = ap.calculateIdWidth().width + 4;
539     }
540
541     Graphics2D g2 = (Graphics2D) g;
542     if (av.antiAlias)
543     {
544       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
545                           RenderingHints.VALUE_ANTIALIAS_ON);
546     }
547
548     drawComponent(g2, width);
549
550   }
551
552   /**
553    * DOCUMENT ME!
554    *
555    * @param g DOCUMENT ME!
556    */
557   public void drawComponent(Graphics g, int width)
558   {
559     if (av.getFont().getSize() < 10)
560     {
561       g.setFont(font);
562     }
563     else
564     {
565       g.setFont(av.getFont());
566     }
567
568     FontMetrics fm = g.getFontMetrics(g.getFont());
569     g.setColor(Color.white);
570     g.fillRect(0, 0, getWidth(), getHeight());
571
572     g.translate(0, scrollOffset);
573     g.setColor(Color.black);
574
575     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
576     int fontHeight = g.getFont().getSize();
577     int y = 0;
578     int x = 0;
579     int graphExtras = 0;
580     int offset =0;
581
582     if (aa != null)
583     {
584       for (int i = 0; i < aa.length; i++)
585       {
586         g.setColor(Color.black);
587
588         if (!aa[i].visible)
589         {
590           continue;
591         }
592
593
594         y += aa[i].height;
595
596         offset = -aa[i].height/2;
597
598         if(aa[i].hasText)
599         {
600           offset += fm.getHeight()/2;
601           offset -= fm.getDescent();
602         }
603         else
604           offset += fm.getDescent();
605
606         x = width - fm.stringWidth(aa[i].label) - 3;
607
608         if (aa[i].graphGroup > -1)
609         {
610           int groupSize = 0;
611           for (int gg = 0; gg < aa.length; gg++)
612           {
613             if (aa[gg].graphGroup == aa[i].graphGroup)
614             {
615               groupSize++;
616             }
617           }
618
619           if (groupSize * (fontHeight + 8) < aa[i].height)
620           {
621             graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
622           }
623
624           for (int gg = 0; gg < aa.length; gg++)
625           {
626             if (aa[gg].graphGroup == aa[i].graphGroup)
627             {
628               x = width - fm.stringWidth(aa[gg].label) - 3;
629               g.drawString(aa[gg].label, x,y - graphExtras);
630               if (aa[gg].annotations[0] != null)
631               {
632                 g.setColor(aa[gg].annotations[0].colour);
633               }
634
635               g.drawLine(x, y - graphExtras - 3,
636                          x + fm.stringWidth(aa[gg].label),
637                          y - graphExtras - 3);
638
639               g.setColor(Color.black);
640               graphExtras += fontHeight + 8;
641             }
642           }
643         }
644         else
645         {
646           if(aa[i].belowAlignment)
647             g.setColor(Color.ORANGE);
648           else
649             g.setColor(Color.red);
650
651           g.drawString(aa[i].label, x, y +offset);
652
653         }
654       }
655     }
656
657     if (resizePanel)
658     {
659       g.drawImage(image, 2, 0 - scrollOffset, this);
660     }
661     else if (dragEvent != null && aa != null)
662     {
663       g.setColor(Color.lightGray);
664       g.drawString(aa[selectedRow].label, dragEvent.getX(),
665                    dragEvent.getY() - scrollOffset);
666     }
667
668     if ( (aa == null) || (aa.length < 1))
669     {
670       g.drawString("Right click", 2, 8);
671       g.drawString("to add annotation", 2, 18);
672     }
673   }
674 }