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