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