Merge branch 'features/JAL-2885UniprotHttps' into releases/Release_2_10_4_Branch
[jalview.git] / src / jalview / appletgui / 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.appletgui;
22
23 import jalview.analysis.AlignmentUtils;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.Annotation;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.MessageManager;
29 import jalview.util.ParseHtmlBodyAndLinks;
30
31 import java.awt.Checkbox;
32 import java.awt.CheckboxMenuItem;
33 import java.awt.Color;
34 import java.awt.Cursor;
35 import java.awt.Dimension;
36 import java.awt.FlowLayout;
37 import java.awt.FontMetrics;
38 import java.awt.Frame;
39 import java.awt.Graphics;
40 import java.awt.Image;
41 import java.awt.MenuItem;
42 import java.awt.Panel;
43 import java.awt.PopupMenu;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.InputEvent;
47 import java.awt.event.ItemEvent;
48 import java.awt.event.ItemListener;
49 import java.awt.event.MouseEvent;
50 import java.awt.event.MouseListener;
51 import java.awt.event.MouseMotionListener;
52 import java.util.Arrays;
53 import java.util.Collections;
54 import java.util.Vector;
55
56 public class AnnotationLabels extends Panel
57         implements ActionListener, MouseListener, MouseMotionListener
58 {
59   Image image;
60
61   /**
62    * width in pixels within which height adjuster arrows are shown and active
63    */
64   private static final int HEIGHT_ADJUSTER_WIDTH = 50;
65
66   /**
67    * height in pixels for allowing height adjuster to be active
68    */
69   private static int HEIGHT_ADJUSTER_HEIGHT = 10;
70
71   boolean active = false;
72
73   AlignmentPanel ap;
74
75   AlignViewport av;
76
77   boolean resizing = false;
78
79   int oldY, mouseX;
80
81   static String ADDNEW = "Add New Row";
82
83   static String EDITNAME = "Edit Label/Description";
84
85   static String HIDE = "Hide This Row";
86
87   static String SHOWALL = "Show All Hidden Rows";
88
89   static String OUTPUT_TEXT = "Show Values In Textbox";
90
91   static String COPYCONS_SEQ = "Copy Consensus Sequence";
92
93   int scrollOffset = 0;
94
95   int selectedRow = -1;
96
97   Tooltip tooltip;
98
99   private boolean hasHiddenRows;
100
101   public AnnotationLabels(AlignmentPanel ap)
102   {
103     this.ap = ap;
104     this.av = ap.av;
105     setLayout(null);
106     addMouseListener(this);
107     addMouseMotionListener(this);
108   }
109
110   public AnnotationLabels(AlignViewport av)
111   {
112     this.av = av;
113   }
114
115   public void setScrollOffset(int y, boolean repaint)
116   {
117     scrollOffset = y;
118     if (repaint)
119     {
120       repaint();
121     }
122   }
123
124   /**
125    * 
126    * @param y
127    * @return -2 if no rows are visible at all, -1 if no visible rows were
128    *         selected
129    */
130   int getSelectedRow(int y)
131   {
132     int row = -2;
133     AlignmentAnnotation[] aa = ap.av.getAlignment()
134             .getAlignmentAnnotation();
135
136     if (aa == null)
137     {
138       return row;
139     }
140     int height = 0;
141     for (int i = 0; i < aa.length; i++)
142     {
143       row = -1;
144       if (!aa[i].visible)
145       {
146         continue;
147       }
148       height += aa[i].height;
149       if (y < height)
150       {
151         row = i;
152         break;
153       }
154     }
155
156     return row;
157   }
158
159   @Override
160   public void actionPerformed(ActionEvent evt)
161   {
162     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
163
164     if (evt.getActionCommand().equals(ADDNEW))
165     {
166       AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
167               new Annotation[ap.av.getAlignment().getWidth()]);
168
169       if (!editLabelDescription(newAnnotation))
170       {
171         return;
172       }
173
174       ap.av.getAlignment().addAnnotation(newAnnotation);
175       ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
176     }
177     else if (evt.getActionCommand().equals(EDITNAME))
178     {
179       editLabelDescription(aa[selectedRow]);
180     }
181     else if (evt.getActionCommand().equals(HIDE))
182     {
183       aa[selectedRow].visible = false;
184     }
185     else if (evt.getActionCommand().equals(SHOWALL))
186     {
187       for (int i = 0; i < aa.length; i++)
188       {
189         aa[i].visible = (aa[i].annotations == null) ? false : true;
190       }
191     }
192     else if (evt.getActionCommand().equals(OUTPUT_TEXT))
193     {
194       CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
195               ap.alignFrame);
196       Frame frame = new Frame();
197       frame.add(cap);
198       jalview.bin.JalviewLite.addFrame(frame,
199               ap.alignFrame.getTitle() + " - " + aa[selectedRow].label, 500,
200               100);
201       cap.setText(aa[selectedRow].toString());
202     }
203     else if (evt.getActionCommand().equals(COPYCONS_SEQ))
204     {
205       SequenceI cons = av.getConsensusSeq();
206       if (cons != null)
207       {
208         copy_annotseqtoclipboard(cons);
209       }
210
211     }
212     refresh();
213   }
214
215   /**
216    * Adjust size and repaint
217    */
218   protected void refresh()
219   {
220     ap.annotationPanel.adjustPanelHeight();
221     setSize(getSize().width, ap.annotationPanel.getSize().height);
222     ap.validate();
223     // TODO: only paint if we needed to
224     ap.paintAlignment(true, true);
225   }
226
227   boolean editLabelDescription(AlignmentAnnotation annotation)
228   {
229     Checkbox padGaps = new Checkbox(
230             "Fill Empty Gaps With \"" + ap.av.getGapCharacter() + "\"",
231             annotation.padGaps);
232
233     EditNameDialog dialog = new EditNameDialog(annotation.label,
234             annotation.description, "      Annotation Label",
235             "Annotation Description", ap.alignFrame,
236             "Edit Annotation Name / Description", 500, 180, false);
237
238     Panel empty = new Panel(new FlowLayout());
239     empty.add(padGaps);
240     dialog.add(empty);
241     dialog.pack();
242
243     dialog.setVisible(true);
244
245     if (dialog.accept)
246     {
247       annotation.label = dialog.getName();
248       annotation.description = dialog.getDescription();
249       annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
250       repaint();
251       return true;
252     }
253     else
254     {
255       return false;
256     }
257
258   }
259
260   boolean resizePanel = false;
261
262   @Override
263   public void mouseMoved(MouseEvent evt)
264   {
265     resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
266             && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
267     setCursor(Cursor.getPredefinedCursor(
268             resizePanel ? Cursor.S_RESIZE_CURSOR : Cursor.DEFAULT_CURSOR));
269     int row = getSelectedRow(evt.getY() + scrollOffset);
270
271     if (row > -1)
272     {
273       ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
274               av.getAlignment().getAlignmentAnnotation()[row]
275                       .getDescription(true),
276               true, "\n");
277       if (tooltip == null)
278       {
279         tooltip = new Tooltip(phb.getNonHtmlContent(), this);
280       }
281       else
282       {
283         tooltip.setTip(phb.getNonHtmlContent());
284       }
285     }
286     else if (tooltip != null)
287     {
288       tooltip.setTip("");
289     }
290   }
291
292   /**
293    * curent drag position
294    */
295   MouseEvent dragEvent = null;
296
297   /**
298    * flag to indicate drag events should be ignored
299    */
300   private boolean dragCancelled = false;
301
302   /**
303    * clear any drag events in progress
304    */
305   public void cancelDrag()
306   {
307     dragEvent = null;
308     dragCancelled = true;
309   }
310
311   @Override
312   public void mouseDragged(MouseEvent evt)
313   {
314     if (dragCancelled)
315     {
316       return;
317     }
318     ;
319     dragEvent = evt;
320
321     if (resizePanel)
322     {
323       Dimension d = ap.annotationPanelHolder.getSize(),
324               e = ap.annotationSpaceFillerHolder.getSize(),
325               f = ap.seqPanelHolder.getSize();
326       int dif = evt.getY() - oldY;
327
328       dif /= ap.av.getCharHeight();
329       dif *= ap.av.getCharHeight();
330
331       if ((d.height - dif) > 20 && (f.height + dif) > 20)
332       {
333         ap.annotationPanel.setSize(d.width, d.height - dif);
334         setSize(new Dimension(e.width, d.height - dif));
335         ap.annotationSpaceFillerHolder
336                 .setSize(new Dimension(e.width, d.height - dif));
337         ap.annotationPanelHolder
338                 .setSize(new Dimension(d.width, d.height - dif));
339         ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
340                 av.calcPanelHeight());
341         f.height += dif;
342         ap.seqPanelHolder.setPreferredSize(f);
343         ap.setScrollValues(av.getRanges().getStartRes(),
344                 av.getRanges().getStartSeq());
345         ap.validate();
346         // ap.paintAlignment(true);
347         ap.addNotify();
348       }
349
350     }
351     else
352     {
353       int diff;
354       if ((diff = 6 - evt.getY()) > 0)
355       {
356         // nudge scroll up
357         ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
358         ap.adjustmentValueChanged(null);
359
360       }
361       else if ((0 < (diff = 6
362               - ap.annotationSpaceFillerHolder.getSize().height
363               + evt.getY())))
364       {
365         // nudge scroll down
366         ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
367         ap.adjustmentValueChanged(null);
368       }
369       repaint();
370     }
371   }
372
373   @Override
374   public void mouseClicked(MouseEvent evt)
375   {
376   }
377
378   @Override
379   public void mouseReleased(MouseEvent evt)
380   {
381     if (!resizePanel && !dragCancelled)
382     {
383       int start = selectedRow;
384
385       int end = getSelectedRow(evt.getY() + scrollOffset);
386
387       if (start > -1 && start != end)
388       {
389         // Swap these annotations
390         AlignmentAnnotation startAA = ap.av.getAlignment()
391                 .getAlignmentAnnotation()[start];
392         if (end == -1)
393         {
394           end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
395         }
396         AlignmentAnnotation endAA = ap.av.getAlignment()
397                 .getAlignmentAnnotation()[end];
398
399         ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
400         ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
401       }
402     }
403     resizePanel = false;
404     dragEvent = null;
405     dragCancelled = false;
406     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
407     repaint();
408     ap.annotationPanel.repaint();
409   }
410
411   @Override
412   public void mouseEntered(MouseEvent evt)
413   {
414     if (evt.getY() < 10 && evt.getX() < 14)
415     {
416       resizePanel = true;
417       repaint();
418     }
419     setCursor(Cursor.getPredefinedCursor(
420             resizePanel ? Cursor.S_RESIZE_CURSOR : Cursor.DEFAULT_CURSOR));
421   }
422
423   @Override
424   public void mouseExited(MouseEvent evt)
425   {
426     dragCancelled = false;
427
428     if (dragEvent == null)
429     {
430       resizePanel = false;
431     }
432     else
433     {
434       if (!resizePanel)
435       {
436         dragEvent = null;
437       }
438     }
439     repaint();
440   }
441
442   @Override
443   public void mousePressed(MouseEvent evt)
444   {
445     oldY = evt.getY();
446     if (resizePanel)
447     {
448       return;
449     }
450     dragCancelled = false;
451     // todo: move below to mouseClicked ?
452     selectedRow = getSelectedRow(evt.getY() + scrollOffset);
453
454     AlignmentAnnotation[] aa = ap.av.getAlignment()
455             .getAlignmentAnnotation();
456
457     // DETECT RIGHT MOUSE BUTTON IN AWT
458     if ((evt.getModifiers()
459             & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
460     {
461
462       PopupMenu popup = new PopupMenu(
463               MessageManager.getString("label.annotations"));
464
465       MenuItem item = new MenuItem(ADDNEW);
466       item.addActionListener(this);
467       popup.add(item);
468       if (selectedRow < 0)
469       {
470         // this never happens at moment: - see comment on JAL-563
471         if (hasHiddenRows)
472         {
473           item = new MenuItem(SHOWALL);
474           item.addActionListener(this);
475           popup.add(item);
476         }
477         this.add(popup);
478         popup.show(this, evt.getX(), evt.getY());
479         return;
480       }
481       // add the rest if there are actually rows to show
482       item = new MenuItem(EDITNAME);
483       item.addActionListener(this);
484       popup.add(item);
485       item = new MenuItem(HIDE);
486       item.addActionListener(this);
487       popup.add(item);
488
489       /*
490        * Hide all <label>:
491        */
492       if (selectedRow < aa.length)
493       {
494         if (aa[selectedRow].sequenceRef != null)
495         {
496           final String label = aa[selectedRow].label;
497           MenuItem hideType = new MenuItem(
498                   MessageManager.getString("label.hide_all") + " " + label);
499           hideType.addActionListener(new ActionListener()
500           {
501             @Override
502             public void actionPerformed(ActionEvent e)
503             {
504               AlignmentUtils.showOrHideSequenceAnnotations(
505                       ap.av.getAlignment(), Collections.singleton(label),
506                       null, false, false);
507               refresh();
508             }
509           });
510           popup.add(hideType);
511         }
512       }
513
514       if (hasHiddenRows)
515       {
516         item = new MenuItem(SHOWALL);
517         item.addActionListener(this);
518         popup.add(item);
519       }
520       this.add(popup);
521       item = new MenuItem(OUTPUT_TEXT);
522       item.addActionListener(this);
523       popup.add(item);
524       if (selectedRow < aa.length)
525       {
526         if (aa[selectedRow].autoCalculated)
527         {
528           if (aa[selectedRow].label.indexOf("Consensus") > -1)
529           {
530             popup.addSeparator();
531             final CheckboxMenuItem cbmi = new CheckboxMenuItem(
532                     MessageManager.getString("label.ignore_gaps_consensus"),
533                     (aa[selectedRow].groupRef != null)
534                             ? aa[selectedRow].groupRef
535                                     .getIgnoreGapsConsensus()
536                             : ap.av.isIgnoreGapsConsensus());
537             final AlignmentAnnotation aaa = aa[selectedRow];
538             cbmi.addItemListener(new ItemListener()
539             {
540               @Override
541               public void itemStateChanged(ItemEvent e)
542               {
543                 if (aaa.groupRef != null)
544                 {
545                   // TODO: pass on reference to ap so the view can be updated.
546                   aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
547                 }
548                 else
549                 {
550                   ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
551                 }
552                 ap.paintAlignment(true, true);
553               }
554             });
555             popup.add(cbmi);
556             if (aaa.groupRef != null)
557             {
558               final CheckboxMenuItem chist = new CheckboxMenuItem(
559                       MessageManager
560                               .getString("label.show_group_histogram"),
561                       aa[selectedRow].groupRef.isShowConsensusHistogram());
562               chist.addItemListener(new ItemListener()
563               {
564                 @Override
565                 public void itemStateChanged(ItemEvent e)
566                 {
567                   // TODO: pass on reference
568                   // to ap
569                   // so the
570                   // view
571                   // can be
572                   // updated.
573                   aaa.groupRef.setShowConsensusHistogram(chist.getState());
574                   ap.repaint();
575                   // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
576                 }
577               });
578               popup.add(chist);
579               final CheckboxMenuItem cprofl = new CheckboxMenuItem(
580                       MessageManager.getString("label.show_group_logo"),
581                       aa[selectedRow].groupRef.isShowSequenceLogo());
582               cprofl.addItemListener(new ItemListener()
583               {
584                 @Override
585                 public void itemStateChanged(ItemEvent e)
586                 {
587                   // TODO: pass on reference
588                   // to ap
589                   // so the
590                   // view
591                   // can be
592                   // updated.
593                   aaa.groupRef.setshowSequenceLogo(cprofl.getState());
594                   ap.repaint();
595                   // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
596                 }
597               });
598
599               popup.add(cprofl);
600               final CheckboxMenuItem cprofn = new CheckboxMenuItem(
601                       MessageManager
602                               .getString("label.normalise_group_logo"),
603                       aa[selectedRow].groupRef.isNormaliseSequenceLogo());
604               cprofn.addItemListener(new ItemListener()
605               {
606                 @Override
607                 public void itemStateChanged(ItemEvent e)
608                 {
609                   // TODO: pass on reference
610                   // to ap
611                   // so the
612                   // view
613                   // can be
614                   // updated.
615                   aaa.groupRef.setshowSequenceLogo(true);
616                   aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
617                   ap.repaint();
618                   // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
619                 }
620               });
621               popup.add(cprofn);
622             }
623             else
624             {
625               final CheckboxMenuItem chist = new CheckboxMenuItem(
626                       MessageManager.getString("label.show_histogram"),
627                       av.isShowConsensusHistogram());
628               chist.addItemListener(new ItemListener()
629               {
630                 @Override
631                 public void itemStateChanged(ItemEvent e)
632                 {
633                   // TODO: pass on reference
634                   // to ap
635                   // so the
636                   // view
637                   // can be
638                   // updated.
639                   av.setShowConsensusHistogram(chist.getState());
640                   ap.alignFrame.showConsensusHistogram
641                           .setState(chist.getState()); // TODO: implement
642                                                        // ap.updateGUI()/alignFrame.updateGUI
643                                                        // for applet
644                   ap.repaint();
645                   // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
646                 }
647               });
648               popup.add(chist);
649               final CheckboxMenuItem cprof = new CheckboxMenuItem(
650                       MessageManager.getString("label.show_logo"),
651                       av.isShowSequenceLogo());
652               cprof.addItemListener(new ItemListener()
653               {
654                 @Override
655                 public void itemStateChanged(ItemEvent e)
656                 {
657                   // TODO: pass on reference
658                   // to ap
659                   // so the
660                   // view
661                   // can be
662                   // updated.
663                   av.setShowSequenceLogo(cprof.getState());
664                   ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
665                                                                              // implement
666                                                                              // ap.updateGUI()/alignFrame.updateGUI
667                                                                              // for
668                                                                              // applet
669                   ap.repaint();
670                   // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
671                 }
672               });
673               popup.add(cprof);
674               final CheckboxMenuItem cprofn = new CheckboxMenuItem(
675                       MessageManager.getString("label.normalise_logo"),
676                       av.isNormaliseSequenceLogo());
677               cprofn.addItemListener(new ItemListener()
678               {
679                 @Override
680                 public void itemStateChanged(ItemEvent e)
681                 {
682                   // TODO: pass on reference
683                   // to ap
684                   // so the
685                   // view
686                   // can be
687                   // updated.
688                   av.setShowSequenceLogo(true);
689                   ap.alignFrame.normSequenceLogo
690                           .setState(cprofn.getState()); // TODO:
691                                                         // implement
692                                                         // ap.updateGUI()/alignFrame.updateGUI
693                                                         // for
694                                                         // applet
695                   av.setNormaliseSequenceLogo(cprofn.getState());
696                   ap.repaint();
697                   // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
698                 }
699               });
700               popup.add(cprofn);
701             }
702
703             item = new MenuItem(COPYCONS_SEQ);
704             item.addActionListener(this);
705             popup.add(item);
706           }
707         }
708       }
709       popup.show(this, evt.getX(), evt.getY());
710     }
711     else
712     {
713       // selection action.
714       if (selectedRow > -1 && selectedRow < aa.length)
715       {
716         if (aa[selectedRow].groupRef != null)
717         {
718           if (evt.getClickCount() >= 2)
719           {
720             // todo: make the ap scroll to the selection - not necessary, first
721             // click highlights/scrolls, second selects
722             ap.seqPanel.ap.idPanel.highlightSearchResults(null);
723             // process modifiers
724             SequenceGroup sg = ap.av.getSelectionGroup();
725             if (sg == null || sg == aa[selectedRow].groupRef
726                     || !(jalview.util.Platform.isControlDown(evt)
727                             || evt.isShiftDown()))
728             {
729               if (jalview.util.Platform.isControlDown(evt)
730                       || evt.isShiftDown())
731               {
732                 // clone a new selection group from the associated group
733                 ap.av.setSelectionGroup(
734                         new SequenceGroup(aa[selectedRow].groupRef));
735               }
736               else
737               {
738                 // set selection to the associated group so it can be edited
739                 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
740               }
741             }
742             else
743             {
744               // modify current selection with associated group
745               int remainToAdd = aa[selectedRow].groupRef.getSize();
746               for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
747               {
748                 if (jalview.util.Platform.isControlDown(evt))
749                 {
750                   sg.addOrRemove(sgs, --remainToAdd == 0);
751                 }
752                 else
753                 {
754                   // notionally, we should also add intermediate sequences from
755                   // last added sequence ?
756                   sg.addSequence(sgs, --remainToAdd == 0);
757                 }
758               }
759             }
760             ap.paintAlignment(false, false);
761             PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
762             ap.av.sendSelection();
763           }
764           else
765           {
766             ap.seqPanel.ap.idPanel.highlightSearchResults(
767                     aa[selectedRow].groupRef.getSequences(null));
768           }
769           return;
770         }
771         else if (aa[selectedRow].sequenceRef != null)
772         {
773           if (evt.getClickCount() == 1)
774           {
775             ap.seqPanel.ap.idPanel
776                     .highlightSearchResults(Arrays.asList(new SequenceI[]
777                     { aa[selectedRow].sequenceRef }));
778           }
779           else if (evt.getClickCount() >= 2)
780           {
781             ap.seqPanel.ap.idPanel.highlightSearchResults(null);
782             SequenceGroup sg = ap.av.getSelectionGroup();
783             if (sg != null)
784             {
785               // we make a copy rather than edit the current selection if no
786               // modifiers pressed
787               // see Enhancement JAL-1557
788               if (!(jalview.util.Platform.isControlDown(evt)
789                       || evt.isShiftDown()))
790               {
791                 sg = new SequenceGroup(sg);
792                 sg.clear();
793                 sg.addSequence(aa[selectedRow].sequenceRef, false);
794               }
795               else
796               {
797                 if (jalview.util.Platform.isControlDown(evt))
798                 {
799                   sg.addOrRemove(aa[selectedRow].sequenceRef, true);
800                 }
801                 else
802                 {
803                   // notionally, we should also add intermediate sequences from
804                   // last added sequence ?
805                   sg.addSequence(aa[selectedRow].sequenceRef, true);
806                 }
807               }
808             }
809             else
810             {
811               sg = new SequenceGroup();
812               sg.setStartRes(0);
813               sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
814               sg.addSequence(aa[selectedRow].sequenceRef, false);
815             }
816             ap.av.setSelectionGroup(sg);
817             ap.paintAlignment(false, false);
818             PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
819             ap.av.sendSelection();
820           }
821
822         }
823       }
824
825     }
826   }
827
828   /**
829    * DOCUMENT ME!
830    * 
831    * @param e
832    *          DOCUMENT ME!
833    */
834   protected void copy_annotseqtoclipboard(SequenceI sq)
835   {
836     if (sq == null || sq.getLength() < 1)
837     {
838       return;
839     }
840     jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
841     jalview.appletgui.AlignFrame.copiedSequences
842             .append(sq.getName() + "\t" + sq.getStart() + "\t" + sq.getEnd()
843                     + "\t" + sq.getSequenceAsString() + "\n");
844     if (av.hasHiddenColumns())
845     {
846       jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector<>(
847               av.getAlignment().getHiddenColumns().getHiddenColumnsCopy());
848     }
849   }
850
851   @Override
852   public void update(Graphics g)
853   {
854     paint(g);
855   }
856
857   @Override
858   public void paint(Graphics g)
859   {
860     int w = getSize().width;
861     int h = getSize().height;
862     if (image == null || w != image.getWidth(this)
863             || h != image.getHeight(this))
864     {
865       image = createImage(w, ap.annotationPanel.getSize().height);
866     }
867
868     drawComponent(image.getGraphics(), w);
869     g.drawImage(image, 0, 0, this);
870   }
871
872   public void drawComponent(Graphics g, int width)
873   {
874     g.setFont(av.getFont());
875     FontMetrics fm = g.getFontMetrics(av.getFont());
876     g.setColor(Color.white);
877     g.fillRect(0, 0, getSize().width, getSize().height);
878
879     g.translate(0, -scrollOffset);
880     g.setColor(Color.black);
881
882     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
883     int y = 0, fy = g.getFont().getSize();
884     int x = 0, offset;
885
886     if (aa != null)
887     {
888       hasHiddenRows = false;
889       for (int i = 0; i < aa.length; i++)
890       {
891         if (!aa[i].visible)
892         {
893           hasHiddenRows = true;
894           continue;
895         }
896
897         x = width - fm.stringWidth(aa[i].label) - 3;
898
899         y += aa[i].height;
900         offset = -(aa[i].height - fy) / 2;
901
902         g.drawString(aa[i].label, x, y + offset);
903       }
904     }
905     g.translate(0, +scrollOffset);
906
907     if (!resizePanel && !dragCancelled && dragEvent != null && aa != null)
908     {
909       g.setColor(Color.lightGray);
910       g.drawString(aa[selectedRow].label, dragEvent.getX(),
911               dragEvent.getY());
912     }
913
914     if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
915     {
916       g.setColor(Color.black);
917       g.drawString(MessageManager.getString("label.right_click"), 2, 8);
918       g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
919               18);
920     }
921   }
922 }