JAL-2361 adding items to applet colour menus, TCoffee colour to desktop
[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     colourMenu.removeAll();
1522     colourMenu.add(textColour);
1523     colourMenu.add(noColourmenuItem);
1524     colourMenu.add(clustalColour);
1525     colourMenu.add(BLOSUM62Colour);
1526     colourMenu.add(PIDColour);
1527     colourMenu.add(zappoColour);
1528     colourMenu.add(taylorColour);
1529     colourMenu.add(hydrophobicityColour);
1530     colourMenu.add(helixColour);
1531     colourMenu.add(strandColour);
1532     colourMenu.add(turnColour);
1533     colourMenu.add(buriedColour);
1534     colourMenu.add(nucleotideColour);
1535     colourMenu.add(purinePyrimidineColour);
1536     colourMenu.add(tcoffeeColour);
1537
1538     SortedMap<String, UserColourScheme> userColourSchemes = UserDefinedColours
1539             .getUserColourSchemes();
1540     if (userColourSchemes != null)
1541     {
1542       for (String userColour : userColourSchemes.keySet())
1543     {
1544         JMenuItem item = new JMenuItem(userColour);
1545         item.addActionListener(new ActionListener()
1546         {
1547           @Override
1548           public void actionPerformed(ActionEvent evt)
1549           {
1550             userDefinedColour_actionPerformed(evt);
1551           }
1552         });
1553         colourMenu.add(item);
1554       }
1555     }
1556     colourMenu.add(userDefinedColour);
1557
1558     colourMenu.addSeparator();
1559     colourMenu.add(abovePIDColour);
1560     colourMenu.add(conservationMenuItem);
1561
1562     /*
1563      * add some of these items to a ButtonGroup so their
1564      * selection is mutually exclusive
1565      */
1566     ButtonGroup colours = new ButtonGroup();
1567     colours.add(noColourmenuItem);
1568     colours.add(clustalColour);
1569     colours.add(BLOSUM62Colour);
1570     colours.add(PIDColour);
1571     colours.add(zappoColour);
1572     colours.add(taylorColour);
1573     colours.add(hydrophobicityColour);
1574     colours.add(helixColour);
1575     colours.add(strandColour);
1576     colours.add(turnColour);
1577     colours.add(buriedColour);
1578     colours.add(purinePyrimidineColour);
1579     colours.add(tcoffeeColour);
1580     colours.add(nucleotideColour);
1581     colours.add(userDefinedColour);
1582     colours.add(abovePIDColour);
1583     // colours.add(RNAInteractionColour);
1584
1585   }
1586
1587   /**
1588    * Check for any annotations on the underlying dataset sequences (for the
1589    * current selection group) which are not 'on the alignment'.If any are found,
1590    * enable the option to add them to the alignment. The criteria for 'on the
1591    * alignment' is finding an alignment annotation on the alignment, matched on
1592    * calcId, label and sequenceRef.
1593    * 
1594    * A tooltip is also constructed that displays the source (calcId) and type
1595    * (label) of the annotations that can be added.
1596    * 
1597    * @param menuItem
1598    * @param forSequences
1599    */
1600   protected void configureReferenceAnnotationsMenu(JMenuItem menuItem,
1601           List<SequenceI> forSequences)
1602   {
1603     menuItem.setEnabled(false);
1604
1605     /*
1606      * Temporary store to hold distinct calcId / type pairs for the tooltip.
1607      * Using TreeMap means calcIds are shown in alphabetical order.
1608      */
1609     SortedMap<String, String> tipEntries = new TreeMap<String, String>();
1610     final Map<SequenceI, List<AlignmentAnnotation>> candidates = new LinkedHashMap<SequenceI, List<AlignmentAnnotation>>();
1611     AlignmentI al = this.ap.av.getAlignment();
1612     AlignmentUtils.findAddableReferenceAnnotations(forSequences,
1613             tipEntries, candidates, al);
1614     if (!candidates.isEmpty())
1615     {
1616       StringBuilder tooltip = new StringBuilder(64);
1617       tooltip.append(MessageManager.getString("label.add_annotations_for"));
1618
1619       /*
1620        * Found annotations that could be added. Enable the menu item, and
1621        * configure its tooltip and action.
1622        */
1623       menuItem.setEnabled(true);
1624       for (String calcId : tipEntries.keySet())
1625       {
1626         tooltip.append("<br/>" + calcId + "/" + tipEntries.get(calcId));
1627       }
1628       String tooltipText = JvSwingUtils.wrapTooltip(true,
1629               tooltip.toString());
1630       menuItem.setToolTipText(tooltipText);
1631
1632       menuItem.addActionListener(new ActionListener()
1633       {
1634         @Override
1635         public void actionPerformed(ActionEvent e)
1636         {
1637           addReferenceAnnotations_actionPerformed(candidates);
1638         }
1639       });
1640     }
1641   }
1642
1643   /**
1644    * Add annotations to the sequences and to the alignment.
1645    * 
1646    * @param candidates
1647    *          a map whose keys are sequences on the alignment, and values a list
1648    *          of annotations to add to each sequence
1649    */
1650   protected void addReferenceAnnotations_actionPerformed(
1651           Map<SequenceI, List<AlignmentAnnotation>> candidates)
1652   {
1653     final SequenceGroup selectionGroup = this.ap.av.getSelectionGroup();
1654     final AlignmentI alignment = this.ap.getAlignment();
1655     AlignmentUtils.addReferenceAnnotations(candidates, alignment,
1656             selectionGroup);
1657     refresh();
1658   }
1659
1660   protected void makeReferenceSeq_actionPerformed(ActionEvent actionEvent)
1661   {
1662     if (!ap.av.getAlignment().hasSeqrep())
1663     {
1664       // initialise the display flags so the user sees something happen
1665       ap.av.setDisplayReferenceSeq(true);
1666       ap.av.setColourByReferenceSeq(true);
1667       ap.av.getAlignment().setSeqrep(sequence);
1668     }
1669     else
1670     {
1671       if (ap.av.getAlignment().getSeqrep() == sequence)
1672       {
1673         ap.av.getAlignment().setSeqrep(null);
1674       }
1675       else
1676       {
1677         ap.av.getAlignment().setSeqrep(sequence);
1678       }
1679     }
1680     refresh();
1681   }
1682
1683   protected void hideInsertions_actionPerformed(ActionEvent actionEvent)
1684   {
1685     if (sequence != null)
1686     {
1687       ColumnSelection cs = ap.av.getColumnSelection();
1688       if (cs == null)
1689       {
1690         cs = new ColumnSelection();
1691       }
1692       cs.hideInsertionsFor(sequence);
1693       ap.av.setColumnSelection(cs);
1694     }
1695     refresh();
1696   }
1697
1698   protected void sequenceSelectionDetails_actionPerformed()
1699   {
1700     createSequenceDetailsReport(ap.av.getSequenceSelection());
1701   }
1702
1703   protected void sequenceDetails_actionPerformed()
1704   {
1705     createSequenceDetailsReport(new SequenceI[] { sequence });
1706   }
1707
1708   public void createSequenceDetailsReport(SequenceI[] sequences)
1709   {
1710     CutAndPasteHtmlTransfer cap = new CutAndPasteHtmlTransfer();
1711     StringBuilder contents = new StringBuilder(128);
1712     for (SequenceI seq : sequences)
1713     {
1714       contents.append("<p><h2>"
1715               + MessageManager
1716                       .formatMessage(
1717                               "label.create_sequence_details_report_annotation_for",
1718                               new Object[] { seq.getDisplayId(true) })
1719               + "</h2></p><p>");
1720       new SequenceAnnotationReport(null)
1721               .createSequenceAnnotationReport(
1722                       contents,
1723                       seq,
1724                       true,
1725                       true,
1726                       (ap.getSeqPanel().seqCanvas.fr != null) ? ap
1727                               .getSeqPanel().seqCanvas.fr.getMinMax()
1728                               : null);
1729       contents.append("</p>");
1730     }
1731     cap.setText("<html>" + contents.toString() + "</html>");
1732
1733     Desktop.addInternalFrame(cap, MessageManager.formatMessage(
1734             "label.sequence_details_for",
1735             (sequences.length == 1 ? new Object[] { sequences[0]
1736                     .getDisplayId(true) } : new Object[] { MessageManager
1737                     .getString("label.selection") })), 500, 400);
1738
1739   }
1740
1741   protected void showNonconserved_actionPerformed()
1742   {
1743     getGroup().setShowNonconserved(displayNonconserved.isSelected());
1744     refresh();
1745   }
1746
1747   /**
1748    * call to refresh view after settings change
1749    */
1750   void refresh()
1751   {
1752     ap.updateAnnotation();
1753     ap.paintAlignment(true);
1754
1755     PaintRefresher.Refresh(this, ap.av.getSequenceSetId());
1756   }
1757
1758   /**
1759    * DOCUMENT ME!
1760    * 
1761    * @param e
1762    *          DOCUMENT ME!
1763    */
1764   protected void clustalColour_actionPerformed()
1765   {
1766     SequenceGroup sg = getGroup();
1767     sg.cs = new ClustalxColourScheme(sg, ap.av.getHiddenRepSequences());
1768     refresh();
1769   }
1770
1771   /**
1772    * DOCUMENT ME!
1773    * 
1774    * @param e
1775    *          DOCUMENT ME!
1776    */
1777   protected void zappoColour_actionPerformed()
1778   {
1779     getGroup().cs = new ZappoColourScheme();
1780     refresh();
1781   }
1782
1783   /**
1784    * DOCUMENT ME!
1785    * 
1786    * @param e
1787    *          DOCUMENT ME!
1788    */
1789   protected void taylorColour_actionPerformed()
1790   {
1791     getGroup().cs = new TaylorColourScheme();
1792     refresh();
1793   }
1794
1795   /**
1796    * DOCUMENT ME!
1797    * 
1798    * @param e
1799    *          DOCUMENT ME!
1800    */
1801   protected void hydrophobicityColour_actionPerformed()
1802   {
1803     getGroup().cs = new HydrophobicColourScheme();
1804     refresh();
1805   }
1806
1807   /**
1808    * DOCUMENT ME!
1809    * 
1810    * @param e
1811    *          DOCUMENT ME!
1812    */
1813   protected void helixColour_actionPerformed()
1814   {
1815     getGroup().cs = new HelixColourScheme();
1816     refresh();
1817   }
1818
1819   /**
1820    * DOCUMENT ME!
1821    * 
1822    * @param e
1823    *          DOCUMENT ME!
1824    */
1825   protected void strandColour_actionPerformed()
1826   {
1827     getGroup().cs = new StrandColourScheme();
1828     refresh();
1829   }
1830
1831   /**
1832    * DOCUMENT ME!
1833    * 
1834    * @param e
1835    *          DOCUMENT ME!
1836    */
1837   protected void turnColour_actionPerformed()
1838   {
1839     getGroup().cs = new TurnColourScheme();
1840     refresh();
1841   }
1842
1843   /**
1844    * DOCUMENT ME!
1845    * 
1846    * @param e
1847    *          DOCUMENT ME!
1848    */
1849   protected void buriedColour_actionPerformed()
1850   {
1851     getGroup().cs = new BuriedColourScheme();
1852     refresh();
1853   }
1854
1855   /**
1856    * DOCUMENT ME!
1857    * 
1858    * @param e
1859    *          DOCUMENT ME!
1860    */
1861   public void nucleotideMenuItem_actionPerformed()
1862   {
1863     getGroup().cs = new NucleotideColourScheme();
1864     refresh();
1865   }
1866
1867   protected void purinePyrimidineColour_actionPerformed()
1868   {
1869     getGroup().cs = new PurinePyrimidineColourScheme();
1870     refresh();
1871   }
1872
1873   protected void tcoffeeColorScheme_actionPerformed()
1874   {
1875     getGroup().cs = new TCoffeeColourScheme(getGroup());
1876     refresh();
1877   }
1878
1879   /*
1880    * protected void covariationColour_actionPerformed() { getGroup().cs = new
1881    * CovariationColourScheme(sequence.getAnnotation()[0]); refresh(); }
1882    */
1883   /**
1884    * DOCUMENT ME!
1885    * 
1886    * @param e
1887    *          DOCUMENT ME!
1888    */
1889   protected void abovePIDColour_actionPerformed()
1890   {
1891     SequenceGroup sg = getGroup();
1892     if (sg.cs == null)
1893     {
1894       return;
1895     }
1896
1897     if (abovePIDColour.isSelected())
1898     {
1899       sg.cs.setConsensus(AAFrequency.calculate(
1900               sg.getSequences(ap.av.getHiddenRepSequences()),
1901               sg.getStartRes(), sg.getEndRes() + 1));
1902
1903       int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs, getGroup()
1904               .getName());
1905
1906       sg.cs.setThreshold(threshold, ap.av.isIgnoreGapsConsensus());
1907
1908       SliderPanel.showPIDSlider();
1909     }
1910     else
1911     // remove PIDColouring
1912     {
1913       sg.cs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
1914     }
1915
1916     refresh();
1917   }
1918
1919   /**
1920    * DOCUMENT ME!
1921    * 
1922    * @param e
1923    *          DOCUMENT ME!
1924    */
1925   protected void userDefinedColour_actionPerformed(ActionEvent e)
1926   {
1927     SequenceGroup sg = getGroup();
1928
1929     if (e.getSource().equals(userDefinedColour))
1930     {
1931       new UserDefinedColours(ap, sg);
1932     }
1933     else
1934     {
1935       UserColourScheme udc = UserDefinedColours
1936               .getUserColourSchemes().get(e.getActionCommand());
1937
1938       sg.cs = udc;
1939     }
1940     refresh();
1941   }
1942
1943   /**
1944    * Open a panel where the user can choose which types of sequence annotation
1945    * to show or hide.
1946    * 
1947    * @param e
1948    */
1949   protected void chooseAnnotations_actionPerformed(ActionEvent e)
1950   {
1951     // todo correct way to guard against opening a duplicate panel?
1952     new AnnotationChooser(ap);
1953   }
1954
1955   /**
1956    * DOCUMENT ME!
1957    * 
1958    * @param e
1959    *          DOCUMENT ME!
1960    */
1961   protected void PIDColour_actionPerformed()
1962   {
1963     SequenceGroup sg = getGroup();
1964     sg.cs = new PIDColourScheme();
1965     sg.cs.setConsensus(AAFrequency.calculate(
1966             sg.getSequences(ap.av.getHiddenRepSequences()),
1967             sg.getStartRes(), sg.getEndRes() + 1));
1968     refresh();
1969   }
1970
1971   /**
1972    * DOCUMENT ME!
1973    * 
1974    * @param e
1975    *          DOCUMENT ME!
1976    */
1977   protected void BLOSUM62Colour_actionPerformed()
1978   {
1979     SequenceGroup sg = getGroup();
1980
1981     sg.cs = new Blosum62ColourScheme();
1982
1983     sg.cs.setConsensus(AAFrequency.calculate(
1984             sg.getSequences(ap.av.getHiddenRepSequences()),
1985             sg.getStartRes(), sg.getEndRes() + 1));
1986
1987     refresh();
1988   }
1989
1990   /**
1991    * DOCUMENT ME!
1992    * 
1993    * @param e
1994    *          DOCUMENT ME!
1995    */
1996   protected void noColourmenuItem_actionPerformed()
1997   {
1998     getGroup().cs = null;
1999     refresh();
2000   }
2001
2002   /**
2003    * DOCUMENT ME!
2004    * 
2005    * @param e
2006    *          DOCUMENT ME!
2007    */
2008   protected void conservationMenuItem_actionPerformed()
2009   {
2010     SequenceGroup sg = getGroup();
2011     if (sg.cs == null)
2012     {
2013       return;
2014     }
2015
2016     if (conservationMenuItem.isSelected())
2017     {
2018       // JBPNote: Conservation name shouldn't be i18n translated
2019       Conservation c = new Conservation("Group", sg.getSequences(ap.av
2020               .getHiddenRepSequences()), sg.getStartRes(),
2021               sg.getEndRes() + 1);
2022
2023       c.calculate();
2024       c.verdict(false, ap.av.getConsPercGaps());
2025
2026       sg.cs.setConservation(c);
2027
2028       SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
2029       SliderPanel.showConservationSlider();
2030     }
2031     else
2032     // remove ConservationColouring
2033     {
2034       sg.cs.setConservation(null);
2035     }
2036
2037     refresh();
2038   }
2039
2040   public void annotationMenuItem_actionPerformed(ActionEvent actionEvent)
2041   {
2042     SequenceGroup sg = getGroup();
2043     if (sg == null)
2044     {
2045       return;
2046     }
2047
2048     AnnotationColourGradient acg = new AnnotationColourGradient(
2049             sequence.getAnnotation()[0], null,
2050             AnnotationColourGradient.NO_THRESHOLD);
2051
2052     acg.setPredefinedColours(true);
2053     sg.cs = acg;
2054
2055     refresh();
2056   }
2057
2058   /**
2059    * DOCUMENT ME!
2060    * 
2061    * @param e
2062    *          DOCUMENT ME!
2063    */
2064   protected void groupName_actionPerformed()
2065   {
2066
2067     SequenceGroup sg = getGroup();
2068     EditNameDialog dialog = new EditNameDialog(sg.getName(),
2069             sg.getDescription(), "       "
2070                     + MessageManager.getString("label.group_name") + " ",
2071             MessageManager.getString("label.group_description") + " ",
2072             MessageManager.getString("label.edit_group_name_description"),
2073             ap.alignFrame);
2074
2075     if (!dialog.accept)
2076     {
2077       return;
2078     }
2079
2080     sg.setName(dialog.getName());
2081     sg.setDescription(dialog.getDescription());
2082     refresh();
2083   }
2084
2085   /**
2086    * Get selection group - adding it to the alignment if necessary.
2087    * 
2088    * @return sequence group to operate on
2089    */
2090   SequenceGroup getGroup()
2091   {
2092     SequenceGroup sg = ap.av.getSelectionGroup();
2093     // this method won't add a new group if it already exists
2094     if (sg != null)
2095     {
2096       ap.av.getAlignment().addGroup(sg);
2097     }
2098
2099     return sg;
2100   }
2101
2102   /**
2103    * DOCUMENT ME!
2104    * 
2105    * @param e
2106    *          DOCUMENT ME!
2107    */
2108   void sequenceName_actionPerformed()
2109   {
2110     EditNameDialog dialog = new EditNameDialog(sequence.getName(),
2111             sequence.getDescription(),
2112             "       " + MessageManager.getString("label.sequence_name")
2113                     + " ",
2114             MessageManager.getString("label.sequence_description") + " ",
2115             MessageManager
2116                     .getString("label.edit_sequence_name_description"),
2117             ap.alignFrame);
2118
2119     if (!dialog.accept)
2120     {
2121       return;
2122     }
2123
2124     if (dialog.getName() != null)
2125     {
2126       if (dialog.getName().indexOf(" ") > -1)
2127       {
2128         JvOptionPane
2129                 .showMessageDialog(
2130                         ap,
2131                         MessageManager
2132                                 .getString("label.spaces_converted_to_backslashes"),
2133                         MessageManager
2134                                 .getString("label.no_spaces_allowed_sequence_name"),
2135                         JvOptionPane.WARNING_MESSAGE);
2136       }
2137
2138       sequence.setName(dialog.getName().replace(' ', '_'));
2139       ap.paintAlignment(false);
2140     }
2141
2142     sequence.setDescription(dialog.getDescription());
2143
2144     ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
2145             .getSequences());
2146
2147   }
2148
2149   /**
2150    * DOCUMENT ME!
2151    * 
2152    * @param e
2153    *          DOCUMENT ME!
2154    */
2155   void unGroupMenuItem_actionPerformed()
2156   {
2157     SequenceGroup sg = ap.av.getSelectionGroup();
2158     ap.av.getAlignment().deleteGroup(sg);
2159     ap.av.setSelectionGroup(null);
2160     refresh();
2161   }
2162
2163   void createGroupMenuItem_actionPerformed()
2164   {
2165     getGroup(); // implicitly creates group - note - should apply defaults / use
2166                 // standard alignment window logic for this
2167     refresh();
2168   }
2169
2170   /**
2171    * DOCUMENT ME!
2172    * 
2173    * @param e
2174    *          DOCUMENT ME!
2175    */
2176   protected void outline_actionPerformed()
2177   {
2178     SequenceGroup sg = getGroup();
2179     Color col = JColorChooser.showDialog(this,
2180             MessageManager.getString("label.select_outline_colour"),
2181             Color.BLUE);
2182
2183     if (col != null)
2184     {
2185       sg.setOutlineColour(col);
2186     }
2187
2188     refresh();
2189   }
2190
2191   /**
2192    * DOCUMENT ME!
2193    * 
2194    * @param e
2195    *          DOCUMENT ME!
2196    */
2197   public void showBoxes_actionPerformed()
2198   {
2199     getGroup().setDisplayBoxes(showBoxes.isSelected());
2200     refresh();
2201   }
2202
2203   /**
2204    * DOCUMENT ME!
2205    * 
2206    * @param e
2207    *          DOCUMENT ME!
2208    */
2209   public void showText_actionPerformed()
2210   {
2211     getGroup().setDisplayText(showText.isSelected());
2212     refresh();
2213   }
2214
2215   /**
2216    * DOCUMENT ME!
2217    * 
2218    * @param e
2219    *          DOCUMENT ME!
2220    */
2221   public void showColourText_actionPerformed()
2222   {
2223     getGroup().setColourText(showColourText.isSelected());
2224     refresh();
2225   }
2226
2227   public void showLink(String url)
2228   {
2229     try
2230     {
2231       jalview.util.BrowserLauncher.openURL(url);
2232     } catch (Exception ex)
2233     {
2234       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2235               MessageManager.getString("label.web_browser_not_found_unix"),
2236               MessageManager.getString("label.web_browser_not_found"),
2237               JvOptionPane.WARNING_MESSAGE);
2238
2239       ex.printStackTrace();
2240     }
2241   }
2242
2243   void hideSequences(boolean representGroup)
2244   {
2245     ap.av.hideSequences(sequence, representGroup);
2246   }
2247
2248   public void copy_actionPerformed()
2249   {
2250     ap.alignFrame.copy_actionPerformed(null);
2251   }
2252
2253   public void cut_actionPerformed()
2254   {
2255     ap.alignFrame.cut_actionPerformed(null);
2256   }
2257
2258   void changeCase(ActionEvent e)
2259   {
2260     Object source = e.getSource();
2261     SequenceGroup sg = ap.av.getSelectionGroup();
2262
2263     if (sg != null)
2264     {
2265       List<int[]> startEnd = ap.av.getVisibleRegionBoundaries(
2266               sg.getStartRes(), sg.getEndRes() + 1);
2267
2268       String description;
2269       int caseChange;
2270
2271       if (source == toggle)
2272       {
2273         description = MessageManager.getString("label.toggle_case");
2274         caseChange = ChangeCaseCommand.TOGGLE_CASE;
2275       }
2276       else if (source == upperCase)
2277       {
2278         description = MessageManager.getString("label.to_upper_case");
2279         caseChange = ChangeCaseCommand.TO_UPPER;
2280       }
2281       else
2282       {
2283         description = MessageManager.getString("label.to_lower_case");
2284         caseChange = ChangeCaseCommand.TO_LOWER;
2285       }
2286
2287       ChangeCaseCommand caseCommand = new ChangeCaseCommand(description,
2288               sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
2289               startEnd, caseChange);
2290
2291       ap.alignFrame.addHistoryItem(caseCommand);
2292
2293       ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
2294               .getSequences());
2295
2296     }
2297   }
2298
2299   public void outputText_actionPerformed(ActionEvent e)
2300   {
2301     CutAndPasteTransfer cap = new CutAndPasteTransfer();
2302     cap.setForInput(null);
2303     Desktop.addInternalFrame(cap, MessageManager.formatMessage(
2304             "label.alignment_output_command",
2305             new Object[] { e.getActionCommand() }), 600, 500);
2306
2307     String[] omitHidden = null;
2308
2309     System.out.println("PROMPT USER HERE"); // TODO: decide if a prompt happens
2310     // or we simply trust the user wants
2311     // wysiwig behaviour
2312
2313     FileFormatI fileFormat = FileFormat.forName(e.getActionCommand());
2314     cap.setText(new FormatAdapter(ap).formatSequences(fileFormat, ap, true));
2315   }
2316
2317   public void sequenceFeature_actionPerformed()
2318   {
2319     SequenceGroup sg = ap.av.getSelectionGroup();
2320     if (sg == null)
2321     {
2322       return;
2323     }
2324
2325     int rsize = 0, gSize = sg.getSize();
2326     SequenceI[] rseqs, seqs = new SequenceI[gSize];
2327     SequenceFeature[] tfeatures, features = new SequenceFeature[gSize];
2328
2329     for (int i = 0; i < gSize; i++)
2330     {
2331       int start = sg.getSequenceAt(i).findPosition(sg.getStartRes());
2332       int end = sg.findEndRes(sg.getSequenceAt(i));
2333       if (start <= end)
2334       {
2335         seqs[rsize] = sg.getSequenceAt(i).getDatasetSequence();
2336         features[rsize] = new SequenceFeature(null, null, null, start, end,
2337                 "Jalview");
2338         rsize++;
2339       }
2340     }
2341     rseqs = new SequenceI[rsize];
2342     tfeatures = new SequenceFeature[rsize];
2343     System.arraycopy(seqs, 0, rseqs, 0, rsize);
2344     System.arraycopy(features, 0, tfeatures, 0, rsize);
2345     features = tfeatures;
2346     seqs = rseqs;
2347     if (ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
2348             features, true, ap))
2349     {
2350       ap.alignFrame.setShowSeqFeatures(true);
2351       ap.highlightSearchResults(null);
2352     }
2353   }
2354
2355   public void textColour_actionPerformed()
2356   {
2357     SequenceGroup sg = getGroup();
2358     if (sg != null)
2359     {
2360       new TextColourChooser().chooseColour(ap, sg);
2361     }
2362   }
2363
2364   public void colourByStructure(String pdbid)
2365   {
2366     Annotation[] anots = ap.av.getStructureSelectionManager()
2367             .colourSequenceFromStructure(sequence, pdbid);
2368
2369     AlignmentAnnotation an = new AlignmentAnnotation("Structure",
2370             "Coloured by " + pdbid, anots);
2371
2372     ap.av.getAlignment().addAnnotation(an);
2373     an.createSequenceMapping(sequence, 0, true);
2374     // an.adjustForAlignment();
2375     ap.av.getAlignment().setAnnotationIndex(an, 0);
2376
2377     ap.adjustAnnotationHeight();
2378
2379     sequence.addAlignmentAnnotation(an);
2380
2381   }
2382
2383   public void editSequence_actionPerformed(ActionEvent actionEvent)
2384   {
2385     SequenceGroup sg = ap.av.getSelectionGroup();
2386
2387     if (sg != null)
2388     {
2389       if (sequence == null)
2390       {
2391         sequence = sg.getSequenceAt(0);
2392       }
2393
2394       EditNameDialog dialog = new EditNameDialog(
2395               sequence.getSequenceAsString(sg.getStartRes(),
2396                       sg.getEndRes() + 1), null,
2397               MessageManager.getString("label.edit_sequence"), null,
2398               MessageManager.getString("label.edit_sequence"),
2399               ap.alignFrame);
2400
2401       if (dialog.accept)
2402       {
2403         EditCommand editCommand = new EditCommand(
2404                 MessageManager.getString("label.edit_sequences"),
2405                 Action.REPLACE, dialog.getName().replace(' ',
2406                         ap.av.getGapCharacter()),
2407                 sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
2408                 sg.getStartRes(), sg.getEndRes() + 1, ap.av.getAlignment());
2409
2410         ap.alignFrame.addHistoryItem(editCommand);
2411
2412         ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
2413                 .getSequences());
2414       }
2415     }
2416   }
2417
2418 }