Merge branch 'develop' into trialMerge
[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.TaylorColourScheme;
56 import jalview.schemes.TurnColourScheme;
57 import jalview.schemes.UserColourScheme;
58 import jalview.schemes.ZappoColourScheme;
59 import jalview.util.GroupUrlLink;
60 import jalview.util.GroupUrlLink.UrlStringTooLongException;
61 import jalview.util.MessageManager;
62 import jalview.util.UrlLink;
63
64 import java.awt.Color;
65 import java.awt.event.ActionEvent;
66 import java.awt.event.ActionListener;
67 import java.util.Arrays;
68 import java.util.Collection;
69 import java.util.Collections;
70 import java.util.Hashtable;
71 import java.util.LinkedHashMap;
72 import java.util.List;
73 import java.util.Map;
74 import java.util.TreeMap;
75 import java.util.Vector;
76
77 import javax.swing.ButtonGroup;
78 import javax.swing.JCheckBoxMenuItem;
79 import javax.swing.JColorChooser;
80 import javax.swing.JMenu;
81 import javax.swing.JMenuItem;
82 import javax.swing.JOptionPane;
83 import javax.swing.JPopupMenu;
84 import javax.swing.JRadioButtonMenuItem;
85
86 /**
87  * DOCUMENT ME!
88  * 
89  * @author $author$
90  * @version $Revision: 1.118 $
91  */
92 public class PopupMenu extends JPopupMenu
93 {
94   JMenu groupMenu = new JMenu();
95
96   JMenuItem groupName = new JMenuItem();
97
98   protected JRadioButtonMenuItem clustalColour = new JRadioButtonMenuItem();
99
100   protected JRadioButtonMenuItem zappoColour = new JRadioButtonMenuItem();
101
102   protected JRadioButtonMenuItem taylorColour = new JRadioButtonMenuItem();
103
104   protected JRadioButtonMenuItem hydrophobicityColour = new JRadioButtonMenuItem();
105
106   protected JRadioButtonMenuItem helixColour = new JRadioButtonMenuItem();
107
108   protected JRadioButtonMenuItem strandColour = new JRadioButtonMenuItem();
109
110   protected JRadioButtonMenuItem turnColour = new JRadioButtonMenuItem();
111
112   protected JRadioButtonMenuItem buriedColour = new JRadioButtonMenuItem();
113
114   protected JCheckBoxMenuItem abovePIDColour = new JCheckBoxMenuItem();
115
116   protected JRadioButtonMenuItem userDefinedColour = new JRadioButtonMenuItem();
117
118   protected JRadioButtonMenuItem PIDColour = new JRadioButtonMenuItem();
119
120   protected JRadioButtonMenuItem BLOSUM62Colour = new JRadioButtonMenuItem();
121
122   protected JRadioButtonMenuItem purinePyrimidineColour = new JRadioButtonMenuItem();
123
124   protected JRadioButtonMenuItem RNAInteractionColour = new JRadioButtonMenuItem();
125
126   JRadioButtonMenuItem noColourmenuItem = new JRadioButtonMenuItem();
127
128   protected JCheckBoxMenuItem conservationMenuItem = new JCheckBoxMenuItem();
129
130   AlignmentPanel ap;
131
132   JMenu sequenceMenu = new JMenu();
133
134   JMenuItem sequenceName = new JMenuItem();
135
136   JMenuItem sequenceDetails = new JMenuItem();
137
138   JMenuItem sequenceSelDetails = new JMenuItem();
139
140   JMenuItem makeReferenceSeq = new JMenuItem();
141
142   JMenuItem chooseAnnotations = new JMenuItem();
143
144   SequenceI sequence;
145
146   JMenuItem createGroupMenuItem = new JMenuItem();
147
148   JMenuItem unGroupMenuItem = new JMenuItem();
149
150   JMenuItem outline = new JMenuItem();
151
152   JRadioButtonMenuItem nucleotideMenuItem = new JRadioButtonMenuItem();
153
154   JMenu colourMenu = new JMenu();
155
156   JCheckBoxMenuItem showBoxes = new JCheckBoxMenuItem();
157
158   JCheckBoxMenuItem showText = new JCheckBoxMenuItem();
159
160   JCheckBoxMenuItem showColourText = new JCheckBoxMenuItem();
161
162   JCheckBoxMenuItem displayNonconserved = new JCheckBoxMenuItem();
163
164   JMenu editMenu = new JMenu();
165
166   JMenuItem cut = new JMenuItem();
167
168   JMenuItem copy = new JMenuItem();
169
170   JMenuItem upperCase = new JMenuItem();
171
172   JMenuItem lowerCase = new JMenuItem();
173
174   JMenuItem toggle = new JMenuItem();
175
176   JMenu pdbMenu = new JMenu();
177
178   JMenu outputMenu = new JMenu();
179
180   JMenu seqShowAnnotationsMenu = new JMenu();
181
182   JMenu seqHideAnnotationsMenu = new JMenu();
183
184   JMenuItem seqAddReferenceAnnotations = new JMenuItem(
185           MessageManager.getString("label.add_reference_annotations"));
186
187   JMenu groupShowAnnotationsMenu = new JMenu();
188
189   JMenu groupHideAnnotationsMenu = new JMenu();
190
191   JMenuItem groupAddReferenceAnnotations = new JMenuItem(
192           MessageManager.getString("label.add_reference_annotations"));
193
194   JMenuItem sequenceFeature = new JMenuItem();
195
196   JMenuItem textColour = new JMenuItem();
197
198   JMenu jMenu1 = new JMenu();
199
200   JMenuItem pdbStructureDialog = new JMenuItem();
201
202   JMenu rnaStructureMenu = new JMenu();
203
204   JMenuItem editSequence = new JMenuItem();
205
206   JMenu groupLinksMenu;
207
208   JMenuItem hideInsertions = new JMenuItem();
209
210   /**
211    * Creates a new PopupMenu object.
212    * 
213    * @param ap
214    *          DOCUMENT ME!
215    * @param seq
216    *          DOCUMENT ME!
217    */
218   public PopupMenu(final AlignmentPanel ap, Sequence seq, List<String> links)
219   {
220     this(ap, seq, links, null);
221   }
222
223   /**
224    * 
225    * @param ap
226    * @param seq
227    * @param links
228    * @param groupLinks
229    */
230   public PopupMenu(final AlignmentPanel ap, final SequenceI seq,
231           List<String> links, List<String> groupLinks)
232   {
233     // /////////////////////////////////////////////////////////
234     // If this is activated from the sequence panel, the user may want to
235     // edit or annotate a particular residue. Therefore display the residue menu
236     //
237     // If from the IDPanel, we must display the sequence menu
238     // ////////////////////////////////////////////////////////
239     this.ap = ap;
240     sequence = seq;
241
242     ButtonGroup colours = new ButtonGroup();
243     colours.add(noColourmenuItem);
244     colours.add(clustalColour);
245     colours.add(zappoColour);
246     colours.add(taylorColour);
247     colours.add(hydrophobicityColour);
248     colours.add(helixColour);
249     colours.add(strandColour);
250     colours.add(turnColour);
251     colours.add(buriedColour);
252     colours.add(abovePIDColour);
253     colours.add(userDefinedColour);
254     colours.add(PIDColour);
255     colours.add(BLOSUM62Colour);
256     colours.add(purinePyrimidineColour);
257     colours.add(RNAInteractionColour);
258
259     for (String ff : FileFormat.getWritableFormats(true))
260     {
261       JMenuItem item = new JMenuItem(ff);
262
263       item.addActionListener(new java.awt.event.ActionListener()
264       {
265         @Override
266         public void actionPerformed(ActionEvent e)
267         {
268           outputText_actionPerformed(e);
269         }
270       });
271
272       outputMenu.add(item);
273     }
274
275     /*
276      * Build menus for annotation types that may be shown or hidden, and for
277      * 'reference annotations' that may be added to the alignment. First for the
278      * currently selected sequence (if there is one):
279      */
280     final List<SequenceI> selectedSequence = (seq == null ? Collections
281             .<SequenceI> emptyList() : Arrays.asList(seq));
282     buildAnnotationTypesMenus(seqShowAnnotationsMenu,
283             seqHideAnnotationsMenu, selectedSequence);
284     configureReferenceAnnotationsMenu(seqAddReferenceAnnotations,
285             selectedSequence);
286
287     /*
288      * And repeat for the current selection group (if there is one):
289      */
290     final List<SequenceI> selectedGroup = (ap.av.getSelectionGroup() == null ? Collections
291             .<SequenceI> emptyList() : ap.av.getSelectionGroup()
292             .getSequences());
293     buildAnnotationTypesMenus(groupShowAnnotationsMenu,
294             groupHideAnnotationsMenu, selectedGroup);
295     configureReferenceAnnotationsMenu(groupAddReferenceAnnotations,
296             selectedGroup);
297
298     try
299     {
300       jbInit();
301     } catch (Exception e)
302     {
303       e.printStackTrace();
304     }
305
306     JMenuItem menuItem;
307     if (seq != null)
308     {
309       sequenceMenu.setText(sequence.getName());
310       if (seq == ap.av.getAlignment().getSeqrep())
311       {
312         makeReferenceSeq.setText(MessageManager
313                 .getString("action.unmark_as_reference"));
314       }
315       else
316       {
317         makeReferenceSeq.setText(MessageManager
318                 .getString("action.set_as_reference"));
319       }
320
321       if (!ap.av.getAlignment().isNucleotide())
322       {
323         remove(rnaStructureMenu);
324       }
325       else
326       {
327         int origCount = rnaStructureMenu.getItemCount();
328         /*
329          * add menu items to 2D-render any alignment or sequence secondary
330          * structure annotation
331          */
332         AlignmentAnnotation[] aas = ap.av.getAlignment()
333                 .getAlignmentAnnotation();
334         if (aas != null)
335         {
336           for (final AlignmentAnnotation aa : aas)
337           {
338             if (aa.isValidStruc() && aa.sequenceRef == null)
339             {
340               /*
341                * valid alignment RNA secondary structure annotation
342                */
343               menuItem = new JMenuItem();
344               menuItem.setText(MessageManager.formatMessage(
345                       "label.2d_rna_structure_line",
346                       new Object[] { aa.label }));
347               menuItem.addActionListener(new java.awt.event.ActionListener()
348               {
349                 @Override
350                 public void actionPerformed(ActionEvent e)
351                 {
352                   new AppVarna(seq, aa, ap);
353                 }
354               });
355               rnaStructureMenu.add(menuItem);
356             }
357           }
358         }
359
360         if (seq.getAnnotation() != null)
361         {
362           AlignmentAnnotation seqAnns[] = seq.getAnnotation();
363           for (final AlignmentAnnotation aa : seqAnns)
364           {
365             if (aa.isValidStruc())
366             {
367               /*
368                * valid sequence RNA secondary structure annotation
369                */
370               // TODO: make rnastrucF a bit more nice
371               menuItem = new JMenuItem();
372               menuItem.setText(MessageManager.formatMessage(
373                       "label.2d_rna_sequence_name",
374                       new Object[] { seq.getName() }));
375               menuItem.addActionListener(new java.awt.event.ActionListener()
376               {
377                 @Override
378                 public void actionPerformed(ActionEvent e)
379                 {
380                   // TODO: VARNA does'nt print gaps in the sequence
381                   new AppVarna(seq, aa, ap);
382                 }
383               });
384               rnaStructureMenu.add(menuItem);
385             }
386           }
387         }
388         if (rnaStructureMenu.getItemCount() == origCount)
389         {
390           remove(rnaStructureMenu);
391         }
392       }
393
394       menuItem = new JMenuItem(
395               MessageManager.getString("action.hide_sequences"));
396       menuItem.addActionListener(new java.awt.event.ActionListener()
397       {
398         @Override
399         public void actionPerformed(ActionEvent e)
400         {
401           hideSequences(false);
402         }
403       });
404       add(menuItem);
405
406       if (ap.av.getSelectionGroup() != null
407               && ap.av.getSelectionGroup().getSize() > 1)
408       {
409         menuItem = new JMenuItem(MessageManager.formatMessage(
410                 "label.represent_group_with",
411                 new Object[] { seq.getName() }));
412         menuItem.addActionListener(new java.awt.event.ActionListener()
413         {
414           @Override
415           public void actionPerformed(ActionEvent e)
416           {
417             hideSequences(true);
418           }
419         });
420         sequenceMenu.add(menuItem);
421       }
422
423       if (ap.av.hasHiddenRows())
424       {
425         final int index = ap.av.getAlignment().findIndex(seq);
426
427         if (ap.av.adjustForHiddenSeqs(index)
428                 - ap.av.adjustForHiddenSeqs(index - 1) > 1)
429         {
430           menuItem = new JMenuItem(
431                   MessageManager.getString("action.reveal_sequences"));
432           menuItem.addActionListener(new ActionListener()
433           {
434             @Override
435             public void actionPerformed(ActionEvent e)
436             {
437               ap.av.showSequence(index);
438               if (ap.overviewPanel != null)
439               {
440                 ap.overviewPanel.updateOverviewImage();
441               }
442             }
443           });
444           add(menuItem);
445         }
446       }
447     }
448     // for the case when no sequences are even visible
449     if (ap.av.hasHiddenRows())
450     {
451       {
452         menuItem = new JMenuItem(
453                 MessageManager.getString("action.reveal_all"));
454         menuItem.addActionListener(new ActionListener()
455         {
456           @Override
457           public void actionPerformed(ActionEvent e)
458           {
459             ap.av.showAllHiddenSeqs();
460             if (ap.overviewPanel != null)
461             {
462               ap.overviewPanel.updateOverviewImage();
463             }
464           }
465         });
466
467         add(menuItem);
468       }
469
470     }
471
472     SequenceGroup sg = ap.av.getSelectionGroup();
473     boolean isDefinedGroup = (sg != null) ? ap.av.getAlignment()
474             .getGroups().contains(sg) : false;
475
476     if (sg != null && sg.getSize() > 0)
477     {
478       groupName.setText(MessageManager
479               .getString("label.edit_name_and_description_current_group"));
480
481       if (sg.cs instanceof ZappoColourScheme)
482       {
483         zappoColour.setSelected(true);
484       }
485       else if (sg.cs instanceof TaylorColourScheme)
486       {
487         taylorColour.setSelected(true);
488       }
489       else if (sg.cs instanceof PIDColourScheme)
490       {
491         PIDColour.setSelected(true);
492       }
493       else if (sg.cs instanceof Blosum62ColourScheme)
494       {
495         BLOSUM62Colour.setSelected(true);
496       }
497       else if (sg.cs instanceof UserColourScheme)
498       {
499         userDefinedColour.setSelected(true);
500       }
501       else if (sg.cs instanceof HydrophobicColourScheme)
502       {
503         hydrophobicityColour.setSelected(true);
504       }
505       else if (sg.cs instanceof HelixColourScheme)
506       {
507         helixColour.setSelected(true);
508       }
509       else if (sg.cs instanceof StrandColourScheme)
510       {
511         strandColour.setSelected(true);
512       }
513       else if (sg.cs instanceof TurnColourScheme)
514       {
515         turnColour.setSelected(true);
516       }
517       else if (sg.cs instanceof BuriedColourScheme)
518       {
519         buriedColour.setSelected(true);
520       }
521       else if (sg.cs instanceof ClustalxColourScheme)
522       {
523         clustalColour.setSelected(true);
524       }
525       else if (sg.cs instanceof PurinePyrimidineColourScheme)
526       {
527         purinePyrimidineColour.setSelected(true);
528       }
529
530       /*
531        * else if (sg.cs instanceof CovariationColourScheme) {
532        * covariationColour.setSelected(true); }
533        */
534       else
535       {
536         noColourmenuItem.setSelected(true);
537       }
538
539       if (sg.cs != null && sg.cs.conservationApplied())
540       {
541         conservationMenuItem.setSelected(true);
542       }
543       displayNonconserved.setSelected(sg.getShowNonconserved());
544       showText.setSelected(sg.getDisplayText());
545       showColourText.setSelected(sg.getColourText());
546       showBoxes.setSelected(sg.getDisplayBoxes());
547       // add any groupURLs to the groupURL submenu and make it visible
548       if (groupLinks != null && groupLinks.size() > 0)
549       {
550         buildGroupURLMenu(sg, groupLinks);
551       }
552       // Add a 'show all structures' for the current selection
553       Hashtable<String, PDBEntry> pdbe = new Hashtable<String, PDBEntry>(), reppdb = new Hashtable<String, PDBEntry>();
554       SequenceI sqass = null;
555       for (SequenceI sq : ap.av.getSequenceSelection())
556       {
557         Vector<PDBEntry> pes = sq.getDatasetSequence().getAllPDBEntries();
558         if (pes != null && pes.size() > 0)
559         {
560           reppdb.put(pes.get(0).getId(), pes.get(0));
561           for (PDBEntry pe : pes)
562           {
563             pdbe.put(pe.getId(), pe);
564             if (sqass == null)
565             {
566               sqass = sq;
567             }
568           }
569         }
570       }
571       if (pdbe.size() > 0)
572       {
573         final PDBEntry[] pe = pdbe.values().toArray(
574                 new PDBEntry[pdbe.size()]), pr = reppdb.values().toArray(
575                 new PDBEntry[reppdb.size()]);
576         final JMenuItem gpdbview, rpdbview;
577       }
578     }
579     else
580     {
581       groupMenu.setVisible(false);
582       editMenu.setVisible(false);
583     }
584
585     if (!isDefinedGroup)
586     {
587       createGroupMenuItem.setVisible(true);
588       unGroupMenuItem.setVisible(false);
589       jMenu1.setText(MessageManager.getString("action.edit_new_group"));
590     }
591     else
592     {
593       createGroupMenuItem.setVisible(false);
594       unGroupMenuItem.setVisible(true);
595       jMenu1.setText(MessageManager.getString("action.edit_group"));
596     }
597
598     if (seq == null)
599     {
600       sequenceMenu.setVisible(false);
601       pdbStructureDialog.setVisible(false);
602       rnaStructureMenu.setVisible(false);
603     }
604
605     if (links != null && links.size() > 0)
606     {
607       addFeatureLinks(seq, links);
608     }
609   }
610
611   /**
612    * Adds a 'Link' menu item with a sub-menu item for each hyperlink provided.
613    * 
614    * @param seq
615    * @param links
616    */
617   void addFeatureLinks(final SequenceI seq, List<String> links)
618   {
619     JMenu linkMenu = new JMenu(MessageManager.getString("action.link"));
620     Map<String, List<String>> linkset = new LinkedHashMap<String, List<String>>();
621
622     for (String link : links)
623     {
624       UrlLink urlLink = null;
625       try
626       {
627         urlLink = new UrlLink(link);
628       } catch (Exception foo)
629       {
630         Cache.log.error("Exception for URLLink '" + link + "'", foo);
631         continue;
632       }
633
634       if (!urlLink.isValid())
635       {
636         Cache.log.error(urlLink.getInvalidMessage());
637         continue;
638       }
639
640       urlLink.createLinksFromSeq(seq, linkset);
641     }
642
643     addshowLinks(linkMenu, linkset.values());
644
645     // disable link menu if there are no valid entries
646     if (linkMenu.getItemCount() > 0)
647     {
648       linkMenu.setEnabled(true);
649     }
650     else
651     {
652       linkMenu.setEnabled(false);
653     }
654
655     if (sequence != null)
656     {
657       sequenceMenu.add(linkMenu);
658     }
659     else
660     {
661       add(linkMenu);
662     }
663
664   }
665
666
667
668   /**
669    * Add annotation types to 'Show annotations' and/or 'Hide annotations' menus.
670    * "All" is added first, followed by a separator. Then add any annotation
671    * types associated with the current selection. Separate menus are built for
672    * the selected sequence group (if any), and the selected sequence.
673    * <p>
674    * Some annotation rows are always rendered together - these can be identified
675    * by a common graphGroup property > -1. Only one of each group will be marked
676    * as visible (to avoid duplication of the display). For such groups we add a
677    * composite type name, e.g.
678    * <p>
679    * IUPredWS (Long), IUPredWS (Short)
680    * 
681    * @param seq
682    */
683   protected void buildAnnotationTypesMenus(JMenu showMenu, JMenu hideMenu,
684           List<SequenceI> forSequences)
685   {
686     showMenu.removeAll();
687     hideMenu.removeAll();
688
689     final List<String> all = Arrays.asList(new String[] { MessageManager
690             .getString("label.all") });
691     addAnnotationTypeToShowHide(showMenu, forSequences, "", all, true, true);
692     addAnnotationTypeToShowHide(hideMenu, forSequences, "", all, true,
693             false);
694     showMenu.addSeparator();
695     hideMenu.addSeparator();
696
697     final AlignmentAnnotation[] annotations = ap.getAlignment()
698             .getAlignmentAnnotation();
699
700     /*
701      * Find shown/hidden annotations types, distinguished by source (calcId),
702      * and grouped by graphGroup. Using LinkedHashMap means we will retrieve in
703      * the insertion order, which is the order of the annotations on the
704      * alignment.
705      */
706     Map<String, List<List<String>>> shownTypes = new LinkedHashMap<String, List<List<String>>>();
707     Map<String, List<List<String>>> hiddenTypes = new LinkedHashMap<String, List<List<String>>>();
708     AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes, hiddenTypes,
709             AlignmentAnnotationUtils.asList(annotations), forSequences);
710
711     for (String calcId : hiddenTypes.keySet())
712     {
713       for (List<String> type : hiddenTypes.get(calcId))
714       {
715         addAnnotationTypeToShowHide(showMenu, forSequences, calcId, type,
716                 false, true);
717       }
718     }
719     // grey out 'show annotations' if none are hidden
720     showMenu.setEnabled(!hiddenTypes.isEmpty());
721
722     for (String calcId : shownTypes.keySet())
723     {
724       for (List<String> type : shownTypes.get(calcId))
725       {
726         addAnnotationTypeToShowHide(hideMenu, forSequences, calcId, type,
727                 false, false);
728       }
729     }
730     // grey out 'hide annotations' if none are shown
731     hideMenu.setEnabled(!shownTypes.isEmpty());
732   }
733
734   /**
735    * Returns a list of sequences - either the current selection group (if there
736    * is one), else the specified single sequence.
737    * 
738    * @param seq
739    * @return
740    */
741   protected List<SequenceI> getSequenceScope(SequenceI seq)
742   {
743     List<SequenceI> forSequences = null;
744     final SequenceGroup selectionGroup = ap.av.getSelectionGroup();
745     if (selectionGroup != null && selectionGroup.getSize() > 0)
746     {
747       forSequences = selectionGroup.getSequences();
748     }
749     else
750     {
751       forSequences = seq == null ? Collections.<SequenceI> emptyList()
752               : Arrays.asList(seq);
753     }
754     return forSequences;
755   }
756
757   /**
758    * Add one annotation type to the 'Show Annotations' or 'Hide Annotations'
759    * menus.
760    * 
761    * @param showOrHideMenu
762    *          the menu to add to
763    * @param forSequences
764    *          the sequences whose annotations may be shown or hidden
765    * @param calcId
766    * @param types
767    *          the label to add
768    * @param allTypes
769    *          if true this is a special label meaning 'All'
770    * @param actionIsShow
771    *          if true, the select menu item action is to show the annotation
772    *          type, else hide
773    */
774   protected void addAnnotationTypeToShowHide(JMenu showOrHideMenu,
775           final List<SequenceI> forSequences, String calcId,
776           final List<String> types, final boolean allTypes,
777           final boolean actionIsShow)
778   {
779     String label = types.toString(); // [a, b, c]
780     label = label.substring(1, label.length() - 1); // a, b, c
781     final JMenuItem item = new JMenuItem(label);
782     item.setToolTipText(calcId);
783     item.addActionListener(new java.awt.event.ActionListener()
784     {
785       @Override
786       public void actionPerformed(ActionEvent e)
787       {
788         AlignmentUtils.showOrHideSequenceAnnotations(ap.getAlignment(),
789                 types, forSequences, allTypes, actionIsShow);
790         refresh();
791       }
792     });
793     showOrHideMenu.add(item);
794   }
795
796   private void buildGroupURLMenu(SequenceGroup sg, List<String> groupLinks)
797   {
798
799     // TODO: usability: thread off the generation of group url content so root
800     // menu appears asap
801     // sequence only URLs
802     // ID/regex match URLs
803     groupLinksMenu = new JMenu(
804             MessageManager.getString("action.group_link"));
805     // three types of url that might be created.
806     JMenu[] linkMenus = new JMenu[] { null,
807         new JMenu(MessageManager.getString("action.ids")),
808         new JMenu(MessageManager.getString("action.sequences")),
809         new JMenu(MessageManager.getString("action.ids_sequences")) };
810
811     SequenceI[] seqs = ap.av.getSelectionAsNewSequence();
812     String[][] idandseqs = GroupUrlLink.formStrings(seqs);
813     Hashtable<String, Object[]> commonDbrefs = new Hashtable<String, Object[]>();
814     for (int sq = 0; sq < seqs.length; sq++)
815     {
816
817       int start = seqs[sq].findPosition(sg.getStartRes()), end = seqs[sq]
818               .findPosition(sg.getEndRes());
819       // just collect ids from dataset sequence
820       // TODO: check if IDs collected from selecton group intersects with the
821       // current selection, too
822       SequenceI sqi = seqs[sq];
823       while (sqi.getDatasetSequence() != null)
824       {
825         sqi = sqi.getDatasetSequence();
826       }
827       DBRefEntry[] dbr = sqi.getDBRefs();
828       if (dbr != null && dbr.length > 0)
829       {
830         for (int d = 0; d < dbr.length; d++)
831         {
832           String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
833           Object[] sarray = commonDbrefs.get(src);
834           if (sarray == null)
835           {
836             sarray = new Object[2];
837             sarray[0] = new int[] { 0 };
838             sarray[1] = new String[seqs.length];
839
840             commonDbrefs.put(src, sarray);
841           }
842
843           if (((String[]) sarray[1])[sq] == null)
844           {
845             if (!dbr[d].hasMap()
846                     || (dbr[d].getMap().locateMappedRange(start, end) != null))
847             {
848               ((String[]) sarray[1])[sq] = dbr[d].getAccessionId();
849               ((int[]) sarray[0])[0]++;
850             }
851           }
852         }
853       }
854     }
855     // now create group links for all distinct ID/sequence sets.
856     boolean addMenu = false; // indicates if there are any group links to give
857                              // to user
858     for (String link : groupLinks)
859     {
860       GroupUrlLink urlLink = null;
861       try
862       {
863         urlLink = new GroupUrlLink(link);
864       } catch (Exception foo)
865       {
866         Cache.log.error("Exception for GroupURLLink '" + link + "'", foo);
867         continue;
868       }
869       ;
870       if (!urlLink.isValid())
871       {
872         Cache.log.error(urlLink.getInvalidMessage());
873         continue;
874       }
875       final String label = urlLink.getLabel();
876       boolean usingNames = false;
877       // Now see which parts of the group apply for this URL
878       String ltarget = urlLink.getTarget(); // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
879       Object[] idset = commonDbrefs.get(ltarget.toUpperCase());
880       String[] seqstr, ids; // input to makeUrl
881       if (idset != null)
882       {
883         int numinput = ((int[]) idset[0])[0];
884         String[] allids = ((String[]) idset[1]);
885         seqstr = new String[numinput];
886         ids = new String[numinput];
887         for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
888         {
889           if (allids[sq] != null)
890           {
891             ids[idcount] = allids[sq];
892             seqstr[idcount++] = idandseqs[1][sq];
893           }
894         }
895       }
896       else
897       {
898         // just use the id/seq set
899         seqstr = idandseqs[1];
900         ids = idandseqs[0];
901         usingNames = true;
902       }
903       // and try and make the groupURL!
904
905       Object[] urlset = null;
906       try
907       {
908         urlset = urlLink.makeUrlStubs(ids, seqstr,
909                 "FromJalview" + System.currentTimeMillis(), false);
910       } catch (UrlStringTooLongException e)
911       {
912       }
913       if (urlset != null)
914       {
915         int type = urlLink.getGroupURLType() & 3;
916         // first two bits ofurlLink type bitfield are sequenceids and sequences
917         // TODO: FUTURE: ensure the groupURL menu structure can be generalised
918         addshowLink(linkMenus[type], label
919                 + (((type & 1) == 1) ? ("("
920                         + (usingNames ? "Names" : ltarget) + ")") : ""),
921                 urlLink, urlset);
922         addMenu = true;
923       }
924     }
925     if (addMenu)
926     {
927       groupLinksMenu = new JMenu(
928               MessageManager.getString("action.group_link"));
929       for (int m = 0; m < linkMenus.length; m++)
930       {
931         if (linkMenus[m] != null
932                 && linkMenus[m].getMenuComponentCount() > 0)
933         {
934           groupLinksMenu.add(linkMenus[m]);
935         }
936       }
937
938       groupMenu.add(groupLinksMenu);
939     }
940   }
941
942   private void addshowLinks(JMenu linkMenu, Collection<List<String>> linkset)
943   {
944     for (List<String> linkstrset : linkset)
945     {
946       // split linkstr into label and url
947       addshowLink(linkMenu, linkstrset.get(1), linkstrset.get(3));
948     }
949   }
950
951   /**
952    * add a show URL menu item to the given linkMenu
953    * 
954    * @param linkMenu
955    * @param label
956    *          - menu label string
957    * @param url
958    *          - url to open
959    */
960   private void addshowLink(JMenu linkMenu, String label, final String url)
961   {
962     JMenuItem item = new JMenuItem(label);
963     item.setToolTipText(MessageManager.formatMessage(
964             "label.open_url_param", new Object[] { url }));
965     item.addActionListener(new java.awt.event.ActionListener()
966     {
967       @Override
968       public void actionPerformed(ActionEvent e)
969       {
970         new Thread(new Runnable()
971         {
972
973           @Override
974           public void run()
975           {
976             showLink(url);
977           }
978
979         }).start();
980       }
981     });
982
983     linkMenu.add(item);
984   }
985
986   /**
987    * add a late bound groupURL item to the given linkMenu
988    * 
989    * @param linkMenu
990    * @param label
991    *          - menu label string
992    * @param urlgenerator
993    *          GroupURLLink used to generate URL
994    * @param urlstub
995    *          Object array returned from the makeUrlStubs function.
996    */
997   private void addshowLink(JMenu linkMenu, String label,
998           final GroupUrlLink urlgenerator, final Object[] urlstub)
999   {
1000     JMenuItem item = new JMenuItem(label);
1001     item.setToolTipText(MessageManager.formatMessage(
1002             "label.open_url_seqs_param",
1003             new Object[] { urlgenerator.getUrl_prefix(),
1004                 urlgenerator.getNumberInvolved(urlstub) }));
1005     // TODO: put in info about what is being sent.
1006     item.addActionListener(new ActionListener()
1007     {
1008       @Override
1009       public void actionPerformed(ActionEvent e)
1010       {
1011         new Thread(new Runnable()
1012         {
1013
1014           @Override
1015           public void run()
1016           {
1017             try
1018             {
1019               showLink(urlgenerator.constructFrom(urlstub));
1020             } catch (UrlStringTooLongException e2)
1021             {
1022             }
1023           }
1024
1025         }).start();
1026       }
1027     });
1028
1029     linkMenu.add(item);
1030   }
1031
1032   /**
1033    * DOCUMENT ME!
1034    * 
1035    * @throws Exception
1036    *           DOCUMENT ME!
1037    */
1038   private void jbInit() throws Exception
1039   {
1040     groupMenu.setText(MessageManager.getString("label.selection"));
1041     groupName.setText(MessageManager.getString("label.name"));
1042     groupName.addActionListener(new java.awt.event.ActionListener()
1043     {
1044       @Override
1045       public void actionPerformed(ActionEvent e)
1046       {
1047         groupName_actionPerformed();
1048       }
1049     });
1050     sequenceMenu.setText(MessageManager.getString("label.sequence"));
1051     sequenceName.setText(MessageManager
1052             .getString("label.edit_name_description"));
1053     sequenceName.addActionListener(new java.awt.event.ActionListener()
1054     {
1055       @Override
1056       public void actionPerformed(ActionEvent e)
1057       {
1058         sequenceName_actionPerformed();
1059       }
1060     });
1061     chooseAnnotations.setText(MessageManager
1062             .getString("action.choose_annotations"));
1063     chooseAnnotations.addActionListener(new java.awt.event.ActionListener()
1064     {
1065       @Override
1066       public void actionPerformed(ActionEvent e)
1067       {
1068         chooseAnnotations_actionPerformed(e);
1069       }
1070     });
1071     sequenceDetails.setText(MessageManager
1072             .getString("label.sequence_details"));
1073     sequenceDetails.addActionListener(new java.awt.event.ActionListener()
1074     {
1075       @Override
1076       public void actionPerformed(ActionEvent e)
1077       {
1078         sequenceDetails_actionPerformed();
1079       }
1080     });
1081     sequenceSelDetails.setText(MessageManager
1082             .getString("label.sequence_details"));
1083     sequenceSelDetails
1084             .addActionListener(new java.awt.event.ActionListener()
1085             {
1086               @Override
1087               public void actionPerformed(ActionEvent e)
1088               {
1089                 sequenceSelectionDetails_actionPerformed();
1090               }
1091             });
1092     PIDColour.setFocusPainted(false);
1093     unGroupMenuItem
1094             .setText(MessageManager.getString("action.remove_group"));
1095     unGroupMenuItem.addActionListener(new java.awt.event.ActionListener()
1096     {
1097       @Override
1098       public void actionPerformed(ActionEvent e)
1099       {
1100         unGroupMenuItem_actionPerformed();
1101       }
1102     });
1103     createGroupMenuItem.setText(MessageManager
1104             .getString("action.create_group"));
1105     createGroupMenuItem
1106             .addActionListener(new java.awt.event.ActionListener()
1107             {
1108               @Override
1109               public void actionPerformed(ActionEvent e)
1110               {
1111                 createGroupMenuItem_actionPerformed();
1112               }
1113             });
1114
1115     outline.setText(MessageManager.getString("action.border_colour"));
1116     outline.addActionListener(new java.awt.event.ActionListener()
1117     {
1118       @Override
1119       public void actionPerformed(ActionEvent e)
1120       {
1121         outline_actionPerformed();
1122       }
1123     });
1124     nucleotideMenuItem
1125             .setText(MessageManager.getString("label.nucleotide"));
1126     nucleotideMenuItem.addActionListener(new ActionListener()
1127     {
1128       @Override
1129       public void actionPerformed(ActionEvent e)
1130       {
1131         nucleotideMenuItem_actionPerformed();
1132       }
1133     });
1134     colourMenu.setText(MessageManager.getString("label.group_colour"));
1135     showBoxes.setText(MessageManager.getString("action.boxes"));
1136     showBoxes.setState(true);
1137     showBoxes.addActionListener(new ActionListener()
1138     {
1139       @Override
1140       public void actionPerformed(ActionEvent e)
1141       {
1142         showBoxes_actionPerformed();
1143       }
1144     });
1145     showText.setText(MessageManager.getString("action.text"));
1146     showText.setState(true);
1147     showText.addActionListener(new ActionListener()
1148     {
1149       @Override
1150       public void actionPerformed(ActionEvent e)
1151       {
1152         showText_actionPerformed();
1153       }
1154     });
1155     showColourText.setText(MessageManager.getString("label.colour_text"));
1156     showColourText.addActionListener(new ActionListener()
1157     {
1158       @Override
1159       public void actionPerformed(ActionEvent e)
1160       {
1161         showColourText_actionPerformed();
1162       }
1163     });
1164     displayNonconserved.setText(MessageManager
1165             .getString("label.show_non_conversed"));
1166     displayNonconserved.setState(true);
1167     displayNonconserved.addActionListener(new ActionListener()
1168     {
1169       @Override
1170       public void actionPerformed(ActionEvent e)
1171       {
1172         showNonconserved_actionPerformed();
1173       }
1174     });
1175     editMenu.setText(MessageManager.getString("action.edit"));
1176     cut.setText(MessageManager.getString("action.cut"));
1177     cut.addActionListener(new ActionListener()
1178     {
1179       @Override
1180       public void actionPerformed(ActionEvent e)
1181       {
1182         cut_actionPerformed();
1183       }
1184     });
1185     upperCase.setText(MessageManager.getString("label.to_upper_case"));
1186     upperCase.addActionListener(new ActionListener()
1187     {
1188       @Override
1189       public void actionPerformed(ActionEvent e)
1190       {
1191         changeCase(e);
1192       }
1193     });
1194     copy.setText(MessageManager.getString("action.copy"));
1195     copy.addActionListener(new ActionListener()
1196     {
1197       @Override
1198       public void actionPerformed(ActionEvent e)
1199       {
1200         copy_actionPerformed();
1201       }
1202     });
1203     lowerCase.setText(MessageManager.getString("label.to_lower_case"));
1204     lowerCase.addActionListener(new ActionListener()
1205     {
1206       @Override
1207       public void actionPerformed(ActionEvent e)
1208       {
1209         changeCase(e);
1210       }
1211     });
1212     toggle.setText(MessageManager.getString("label.toggle_case"));
1213     toggle.addActionListener(new ActionListener()
1214     {
1215       @Override
1216       public void actionPerformed(ActionEvent e)
1217       {
1218         changeCase(e);
1219       }
1220     });
1221     outputMenu.setText(MessageManager.getString("label.out_to_textbox")
1222             + "...");
1223     seqShowAnnotationsMenu.setText(MessageManager
1224             .getString("label.show_annotations"));
1225     seqHideAnnotationsMenu.setText(MessageManager
1226             .getString("label.hide_annotations"));
1227     groupShowAnnotationsMenu.setText(MessageManager
1228             .getString("label.show_annotations"));
1229     groupHideAnnotationsMenu.setText(MessageManager
1230             .getString("label.hide_annotations"));
1231     sequenceFeature.setText(MessageManager
1232             .getString("label.create_sequence_feature"));
1233     sequenceFeature.addActionListener(new ActionListener()
1234     {
1235       @Override
1236       public void actionPerformed(ActionEvent e)
1237       {
1238         sequenceFeature_actionPerformed();
1239       }
1240     });
1241     textColour.setText(MessageManager.getString("label.text_colour"));
1242     textColour.addActionListener(new ActionListener()
1243     {
1244       @Override
1245       public void actionPerformed(ActionEvent e)
1246       {
1247         textColour_actionPerformed();
1248       }
1249     });
1250     jMenu1.setText(MessageManager.getString("label.group"));
1251     pdbStructureDialog.setText(MessageManager
1252             .getString("label.show_pdbstruct_dialog"));
1253     pdbStructureDialog.addActionListener(new ActionListener()
1254     {
1255       @Override
1256       public void actionPerformed(ActionEvent actionEvent)
1257       {
1258         SequenceI[] selectedSeqs = new SequenceI[] { sequence };
1259         if (ap.av.getSelectionGroup() != null)
1260         {
1261           selectedSeqs = ap.av.getSequenceSelection();
1262         }
1263         new StructureChooser(selectedSeqs, sequence, ap);
1264       }
1265     });
1266
1267     rnaStructureMenu.setText(MessageManager
1268             .getString("label.view_rna_structure"));
1269
1270     // colStructureMenu.setText("Colour By Structure");
1271     editSequence.setText(MessageManager.getString("label.edit_sequence")
1272             + "...");
1273     editSequence.addActionListener(new ActionListener()
1274     {
1275       @Override
1276       public void actionPerformed(ActionEvent actionEvent)
1277       {
1278         editSequence_actionPerformed(actionEvent);
1279       }
1280     });
1281     makeReferenceSeq.setText(MessageManager
1282             .getString("label.mark_as_representative"));
1283     makeReferenceSeq.addActionListener(new ActionListener()
1284     {
1285
1286       @Override
1287       public void actionPerformed(ActionEvent actionEvent)
1288       {
1289         makeReferenceSeq_actionPerformed(actionEvent);
1290
1291       }
1292     });
1293     hideInsertions.setText(MessageManager
1294             .getString("label.hide_insertions"));
1295     hideInsertions.addActionListener(new ActionListener()
1296     {
1297
1298       @Override
1299       public void actionPerformed(ActionEvent e)
1300       {
1301         hideInsertions_actionPerformed(e);
1302       }
1303     });
1304     /*
1305      * annotationMenuItem.setText("By Annotation");
1306      * annotationMenuItem.addActionListener(new ActionListener() { public void
1307      * actionPerformed(ActionEvent actionEvent) {
1308      * annotationMenuItem_actionPerformed(actionEvent); } });
1309      */
1310     groupMenu.add(sequenceSelDetails);
1311     add(groupMenu);
1312     add(sequenceMenu);
1313     add(rnaStructureMenu);
1314     add(pdbStructureDialog);
1315     if (sequence != null)
1316     {
1317       add(hideInsertions);
1318     }
1319     // annotations configuration panel suppressed for now
1320     // groupMenu.add(chooseAnnotations);
1321
1322     /*
1323      * Add show/hide annotations to the Sequence menu, and to the Selection menu
1324      * (if a selection group is in force).
1325      */
1326     sequenceMenu.add(seqShowAnnotationsMenu);
1327     sequenceMenu.add(seqHideAnnotationsMenu);
1328     sequenceMenu.add(seqAddReferenceAnnotations);
1329     groupMenu.add(groupShowAnnotationsMenu);
1330     groupMenu.add(groupHideAnnotationsMenu);
1331     groupMenu.add(groupAddReferenceAnnotations);
1332     groupMenu.add(editMenu);
1333     groupMenu.add(outputMenu);
1334     groupMenu.add(sequenceFeature);
1335     groupMenu.add(createGroupMenuItem);
1336     groupMenu.add(unGroupMenuItem);
1337     groupMenu.add(jMenu1);
1338     sequenceMenu.add(sequenceName);
1339     sequenceMenu.add(sequenceDetails);
1340     sequenceMenu.add(makeReferenceSeq);
1341     colourMenu.add(textColour);
1342     colourMenu.add(noColourmenuItem);
1343     colourMenu.add(clustalColour);
1344     colourMenu.add(BLOSUM62Colour);
1345     colourMenu.add(PIDColour);
1346     colourMenu.add(zappoColour);
1347     colourMenu.add(taylorColour);
1348     colourMenu.add(hydrophobicityColour);
1349     colourMenu.add(helixColour);
1350     colourMenu.add(strandColour);
1351     colourMenu.add(turnColour);
1352     colourMenu.add(buriedColour);
1353     colourMenu.add(nucleotideMenuItem);
1354     if (ap.getAlignment().isNucleotide())
1355     {
1356       // JBPNote - commented since the colourscheme isn't functional
1357       colourMenu.add(purinePyrimidineColour);
1358     }
1359     colourMenu.add(userDefinedColour);
1360
1361     if (jalview.gui.UserDefinedColours.getUserColourSchemes() != null)
1362     {
1363       java.util.Enumeration userColours = jalview.gui.UserDefinedColours
1364               .getUserColourSchemes().keys();
1365
1366       while (userColours.hasMoreElements())
1367       {
1368         JMenuItem item = new JMenuItem(userColours.nextElement().toString());
1369         item.addActionListener(new ActionListener()
1370         {
1371           @Override
1372           public void actionPerformed(ActionEvent evt)
1373           {
1374             userDefinedColour_actionPerformed(evt);
1375           }
1376         });
1377         colourMenu.add(item);
1378       }
1379     }
1380
1381     colourMenu.addSeparator();
1382     colourMenu.add(abovePIDColour);
1383     colourMenu.add(conservationMenuItem);
1384     editMenu.add(copy);
1385     editMenu.add(cut);
1386     editMenu.add(editSequence);
1387     editMenu.add(upperCase);
1388     editMenu.add(lowerCase);
1389     editMenu.add(toggle);
1390     // JBPNote: These shouldn't be added here - should appear in a generic
1391     // 'apply web service to this sequence menu'
1392     // pdbMenu.add(RNAFold);
1393     // pdbMenu.add(ContraFold);
1394     jMenu1.add(groupName);
1395     jMenu1.add(colourMenu);
1396     jMenu1.add(showBoxes);
1397     jMenu1.add(showText);
1398     jMenu1.add(showColourText);
1399     jMenu1.add(outline);
1400     jMenu1.add(displayNonconserved);
1401     noColourmenuItem.setText(MessageManager.getString("label.none"));
1402     noColourmenuItem.addActionListener(new java.awt.event.ActionListener()
1403     {
1404       @Override
1405       public void actionPerformed(ActionEvent e)
1406       {
1407         noColourmenuItem_actionPerformed();
1408       }
1409     });
1410
1411     clustalColour.setText(MessageManager
1412             .getString("label.clustalx_colours"));
1413     clustalColour.addActionListener(new java.awt.event.ActionListener()
1414     {
1415       @Override
1416       public void actionPerformed(ActionEvent e)
1417       {
1418         clustalColour_actionPerformed();
1419       }
1420     });
1421     zappoColour.setText(MessageManager.getString("label.zappo"));
1422     zappoColour.addActionListener(new java.awt.event.ActionListener()
1423     {
1424       @Override
1425       public void actionPerformed(ActionEvent e)
1426       {
1427         zappoColour_actionPerformed();
1428       }
1429     });
1430     taylorColour.setText(MessageManager.getString("label.taylor"));
1431     taylorColour.addActionListener(new java.awt.event.ActionListener()
1432     {
1433       @Override
1434       public void actionPerformed(ActionEvent e)
1435       {
1436         taylorColour_actionPerformed();
1437       }
1438     });
1439     hydrophobicityColour.setText(MessageManager
1440             .getString("label.hydrophobicity"));
1441     hydrophobicityColour
1442             .addActionListener(new java.awt.event.ActionListener()
1443             {
1444               @Override
1445               public void actionPerformed(ActionEvent e)
1446               {
1447                 hydrophobicityColour_actionPerformed();
1448               }
1449             });
1450     helixColour.setText(MessageManager.getString("label.helix_propensity"));
1451     helixColour.addActionListener(new java.awt.event.ActionListener()
1452     {
1453       @Override
1454       public void actionPerformed(ActionEvent e)
1455       {
1456         helixColour_actionPerformed();
1457       }
1458     });
1459     strandColour.setText(MessageManager
1460             .getString("label.strand_propensity"));
1461     strandColour.addActionListener(new java.awt.event.ActionListener()
1462     {
1463       @Override
1464       public void actionPerformed(ActionEvent e)
1465       {
1466         strandColour_actionPerformed();
1467       }
1468     });
1469     turnColour.setText(MessageManager.getString("label.turn_propensity"));
1470     turnColour.addActionListener(new java.awt.event.ActionListener()
1471     {
1472       @Override
1473       public void actionPerformed(ActionEvent e)
1474       {
1475         turnColour_actionPerformed();
1476       }
1477     });
1478     buriedColour.setText(MessageManager.getString("label.buried_index"));
1479     buriedColour.addActionListener(new java.awt.event.ActionListener()
1480     {
1481       @Override
1482       public void actionPerformed(ActionEvent e)
1483       {
1484         buriedColour_actionPerformed();
1485       }
1486     });
1487     abovePIDColour.setText(MessageManager
1488             .getString("label.above_identity_percentage"));
1489     abovePIDColour.addActionListener(new java.awt.event.ActionListener()
1490     {
1491       @Override
1492       public void actionPerformed(ActionEvent e)
1493       {
1494         abovePIDColour_actionPerformed();
1495       }
1496     });
1497     userDefinedColour.setText(MessageManager
1498             .getString("action.user_defined"));
1499     userDefinedColour.addActionListener(new java.awt.event.ActionListener()
1500     {
1501       @Override
1502       public void actionPerformed(ActionEvent e)
1503       {
1504         userDefinedColour_actionPerformed(e);
1505       }
1506     });
1507     PIDColour
1508             .setText(MessageManager.getString("label.percentage_identity"));
1509     PIDColour.addActionListener(new java.awt.event.ActionListener()
1510     {
1511       @Override
1512       public void actionPerformed(ActionEvent e)
1513       {
1514         PIDColour_actionPerformed();
1515       }
1516     });
1517     BLOSUM62Colour.setText(MessageManager.getString("label.blosum62"));
1518     BLOSUM62Colour.addActionListener(new java.awt.event.ActionListener()
1519     {
1520       @Override
1521       public void actionPerformed(ActionEvent e)
1522       {
1523         BLOSUM62Colour_actionPerformed();
1524       }
1525     });
1526     purinePyrimidineColour.setText(MessageManager
1527             .getString("label.purine_pyrimidine"));
1528     purinePyrimidineColour
1529             .addActionListener(new java.awt.event.ActionListener()
1530             {
1531               @Override
1532               public void actionPerformed(ActionEvent e)
1533               {
1534                 purinePyrimidineColour_actionPerformed();
1535               }
1536             });
1537
1538     /*
1539      * covariationColour.addActionListener(new java.awt.event.ActionListener() {
1540      * public void actionPerformed(ActionEvent e) {
1541      * covariationColour_actionPerformed(); } });
1542      */
1543
1544     conservationMenuItem.setText(MessageManager
1545             .getString("label.conservation"));
1546     conservationMenuItem
1547             .addActionListener(new java.awt.event.ActionListener()
1548             {
1549               @Override
1550               public void actionPerformed(ActionEvent e)
1551               {
1552                 conservationMenuItem_actionPerformed();
1553               }
1554             });
1555   }
1556
1557   /**
1558    * Check for any annotations on the underlying dataset sequences (for the
1559    * current selection group) which are not 'on the alignment'.If any are found,
1560    * enable the option to add them to the alignment. The criteria for 'on the
1561    * alignment' is finding an alignment annotation on the alignment, matched on
1562    * calcId, label and sequenceRef.
1563    * 
1564    * A tooltip is also constructed that displays the source (calcId) and type
1565    * (label) of the annotations that can be added.
1566    * 
1567    * @param menuItem
1568    * @param forSequences
1569    */
1570   protected void configureReferenceAnnotationsMenu(JMenuItem menuItem,
1571           List<SequenceI> forSequences)
1572   {
1573     menuItem.setEnabled(false);
1574
1575     /*
1576      * Temporary store to hold distinct calcId / type pairs for the tooltip.
1577      * Using TreeMap means calcIds are shown in alphabetical order.
1578      */
1579     Map<String, String> tipEntries = new TreeMap<String, String>();
1580     final Map<SequenceI, List<AlignmentAnnotation>> candidates = new LinkedHashMap<SequenceI, List<AlignmentAnnotation>>();
1581     AlignmentI al = this.ap.av.getAlignment();
1582     AlignmentUtils.findAddableReferenceAnnotations(forSequences,
1583             tipEntries, candidates, al);
1584     if (!candidates.isEmpty())
1585     {
1586       StringBuilder tooltip = new StringBuilder(64);
1587       tooltip.append(MessageManager.getString("label.add_annotations_for"));
1588
1589       /*
1590        * Found annotations that could be added. Enable the menu item, and
1591        * configure its tooltip and action.
1592        */
1593       menuItem.setEnabled(true);
1594       for (String calcId : tipEntries.keySet())
1595       {
1596         tooltip.append("<br/>" + calcId + "/" + tipEntries.get(calcId));
1597       }
1598       String tooltipText = JvSwingUtils.wrapTooltip(true,
1599               tooltip.toString());
1600       menuItem.setToolTipText(tooltipText);
1601
1602       menuItem.addActionListener(new ActionListener()
1603       {
1604         @Override
1605         public void actionPerformed(ActionEvent e)
1606         {
1607           addReferenceAnnotations_actionPerformed(candidates);
1608         }
1609       });
1610     }
1611   }
1612
1613   /**
1614    * Add annotations to the sequences and to the alignment.
1615    * 
1616    * @param candidates
1617    *          a map whose keys are sequences on the alignment, and values a list
1618    *          of annotations to add to each sequence
1619    */
1620   protected void addReferenceAnnotations_actionPerformed(
1621           Map<SequenceI, List<AlignmentAnnotation>> candidates)
1622   {
1623     final SequenceGroup selectionGroup = this.ap.av.getSelectionGroup();
1624     final AlignmentI alignment = this.ap.getAlignment();
1625     AlignmentUtils.addReferenceAnnotations(candidates, alignment,
1626             selectionGroup);
1627     refresh();
1628   }
1629
1630   protected void makeReferenceSeq_actionPerformed(ActionEvent actionEvent)
1631   {
1632     if (!ap.av.getAlignment().hasSeqrep())
1633     {
1634       // initialise the display flags so the user sees something happen
1635       ap.av.setDisplayReferenceSeq(true);
1636       ap.av.setColourByReferenceSeq(true);
1637       ap.av.getAlignment().setSeqrep(sequence);
1638     }
1639     else
1640     {
1641       if (ap.av.getAlignment().getSeqrep() == sequence)
1642       {
1643         ap.av.getAlignment().setSeqrep(null);
1644       }
1645       else
1646       {
1647         ap.av.getAlignment().setSeqrep(sequence);
1648       }
1649     }
1650     refresh();
1651   }
1652
1653   protected void hideInsertions_actionPerformed(ActionEvent actionEvent)
1654   {
1655     if (sequence != null)
1656     {
1657       ColumnSelection cs = ap.av.getColumnSelection();
1658       if (cs == null)
1659       {
1660         cs = new ColumnSelection();
1661       }
1662       cs.hideInsertionsFor(sequence);
1663       ap.av.setColumnSelection(cs);
1664     }
1665     refresh();
1666   }
1667
1668   protected void sequenceSelectionDetails_actionPerformed()
1669   {
1670     createSequenceDetailsReport(ap.av.getSequenceSelection());
1671   }
1672
1673   protected void sequenceDetails_actionPerformed()
1674   {
1675     createSequenceDetailsReport(new SequenceI[] { sequence });
1676   }
1677
1678   public void createSequenceDetailsReport(SequenceI[] sequences)
1679   {
1680     CutAndPasteHtmlTransfer cap = new CutAndPasteHtmlTransfer();
1681     StringBuilder contents = new StringBuilder(128);
1682     for (SequenceI seq : sequences)
1683     {
1684       contents.append("<p><h2>"
1685               + MessageManager
1686                       .formatMessage(
1687                               "label.create_sequence_details_report_annotation_for",
1688                               new Object[] { seq.getDisplayId(true) })
1689               + "</h2></p><p>");
1690       new SequenceAnnotationReport(null)
1691               .createSequenceAnnotationReport(
1692                       contents,
1693                       seq,
1694                       true,
1695                       true,
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         JvOptionPane
2093                 .showMessageDialog(
2094                         ap,
2095                         MessageManager
2096                                 .getString("label.spaces_converted_to_backslashes"),
2097                         MessageManager
2098                                 .getString("label.no_spaces_allowed_sequence_name"),
2099                         JvOptionPane.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       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2199               MessageManager.getString("label.web_browser_not_found_unix"),
2200               MessageManager.getString("label.web_browser_not_found"),
2201               JvOptionPane.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     FileFormatI fileFormat = FileFormat.forName(e.getActionCommand());
2278     cap.setText(new FormatAdapter(ap).formatSequences(fileFormat, 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 }