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