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