Merge branch 'features/JAL-1723_sequenceReport' into develop
[jalview.git] / src / jalview / gui / PopupMenu.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.analysis.AAFrequency;
24 import jalview.analysis.AlignmentAnnotationUtils;
25 import jalview.analysis.AlignmentUtils;
26 import jalview.analysis.Conservation;
27 import jalview.bin.Cache;
28 import jalview.commands.ChangeCaseCommand;
29 import jalview.commands.EditCommand;
30 import jalview.commands.EditCommand.Action;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.Annotation;
34 import jalview.datamodel.ColumnSelection;
35 import jalview.datamodel.DBRefEntry;
36 import jalview.datamodel.PDBEntry;
37 import jalview.datamodel.Sequence;
38 import jalview.datamodel.SequenceFeature;
39 import jalview.datamodel.SequenceGroup;
40 import jalview.datamodel.SequenceI;
41 import jalview.io.FormatAdapter;
42 import jalview.io.SequenceAnnotationReport;
43 import jalview.schemes.AnnotationColourGradient;
44 import jalview.schemes.Blosum62ColourScheme;
45 import jalview.schemes.BuriedColourScheme;
46 import jalview.schemes.ClustalxColourScheme;
47 import jalview.schemes.HelixColourScheme;
48 import jalview.schemes.HydrophobicColourScheme;
49 import jalview.schemes.NucleotideColourScheme;
50 import jalview.schemes.PIDColourScheme;
51 import jalview.schemes.PurinePyrimidineColourScheme;
52 import jalview.schemes.StrandColourScheme;
53 import jalview.schemes.TaylorColourScheme;
54 import jalview.schemes.TurnColourScheme;
55 import jalview.schemes.UserColourScheme;
56 import jalview.schemes.ZappoColourScheme;
57 import jalview.util.GroupUrlLink;
58 import jalview.util.GroupUrlLink.UrlStringTooLongException;
59 import jalview.util.MessageManager;
60 import jalview.util.UrlLink;
61
62 import java.awt.Color;
63 import java.awt.event.ActionEvent;
64 import java.awt.event.ActionListener;
65 import java.util.Arrays;
66 import java.util.Collection;
67 import java.util.Collections;
68 import java.util.Hashtable;
69 import java.util.LinkedHashMap;
70 import java.util.List;
71 import java.util.Map;
72 import java.util.TreeMap;
73 import java.util.Vector;
74
75 import javax.swing.ButtonGroup;
76 import javax.swing.JCheckBoxMenuItem;
77 import javax.swing.JColorChooser;
78 import javax.swing.JMenu;
79 import javax.swing.JMenuItem;
80 import javax.swing.JOptionPane;
81 import javax.swing.JPopupMenu;
82 import javax.swing.JRadioButtonMenuItem;
83
84 /**
85  * DOCUMENT ME!
86  * 
87  * @author $author$
88  * @version $Revision: 1.118 $
89  */
90 public class PopupMenu extends JPopupMenu
91 {
92   JMenu groupMenu = new JMenu();
93
94   JMenuItem groupName = new JMenuItem();
95
96   protected JRadioButtonMenuItem clustalColour = new JRadioButtonMenuItem();
97
98   protected JRadioButtonMenuItem zappoColour = new JRadioButtonMenuItem();
99
100   protected JRadioButtonMenuItem taylorColour = new JRadioButtonMenuItem();
101
102   protected JRadioButtonMenuItem hydrophobicityColour = new JRadioButtonMenuItem();
103
104   protected JRadioButtonMenuItem helixColour = new JRadioButtonMenuItem();
105
106   protected JRadioButtonMenuItem strandColour = new JRadioButtonMenuItem();
107
108   protected JRadioButtonMenuItem turnColour = new JRadioButtonMenuItem();
109
110   protected JRadioButtonMenuItem buriedColour = new JRadioButtonMenuItem();
111
112   protected JCheckBoxMenuItem abovePIDColour = new JCheckBoxMenuItem();
113
114   protected JRadioButtonMenuItem userDefinedColour = new JRadioButtonMenuItem();
115
116   protected JRadioButtonMenuItem PIDColour = new JRadioButtonMenuItem();
117
118   protected JRadioButtonMenuItem BLOSUM62Colour = new JRadioButtonMenuItem();
119
120   protected JRadioButtonMenuItem purinePyrimidineColour = new JRadioButtonMenuItem();
121
122   protected JRadioButtonMenuItem RNAInteractionColour = new JRadioButtonMenuItem();
123
124   JRadioButtonMenuItem noColourmenuItem = new JRadioButtonMenuItem();
125
126   protected JCheckBoxMenuItem conservationMenuItem = new JCheckBoxMenuItem();
127
128   AlignmentPanel ap;
129
130   JMenu sequenceMenu = new JMenu();
131
132   JMenuItem sequenceName = new JMenuItem();
133
134   JMenuItem sequenceDetails = new JMenuItem();
135
136   JMenuItem sequenceSelDetails = new JMenuItem();
137
138   JMenuItem makeReferenceSeq = new JMenuItem();
139
140   JMenuItem chooseAnnotations = new JMenuItem();
141
142   SequenceI sequence;
143
144   JMenuItem createGroupMenuItem = new JMenuItem();
145
146   JMenuItem unGroupMenuItem = new JMenuItem();
147
148   JMenuItem outline = new JMenuItem();
149
150   JRadioButtonMenuItem nucleotideMenuItem = new JRadioButtonMenuItem();
151
152   JMenu colourMenu = new JMenu();
153
154   JCheckBoxMenuItem showBoxes = new JCheckBoxMenuItem();
155
156   JCheckBoxMenuItem showText = new JCheckBoxMenuItem();
157
158   JCheckBoxMenuItem showColourText = new JCheckBoxMenuItem();
159
160   JCheckBoxMenuItem displayNonconserved = new JCheckBoxMenuItem();
161
162   JMenu editMenu = new JMenu();
163
164   JMenuItem cut = new JMenuItem();
165
166   JMenuItem copy = new JMenuItem();
167
168   JMenuItem upperCase = new JMenuItem();
169
170   JMenuItem lowerCase = new JMenuItem();
171
172   JMenuItem toggle = new JMenuItem();
173
174   JMenu pdbMenu = new JMenu();
175
176   JMenu outputMenu = new JMenu();
177
178   JMenu seqShowAnnotationsMenu = new JMenu();
179
180   JMenu seqHideAnnotationsMenu = new JMenu();
181
182   JMenuItem seqAddReferenceAnnotations = new JMenuItem(
183           MessageManager.getString("label.add_reference_annotations"));
184
185   JMenu groupShowAnnotationsMenu = new JMenu();
186
187   JMenu groupHideAnnotationsMenu = new JMenu();
188
189   JMenuItem groupAddReferenceAnnotations = new JMenuItem(
190           MessageManager.getString("label.add_reference_annotations"));
191
192   JMenuItem sequenceFeature = new JMenuItem();
193
194   JMenuItem textColour = new JMenuItem();
195
196   JMenu jMenu1 = new JMenu();
197
198   JMenuItem pdbStructureDialog = new JMenuItem();
199
200   JMenu rnaStructureMenu = new JMenu();
201
202   JMenuItem editSequence = new JMenuItem();
203
204   JMenu groupLinksMenu;
205
206   JMenuItem hideInsertions = new JMenuItem();
207
208   /**
209    * Creates a new PopupMenu object.
210    * 
211    * @param ap
212    *          DOCUMENT ME!
213    * @param seq
214    *          DOCUMENT ME!
215    */
216   public PopupMenu(final AlignmentPanel ap, Sequence seq, List<String> links)
217   {
218     this(ap, seq, links, null);
219   }
220
221   /**
222    * 
223    * @param ap
224    * @param seq
225    * @param links
226    * @param groupLinks
227    */
228   public PopupMenu(final AlignmentPanel ap, final SequenceI seq,
229           List<String> links, List<String> groupLinks)
230   {
231     // /////////////////////////////////////////////////////////
232     // If this is activated from the sequence panel, the user may want to
233     // edit or annotate a particular residue. Therefore display the residue menu
234     //
235     // If from the IDPanel, we must display the sequence menu
236     // ////////////////////////////////////////////////////////
237     this.ap = ap;
238     sequence = seq;
239
240     ButtonGroup colours = new ButtonGroup();
241     colours.add(noColourmenuItem);
242     colours.add(clustalColour);
243     colours.add(zappoColour);
244     colours.add(taylorColour);
245     colours.add(hydrophobicityColour);
246     colours.add(helixColour);
247     colours.add(strandColour);
248     colours.add(turnColour);
249     colours.add(buriedColour);
250     colours.add(abovePIDColour);
251     colours.add(userDefinedColour);
252     colours.add(PIDColour);
253     colours.add(BLOSUM62Colour);
254     colours.add(purinePyrimidineColour);
255     colours.add(RNAInteractionColour);
256
257     for (int i = 0; i < jalview.io.FormatAdapter.WRITEABLE_FORMATS.length; i++)
258     {
259       JMenuItem item = new JMenuItem(
260               jalview.io.FormatAdapter.WRITEABLE_FORMATS[i]);
261
262       item.addActionListener(new java.awt.event.ActionListener()
263       {
264         @Override
265         public void actionPerformed(ActionEvent e)
266         {
267           outputText_actionPerformed(e);
268         }
269       });
270
271       outputMenu.add(item);
272     }
273
274     /*
275      * Build menus for annotation types that may be shown or hidden, and for
276      * 'reference annotations' that may be added to the alignment. First for the
277      * currently selected sequence (if there is one):
278      */
279     final List<SequenceI> selectedSequence = (seq == null ? Collections
280             .<SequenceI> emptyList() : Arrays.asList(seq));
281     buildAnnotationTypesMenus(seqShowAnnotationsMenu,
282             seqHideAnnotationsMenu, selectedSequence);
283     configureReferenceAnnotationsMenu(seqAddReferenceAnnotations,
284             selectedSequence);
285
286     /*
287      * And repeat for the current selection group (if there is one):
288      */
289     final List<SequenceI> selectedGroup = (ap.av.getSelectionGroup() == null ? Collections
290             .<SequenceI> emptyList() : ap.av.getSelectionGroup()
291             .getSequences());
292     buildAnnotationTypesMenus(groupShowAnnotationsMenu,
293             groupHideAnnotationsMenu, selectedGroup);
294     configureReferenceAnnotationsMenu(groupAddReferenceAnnotations,
295             selectedGroup);
296
297     try
298     {
299       jbInit();
300     } catch (Exception e)
301     {
302       e.printStackTrace();
303     }
304
305     JMenuItem menuItem;
306     if (seq != null)
307     {
308       sequenceMenu.setText(sequence.getName());
309       if (seq == ap.av.getAlignment().getSeqrep())
310       {
311         makeReferenceSeq.setText(MessageManager
312                 .getString("action.unmark_as_reference"));
313       }
314       else
315       {
316         makeReferenceSeq.setText(MessageManager
317                 .getString("action.set_as_reference"));
318       }
319
320       if (!ap.av.getAlignment().isNucleotide())
321       {
322         remove(rnaStructureMenu);
323       }
324       else
325       {
326         int origCount = rnaStructureMenu.getItemCount();
327         /*
328          * add menu items to 2D-render any alignment or sequence secondary
329          * structure annotation
330          */
331         AlignmentAnnotation[] aas = ap.av.getAlignment()
332                 .getAlignmentAnnotation();
333         if (aas != null)
334         {
335           for (final AlignmentAnnotation aa : aas)
336           {
337             if (aa.isValidStruc() && aa.sequenceRef == null)
338             {
339               /*
340                * valid alignment RNA secondary structure annotation
341                */
342               menuItem = new JMenuItem();
343               menuItem.setText(MessageManager.formatMessage(
344                       "label.2d_rna_structure_line",
345                       new Object[] { aa.label }));
346               menuItem.addActionListener(new java.awt.event.ActionListener()
347               {
348                 @Override
349                 public void actionPerformed(ActionEvent e)
350                 {
351                   new AppVarna(seq, aa, ap);
352                 }
353               });
354               rnaStructureMenu.add(menuItem);
355             }
356           }
357         }
358
359         if (seq.getAnnotation() != null)
360         {
361           AlignmentAnnotation seqAnns[] = seq.getAnnotation();
362           for (final AlignmentAnnotation aa : seqAnns)
363           {
364             if (aa.isValidStruc())
365             {
366               /*
367                * valid sequence RNA secondary structure annotation
368                */
369               // TODO: make rnastrucF a bit more nice
370               menuItem = new JMenuItem();
371               menuItem.setText(MessageManager.formatMessage(
372                       "label.2d_rna_sequence_name",
373                       new Object[] { seq.getName() }));
374               menuItem.addActionListener(new java.awt.event.ActionListener()
375               {
376                 @Override
377                 public void actionPerformed(ActionEvent e)
378                 {
379                   // TODO: VARNA does'nt print gaps in the sequence
380                   new AppVarna(seq, aa, ap);
381                 }
382               });
383               rnaStructureMenu.add(menuItem);
384             }
385           }
386         }
387         if (rnaStructureMenu.getItemCount() == origCount)
388         {
389           remove(rnaStructureMenu);
390         }
391       }
392
393       menuItem = new JMenuItem(
394               MessageManager.getString("action.hide_sequences"));
395       menuItem.addActionListener(new java.awt.event.ActionListener()
396       {
397         @Override
398         public void actionPerformed(ActionEvent e)
399         {
400           hideSequences(false);
401         }
402       });
403       add(menuItem);
404
405       if (ap.av.getSelectionGroup() != null
406               && ap.av.getSelectionGroup().getSize() > 1)
407       {
408         menuItem = new JMenuItem(MessageManager.formatMessage(
409                 "label.represent_group_with",
410                 new Object[] { seq.getName() }));
411         menuItem.addActionListener(new java.awt.event.ActionListener()
412         {
413           @Override
414           public void actionPerformed(ActionEvent e)
415           {
416             hideSequences(true);
417           }
418         });
419         sequenceMenu.add(menuItem);
420       }
421
422       if (ap.av.hasHiddenRows())
423       {
424         final int index = ap.av.getAlignment().findIndex(seq);
425
426         if (ap.av.adjustForHiddenSeqs(index)
427                 - ap.av.adjustForHiddenSeqs(index - 1) > 1)
428         {
429           menuItem = new JMenuItem(
430                   MessageManager.getString("action.reveal_sequences"));
431           menuItem.addActionListener(new ActionListener()
432           {
433             @Override
434             public void actionPerformed(ActionEvent e)
435             {
436               ap.av.showSequence(index);
437               if (ap.overviewPanel != null)
438               {
439                 ap.overviewPanel.updateOverviewImage();
440               }
441             }
442           });
443           add(menuItem);
444         }
445       }
446     }
447     // for the case when no sequences are even visible
448     if (ap.av.hasHiddenRows())
449     {
450       {
451         menuItem = new JMenuItem(
452                 MessageManager.getString("action.reveal_all"));
453         menuItem.addActionListener(new ActionListener()
454         {
455           @Override
456           public void actionPerformed(ActionEvent e)
457           {
458             ap.av.showAllHiddenSeqs();
459             if (ap.overviewPanel != null)
460             {
461               ap.overviewPanel.updateOverviewImage();
462             }
463           }
464         });
465
466         add(menuItem);
467       }
468
469     }
470
471     SequenceGroup sg = ap.av.getSelectionGroup();
472     boolean isDefinedGroup = (sg != null) ? ap.av.getAlignment()
473             .getGroups().contains(sg) : false;
474
475     if (sg != null && sg.getSize() > 0)
476     {
477       groupName.setText(MessageManager
478               .getString("label.edit_name_and_description_current_group"));
479
480       if (sg.cs instanceof ZappoColourScheme)
481       {
482         zappoColour.setSelected(true);
483       }
484       else if (sg.cs instanceof TaylorColourScheme)
485       {
486         taylorColour.setSelected(true);
487       }
488       else if (sg.cs instanceof PIDColourScheme)
489       {
490         PIDColour.setSelected(true);
491       }
492       else if (sg.cs instanceof Blosum62ColourScheme)
493       {
494         BLOSUM62Colour.setSelected(true);
495       }
496       else if (sg.cs instanceof UserColourScheme)
497       {
498         userDefinedColour.setSelected(true);
499       }
500       else if (sg.cs instanceof HydrophobicColourScheme)
501       {
502         hydrophobicityColour.setSelected(true);
503       }
504       else if (sg.cs instanceof HelixColourScheme)
505       {
506         helixColour.setSelected(true);
507       }
508       else if (sg.cs instanceof StrandColourScheme)
509       {
510         strandColour.setSelected(true);
511       }
512       else if (sg.cs instanceof TurnColourScheme)
513       {
514         turnColour.setSelected(true);
515       }
516       else if (sg.cs instanceof BuriedColourScheme)
517       {
518         buriedColour.setSelected(true);
519       }
520       else if (sg.cs instanceof ClustalxColourScheme)
521       {
522         clustalColour.setSelected(true);
523       }
524       else if (sg.cs instanceof PurinePyrimidineColourScheme)
525       {
526         purinePyrimidineColour.setSelected(true);
527       }
528
529       /*
530        * else if (sg.cs instanceof CovariationColourScheme) {
531        * covariationColour.setSelected(true); }
532        */
533       else
534       {
535         noColourmenuItem.setSelected(true);
536       }
537
538       if (sg.cs != null && sg.cs.conservationApplied())
539       {
540         conservationMenuItem.setSelected(true);
541       }
542       displayNonconserved.setSelected(sg.getShowNonconserved());
543       showText.setSelected(sg.getDisplayText());
544       showColourText.setSelected(sg.getColourText());
545       showBoxes.setSelected(sg.getDisplayBoxes());
546       // add any groupURLs to the groupURL submenu and make it visible
547       if (groupLinks != null && groupLinks.size() > 0)
548       {
549         buildGroupURLMenu(sg, groupLinks);
550       }
551       // Add a 'show all structures' for the current selection
552       Hashtable<String, PDBEntry> pdbe = new Hashtable<String, PDBEntry>(), reppdb = new Hashtable<String, PDBEntry>();
553       SequenceI sqass = null;
554       for (SequenceI sq : ap.av.getSequenceSelection())
555       {
556         Vector<PDBEntry> pes = sq.getDatasetSequence().getAllPDBEntries();
557         if (pes != null && pes.size() > 0)
558         {
559           reppdb.put(pes.get(0).getId(), pes.get(0));
560           for (PDBEntry pe : pes)
561           {
562             pdbe.put(pe.getId(), pe);
563             if (sqass == null)
564             {
565               sqass = sq;
566             }
567           }
568         }
569       }
570       if (pdbe.size() > 0)
571       {
572         final PDBEntry[] pe = pdbe.values().toArray(
573                 new PDBEntry[pdbe.size()]), pr = reppdb.values().toArray(
574                 new PDBEntry[reppdb.size()]);
575         final JMenuItem gpdbview, rpdbview;
576       }
577     }
578     else
579     {
580       groupMenu.setVisible(false);
581       editMenu.setVisible(false);
582     }
583
584     if (!isDefinedGroup)
585     {
586       createGroupMenuItem.setVisible(true);
587       unGroupMenuItem.setVisible(false);
588       jMenu1.setText(MessageManager.getString("action.edit_new_group"));
589     }
590     else
591     {
592       createGroupMenuItem.setVisible(false);
593       unGroupMenuItem.setVisible(true);
594       jMenu1.setText(MessageManager.getString("action.edit_group"));
595     }
596
597     if (seq == null)
598     {
599       sequenceMenu.setVisible(false);
600       pdbStructureDialog.setVisible(false);
601       rnaStructureMenu.setVisible(false);
602     }
603
604     if (links != null && links.size() > 0)
605     {
606       addFeatureLinks(seq, links);
607     }
608   }
609
610   /**
611    * Adds a 'Link' menu item with a sub-menu item for each hyperlink provided.
612    * 
613    * @param seq
614    * @param links
615    */
616   void addFeatureLinks(final SequenceI seq, List<String> links)
617   {
618     JMenu linkMenu = new JMenu(MessageManager.getString("action.link"));
619     Map<String, List<String>> linkset = new LinkedHashMap<String, List<String>>();
620
621     for (String link : links)
622     {
623       UrlLink urlLink = null;
624       try
625       {
626         urlLink = new UrlLink(link);
627       } catch (Exception foo)
628       {
629         Cache.log.error("Exception for URLLink '" + link + "'", foo);
630         continue;
631       }
632
633       if (!urlLink.isValid())
634       {
635         Cache.log.error(urlLink.getInvalidMessage());
636         continue;
637       }
638
639       urlLink.createLinksFromSeq(seq, linkset);
640     }
641
642     addshowLinks(linkMenu, linkset.values());
643
644     // disable link menu if there are no valid entries
645     if (linkMenu.getItemCount() > 0)
646     {
647       linkMenu.setEnabled(true);
648     }
649     else
650     {
651       linkMenu.setEnabled(false);
652     }
653
654     if (sequence != null)
655     {
656       sequenceMenu.add(linkMenu);
657     }
658     else
659     {
660       add(linkMenu);
661     }
662
663   }
664
665
666
667   /**
668    * Add annotation types to 'Show annotations' and/or 'Hide annotations' menus.
669    * "All" is added first, followed by a separator. Then add any annotation
670    * types associated with the current selection. Separate menus are built for
671    * the selected sequence group (if any), and the selected sequence.
672    * <p>
673    * Some annotation rows are always rendered together - these can be identified
674    * by a common graphGroup property > -1. Only one of each group will be marked
675    * as visible (to avoid duplication of the display). For such groups we add a
676    * composite type name, e.g.
677    * <p>
678    * IUPredWS (Long), IUPredWS (Short)
679    * 
680    * @param seq
681    */
682   protected void buildAnnotationTypesMenus(JMenu showMenu, JMenu hideMenu,
683           List<SequenceI> forSequences)
684   {
685     showMenu.removeAll();
686     hideMenu.removeAll();
687
688     final List<String> all = Arrays.asList(new String[] { MessageManager
689             .getString("label.all") });
690     addAnnotationTypeToShowHide(showMenu, forSequences, "", all, true, true);
691     addAnnotationTypeToShowHide(hideMenu, forSequences, "", all, true,
692             false);
693     showMenu.addSeparator();
694     hideMenu.addSeparator();
695
696     final AlignmentAnnotation[] annotations = ap.getAlignment()
697             .getAlignmentAnnotation();
698
699     /*
700      * Find shown/hidden annotations types, distinguished by source (calcId),
701      * and grouped by graphGroup. Using LinkedHashMap means we will retrieve in
702      * the insertion order, which is the order of the annotations on the
703      * alignment.
704      */
705     Map<String, List<List<String>>> shownTypes = new LinkedHashMap<String, List<List<String>>>();
706     Map<String, List<List<String>>> hiddenTypes = new LinkedHashMap<String, List<List<String>>>();
707     AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes, hiddenTypes,
708             AlignmentAnnotationUtils.asList(annotations), forSequences);
709
710     for (String calcId : hiddenTypes.keySet())
711     {
712       for (List<String> type : hiddenTypes.get(calcId))
713       {
714         addAnnotationTypeToShowHide(showMenu, forSequences, calcId, type,
715                 false, true);
716       }
717     }
718     // grey out 'show annotations' if none are hidden
719     showMenu.setEnabled(!hiddenTypes.isEmpty());
720
721     for (String calcId : shownTypes.keySet())
722     {
723       for (List<String> type : shownTypes.get(calcId))
724       {
725         addAnnotationTypeToShowHide(hideMenu, forSequences, calcId, type,
726                 false, false);
727       }
728     }
729     // grey out 'hide annotations' if none are shown
730     hideMenu.setEnabled(!shownTypes.isEmpty());
731   }
732
733   /**
734    * Returns a list of sequences - either the current selection group (if there
735    * is one), else the specified single sequence.
736    * 
737    * @param seq
738    * @return
739    */
740   protected List<SequenceI> getSequenceScope(SequenceI seq)
741   {
742     List<SequenceI> forSequences = null;
743     final SequenceGroup selectionGroup = ap.av.getSelectionGroup();
744     if (selectionGroup != null && selectionGroup.getSize() > 0)
745     {
746       forSequences = selectionGroup.getSequences();
747     }
748     else
749     {
750       forSequences = seq == null ? Collections.<SequenceI> emptyList()
751               : Arrays.asList(seq);
752     }
753     return forSequences;
754   }
755
756   /**
757    * Add one annotation type to the 'Show Annotations' or 'Hide Annotations'
758    * menus.
759    * 
760    * @param showOrHideMenu
761    *          the menu to add to
762    * @param forSequences
763    *          the sequences whose annotations may be shown or hidden
764    * @param calcId
765    * @param types
766    *          the label to add
767    * @param allTypes
768    *          if true this is a special label meaning 'All'
769    * @param actionIsShow
770    *          if true, the select menu item action is to show the annotation
771    *          type, else hide
772    */
773   protected void addAnnotationTypeToShowHide(JMenu showOrHideMenu,
774           final List<SequenceI> forSequences, String calcId,
775           final List<String> types, final boolean allTypes,
776           final boolean actionIsShow)
777   {
778     String label = types.toString(); // [a, b, c]
779     label = label.substring(1, label.length() - 1); // a, b, c
780     final JMenuItem item = new JMenuItem(label);
781     item.setToolTipText(calcId);
782     item.addActionListener(new java.awt.event.ActionListener()
783     {
784       @Override
785       public void actionPerformed(ActionEvent e)
786       {
787         AlignmentUtils.showOrHideSequenceAnnotations(ap.getAlignment(),
788                 types, forSequences, allTypes, actionIsShow);
789         refresh();
790       }
791     });
792     showOrHideMenu.add(item);
793   }
794
795   private void buildGroupURLMenu(SequenceGroup sg, List<String> groupLinks)
796   {
797
798     // TODO: usability: thread off the generation of group url content so root
799     // menu appears asap
800     // sequence only URLs
801     // ID/regex match URLs
802     groupLinksMenu = new JMenu(
803             MessageManager.getString("action.group_link"));
804     // three types of url that might be created.
805     JMenu[] linkMenus = new JMenu[] { null,
806         new JMenu(MessageManager.getString("action.ids")),
807         new JMenu(MessageManager.getString("action.sequences")),
808         new JMenu(MessageManager.getString("action.ids_sequences")) };
809
810     SequenceI[] seqs = ap.av.getSelectionAsNewSequence();
811     String[][] idandseqs = GroupUrlLink.formStrings(seqs);
812     Hashtable<String, Object[]> commonDbrefs = new Hashtable<String, Object[]>();
813     for (int sq = 0; sq < seqs.length; sq++)
814     {
815
816       int start = seqs[sq].findPosition(sg.getStartRes()), end = seqs[sq]
817               .findPosition(sg.getEndRes());
818       // just collect ids from dataset sequence
819       // TODO: check if IDs collected from selecton group intersects with the
820       // current selection, too
821       SequenceI sqi = seqs[sq];
822       while (sqi.getDatasetSequence() != null)
823       {
824         sqi = sqi.getDatasetSequence();
825       }
826       DBRefEntry[] dbr = sqi.getDBRefs();
827       if (dbr != null && dbr.length > 0)
828       {
829         for (int d = 0; d < dbr.length; d++)
830         {
831           String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
832           Object[] sarray = commonDbrefs.get(src);
833           if (sarray == null)
834           {
835             sarray = new Object[2];
836             sarray[0] = new int[] { 0 };
837             sarray[1] = new String[seqs.length];
838
839             commonDbrefs.put(src, sarray);
840           }
841
842           if (((String[]) sarray[1])[sq] == null)
843           {
844             if (!dbr[d].hasMap()
845                     || (dbr[d].getMap().locateMappedRange(start, end) != null))
846             {
847               ((String[]) sarray[1])[sq] = dbr[d].getAccessionId();
848               ((int[]) sarray[0])[0]++;
849             }
850           }
851         }
852       }
853     }
854     // now create group links for all distinct ID/sequence sets.
855     boolean addMenu = false; // indicates if there are any group links to give
856                              // to user
857     for (String link : groupLinks)
858     {
859       GroupUrlLink urlLink = null;
860       try
861       {
862         urlLink = new GroupUrlLink(link);
863       } catch (Exception foo)
864       {
865         Cache.log.error("Exception for GroupURLLink '" + link + "'", foo);
866         continue;
867       }
868       ;
869       if (!urlLink.isValid())
870       {
871         Cache.log.error(urlLink.getInvalidMessage());
872         continue;
873       }
874       final String label = urlLink.getLabel();
875       boolean usingNames = false;
876       // Now see which parts of the group apply for this URL
877       String ltarget = urlLink.getTarget(); // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
878       Object[] idset = commonDbrefs.get(ltarget.toUpperCase());
879       String[] seqstr, ids; // input to makeUrl
880       if (idset != null)
881       {
882         int numinput = ((int[]) idset[0])[0];
883         String[] allids = ((String[]) idset[1]);
884         seqstr = new String[numinput];
885         ids = new String[numinput];
886         for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
887         {
888           if (allids[sq] != null)
889           {
890             ids[idcount] = allids[sq];
891             seqstr[idcount++] = idandseqs[1][sq];
892           }
893         }
894       }
895       else
896       {
897         // just use the id/seq set
898         seqstr = idandseqs[1];
899         ids = idandseqs[0];
900         usingNames = true;
901       }
902       // and try and make the groupURL!
903
904       Object[] urlset = null;
905       try
906       {
907         urlset = urlLink.makeUrlStubs(ids, seqstr,
908                 "FromJalview" + System.currentTimeMillis(), false);
909       } catch (UrlStringTooLongException e)
910       {
911       }
912       if (urlset != null)
913       {
914         int type = urlLink.getGroupURLType() & 3;
915         // first two bits ofurlLink type bitfield are sequenceids and sequences
916         // TODO: FUTURE: ensure the groupURL menu structure can be generalised
917         addshowLink(linkMenus[type], label
918                 + (((type & 1) == 1) ? ("("
919                         + (usingNames ? "Names" : ltarget) + ")") : ""),
920                 urlLink, urlset);
921         addMenu = true;
922       }
923     }
924     if (addMenu)
925     {
926       groupLinksMenu = new JMenu(
927               MessageManager.getString("action.group_link"));
928       for (int m = 0; m < linkMenus.length; m++)
929       {
930         if (linkMenus[m] != null
931                 && linkMenus[m].getMenuComponentCount() > 0)
932         {
933           groupLinksMenu.add(linkMenus[m]);
934         }
935       }
936
937       groupMenu.add(groupLinksMenu);
938     }
939   }
940
941   private void addshowLinks(JMenu linkMenu, Collection<List<String>> linkset)
942   {
943     for (List<String> linkstrset : linkset)
944     {
945       // split linkstr into label and url
946       addshowLink(linkMenu, linkstrset.get(1), linkstrset.get(3));
947     }
948   }
949
950   /**
951    * add a show URL menu item to the given linkMenu
952    * 
953    * @param linkMenu
954    * @param label
955    *          - menu label string
956    * @param url
957    *          - url to open
958    */
959   private void addshowLink(JMenu linkMenu, String label, final String url)
960   {
961     JMenuItem item = new JMenuItem(label);
962     item.setToolTipText(MessageManager.formatMessage(
963             "label.open_url_param", new Object[] { url }));
964     item.addActionListener(new java.awt.event.ActionListener()
965     {
966       @Override
967       public void actionPerformed(ActionEvent e)
968       {
969         new Thread(new Runnable()
970         {
971
972           @Override
973           public void run()
974           {
975             showLink(url);
976           }
977
978         }).start();
979       }
980     });
981
982     linkMenu.add(item);
983   }
984
985   /**
986    * add a late bound groupURL item to the given linkMenu
987    * 
988    * @param linkMenu
989    * @param label
990    *          - menu label string
991    * @param urlgenerator
992    *          GroupURLLink used to generate URL
993    * @param urlstub
994    *          Object array returned from the makeUrlStubs function.
995    */
996   private void addshowLink(JMenu linkMenu, String label,
997           final GroupUrlLink urlgenerator, final Object[] urlstub)
998   {
999     JMenuItem item = new JMenuItem(label);
1000     item.setToolTipText(MessageManager.formatMessage(
1001             "label.open_url_seqs_param",
1002             new Object[] { urlgenerator.getUrl_prefix(),
1003                 urlgenerator.getNumberInvolved(urlstub) }));
1004     // TODO: put in info about what is being sent.
1005     item.addActionListener(new ActionListener()
1006     {
1007       @Override
1008       public void actionPerformed(ActionEvent e)
1009       {
1010         new Thread(new Runnable()
1011         {
1012
1013           @Override
1014           public void run()
1015           {
1016             try
1017             {
1018               showLink(urlgenerator.constructFrom(urlstub));
1019             } catch (UrlStringTooLongException e2)
1020             {
1021             }
1022           }
1023
1024         }).start();
1025       }
1026     });
1027
1028     linkMenu.add(item);
1029   }
1030
1031   /**
1032    * DOCUMENT ME!
1033    * 
1034    * @throws Exception
1035    *           DOCUMENT ME!
1036    */
1037   private void jbInit() throws Exception
1038   {
1039     groupMenu.setText(MessageManager.getString("label.selection"));
1040     groupName.setText(MessageManager.getString("label.name"));
1041     groupName.addActionListener(new java.awt.event.ActionListener()
1042     {
1043       @Override
1044       public void actionPerformed(ActionEvent e)
1045       {
1046         groupName_actionPerformed();
1047       }
1048     });
1049     sequenceMenu.setText(MessageManager.getString("label.sequence"));
1050     sequenceName.setText(MessageManager
1051             .getString("label.edit_name_description"));
1052     sequenceName.addActionListener(new java.awt.event.ActionListener()
1053     {
1054       @Override
1055       public void actionPerformed(ActionEvent e)
1056       {
1057         sequenceName_actionPerformed();
1058       }
1059     });
1060     chooseAnnotations.setText(MessageManager
1061             .getString("action.choose_annotations"));
1062     chooseAnnotations.addActionListener(new java.awt.event.ActionListener()
1063     {
1064       @Override
1065       public void actionPerformed(ActionEvent e)
1066       {
1067         chooseAnnotations_actionPerformed(e);
1068       }
1069     });
1070     sequenceDetails.setText(MessageManager
1071             .getString("label.sequence_details"));
1072     sequenceDetails.addActionListener(new java.awt.event.ActionListener()
1073     {
1074       @Override
1075       public void actionPerformed(ActionEvent e)
1076       {
1077         sequenceDetails_actionPerformed();
1078       }
1079     });
1080     sequenceSelDetails.setText(MessageManager
1081             .getString("label.sequence_details"));
1082     sequenceSelDetails
1083             .addActionListener(new java.awt.event.ActionListener()
1084             {
1085               @Override
1086               public void actionPerformed(ActionEvent e)
1087               {
1088                 sequenceSelectionDetails_actionPerformed();
1089               }
1090             });
1091     PIDColour.setFocusPainted(false);
1092     unGroupMenuItem
1093             .setText(MessageManager.getString("action.remove_group"));
1094     unGroupMenuItem.addActionListener(new java.awt.event.ActionListener()
1095     {
1096       @Override
1097       public void actionPerformed(ActionEvent e)
1098       {
1099         unGroupMenuItem_actionPerformed();
1100       }
1101     });
1102     createGroupMenuItem.setText(MessageManager
1103             .getString("action.create_group"));
1104     createGroupMenuItem
1105             .addActionListener(new java.awt.event.ActionListener()
1106             {
1107               @Override
1108               public void actionPerformed(ActionEvent e)
1109               {
1110                 createGroupMenuItem_actionPerformed();
1111               }
1112             });
1113
1114     outline.setText(MessageManager.getString("action.border_colour"));
1115     outline.addActionListener(new java.awt.event.ActionListener()
1116     {
1117       @Override
1118       public void actionPerformed(ActionEvent e)
1119       {
1120         outline_actionPerformed();
1121       }
1122     });
1123     nucleotideMenuItem
1124             .setText(MessageManager.getString("label.nucleotide"));
1125     nucleotideMenuItem.addActionListener(new ActionListener()
1126     {
1127       @Override
1128       public void actionPerformed(ActionEvent e)
1129       {
1130         nucleotideMenuItem_actionPerformed();
1131       }
1132     });
1133     colourMenu.setText(MessageManager.getString("label.group_colour"));
1134     showBoxes.setText(MessageManager.getString("action.boxes"));
1135     showBoxes.setState(true);
1136     showBoxes.addActionListener(new ActionListener()
1137     {
1138       @Override
1139       public void actionPerformed(ActionEvent e)
1140       {
1141         showBoxes_actionPerformed();
1142       }
1143     });
1144     showText.setText(MessageManager.getString("action.text"));
1145     showText.setState(true);
1146     showText.addActionListener(new ActionListener()
1147     {
1148       @Override
1149       public void actionPerformed(ActionEvent e)
1150       {
1151         showText_actionPerformed();
1152       }
1153     });
1154     showColourText.setText(MessageManager.getString("label.colour_text"));
1155     showColourText.addActionListener(new ActionListener()
1156     {
1157       @Override
1158       public void actionPerformed(ActionEvent e)
1159       {
1160         showColourText_actionPerformed();
1161       }
1162     });
1163     displayNonconserved.setText(MessageManager
1164             .getString("label.show_non_conversed"));
1165     displayNonconserved.setState(true);
1166     displayNonconserved.addActionListener(new ActionListener()
1167     {
1168       @Override
1169       public void actionPerformed(ActionEvent e)
1170       {
1171         showNonconserved_actionPerformed();
1172       }
1173     });
1174     editMenu.setText(MessageManager.getString("action.edit"));
1175     cut.setText(MessageManager.getString("action.cut"));
1176     cut.addActionListener(new ActionListener()
1177     {
1178       @Override
1179       public void actionPerformed(ActionEvent e)
1180       {
1181         cut_actionPerformed();
1182       }
1183     });
1184     upperCase.setText(MessageManager.getString("label.to_upper_case"));
1185     upperCase.addActionListener(new ActionListener()
1186     {
1187       @Override
1188       public void actionPerformed(ActionEvent e)
1189       {
1190         changeCase(e);
1191       }
1192     });
1193     copy.setText(MessageManager.getString("action.copy"));
1194     copy.addActionListener(new ActionListener()
1195     {
1196       @Override
1197       public void actionPerformed(ActionEvent e)
1198       {
1199         copy_actionPerformed();
1200       }
1201     });
1202     lowerCase.setText(MessageManager.getString("label.to_lower_case"));
1203     lowerCase.addActionListener(new ActionListener()
1204     {
1205       @Override
1206       public void actionPerformed(ActionEvent e)
1207       {
1208         changeCase(e);
1209       }
1210     });
1211     toggle.setText(MessageManager.getString("label.toggle_case"));
1212     toggle.addActionListener(new ActionListener()
1213     {
1214       @Override
1215       public void actionPerformed(ActionEvent e)
1216       {
1217         changeCase(e);
1218       }
1219     });
1220     outputMenu.setText(MessageManager.getString("label.out_to_textbox")
1221             + "...");
1222     seqShowAnnotationsMenu.setText(MessageManager
1223             .getString("label.show_annotations"));
1224     seqHideAnnotationsMenu.setText(MessageManager
1225             .getString("label.hide_annotations"));
1226     groupShowAnnotationsMenu.setText(MessageManager
1227             .getString("label.show_annotations"));
1228     groupHideAnnotationsMenu.setText(MessageManager
1229             .getString("label.hide_annotations"));
1230     sequenceFeature.setText(MessageManager
1231             .getString("label.create_sequence_feature"));
1232     sequenceFeature.addActionListener(new ActionListener()
1233     {
1234       @Override
1235       public void actionPerformed(ActionEvent e)
1236       {
1237         sequenceFeature_actionPerformed();
1238       }
1239     });
1240     textColour.setText(MessageManager.getString("label.text_colour"));
1241     textColour.addActionListener(new ActionListener()
1242     {
1243       @Override
1244       public void actionPerformed(ActionEvent e)
1245       {
1246         textColour_actionPerformed();
1247       }
1248     });
1249     jMenu1.setText(MessageManager.getString("label.group"));
1250     pdbStructureDialog.setText(MessageManager
1251             .getString("label.show_pdbstruct_dialog"));
1252     pdbStructureDialog.addActionListener(new ActionListener()
1253     {
1254       @Override
1255       public void actionPerformed(ActionEvent actionEvent)
1256       {
1257         SequenceI[] selectedSeqs = new SequenceI[] { sequence };
1258         if (ap.av.getSelectionGroup() != null)
1259         {
1260           selectedSeqs = ap.av.getSequenceSelection();
1261         }
1262         new StructureChooser(selectedSeqs, sequence, ap);
1263       }
1264     });
1265
1266     rnaStructureMenu.setText(MessageManager
1267             .getString("label.view_rna_structure"));
1268
1269     // colStructureMenu.setText("Colour By Structure");
1270     editSequence.setText(MessageManager.getString("label.edit_sequence")
1271             + "...");
1272     editSequence.addActionListener(new ActionListener()
1273     {
1274       @Override
1275       public void actionPerformed(ActionEvent actionEvent)
1276       {
1277         editSequence_actionPerformed(actionEvent);
1278       }
1279     });
1280     makeReferenceSeq.setText(MessageManager
1281             .getString("label.mark_as_representative"));
1282     makeReferenceSeq.addActionListener(new ActionListener()
1283     {
1284
1285       @Override
1286       public void actionPerformed(ActionEvent actionEvent)
1287       {
1288         makeReferenceSeq_actionPerformed(actionEvent);
1289
1290       }
1291     });
1292     hideInsertions.setText(MessageManager
1293             .getString("label.hide_insertions"));
1294     hideInsertions.addActionListener(new ActionListener()
1295     {
1296
1297       @Override
1298       public void actionPerformed(ActionEvent e)
1299       {
1300         hideInsertions_actionPerformed(e);
1301       }
1302     });
1303     /*
1304      * annotationMenuItem.setText("By Annotation");
1305      * annotationMenuItem.addActionListener(new ActionListener() { public void
1306      * actionPerformed(ActionEvent actionEvent) {
1307      * annotationMenuItem_actionPerformed(actionEvent); } });
1308      */
1309     groupMenu.add(sequenceSelDetails);
1310     add(groupMenu);
1311     add(sequenceMenu);
1312     add(rnaStructureMenu);
1313     add(pdbStructureDialog);
1314     if (sequence != null)
1315     {
1316       add(hideInsertions);
1317     }
1318     // annotations configuration panel suppressed for now
1319     // groupMenu.add(chooseAnnotations);
1320
1321     /*
1322      * Add show/hide annotations to the Sequence menu, and to the Selection menu
1323      * (if a selection group is in force).
1324      */
1325     sequenceMenu.add(seqShowAnnotationsMenu);
1326     sequenceMenu.add(seqHideAnnotationsMenu);
1327     sequenceMenu.add(seqAddReferenceAnnotations);
1328     groupMenu.add(groupShowAnnotationsMenu);
1329     groupMenu.add(groupHideAnnotationsMenu);
1330     groupMenu.add(groupAddReferenceAnnotations);
1331     groupMenu.add(editMenu);
1332     groupMenu.add(outputMenu);
1333     groupMenu.add(sequenceFeature);
1334     groupMenu.add(createGroupMenuItem);
1335     groupMenu.add(unGroupMenuItem);
1336     groupMenu.add(jMenu1);
1337     sequenceMenu.add(sequenceName);
1338     sequenceMenu.add(sequenceDetails);
1339     sequenceMenu.add(makeReferenceSeq);
1340     colourMenu.add(textColour);
1341     colourMenu.add(noColourmenuItem);
1342     colourMenu.add(clustalColour);
1343     colourMenu.add(BLOSUM62Colour);
1344     colourMenu.add(PIDColour);
1345     colourMenu.add(zappoColour);
1346     colourMenu.add(taylorColour);
1347     colourMenu.add(hydrophobicityColour);
1348     colourMenu.add(helixColour);
1349     colourMenu.add(strandColour);
1350     colourMenu.add(turnColour);
1351     colourMenu.add(buriedColour);
1352     colourMenu.add(nucleotideMenuItem);
1353     if (ap.getAlignment().isNucleotide())
1354     {
1355       // JBPNote - commented since the colourscheme isn't functional
1356       colourMenu.add(purinePyrimidineColour);
1357     }
1358     colourMenu.add(userDefinedColour);
1359
1360     if (jalview.gui.UserDefinedColours.getUserColourSchemes() != null)
1361     {
1362       java.util.Enumeration userColours = jalview.gui.UserDefinedColours
1363               .getUserColourSchemes().keys();
1364
1365       while (userColours.hasMoreElements())
1366       {
1367         JMenuItem item = new JMenuItem(userColours.nextElement().toString());
1368         item.addActionListener(new ActionListener()
1369         {
1370           @Override
1371           public void actionPerformed(ActionEvent evt)
1372           {
1373             userDefinedColour_actionPerformed(evt);
1374           }
1375         });
1376         colourMenu.add(item);
1377       }
1378     }
1379
1380     colourMenu.addSeparator();
1381     colourMenu.add(abovePIDColour);
1382     colourMenu.add(conservationMenuItem);
1383     editMenu.add(copy);
1384     editMenu.add(cut);
1385     editMenu.add(editSequence);
1386     editMenu.add(upperCase);
1387     editMenu.add(lowerCase);
1388     editMenu.add(toggle);
1389     // JBPNote: These shouldn't be added here - should appear in a generic
1390     // 'apply web service to this sequence menu'
1391     // pdbMenu.add(RNAFold);
1392     // pdbMenu.add(ContraFold);
1393     jMenu1.add(groupName);
1394     jMenu1.add(colourMenu);
1395     jMenu1.add(showBoxes);
1396     jMenu1.add(showText);
1397     jMenu1.add(showColourText);
1398     jMenu1.add(outline);
1399     jMenu1.add(displayNonconserved);
1400     noColourmenuItem.setText(MessageManager.getString("label.none"));
1401     noColourmenuItem.addActionListener(new java.awt.event.ActionListener()
1402     {
1403       @Override
1404       public void actionPerformed(ActionEvent e)
1405       {
1406         noColourmenuItem_actionPerformed();
1407       }
1408     });
1409
1410     clustalColour.setText(MessageManager
1411             .getString("label.clustalx_colours"));
1412     clustalColour.addActionListener(new java.awt.event.ActionListener()
1413     {
1414       @Override
1415       public void actionPerformed(ActionEvent e)
1416       {
1417         clustalColour_actionPerformed();
1418       }
1419     });
1420     zappoColour.setText(MessageManager.getString("label.zappo"));
1421     zappoColour.addActionListener(new java.awt.event.ActionListener()
1422     {
1423       @Override
1424       public void actionPerformed(ActionEvent e)
1425       {
1426         zappoColour_actionPerformed();
1427       }
1428     });
1429     taylorColour.setText(MessageManager.getString("label.taylor"));
1430     taylorColour.addActionListener(new java.awt.event.ActionListener()
1431     {
1432       @Override
1433       public void actionPerformed(ActionEvent e)
1434       {
1435         taylorColour_actionPerformed();
1436       }
1437     });
1438     hydrophobicityColour.setText(MessageManager
1439             .getString("label.hydrophobicity"));
1440     hydrophobicityColour
1441             .addActionListener(new java.awt.event.ActionListener()
1442             {
1443               @Override
1444               public void actionPerformed(ActionEvent e)
1445               {
1446                 hydrophobicityColour_actionPerformed();
1447               }
1448             });
1449     helixColour.setText(MessageManager.getString("label.helix_propensity"));
1450     helixColour.addActionListener(new java.awt.event.ActionListener()
1451     {
1452       @Override
1453       public void actionPerformed(ActionEvent e)
1454       {
1455         helixColour_actionPerformed();
1456       }
1457     });
1458     strandColour.setText(MessageManager
1459             .getString("label.strand_propensity"));
1460     strandColour.addActionListener(new java.awt.event.ActionListener()
1461     {
1462       @Override
1463       public void actionPerformed(ActionEvent e)
1464       {
1465         strandColour_actionPerformed();
1466       }
1467     });
1468     turnColour.setText(MessageManager.getString("label.turn_propensity"));
1469     turnColour.addActionListener(new java.awt.event.ActionListener()
1470     {
1471       @Override
1472       public void actionPerformed(ActionEvent e)
1473       {
1474         turnColour_actionPerformed();
1475       }
1476     });
1477     buriedColour.setText(MessageManager.getString("label.buried_index"));
1478     buriedColour.addActionListener(new java.awt.event.ActionListener()
1479     {
1480       @Override
1481       public void actionPerformed(ActionEvent e)
1482       {
1483         buriedColour_actionPerformed();
1484       }
1485     });
1486     abovePIDColour.setText(MessageManager
1487             .getString("label.above_identity_percentage"));
1488     abovePIDColour.addActionListener(new java.awt.event.ActionListener()
1489     {
1490       @Override
1491       public void actionPerformed(ActionEvent e)
1492       {
1493         abovePIDColour_actionPerformed();
1494       }
1495     });
1496     userDefinedColour.setText(MessageManager
1497             .getString("action.user_defined"));
1498     userDefinedColour.addActionListener(new java.awt.event.ActionListener()
1499     {
1500       @Override
1501       public void actionPerformed(ActionEvent e)
1502       {
1503         userDefinedColour_actionPerformed(e);
1504       }
1505     });
1506     PIDColour
1507             .setText(MessageManager.getString("label.percentage_identity"));
1508     PIDColour.addActionListener(new java.awt.event.ActionListener()
1509     {
1510       @Override
1511       public void actionPerformed(ActionEvent e)
1512       {
1513         PIDColour_actionPerformed();
1514       }
1515     });
1516     BLOSUM62Colour.setText(MessageManager.getString("label.blosum62"));
1517     BLOSUM62Colour.addActionListener(new java.awt.event.ActionListener()
1518     {
1519       @Override
1520       public void actionPerformed(ActionEvent e)
1521       {
1522         BLOSUM62Colour_actionPerformed();
1523       }
1524     });
1525     purinePyrimidineColour.setText(MessageManager
1526             .getString("label.purine_pyrimidine"));
1527     purinePyrimidineColour
1528             .addActionListener(new java.awt.event.ActionListener()
1529             {
1530               @Override
1531               public void actionPerformed(ActionEvent e)
1532               {
1533                 purinePyrimidineColour_actionPerformed();
1534               }
1535             });
1536
1537     /*
1538      * covariationColour.addActionListener(new java.awt.event.ActionListener() {
1539      * public void actionPerformed(ActionEvent e) {
1540      * covariationColour_actionPerformed(); } });
1541      */
1542
1543     conservationMenuItem.setText(MessageManager
1544             .getString("label.conservation"));
1545     conservationMenuItem
1546             .addActionListener(new java.awt.event.ActionListener()
1547             {
1548               @Override
1549               public void actionPerformed(ActionEvent e)
1550               {
1551                 conservationMenuItem_actionPerformed();
1552               }
1553             });
1554   }
1555
1556   /**
1557    * Check for any annotations on the underlying dataset sequences (for the
1558    * current selection group) which are not 'on the alignment'.If any are found,
1559    * enable the option to add them to the alignment. The criteria for 'on the
1560    * alignment' is finding an alignment annotation on the alignment, matched on
1561    * calcId, label and sequenceRef.
1562    * 
1563    * A tooltip is also constructed that displays the source (calcId) and type
1564    * (label) of the annotations that can be added.
1565    * 
1566    * @param menuItem
1567    * @param forSequences
1568    */
1569   protected void configureReferenceAnnotationsMenu(JMenuItem menuItem,
1570           List<SequenceI> forSequences)
1571   {
1572     menuItem.setEnabled(false);
1573
1574     /*
1575      * Temporary store to hold distinct calcId / type pairs for the tooltip.
1576      * Using TreeMap means calcIds are shown in alphabetical order.
1577      */
1578     Map<String, String> tipEntries = new TreeMap<String, String>();
1579     final Map<SequenceI, List<AlignmentAnnotation>> candidates = new LinkedHashMap<SequenceI, List<AlignmentAnnotation>>();
1580     AlignmentI al = this.ap.av.getAlignment();
1581     AlignmentUtils.findAddableReferenceAnnotations(forSequences,
1582             tipEntries, candidates, al);
1583     if (!candidates.isEmpty())
1584     {
1585       StringBuilder tooltip = new StringBuilder(64);
1586       tooltip.append(MessageManager.getString("label.add_annotations_for"));
1587
1588       /*
1589        * Found annotations that could be added. Enable the menu item, and
1590        * configure its tooltip and action.
1591        */
1592       menuItem.setEnabled(true);
1593       for (String calcId : tipEntries.keySet())
1594       {
1595         tooltip.append("<br/>" + calcId + "/" + tipEntries.get(calcId));
1596       }
1597       String tooltipText = JvSwingUtils.wrapTooltip(true,
1598               tooltip.toString());
1599       menuItem.setToolTipText(tooltipText);
1600
1601       menuItem.addActionListener(new ActionListener()
1602       {
1603         @Override
1604         public void actionPerformed(ActionEvent e)
1605         {
1606           addReferenceAnnotations_actionPerformed(candidates);
1607         }
1608       });
1609     }
1610   }
1611
1612   /**
1613    * Add annotations to the sequences and to the alignment.
1614    * 
1615    * @param candidates
1616    *          a map whose keys are sequences on the alignment, and values a list
1617    *          of annotations to add to each sequence
1618    */
1619   protected void addReferenceAnnotations_actionPerformed(
1620           Map<SequenceI, List<AlignmentAnnotation>> candidates)
1621   {
1622     final SequenceGroup selectionGroup = this.ap.av.getSelectionGroup();
1623     final AlignmentI alignment = this.ap.getAlignment();
1624     AlignmentUtils.addReferenceAnnotations(candidates, alignment,
1625             selectionGroup);
1626     refresh();
1627   }
1628
1629   protected void makeReferenceSeq_actionPerformed(ActionEvent actionEvent)
1630   {
1631     if (!ap.av.getAlignment().hasSeqrep())
1632     {
1633       // initialise the display flags so the user sees something happen
1634       ap.av.setDisplayReferenceSeq(true);
1635       ap.av.setColourByReferenceSeq(true);
1636       ap.av.getAlignment().setSeqrep(sequence);
1637     }
1638     else
1639     {
1640       if (ap.av.getAlignment().getSeqrep() == sequence)
1641       {
1642         ap.av.getAlignment().setSeqrep(null);
1643       }
1644       else
1645       {
1646         ap.av.getAlignment().setSeqrep(sequence);
1647       }
1648     }
1649     refresh();
1650   }
1651
1652   protected void hideInsertions_actionPerformed(ActionEvent actionEvent)
1653   {
1654     if (sequence != null)
1655     {
1656       ColumnSelection cs = ap.av.getColumnSelection();
1657       if (cs == null)
1658       {
1659         cs = new ColumnSelection();
1660       }
1661       cs.hideInsertionsFor(sequence);
1662       ap.av.setColumnSelection(cs);
1663     }
1664     refresh();
1665   }
1666
1667   protected void sequenceSelectionDetails_actionPerformed()
1668   {
1669     createSequenceDetailsReport(ap.av.getSequenceSelection());
1670   }
1671
1672   protected void sequenceDetails_actionPerformed()
1673   {
1674     createSequenceDetailsReport(new SequenceI[] { sequence });
1675   }
1676
1677   public void createSequenceDetailsReport(SequenceI[] sequences)
1678   {
1679     CutAndPasteHtmlTransfer cap = new CutAndPasteHtmlTransfer();
1680     StringBuilder contents = new StringBuilder(128);
1681     for (SequenceI seq : sequences)
1682     {
1683       contents.append("<p><h2>"
1684               + MessageManager
1685                       .formatMessage(
1686                               "label.create_sequence_details_report_annotation_for",
1687                               new Object[] { seq.getDisplayId(true) })
1688               + "</h2></p><p>");
1689       new SequenceAnnotationReport(null)
1690               .createSequenceAnnotationReport(
1691                       contents,
1692                       seq,
1693                       true,
1694                       true,
1695                       (ap.getSeqPanel().seqCanvas.fr != null) ? ap
1696                               .getSeqPanel().seqCanvas.fr.getMinMax()
1697                               : null);
1698       contents.append("</p>");
1699     }
1700     cap.setText("<html>" + contents.toString() + "</html>");
1701
1702     Desktop.addInternalFrame(cap, MessageManager.formatMessage(
1703             "label.sequence_details_for",
1704             (sequences.length == 1 ? new Object[] { sequences[0]
1705                     .getDisplayId(true) } : new Object[] { MessageManager
1706                     .getString("label.selection") })), 500, 400);
1707
1708   }
1709
1710   protected void showNonconserved_actionPerformed()
1711   {
1712     getGroup().setShowNonconserved(displayNonconserved.isSelected());
1713     refresh();
1714   }
1715
1716   /**
1717    * call to refresh view after settings change
1718    */
1719   void refresh()
1720   {
1721     ap.updateAnnotation();
1722     ap.paintAlignment(true);
1723
1724     PaintRefresher.Refresh(this, ap.av.getSequenceSetId());
1725   }
1726
1727   /**
1728    * DOCUMENT ME!
1729    * 
1730    * @param e
1731    *          DOCUMENT ME!
1732    */
1733   protected void clustalColour_actionPerformed()
1734   {
1735     SequenceGroup sg = getGroup();
1736     sg.cs = new ClustalxColourScheme(sg, ap.av.getHiddenRepSequences());
1737     refresh();
1738   }
1739
1740   /**
1741    * DOCUMENT ME!
1742    * 
1743    * @param e
1744    *          DOCUMENT ME!
1745    */
1746   protected void zappoColour_actionPerformed()
1747   {
1748     getGroup().cs = new ZappoColourScheme();
1749     refresh();
1750   }
1751
1752   /**
1753    * DOCUMENT ME!
1754    * 
1755    * @param e
1756    *          DOCUMENT ME!
1757    */
1758   protected void taylorColour_actionPerformed()
1759   {
1760     getGroup().cs = new TaylorColourScheme();
1761     refresh();
1762   }
1763
1764   /**
1765    * DOCUMENT ME!
1766    * 
1767    * @param e
1768    *          DOCUMENT ME!
1769    */
1770   protected void hydrophobicityColour_actionPerformed()
1771   {
1772     getGroup().cs = new HydrophobicColourScheme();
1773     refresh();
1774   }
1775
1776   /**
1777    * DOCUMENT ME!
1778    * 
1779    * @param e
1780    *          DOCUMENT ME!
1781    */
1782   protected void helixColour_actionPerformed()
1783   {
1784     getGroup().cs = new HelixColourScheme();
1785     refresh();
1786   }
1787
1788   /**
1789    * DOCUMENT ME!
1790    * 
1791    * @param e
1792    *          DOCUMENT ME!
1793    */
1794   protected void strandColour_actionPerformed()
1795   {
1796     getGroup().cs = new StrandColourScheme();
1797     refresh();
1798   }
1799
1800   /**
1801    * DOCUMENT ME!
1802    * 
1803    * @param e
1804    *          DOCUMENT ME!
1805    */
1806   protected void turnColour_actionPerformed()
1807   {
1808     getGroup().cs = new TurnColourScheme();
1809     refresh();
1810   }
1811
1812   /**
1813    * DOCUMENT ME!
1814    * 
1815    * @param e
1816    *          DOCUMENT ME!
1817    */
1818   protected void buriedColour_actionPerformed()
1819   {
1820     getGroup().cs = new BuriedColourScheme();
1821     refresh();
1822   }
1823
1824   /**
1825    * DOCUMENT ME!
1826    * 
1827    * @param e
1828    *          DOCUMENT ME!
1829    */
1830   public void nucleotideMenuItem_actionPerformed()
1831   {
1832     getGroup().cs = new NucleotideColourScheme();
1833     refresh();
1834   }
1835
1836   protected void purinePyrimidineColour_actionPerformed()
1837   {
1838     getGroup().cs = new PurinePyrimidineColourScheme();
1839     refresh();
1840   }
1841
1842   /*
1843    * protected void covariationColour_actionPerformed() { getGroup().cs = new
1844    * CovariationColourScheme(sequence.getAnnotation()[0]); refresh(); }
1845    */
1846   /**
1847    * DOCUMENT ME!
1848    * 
1849    * @param e
1850    *          DOCUMENT ME!
1851    */
1852   protected void abovePIDColour_actionPerformed()
1853   {
1854     SequenceGroup sg = getGroup();
1855     if (sg.cs == null)
1856     {
1857       return;
1858     }
1859
1860     if (abovePIDColour.isSelected())
1861     {
1862       sg.cs.setConsensus(AAFrequency.calculate(
1863               sg.getSequences(ap.av.getHiddenRepSequences()),
1864               sg.getStartRes(), sg.getEndRes() + 1));
1865
1866       int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs, getGroup()
1867               .getName());
1868
1869       sg.cs.setThreshold(threshold, ap.av.isIgnoreGapsConsensus());
1870
1871       SliderPanel.showPIDSlider();
1872     }
1873     else
1874     // remove PIDColouring
1875     {
1876       sg.cs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
1877     }
1878
1879     refresh();
1880   }
1881
1882   /**
1883    * DOCUMENT ME!
1884    * 
1885    * @param e
1886    *          DOCUMENT ME!
1887    */
1888   protected void userDefinedColour_actionPerformed(ActionEvent e)
1889   {
1890     SequenceGroup sg = getGroup();
1891
1892     if (e.getSource().equals(userDefinedColour))
1893     {
1894       new UserDefinedColours(ap, sg);
1895     }
1896     else
1897     {
1898       UserColourScheme udc = (UserColourScheme) UserDefinedColours
1899               .getUserColourSchemes().get(e.getActionCommand());
1900
1901       sg.cs = udc;
1902     }
1903     refresh();
1904   }
1905
1906   /**
1907    * Open a panel where the user can choose which types of sequence annotation
1908    * to show or hide.
1909    * 
1910    * @param e
1911    */
1912   protected void chooseAnnotations_actionPerformed(ActionEvent e)
1913   {
1914     // todo correct way to guard against opening a duplicate panel?
1915     new AnnotationChooser(ap);
1916   }
1917
1918   /**
1919    * DOCUMENT ME!
1920    * 
1921    * @param e
1922    *          DOCUMENT ME!
1923    */
1924   protected void PIDColour_actionPerformed()
1925   {
1926     SequenceGroup sg = getGroup();
1927     sg.cs = new PIDColourScheme();
1928     sg.cs.setConsensus(AAFrequency.calculate(
1929             sg.getSequences(ap.av.getHiddenRepSequences()),
1930             sg.getStartRes(), sg.getEndRes() + 1));
1931     refresh();
1932   }
1933
1934   /**
1935    * DOCUMENT ME!
1936    * 
1937    * @param e
1938    *          DOCUMENT ME!
1939    */
1940   protected void BLOSUM62Colour_actionPerformed()
1941   {
1942     SequenceGroup sg = getGroup();
1943
1944     sg.cs = new Blosum62ColourScheme();
1945
1946     sg.cs.setConsensus(AAFrequency.calculate(
1947             sg.getSequences(ap.av.getHiddenRepSequences()),
1948             sg.getStartRes(), sg.getEndRes() + 1));
1949
1950     refresh();
1951   }
1952
1953   /**
1954    * DOCUMENT ME!
1955    * 
1956    * @param e
1957    *          DOCUMENT ME!
1958    */
1959   protected void noColourmenuItem_actionPerformed()
1960   {
1961     getGroup().cs = null;
1962     refresh();
1963   }
1964
1965   /**
1966    * DOCUMENT ME!
1967    * 
1968    * @param e
1969    *          DOCUMENT ME!
1970    */
1971   protected void conservationMenuItem_actionPerformed()
1972   {
1973     SequenceGroup sg = getGroup();
1974     if (sg.cs == null)
1975     {
1976       return;
1977     }
1978
1979     if (conservationMenuItem.isSelected())
1980     {
1981       // JBPNote: Conservation name shouldn't be i18n translated
1982       Conservation c = new Conservation("Group", sg.getSequences(ap.av
1983               .getHiddenRepSequences()), sg.getStartRes(),
1984               sg.getEndRes() + 1);
1985
1986       c.calculate();
1987       c.verdict(false, ap.av.getConsPercGaps());
1988
1989       sg.cs.setConservation(c);
1990
1991       SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
1992       SliderPanel.showConservationSlider();
1993     }
1994     else
1995     // remove ConservationColouring
1996     {
1997       sg.cs.setConservation(null);
1998     }
1999
2000     refresh();
2001   }
2002
2003   public void annotationMenuItem_actionPerformed(ActionEvent actionEvent)
2004   {
2005     SequenceGroup sg = getGroup();
2006     if (sg == null)
2007     {
2008       return;
2009     }
2010
2011     AnnotationColourGradient acg = new AnnotationColourGradient(
2012             sequence.getAnnotation()[0], null,
2013             AnnotationColourGradient.NO_THRESHOLD);
2014
2015     acg.setPredefinedColours(true);
2016     sg.cs = acg;
2017
2018     refresh();
2019   }
2020
2021   /**
2022    * DOCUMENT ME!
2023    * 
2024    * @param e
2025    *          DOCUMENT ME!
2026    */
2027   protected void groupName_actionPerformed()
2028   {
2029
2030     SequenceGroup sg = getGroup();
2031     EditNameDialog dialog = new EditNameDialog(sg.getName(),
2032             sg.getDescription(), "       "
2033                     + MessageManager.getString("label.group_name") + " ",
2034             MessageManager.getString("label.group_description") + " ",
2035             MessageManager.getString("label.edit_group_name_description"),
2036             ap.alignFrame);
2037
2038     if (!dialog.accept)
2039     {
2040       return;
2041     }
2042
2043     sg.setName(dialog.getName());
2044     sg.setDescription(dialog.getDescription());
2045     refresh();
2046   }
2047
2048   /**
2049    * Get selection group - adding it to the alignment if necessary.
2050    * 
2051    * @return sequence group to operate on
2052    */
2053   SequenceGroup getGroup()
2054   {
2055     SequenceGroup sg = ap.av.getSelectionGroup();
2056     // this method won't add a new group if it already exists
2057     if (sg != null)
2058     {
2059       ap.av.getAlignment().addGroup(sg);
2060     }
2061
2062     return sg;
2063   }
2064
2065   /**
2066    * DOCUMENT ME!
2067    * 
2068    * @param e
2069    *          DOCUMENT ME!
2070    */
2071   void sequenceName_actionPerformed()
2072   {
2073     EditNameDialog dialog = new EditNameDialog(sequence.getName(),
2074             sequence.getDescription(),
2075             "       " + MessageManager.getString("label.sequence_name")
2076                     + " ",
2077             MessageManager.getString("label.sequence_description") + " ",
2078             MessageManager
2079                     .getString("label.edit_sequence_name_description"),
2080             ap.alignFrame);
2081
2082     if (!dialog.accept)
2083     {
2084       return;
2085     }
2086
2087     if (dialog.getName() != null)
2088     {
2089       if (dialog.getName().indexOf(" ") > -1)
2090       {
2091         JOptionPane
2092                 .showMessageDialog(
2093                         ap,
2094                         MessageManager
2095                                 .getString("label.spaces_converted_to_backslashes"),
2096                         MessageManager
2097                                 .getString("label.no_spaces_allowed_sequence_name"),
2098                         JOptionPane.WARNING_MESSAGE);
2099       }
2100
2101       sequence.setName(dialog.getName().replace(' ', '_'));
2102       ap.paintAlignment(false);
2103     }
2104
2105     sequence.setDescription(dialog.getDescription());
2106
2107     ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
2108             .getSequences());
2109
2110   }
2111
2112   /**
2113    * DOCUMENT ME!
2114    * 
2115    * @param e
2116    *          DOCUMENT ME!
2117    */
2118   void unGroupMenuItem_actionPerformed()
2119   {
2120     SequenceGroup sg = ap.av.getSelectionGroup();
2121     ap.av.getAlignment().deleteGroup(sg);
2122     ap.av.setSelectionGroup(null);
2123     refresh();
2124   }
2125
2126   void createGroupMenuItem_actionPerformed()
2127   {
2128     getGroup(); // implicitly creates group - note - should apply defaults / use
2129                 // standard alignment window logic for this
2130     refresh();
2131   }
2132
2133   /**
2134    * DOCUMENT ME!
2135    * 
2136    * @param e
2137    *          DOCUMENT ME!
2138    */
2139   protected void outline_actionPerformed()
2140   {
2141     SequenceGroup sg = getGroup();
2142     Color col = JColorChooser.showDialog(this,
2143             MessageManager.getString("label.select_outline_colour"),
2144             Color.BLUE);
2145
2146     if (col != null)
2147     {
2148       sg.setOutlineColour(col);
2149     }
2150
2151     refresh();
2152   }
2153
2154   /**
2155    * DOCUMENT ME!
2156    * 
2157    * @param e
2158    *          DOCUMENT ME!
2159    */
2160   public void showBoxes_actionPerformed()
2161   {
2162     getGroup().setDisplayBoxes(showBoxes.isSelected());
2163     refresh();
2164   }
2165
2166   /**
2167    * DOCUMENT ME!
2168    * 
2169    * @param e
2170    *          DOCUMENT ME!
2171    */
2172   public void showText_actionPerformed()
2173   {
2174     getGroup().setDisplayText(showText.isSelected());
2175     refresh();
2176   }
2177
2178   /**
2179    * DOCUMENT ME!
2180    * 
2181    * @param e
2182    *          DOCUMENT ME!
2183    */
2184   public void showColourText_actionPerformed()
2185   {
2186     getGroup().setColourText(showColourText.isSelected());
2187     refresh();
2188   }
2189
2190   public void showLink(String url)
2191   {
2192     try
2193     {
2194       jalview.util.BrowserLauncher.openURL(url);
2195     } catch (Exception ex)
2196     {
2197       JOptionPane.showInternalMessageDialog(Desktop.desktop,
2198               MessageManager.getString("label.web_browser_not_found_unix"),
2199               MessageManager.getString("label.web_browser_not_found"),
2200               JOptionPane.WARNING_MESSAGE);
2201
2202       ex.printStackTrace();
2203     }
2204   }
2205
2206   void hideSequences(boolean representGroup)
2207   {
2208     ap.av.hideSequences(sequence, representGroup);
2209   }
2210
2211   public void copy_actionPerformed()
2212   {
2213     ap.alignFrame.copy_actionPerformed(null);
2214   }
2215
2216   public void cut_actionPerformed()
2217   {
2218     ap.alignFrame.cut_actionPerformed(null);
2219   }
2220
2221   void changeCase(ActionEvent e)
2222   {
2223     Object source = e.getSource();
2224     SequenceGroup sg = ap.av.getSelectionGroup();
2225
2226     if (sg != null)
2227     {
2228       List<int[]> startEnd = ap.av.getVisibleRegionBoundaries(
2229               sg.getStartRes(), sg.getEndRes() + 1);
2230
2231       String description;
2232       int caseChange;
2233
2234       if (source == toggle)
2235       {
2236         description = MessageManager.getString("label.toggle_case");
2237         caseChange = ChangeCaseCommand.TOGGLE_CASE;
2238       }
2239       else if (source == upperCase)
2240       {
2241         description = MessageManager.getString("label.to_upper_case");
2242         caseChange = ChangeCaseCommand.TO_UPPER;
2243       }
2244       else
2245       {
2246         description = MessageManager.getString("label.to_lower_case");
2247         caseChange = ChangeCaseCommand.TO_LOWER;
2248       }
2249
2250       ChangeCaseCommand caseCommand = new ChangeCaseCommand(description,
2251               sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
2252               startEnd, caseChange);
2253
2254       ap.alignFrame.addHistoryItem(caseCommand);
2255
2256       ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
2257               .getSequences());
2258
2259     }
2260   }
2261
2262   public void outputText_actionPerformed(ActionEvent e)
2263   {
2264     CutAndPasteTransfer cap = new CutAndPasteTransfer();
2265     cap.setForInput(null);
2266     Desktop.addInternalFrame(cap, MessageManager.formatMessage(
2267             "label.alignment_output_command",
2268             new Object[] { e.getActionCommand() }), 600, 500);
2269
2270     String[] omitHidden = null;
2271
2272     System.out.println("PROMPT USER HERE"); // TODO: decide if a prompt happens
2273     // or we simply trust the user wants
2274     // wysiwig behaviour
2275
2276     cap.setText(new FormatAdapter(ap).formatSequences(e.getActionCommand(),
2277             ap, true));
2278   }
2279
2280   public void sequenceFeature_actionPerformed()
2281   {
2282     SequenceGroup sg = ap.av.getSelectionGroup();
2283     if (sg == null)
2284     {
2285       return;
2286     }
2287
2288     int rsize = 0, gSize = sg.getSize();
2289     SequenceI[] rseqs, seqs = new SequenceI[gSize];
2290     SequenceFeature[] tfeatures, features = new SequenceFeature[gSize];
2291
2292     for (int i = 0; i < gSize; i++)
2293     {
2294       int start = sg.getSequenceAt(i).findPosition(sg.getStartRes());
2295       int end = sg.findEndRes(sg.getSequenceAt(i));
2296       if (start <= end)
2297       {
2298         seqs[rsize] = sg.getSequenceAt(i).getDatasetSequence();
2299         features[rsize] = new SequenceFeature(null, null, null, start, end,
2300                 "Jalview");
2301         rsize++;
2302       }
2303     }
2304     rseqs = new SequenceI[rsize];
2305     tfeatures = new SequenceFeature[rsize];
2306     System.arraycopy(seqs, 0, rseqs, 0, rsize);
2307     System.arraycopy(features, 0, tfeatures, 0, rsize);
2308     features = tfeatures;
2309     seqs = rseqs;
2310     if (ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
2311             features, true, ap))
2312     {
2313       ap.alignFrame.setShowSeqFeatures(true);
2314       ap.highlightSearchResults(null);
2315     }
2316   }
2317
2318   public void textColour_actionPerformed()
2319   {
2320     SequenceGroup sg = getGroup();
2321     if (sg != null)
2322     {
2323       new TextColourChooser().chooseColour(ap, sg);
2324     }
2325   }
2326
2327   public void colourByStructure(String pdbid)
2328   {
2329     Annotation[] anots = ap.av.getStructureSelectionManager()
2330             .colourSequenceFromStructure(sequence, pdbid);
2331
2332     AlignmentAnnotation an = new AlignmentAnnotation("Structure",
2333             "Coloured by " + pdbid, anots);
2334
2335     ap.av.getAlignment().addAnnotation(an);
2336     an.createSequenceMapping(sequence, 0, true);
2337     // an.adjustForAlignment();
2338     ap.av.getAlignment().setAnnotationIndex(an, 0);
2339
2340     ap.adjustAnnotationHeight();
2341
2342     sequence.addAlignmentAnnotation(an);
2343
2344   }
2345
2346   public void editSequence_actionPerformed(ActionEvent actionEvent)
2347   {
2348     SequenceGroup sg = ap.av.getSelectionGroup();
2349
2350     if (sg != null)
2351     {
2352       if (sequence == null)
2353       {
2354         sequence = sg.getSequenceAt(0);
2355       }
2356
2357       EditNameDialog dialog = new EditNameDialog(
2358               sequence.getSequenceAsString(sg.getStartRes(),
2359                       sg.getEndRes() + 1), null,
2360               MessageManager.getString("label.edit_sequence"), null,
2361               MessageManager.getString("label.edit_sequence"),
2362               ap.alignFrame);
2363
2364       if (dialog.accept)
2365       {
2366         EditCommand editCommand = new EditCommand(
2367                 MessageManager.getString("label.edit_sequences"),
2368                 Action.REPLACE, dialog.getName().replace(' ',
2369                         ap.av.getGapCharacter()),
2370                 sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
2371                 sg.getStartRes(), sg.getEndRes() + 1, ap.av.getAlignment());
2372
2373         ap.alignFrame.addHistoryItem(editCommand);
2374
2375         ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
2376                 .getSequences());
2377       }
2378     }
2379   }
2380
2381 }