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