JAL-2077 test isPopupTrigger in mouseReleased or mouseClicked (for
[jalview.git] / src / jalview / gui / AnnotationLabels.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.analysis.AlignmentUtils;
24 import jalview.datamodel.Alignment;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceGroup;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.FormatAdapter;
31 import jalview.util.MessageManager;
32
33 import java.awt.Color;
34 import java.awt.Dimension;
35 import java.awt.Font;
36 import java.awt.FontMetrics;
37 import java.awt.Graphics;
38 import java.awt.Graphics2D;
39 import java.awt.Image;
40 import java.awt.MediaTracker;
41 import java.awt.RenderingHints;
42 import java.awt.Toolkit;
43 import java.awt.datatransfer.StringSelection;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.MouseEvent;
47 import java.awt.event.MouseListener;
48 import java.awt.event.MouseMotionListener;
49 import java.awt.geom.AffineTransform;
50 import java.awt.image.BufferedImage;
51 import java.util.ArrayList;
52 import java.util.Arrays;
53 import java.util.Collections;
54 import java.util.List;
55 import java.util.regex.Pattern;
56
57 import javax.swing.JCheckBoxMenuItem;
58 import javax.swing.JMenuItem;
59 import javax.swing.JPanel;
60 import javax.swing.JPopupMenu;
61 import javax.swing.SwingUtilities;
62 import javax.swing.ToolTipManager;
63
64 /**
65  * DOCUMENT ME!
66  * 
67  * @author $author$
68  * @version $Revision$
69  */
70 public class AnnotationLabels extends JPanel implements MouseListener,
71         MouseMotionListener, ActionListener
72 {
73   private static final Pattern LEFT_ANGLE_BRACKET_PATTERN = Pattern
74           .compile("<");
75
76   String TOGGLE_LABELSCALE = MessageManager
77           .getString("label.scale_label_to_column");
78
79   String ADDNEW = MessageManager.getString("label.add_new_row");
80
81   String EDITNAME = MessageManager
82           .getString("label.edit_label_description");
83
84   String HIDE = MessageManager.getString("label.hide_row");
85
86   String DELETE = MessageManager.getString("label.delete_row");
87
88   String SHOWALL = MessageManager.getString("label.show_all_hidden_rows");
89
90   String OUTPUT_TEXT = MessageManager.getString("label.export_annotation");
91
92   String COPYCONS_SEQ = MessageManager
93           .getString("label.copy_consensus_sequence");
94
95   boolean resizePanel = false;
96
97   Image image;
98
99   AlignmentPanel ap;
100
101   AlignViewport av;
102
103   boolean resizing = false;
104
105   MouseEvent dragEvent;
106
107   int oldY;
108
109   int selectedRow;
110
111   private int scrollOffset = 0;
112
113   Font font = new Font("Arial", Font.PLAIN, 11);
114
115   private boolean hasHiddenRows;
116
117   /**
118    * Creates a new AnnotationLabels object.
119    * 
120    * @param ap
121    *          DOCUMENT ME!
122    */
123   public AnnotationLabels(AlignmentPanel ap)
124   {
125     this.ap = ap;
126     av = ap.av;
127     ToolTipManager.sharedInstance().registerComponent(this);
128
129     java.net.URL url = getClass().getResource("/images/idwidth.gif");
130     Image temp = null;
131
132     if (url != null)
133     {
134       temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
135     }
136
137     try
138     {
139       MediaTracker mt = new MediaTracker(this);
140       mt.addImage(temp, 0);
141       mt.waitForID(0);
142     } catch (Exception ex)
143     {
144     }
145
146     BufferedImage bi = new BufferedImage(temp.getHeight(this),
147             temp.getWidth(this), BufferedImage.TYPE_INT_RGB);
148     Graphics2D g = (Graphics2D) bi.getGraphics();
149     g.rotate(Math.toRadians(90));
150     g.drawImage(temp, 0, -bi.getWidth(this), this);
151     image = bi;
152
153     addMouseListener(this);
154     addMouseMotionListener(this);
155     addMouseWheelListener(ap.getAnnotationPanel());
156   }
157
158   public AnnotationLabels(AlignViewport av)
159   {
160     this.av = av;
161   }
162
163   /**
164    * DOCUMENT ME!
165    * 
166    * @param y
167    *          DOCUMENT ME!
168    */
169   public void setScrollOffset(int y)
170   {
171     scrollOffset = y;
172     repaint();
173   }
174
175   /**
176    * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
177    * y
178    * 
179    * @param y
180    *          coordinate position to search for a row
181    */
182   void getSelectedRow(int y)
183   {
184     int height = 0;
185     AlignmentAnnotation[] aa = ap.av.getAlignment()
186             .getAlignmentAnnotation();
187     selectedRow = -2;
188     if (aa != null)
189     {
190       for (int i = 0; i < aa.length; i++)
191       {
192         selectedRow = -1;
193         if (!aa[i].visible)
194         {
195           continue;
196         }
197
198         height += aa[i].height;
199
200         if (y < height)
201         {
202           selectedRow = i;
203
204           break;
205         }
206       }
207     }
208   }
209
210   /**
211    * DOCUMENT ME!
212    * 
213    * @param evt
214    *          DOCUMENT ME!
215    */
216   @Override
217   public void actionPerformed(ActionEvent evt)
218   {
219     AlignmentAnnotation[] aa = ap.av.getAlignment()
220             .getAlignmentAnnotation();
221
222     if (evt.getActionCommand().equals(ADDNEW))
223     {
224       AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
225               null, new Annotation[ap.av.getAlignment().getWidth()]);
226
227       if (!editLabelDescription(newAnnotation))
228       {
229         return;
230       }
231
232       ap.av.getAlignment().addAnnotation(newAnnotation);
233       ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
234     }
235     else if (evt.getActionCommand().equals(EDITNAME))
236     {
237       editLabelDescription(aa[selectedRow]);
238       repaint();
239     }
240     else if (evt.getActionCommand().equals(HIDE))
241     {
242       aa[selectedRow].visible = false;
243     }
244     else if (evt.getActionCommand().equals(DELETE))
245     {
246       ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
247       ap.av.getCalcManager().removeWorkerForAnnotation(aa[selectedRow]);
248     }
249     else if (evt.getActionCommand().equals(SHOWALL))
250     {
251       for (int i = 0; i < aa.length; i++)
252       {
253         if (!aa[i].visible && aa[i].annotations != null)
254         {
255           aa[i].visible = true;
256         }
257       }
258     }
259     else if (evt.getActionCommand().equals(OUTPUT_TEXT))
260     {
261       new AnnotationExporter().exportAnnotations(ap,
262               new AlignmentAnnotation[] { aa[selectedRow] });
263     }
264     else if (evt.getActionCommand().equals(COPYCONS_SEQ))
265     {
266       SequenceI cons = null;
267       if (aa[selectedRow].groupRef != null)
268       {
269         cons = aa[selectedRow].groupRef.getConsensusSeq();
270       }
271       else
272       {
273         cons = av.getConsensusSeq();
274       }
275       if (cons != null)
276       {
277         copy_annotseqtoclipboard(cons);
278       }
279
280     }
281     else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
282     {
283       aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
284     }
285
286     refresh();
287
288   }
289
290   /**
291    * Redraw sensibly.
292    */
293   protected void refresh()
294   {
295     ap.validateAnnotationDimensions(false);
296     ap.addNotify();
297     ap.repaint();
298   }
299
300   /**
301    * DOCUMENT ME!
302    * 
303    * @param e
304    *          DOCUMENT ME!
305    */
306   boolean editLabelDescription(AlignmentAnnotation annotation)
307   {
308     EditNameDialog dialog = new EditNameDialog(annotation.label,
309             annotation.description, "       Annotation Name ",
310             "Annotation Description ", "Edit Annotation Name/Description",
311             ap.alignFrame);
312
313     if (!dialog.accept)
314     {
315       return false;
316     }
317
318     annotation.label = dialog.getName();
319
320     String text = dialog.getDescription();
321     if (text != null && text.length() == 0)
322     {
323       text = null;
324     }
325     annotation.description = text;
326
327     return true;
328   }
329
330   @Override
331   public void mousePressed(MouseEvent evt)
332   {
333     getSelectedRow(evt.getY() - getScrollOffset());
334     oldY = evt.getY();
335     if (evt.isPopupTrigger())
336     {
337       showPopupMenu(evt);
338     }
339   }
340
341   /**
342    * Build and show the Pop-up menu at the right-click mouse position
343    * 
344    * @param evt
345    */
346   void showPopupMenu(MouseEvent evt)
347   {
348     evt.consume();
349     final AlignmentAnnotation[] aa = ap.av.getAlignment()
350             .getAlignmentAnnotation();
351
352     JPopupMenu pop = new JPopupMenu(
353             MessageManager.getString("label.annotations"));
354     JMenuItem item = new JMenuItem(ADDNEW);
355     item.addActionListener(this);
356     pop.add(item);
357     if (selectedRow < 0)
358     {
359       if (hasHiddenRows)
360       { // let the user make everything visible again
361         item = new JMenuItem(SHOWALL);
362         item.addActionListener(this);
363         pop.add(item);
364       }
365       pop.show(this, evt.getX(), evt.getY());
366       return;
367     }
368     item = new JMenuItem(EDITNAME);
369     item.addActionListener(this);
370     pop.add(item);
371     item = new JMenuItem(HIDE);
372     item.addActionListener(this);
373     pop.add(item);
374     // JAL-1264 hide all sequence-specific annotations of this type
375     if (selectedRow < aa.length)
376     {
377       if (aa[selectedRow].sequenceRef != null)
378       {
379         final String label = aa[selectedRow].label;
380         JMenuItem hideType = new JMenuItem();
381         String text = MessageManager.getString("label.hide_all") + " "
382                 + label;
383         hideType.setText(text);
384         hideType.addActionListener(new ActionListener()
385         {
386           @Override
387           public void actionPerformed(ActionEvent e)
388           {
389             AlignmentUtils.showOrHideSequenceAnnotations(
390                     ap.av.getAlignment(), Collections.singleton(label),
391                     null, false, false);
392             // for (AlignmentAnnotation ann : ap.av.getAlignment()
393             // .getAlignmentAnnotation())
394             // {
395             // if (ann.sequenceRef != null && ann.label != null
396             // && ann.label.equals(label))
397             // {
398             // ann.visible = false;
399             // }
400             // }
401             refresh();
402           }
403         });
404         pop.add(hideType);
405       }
406     }
407     item = new JMenuItem(DELETE);
408     item.addActionListener(this);
409     pop.add(item);
410     if (hasHiddenRows)
411     {
412       item = new JMenuItem(SHOWALL);
413       item.addActionListener(this);
414       pop.add(item);
415     }
416     item = new JMenuItem(OUTPUT_TEXT);
417     item.addActionListener(this);
418     pop.add(item);
419     // TODO: annotation object should be typed for autocalculated/derived
420     // property methods
421     if (selectedRow < aa.length)
422     {
423       final String label = aa[selectedRow].label;
424       if (!aa[selectedRow].autoCalculated)
425       {
426         if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
427         {
428           // display formatting settings for this row.
429           pop.addSeparator();
430           // av and sequencegroup need to implement same interface for
431           item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
432                   aa[selectedRow].scaleColLabel);
433           item.addActionListener(this);
434           pop.add(item);
435         }
436       }
437       else if (label.indexOf("Consensus") > -1)
438       {
439         pop.addSeparator();
440         // av and sequencegroup need to implement same interface for
441         final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
442                 MessageManager.getString("label.ignore_gaps_consensus"),
443                 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
444                         .getIgnoreGapsConsensus() : ap.av
445                         .isIgnoreGapsConsensus());
446         final AlignmentAnnotation aaa = aa[selectedRow];
447         cbmi.addActionListener(new ActionListener()
448         {
449           @Override
450           public void actionPerformed(ActionEvent e)
451           {
452             if (aaa.groupRef != null)
453             {
454               // TODO: pass on reference to ap so the view can be updated.
455               aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
456               ap.getAnnotationPanel().paint(
457                       ap.getAnnotationPanel().getGraphics());
458             }
459             else
460             {
461               ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
462             }
463           }
464         });
465         pop.add(cbmi);
466         // av and sequencegroup need to implement same interface for
467         if (aaa.groupRef != null)
468         {
469           final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
470                   MessageManager.getString("label.show_group_histogram"),
471                   aa[selectedRow].groupRef.isShowConsensusHistogram());
472           chist.addActionListener(new ActionListener()
473           {
474             @Override
475             public void actionPerformed(ActionEvent e)
476             {
477               // TODO: pass on reference
478               // to ap
479               // so the
480               // view
481               // can be
482               // updated.
483               aaa.groupRef.setShowConsensusHistogram(chist.getState());
484               ap.repaint();
485               // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
486             }
487           });
488           pop.add(chist);
489           final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
490                   MessageManager.getString("label.show_group_logo"),
491                   aa[selectedRow].groupRef.isShowSequenceLogo());
492           cprofl.addActionListener(new ActionListener()
493           {
494             @Override
495             public void actionPerformed(ActionEvent e)
496             {
497               // TODO: pass on reference
498               // to ap
499               // so the
500               // view
501               // can be
502               // updated.
503               aaa.groupRef.setshowSequenceLogo(cprofl.getState());
504               ap.repaint();
505               // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
506             }
507           });
508           pop.add(cprofl);
509           final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
510                   MessageManager.getString("label.normalise_group_logo"),
511                   aa[selectedRow].groupRef.isNormaliseSequenceLogo());
512           cproflnorm.addActionListener(new ActionListener()
513           {
514             @Override
515             public void actionPerformed(ActionEvent e)
516             {
517
518               // TODO: pass on reference
519               // to ap
520               // so the
521               // view
522               // can be
523               // updated.
524               aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
525               // automatically enable logo display if we're clicked
526               aaa.groupRef.setshowSequenceLogo(true);
527               ap.repaint();
528               // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
529             }
530           });
531           pop.add(cproflnorm);
532         }
533         else
534         {
535           final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
536                   MessageManager.getString("label.show_histogram"),
537                   av.isShowConsensusHistogram());
538           chist.addActionListener(new ActionListener()
539           {
540             @Override
541             public void actionPerformed(ActionEvent e)
542             {
543               // TODO: pass on reference
544               // to ap
545               // so the
546               // view
547               // can be
548               // updated.
549               av.setShowConsensusHistogram(chist.getState());
550               ap.alignFrame.setMenusForViewport();
551               ap.repaint();
552               // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
553             }
554           });
555           pop.add(chist);
556           final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
557                   MessageManager.getString("label.show_logo"),
558                   av.isShowSequenceLogo());
559           cprof.addActionListener(new ActionListener()
560           {
561             @Override
562             public void actionPerformed(ActionEvent e)
563             {
564               // TODO: pass on reference
565               // to ap
566               // so the
567               // view
568               // can be
569               // updated.
570               av.setShowSequenceLogo(cprof.getState());
571               ap.alignFrame.setMenusForViewport();
572               ap.repaint();
573               // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
574             }
575           });
576           pop.add(cprof);
577           final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
578                   MessageManager.getString("label.normalise_logo"),
579                   av.isNormaliseSequenceLogo());
580           cprofnorm.addActionListener(new ActionListener()
581           {
582             @Override
583             public void actionPerformed(ActionEvent e)
584             {
585               // TODO: pass on reference
586               // to ap
587               // so the
588               // view
589               // can be
590               // updated.
591               av.setShowSequenceLogo(true);
592               av.setNormaliseSequenceLogo(cprofnorm.getState());
593               ap.alignFrame.setMenusForViewport();
594               ap.repaint();
595               // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
596             }
597           });
598           pop.add(cprofnorm);
599         }
600         final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
601         consclipbrd.addActionListener(this);
602         pop.add(consclipbrd);
603       }
604     }
605     pop.show(this, evt.getX(), evt.getY());
606   }
607
608   /**
609    * DOCUMENT ME!
610    * 
611    * @param evt
612    *          DOCUMENT ME!
613    */
614   @Override
615   public void mouseReleased(MouseEvent evt)
616   {
617     if (evt.isPopupTrigger())
618     {
619       showPopupMenu(evt);
620       return;
621     }
622
623     int start = selectedRow;
624     getSelectedRow(evt.getY() - getScrollOffset());
625     int end = selectedRow;
626
627     if (start != end)
628     {
629       // Swap these annotations
630       AlignmentAnnotation startAA = ap.av.getAlignment()
631               .getAlignmentAnnotation()[start];
632       if (end == -1)
633       {
634         end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
635       }
636       AlignmentAnnotation endAA = ap.av.getAlignment()
637               .getAlignmentAnnotation()[end];
638
639       ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
640       ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
641     }
642
643     resizePanel = false;
644     dragEvent = null;
645     repaint();
646     ap.getAnnotationPanel().repaint();
647   }
648
649   /**
650    * DOCUMENT ME!
651    * 
652    * @param evt
653    *          DOCUMENT ME!
654    */
655   @Override
656   public void mouseEntered(MouseEvent evt)
657   {
658     if (evt.getY() < 10)
659     {
660       resizePanel = true;
661       repaint();
662     }
663   }
664
665   /**
666    * DOCUMENT ME!
667    * 
668    * @param evt
669    *          DOCUMENT ME!
670    */
671   @Override
672   public void mouseExited(MouseEvent evt)
673   {
674     if (dragEvent == null)
675     {
676       resizePanel = false;
677       repaint();
678     }
679   }
680
681   /**
682    * DOCUMENT ME!
683    * 
684    * @param evt
685    *          DOCUMENT ME!
686    */
687   @Override
688   public void mouseDragged(MouseEvent evt)
689   {
690     dragEvent = evt;
691
692     if (resizePanel)
693     {
694       Dimension d = ap.annotationScroller.getPreferredSize();
695       int dif = evt.getY() - oldY;
696
697       dif /= ap.av.getCharHeight();
698       dif *= ap.av.getCharHeight();
699
700       if ((d.height - dif) > 20)
701       {
702         ap.annotationScroller.setPreferredSize(new Dimension(d.width,
703                 d.height - dif));
704         d = ap.annotationSpaceFillerHolder.getPreferredSize();
705         ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
706                 d.width, d.height - dif));
707         ap.paintAlignment(true);
708       }
709
710       ap.addNotify();
711     }
712     else
713     {
714       repaint();
715     }
716   }
717
718   /**
719    * DOCUMENT ME!
720    * 
721    * @param evt
722    *          DOCUMENT ME!
723    */
724   @Override
725   public void mouseMoved(MouseEvent evt)
726   {
727     resizePanel = evt.getY() < 10;
728
729     getSelectedRow(evt.getY() - getScrollOffset());
730
731     if (selectedRow > -1
732             && ap.av.getAlignment().getAlignmentAnnotation().length > selectedRow)
733     {
734       AlignmentAnnotation aa = ap.av.getAlignment()
735               .getAlignmentAnnotation()[selectedRow];
736
737       StringBuffer desc = new StringBuffer();
738       if (aa.description != null
739               && !aa.description.equals("New description"))
740       {
741         // TODO: we could refactor and merge this code with the code in
742         // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
743         // tooltips
744         desc.append(aa.getDescription(true).trim());
745         // check to see if the description is an html fragment.
746         if (desc.length() < 6
747                 || (desc.substring(0, 6).toLowerCase().indexOf("<html>") < 0))
748         {
749           // clean the description ready for embedding in html
750           desc = new StringBuffer(LEFT_ANGLE_BRACKET_PATTERN.matcher(desc)
751                   .replaceAll("&lt;"));
752           desc.insert(0, "<html>");
753         }
754         else
755         {
756           // remove terminating html if any
757           int i = desc.substring(desc.length() - 7).toLowerCase()
758                   .lastIndexOf("</html>");
759           if (i > -1)
760           {
761             desc.setLength(desc.length() - 7 + i);
762           }
763         }
764         if (aa.hasScore())
765         {
766           desc.append("<br/>");
767         }
768         // if (aa.hasProperties())
769         // {
770         // desc.append("<table>");
771         // for (String prop : aa.getProperties())
772         // {
773         // desc.append("<tr><td>" + prop + "</td><td>"
774         // + aa.getProperty(prop) + "</td><tr>");
775         // }
776         // desc.append("</table>");
777         // }
778       }
779       else
780       {
781         // begin the tooltip's html fragment
782         desc.append("<html>");
783         if (aa.hasScore())
784         {
785           // TODO: limit precision of score to avoid noise from imprecise
786           // doubles
787           // (64.7 becomes 64.7+/some tiny value).
788           desc.append(" Score: " + aa.score);
789         }
790       }
791       if (desc.length() > 6)
792       {
793         desc.append("</html>");
794         this.setToolTipText(desc.toString());
795       }
796       else
797       {
798         this.setToolTipText(null);
799       }
800     }
801   }
802
803   @Override
804   public void mouseClicked(MouseEvent evt)
805   {
806     final AlignmentAnnotation[] aa = ap.av.getAlignment()
807             .getAlignmentAnnotation();
808     if (!evt.isPopupTrigger() && SwingUtilities.isLeftMouseButton(evt))
809     {
810       if (selectedRow > -1 && selectedRow < aa.length)
811       {
812         if (aa[selectedRow].groupRef != null)
813         {
814           if (evt.getClickCount() >= 2)
815           {
816             // todo: make the ap scroll to the selection - not necessary, first
817             // click highlights/scrolls, second selects
818             ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
819             // process modifiers
820             SequenceGroup sg = ap.av.getSelectionGroup();
821             if (sg == null
822                     || sg == aa[selectedRow].groupRef
823                     || !(jalview.util.Platform.isControlDown(evt) || evt
824                             .isShiftDown()))
825             {
826               if (jalview.util.Platform.isControlDown(evt)
827                       || evt.isShiftDown())
828               {
829                 // clone a new selection group from the associated group
830                 ap.av.setSelectionGroup(new SequenceGroup(
831                         aa[selectedRow].groupRef));
832               }
833               else
834               {
835                 // set selection to the associated group so it can be edited
836                 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
837               }
838             }
839             else
840             {
841               // modify current selection with associated group
842               int remainToAdd = aa[selectedRow].groupRef.getSize();
843               for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
844               {
845                 if (jalview.util.Platform.isControlDown(evt))
846                 {
847                   sg.addOrRemove(sgs, --remainToAdd == 0);
848                 }
849                 else
850                 {
851                   // notionally, we should also add intermediate sequences from
852                   // last added sequence ?
853                   sg.addSequence(sgs, --remainToAdd == 0);
854                 }
855               }
856             }
857
858             ap.paintAlignment(false);
859             PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
860             ap.av.sendSelection();
861           }
862           else
863           {
864             ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(
865                     aa[selectedRow].groupRef.getSequences(null));
866           }
867           return;
868         }
869         else if (aa[selectedRow].sequenceRef != null)
870         {
871           if (evt.getClickCount() == 1)
872           {
873             ap.getSeqPanel().ap
874                     .getIdPanel()
875                     .highlightSearchResults(
876                             Arrays.asList(new SequenceI[] { aa[selectedRow].sequenceRef }));
877           }
878           else if (evt.getClickCount() >= 2)
879           {
880             ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
881             SequenceGroup sg = ap.av.getSelectionGroup();
882             if (sg != null)
883             {
884               // we make a copy rather than edit the current selection if no
885               // modifiers pressed
886               // see Enhancement JAL-1557
887               if (!(jalview.util.Platform.isControlDown(evt) || evt
888                       .isShiftDown()))
889               {
890                 sg = new SequenceGroup(sg);
891                 sg.clear();
892                 sg.addSequence(aa[selectedRow].sequenceRef, false);
893               }
894               else
895               {
896                 if (jalview.util.Platform.isControlDown(evt))
897                 {
898                   sg.addOrRemove(aa[selectedRow].sequenceRef, true);
899                 }
900                 else
901                 {
902                   // notionally, we should also add intermediate sequences from
903                   // last added sequence ?
904                   sg.addSequence(aa[selectedRow].sequenceRef, true);
905                 }
906               }
907             }
908             else
909             {
910               sg = new SequenceGroup();
911               sg.setStartRes(0);
912               sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
913               sg.addSequence(aa[selectedRow].sequenceRef, false);
914             }
915             ap.av.setSelectionGroup(sg);
916             ap.paintAlignment(false);
917             PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
918             ap.av.sendSelection();
919           }
920
921         }
922       }
923       return;
924     }
925   }
926
927   /**
928    * do a single sequence copy to jalview and the system clipboard
929    * 
930    * @param sq
931    *          sequence to be copied to clipboard
932    */
933   protected void copy_annotseqtoclipboard(SequenceI sq)
934   {
935     SequenceI[] seqs = new SequenceI[] { sq };
936     String[] omitHidden = null;
937     SequenceI[] dseqs = new SequenceI[] { sq.getDatasetSequence() };
938     if (dseqs[0] == null)
939     {
940       dseqs[0] = new Sequence(sq);
941       dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
942               jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
943
944       sq.setDatasetSequence(dseqs[0]);
945     }
946     Alignment ds = new Alignment(dseqs);
947     if (av.hasHiddenColumns())
948     {
949       omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
950               sq.getLength(), seqs);
951     }
952
953     int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 };
954     List<int[]> hiddenCols = av.getColumnSelection().getHiddenColumns();
955     if (hiddenCols != null)
956     {
957       alignmentStartEnd = av.getAlignment().getVisibleStartAndEndIndex(
958               hiddenCols);
959     }
960     String output = new FormatAdapter().formatSequences("Fasta", seqs,
961             omitHidden, alignmentStartEnd);
962
963     Toolkit.getDefaultToolkit().getSystemClipboard()
964             .setContents(new StringSelection(output), Desktop.instance);
965
966     ArrayList<int[]> hiddenColumns = null;
967     if (av.hasHiddenColumns())
968     {
969       hiddenColumns = new ArrayList<int[]>();
970       for (int[] region : av.getColumnSelection().getHiddenColumns())
971       {
972         hiddenColumns.add(new int[] { region[0], region[1] });
973       }
974     }
975
976     Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset
977                                                         // of a consensus
978                                                         // sequence ? need to
979                                                         // flag
980         // sequence as special.
981         hiddenColumns };
982   }
983
984   /**
985    * DOCUMENT ME!
986    * 
987    * @param g1
988    *          DOCUMENT ME!
989    */
990   @Override
991   public void paintComponent(Graphics g)
992   {
993
994     int width = getWidth();
995     if (width == 0)
996     {
997       width = ap.calculateIdWidth().width + 4;
998     }
999
1000     Graphics2D g2 = (Graphics2D) g;
1001     if (av.antiAlias)
1002     {
1003       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1004               RenderingHints.VALUE_ANTIALIAS_ON);
1005     }
1006
1007     drawComponent(g2, true, width);
1008
1009   }
1010
1011   /**
1012    * Draw the full set of annotation Labels for the alignment at the given
1013    * cursor
1014    * 
1015    * @param g
1016    *          Graphics2D instance (needed for font scaling)
1017    * @param width
1018    *          Width for scaling labels
1019    * 
1020    */
1021   public void drawComponent(Graphics g, int width)
1022   {
1023     drawComponent(g, false, width);
1024   }
1025
1026   private final boolean debugRedraw = false;
1027
1028   /**
1029    * Draw the full set of annotation Labels for the alignment at the given
1030    * cursor
1031    * 
1032    * @param g
1033    *          Graphics2D instance (needed for font scaling)
1034    * @param clip
1035    *          - true indicates that only current visible area needs to be
1036    *          rendered
1037    * @param width
1038    *          Width for scaling labels
1039    */
1040   public void drawComponent(Graphics g, boolean clip, int width)
1041   {
1042     if (av.getFont().getSize() < 10)
1043     {
1044       g.setFont(font);
1045     }
1046     else
1047     {
1048       g.setFont(av.getFont());
1049     }
1050
1051     FontMetrics fm = g.getFontMetrics(g.getFont());
1052     g.setColor(Color.white);
1053     g.fillRect(0, 0, getWidth(), getHeight());
1054
1055     g.translate(0, getScrollOffset());
1056     g.setColor(Color.black);
1057
1058     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1059     int fontHeight = g.getFont().getSize();
1060     int y = 0;
1061     int x = 0;
1062     int graphExtras = 0;
1063     int offset = 0;
1064     Font baseFont = g.getFont();
1065     FontMetrics baseMetrics = fm;
1066     int ofontH = fontHeight;
1067     int sOffset = 0;
1068     int visHeight = 0;
1069     int[] visr = (ap != null && ap.getAnnotationPanel() != null) ? ap
1070             .getAnnotationPanel().getVisibleVRange() : null;
1071     if (clip && visr != null)
1072     {
1073       sOffset = visr[0];
1074       visHeight = visr[1];
1075     }
1076     boolean visible = true, before = false, after = false;
1077     if (aa != null)
1078     {
1079       hasHiddenRows = false;
1080       int olY = 0;
1081       for (int i = 0; i < aa.length; i++)
1082       {
1083         visible = true;
1084         if (!aa[i].visible)
1085         {
1086           hasHiddenRows = true;
1087           continue;
1088         }
1089         olY = y;
1090         y += aa[i].height;
1091         if (clip)
1092         {
1093           if (y < sOffset)
1094           {
1095             if (!before)
1096             {
1097               if (debugRedraw)
1098               {
1099                 System.out.println("before vis: " + i);
1100               }
1101               before = true;
1102             }
1103             // don't draw what isn't visible
1104             continue;
1105           }
1106           if (olY > visHeight)
1107           {
1108
1109             if (!after)
1110             {
1111               if (debugRedraw)
1112               {
1113                 System.out.println("Scroll offset: " + sOffset
1114                         + " after vis: " + i);
1115               }
1116               after = true;
1117             }
1118             // don't draw what isn't visible
1119             continue;
1120           }
1121         }
1122         g.setColor(Color.black);
1123
1124         offset = -aa[i].height / 2;
1125
1126         if (aa[i].hasText)
1127         {
1128           offset += fm.getHeight() / 2;
1129           offset -= fm.getDescent();
1130         }
1131         else
1132         {
1133           offset += fm.getDescent();
1134         }
1135
1136         x = width - fm.stringWidth(aa[i].label) - 3;
1137
1138         if (aa[i].graphGroup > -1)
1139         {
1140           int groupSize = 0;
1141           // TODO: JAL-1291 revise rendering model so the graphGroup map is
1142           // computed efficiently for all visible labels
1143           for (int gg = 0; gg < aa.length; gg++)
1144           {
1145             if (aa[gg].graphGroup == aa[i].graphGroup)
1146             {
1147               groupSize++;
1148             }
1149           }
1150           if (groupSize * (fontHeight + 8) < aa[i].height)
1151           {
1152             graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
1153           }
1154           else
1155           {
1156             // scale font to fit
1157             float h = aa[i].height / (float) groupSize, s;
1158             if (h < 9)
1159             {
1160               visible = false;
1161             }
1162             else
1163             {
1164               fontHeight = -8 + (int) h;
1165               s = ((float) fontHeight) / (float) ofontH;
1166               Font f = baseFont.deriveFont(AffineTransform
1167                       .getScaleInstance(s, s));
1168               g.setFont(f);
1169               fm = g.getFontMetrics();
1170               graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
1171             }
1172           }
1173           if (visible)
1174           {
1175             for (int gg = 0; gg < aa.length; gg++)
1176             {
1177               if (aa[gg].graphGroup == aa[i].graphGroup)
1178               {
1179                 x = width - fm.stringWidth(aa[gg].label) - 3;
1180                 g.drawString(aa[gg].label, x, y - graphExtras);
1181
1182                 if (aa[gg]._linecolour != null)
1183                 {
1184
1185                   g.setColor(aa[gg]._linecolour);
1186                   g.drawLine(x, y - graphExtras + 3,
1187                           x + fm.stringWidth(aa[gg].label), y - graphExtras
1188                                   + 3);
1189                 }
1190
1191                 g.setColor(Color.black);
1192                 graphExtras += fontHeight + 8;
1193               }
1194             }
1195           }
1196           g.setFont(baseFont);
1197           fm = baseMetrics;
1198           fontHeight = ofontH;
1199         }
1200         else
1201         {
1202           g.drawString(aa[i].label, x, y + offset);
1203         }
1204       }
1205     }
1206
1207     if (resizePanel)
1208     {
1209       g.drawImage(image, 2, 0 - getScrollOffset(), this);
1210     }
1211     else if (dragEvent != null && aa != null)
1212     {
1213       g.setColor(Color.lightGray);
1214       g.drawString(aa[selectedRow].label, dragEvent.getX(),
1215               dragEvent.getY() - getScrollOffset());
1216     }
1217
1218     if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
1219     {
1220       g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1221       g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
1222               18);
1223     }
1224   }
1225
1226   public int getScrollOffset()
1227   {
1228     return scrollOffset;
1229   }
1230 }