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