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