JAL-2361 extract methods to (re-)build colour menu
[jalview.git] / src / jalview / gui / PopupMenu.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.analysis.AAFrequency;
24 import jalview.analysis.AlignmentAnnotationUtils;
25 import jalview.analysis.AlignmentUtils;
26 import jalview.analysis.Conservation;
27 import jalview.bin.Cache;
28 import jalview.commands.ChangeCaseCommand;
29 import jalview.commands.EditCommand;
30 import jalview.commands.EditCommand.Action;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.Annotation;
34 import jalview.datamodel.ColumnSelection;
35 import jalview.datamodel.DBRefEntry;
36 import jalview.datamodel.PDBEntry;
37 import jalview.datamodel.Sequence;
38 import jalview.datamodel.SequenceFeature;
39 import jalview.datamodel.SequenceGroup;
40 import jalview.datamodel.SequenceI;
41 import jalview.io.FileFormat;
42 import jalview.io.FileFormatI;
43 import jalview.io.FormatAdapter;
44 import jalview.io.SequenceAnnotationReport;
45 import jalview.schemes.AnnotationColourGradient;
46 import jalview.schemes.Blosum62ColourScheme;
47 import jalview.schemes.BuriedColourScheme;
48 import jalview.schemes.ClustalxColourScheme;
49 import jalview.schemes.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.SortedMap;
75 import java.util.TreeMap;
76 import java.util.Vector;
77
78 import javax.swing.ButtonGroup;
79 import javax.swing.JCheckBoxMenuItem;
80 import javax.swing.JColorChooser;
81 import javax.swing.JMenu;
82 import javax.swing.JMenuItem;
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       colourMenu.add(purinePyrimidineColour);
1357     }
1358
1359     SortedMap<String, UserColourScheme> userColourSchemes = UserDefinedColours
1360             .getUserColourSchemes();
1361     if (userColourSchemes != null)
1362     {
1363       for (String userColour : userColourSchemes.keySet())
1364     {
1365         JMenuItem item = new JMenuItem(userColour);
1366         item.addActionListener(new ActionListener()
1367         {
1368           @Override
1369           public void actionPerformed(ActionEvent evt)
1370           {
1371             userDefinedColour_actionPerformed(evt);
1372           }
1373         });
1374         colourMenu.add(item);
1375       }
1376     }
1377     colourMenu.add(userDefinedColour);
1378
1379     colourMenu.addSeparator();
1380     colourMenu.add(abovePIDColour);
1381     colourMenu.add(conservationMenuItem);
1382     editMenu.add(copy);
1383     editMenu.add(cut);
1384     editMenu.add(editSequence);
1385     editMenu.add(upperCase);
1386     editMenu.add(lowerCase);
1387     editMenu.add(toggle);
1388     // JBPNote: These shouldn't be added here - should appear in a generic
1389     // 'apply web service to this sequence menu'
1390     // pdbMenu.add(RNAFold);
1391     // pdbMenu.add(ContraFold);
1392     jMenu1.add(groupName);
1393     jMenu1.add(colourMenu);
1394     jMenu1.add(showBoxes);
1395     jMenu1.add(showText);
1396     jMenu1.add(showColourText);
1397     jMenu1.add(outline);
1398     jMenu1.add(displayNonconserved);
1399     noColourmenuItem.setText(MessageManager.getString("label.none"));
1400     noColourmenuItem.addActionListener(new java.awt.event.ActionListener()
1401     {
1402       @Override
1403       public void actionPerformed(ActionEvent e)
1404       {
1405         noColourmenuItem_actionPerformed();
1406       }
1407     });
1408
1409     clustalColour.setText(MessageManager.getString("label.clustalx"));
1410     clustalColour.addActionListener(new java.awt.event.ActionListener()
1411     {
1412       @Override
1413       public void actionPerformed(ActionEvent e)
1414       {
1415         clustalColour_actionPerformed();
1416       }
1417     });
1418     zappoColour.setText(MessageManager.getString("label.zappo"));
1419     zappoColour.addActionListener(new java.awt.event.ActionListener()
1420     {
1421       @Override
1422       public void actionPerformed(ActionEvent e)
1423       {
1424         zappoColour_actionPerformed();
1425       }
1426     });
1427     taylorColour.setText(MessageManager.getString("label.taylor"));
1428     taylorColour.addActionListener(new java.awt.event.ActionListener()
1429     {
1430       @Override
1431       public void actionPerformed(ActionEvent e)
1432       {
1433         taylorColour_actionPerformed();
1434       }
1435     });
1436     hydrophobicityColour.setText(MessageManager
1437             .getString("label.hydrophobicity"));
1438     hydrophobicityColour
1439             .addActionListener(new java.awt.event.ActionListener()
1440             {
1441               @Override
1442               public void actionPerformed(ActionEvent e)
1443               {
1444                 hydrophobicityColour_actionPerformed();
1445               }
1446             });
1447     helixColour.setText(MessageManager.getString("label.helix_propensity"));
1448     helixColour.addActionListener(new java.awt.event.ActionListener()
1449     {
1450       @Override
1451       public void actionPerformed(ActionEvent e)
1452       {
1453         helixColour_actionPerformed();
1454       }
1455     });
1456     strandColour.setText(MessageManager
1457             .getString("label.strand_propensity"));
1458     strandColour.addActionListener(new java.awt.event.ActionListener()
1459     {
1460       @Override
1461       public void actionPerformed(ActionEvent e)
1462       {
1463         strandColour_actionPerformed();
1464       }
1465     });
1466     turnColour.setText(MessageManager.getString("label.turn_propensity"));
1467     turnColour.addActionListener(new java.awt.event.ActionListener()
1468     {
1469       @Override
1470       public void actionPerformed(ActionEvent e)
1471       {
1472         turnColour_actionPerformed();
1473       }
1474     });
1475     buriedColour.setText(MessageManager.getString("label.buried_index"));
1476     buriedColour.addActionListener(new java.awt.event.ActionListener()
1477     {
1478       @Override
1479       public void actionPerformed(ActionEvent e)
1480       {
1481         buriedColour_actionPerformed();
1482       }
1483     });
1484     abovePIDColour.setText(MessageManager
1485             .getString("label.above_identity_percentage"));
1486     abovePIDColour.addActionListener(new java.awt.event.ActionListener()
1487     {
1488       @Override
1489       public void actionPerformed(ActionEvent e)
1490       {
1491         abovePIDColour_actionPerformed();
1492       }
1493     });
1494     userDefinedColour.setText(MessageManager
1495             .getString("action.user_defined"));
1496     userDefinedColour.addActionListener(new java.awt.event.ActionListener()
1497     {
1498       @Override
1499       public void actionPerformed(ActionEvent e)
1500       {
1501         userDefinedColour_actionPerformed(e);
1502       }
1503     });
1504     PIDColour
1505             .setText(MessageManager.getString("label.percentage_identity"));
1506     PIDColour.addActionListener(new java.awt.event.ActionListener()
1507     {
1508       @Override
1509       public void actionPerformed(ActionEvent e)
1510       {
1511         PIDColour_actionPerformed();
1512       }
1513     });
1514     BLOSUM62Colour
1515             .setText(MessageManager.getString("label.blosum62_score"));
1516     BLOSUM62Colour.addActionListener(new java.awt.event.ActionListener()
1517     {
1518       @Override
1519       public void actionPerformed(ActionEvent e)
1520       {
1521         BLOSUM62Colour_actionPerformed();
1522       }
1523     });
1524     purinePyrimidineColour.setText(MessageManager
1525             .getString("label.purine_pyrimidine"));
1526     purinePyrimidineColour
1527             .addActionListener(new java.awt.event.ActionListener()
1528             {
1529               @Override
1530               public void actionPerformed(ActionEvent e)
1531               {
1532                 purinePyrimidineColour_actionPerformed();
1533               }
1534             });
1535
1536     /*
1537      * covariationColour.addActionListener(new java.awt.event.ActionListener() {
1538      * public void actionPerformed(ActionEvent e) {
1539      * covariationColour_actionPerformed(); } });
1540      */
1541
1542     conservationMenuItem.setText(MessageManager
1543             .getString("label.conservation"));
1544     conservationMenuItem
1545             .addActionListener(new java.awt.event.ActionListener()
1546             {
1547               @Override
1548               public void actionPerformed(ActionEvent e)
1549               {
1550                 conservationMenuItem_actionPerformed();
1551               }
1552             });
1553   }
1554
1555   /**
1556    * Check for any annotations on the underlying dataset sequences (for the
1557    * current selection group) which are not 'on the alignment'.If any are found,
1558    * enable the option to add them to the alignment. The criteria for 'on the
1559    * alignment' is finding an alignment annotation on the alignment, matched on
1560    * calcId, label and sequenceRef.
1561    * 
1562    * A tooltip is also constructed that displays the source (calcId) and type
1563    * (label) of the annotations that can be added.
1564    * 
1565    * @param menuItem
1566    * @param forSequences
1567    */
1568   protected void configureReferenceAnnotationsMenu(JMenuItem menuItem,
1569           List<SequenceI> forSequences)
1570   {
1571     menuItem.setEnabled(false);
1572
1573     /*
1574      * Temporary store to hold distinct calcId / type pairs for the tooltip.
1575      * Using TreeMap means calcIds are shown in alphabetical order.
1576      */
1577     SortedMap<String, String> tipEntries = new TreeMap<String, String>();
1578     final Map<SequenceI, List<AlignmentAnnotation>> candidates = new LinkedHashMap<SequenceI, List<AlignmentAnnotation>>();
1579     AlignmentI al = this.ap.av.getAlignment();
1580     AlignmentUtils.findAddableReferenceAnnotations(forSequences,
1581             tipEntries, candidates, al);
1582     if (!candidates.isEmpty())
1583     {
1584       StringBuilder tooltip = new StringBuilder(64);
1585       tooltip.append(MessageManager.getString("label.add_annotations_for"));
1586
1587       /*
1588        * Found annotations that could be added. Enable the menu item, and
1589        * configure its tooltip and action.
1590        */
1591       menuItem.setEnabled(true);
1592       for (String calcId : tipEntries.keySet())
1593       {
1594         tooltip.append("<br/>" + calcId + "/" + tipEntries.get(calcId));
1595       }
1596       String tooltipText = JvSwingUtils.wrapTooltip(true,
1597               tooltip.toString());
1598       menuItem.setToolTipText(tooltipText);
1599
1600       menuItem.addActionListener(new ActionListener()
1601       {
1602         @Override
1603         public void actionPerformed(ActionEvent e)
1604         {
1605           addReferenceAnnotations_actionPerformed(candidates);
1606         }
1607       });
1608     }
1609   }
1610
1611   /**
1612    * Add annotations to the sequences and to the alignment.
1613    * 
1614    * @param candidates
1615    *          a map whose keys are sequences on the alignment, and values a list
1616    *          of annotations to add to each sequence
1617    */
1618   protected void addReferenceAnnotations_actionPerformed(
1619           Map<SequenceI, List<AlignmentAnnotation>> candidates)
1620   {
1621     final SequenceGroup selectionGroup = this.ap.av.getSelectionGroup();
1622     final AlignmentI alignment = this.ap.getAlignment();
1623     AlignmentUtils.addReferenceAnnotations(candidates, alignment,
1624             selectionGroup);
1625     refresh();
1626   }
1627
1628   protected void makeReferenceSeq_actionPerformed(ActionEvent actionEvent)
1629   {
1630     if (!ap.av.getAlignment().hasSeqrep())
1631     {
1632       // initialise the display flags so the user sees something happen
1633       ap.av.setDisplayReferenceSeq(true);
1634       ap.av.setColourByReferenceSeq(true);
1635       ap.av.getAlignment().setSeqrep(sequence);
1636     }
1637     else
1638     {
1639       if (ap.av.getAlignment().getSeqrep() == sequence)
1640       {
1641         ap.av.getAlignment().setSeqrep(null);
1642       }
1643       else
1644       {
1645         ap.av.getAlignment().setSeqrep(sequence);
1646       }
1647     }
1648     refresh();
1649   }
1650
1651   protected void hideInsertions_actionPerformed(ActionEvent actionEvent)
1652   {
1653     if (sequence != null)
1654     {
1655       ColumnSelection cs = ap.av.getColumnSelection();
1656       if (cs == null)
1657       {
1658         cs = new ColumnSelection();
1659       }
1660       cs.hideInsertionsFor(sequence);
1661       ap.av.setColumnSelection(cs);
1662     }
1663     refresh();
1664   }
1665
1666   protected void sequenceSelectionDetails_actionPerformed()
1667   {
1668     createSequenceDetailsReport(ap.av.getSequenceSelection());
1669   }
1670
1671   protected void sequenceDetails_actionPerformed()
1672   {
1673     createSequenceDetailsReport(new SequenceI[] { sequence });
1674   }
1675
1676   public void createSequenceDetailsReport(SequenceI[] sequences)
1677   {
1678     CutAndPasteHtmlTransfer cap = new CutAndPasteHtmlTransfer();
1679     StringBuilder contents = new StringBuilder(128);
1680     for (SequenceI seq : sequences)
1681     {
1682       contents.append("<p><h2>"
1683               + MessageManager
1684                       .formatMessage(
1685                               "label.create_sequence_details_report_annotation_for",
1686                               new Object[] { seq.getDisplayId(true) })
1687               + "</h2></p><p>");
1688       new SequenceAnnotationReport(null)
1689               .createSequenceAnnotationReport(
1690                       contents,
1691                       seq,
1692                       true,
1693                       true,
1694                       (ap.getSeqPanel().seqCanvas.fr != null) ? ap
1695                               .getSeqPanel().seqCanvas.fr.getMinMax()
1696                               : null);
1697       contents.append("</p>");
1698     }
1699     cap.setText("<html>" + contents.toString() + "</html>");
1700
1701     Desktop.addInternalFrame(cap, MessageManager.formatMessage(
1702             "label.sequence_details_for",
1703             (sequences.length == 1 ? new Object[] { sequences[0]
1704                     .getDisplayId(true) } : new Object[] { MessageManager
1705                     .getString("label.selection") })), 500, 400);
1706
1707   }
1708
1709   protected void showNonconserved_actionPerformed()
1710   {
1711     getGroup().setShowNonconserved(displayNonconserved.isSelected());
1712     refresh();
1713   }
1714
1715   /**
1716    * call to refresh view after settings change
1717    */
1718   void refresh()
1719   {
1720     ap.updateAnnotation();
1721     ap.paintAlignment(true);
1722
1723     PaintRefresher.Refresh(this, ap.av.getSequenceSetId());
1724   }
1725
1726   /**
1727    * DOCUMENT ME!
1728    * 
1729    * @param e
1730    *          DOCUMENT ME!
1731    */
1732   protected void clustalColour_actionPerformed()
1733   {
1734     SequenceGroup sg = getGroup();
1735     sg.cs = new ClustalxColourScheme(sg, ap.av.getHiddenRepSequences());
1736     refresh();
1737   }
1738
1739   /**
1740    * DOCUMENT ME!
1741    * 
1742    * @param e
1743    *          DOCUMENT ME!
1744    */
1745   protected void zappoColour_actionPerformed()
1746   {
1747     getGroup().cs = new ZappoColourScheme();
1748     refresh();
1749   }
1750
1751   /**
1752    * DOCUMENT ME!
1753    * 
1754    * @param e
1755    *          DOCUMENT ME!
1756    */
1757   protected void taylorColour_actionPerformed()
1758   {
1759     getGroup().cs = new TaylorColourScheme();
1760     refresh();
1761   }
1762
1763   /**
1764    * DOCUMENT ME!
1765    * 
1766    * @param e
1767    *          DOCUMENT ME!
1768    */
1769   protected void hydrophobicityColour_actionPerformed()
1770   {
1771     getGroup().cs = new HydrophobicColourScheme();
1772     refresh();
1773   }
1774
1775   /**
1776    * DOCUMENT ME!
1777    * 
1778    * @param e
1779    *          DOCUMENT ME!
1780    */
1781   protected void helixColour_actionPerformed()
1782   {
1783     getGroup().cs = new HelixColourScheme();
1784     refresh();
1785   }
1786
1787   /**
1788    * DOCUMENT ME!
1789    * 
1790    * @param e
1791    *          DOCUMENT ME!
1792    */
1793   protected void strandColour_actionPerformed()
1794   {
1795     getGroup().cs = new StrandColourScheme();
1796     refresh();
1797   }
1798
1799   /**
1800    * DOCUMENT ME!
1801    * 
1802    * @param e
1803    *          DOCUMENT ME!
1804    */
1805   protected void turnColour_actionPerformed()
1806   {
1807     getGroup().cs = new TurnColourScheme();
1808     refresh();
1809   }
1810
1811   /**
1812    * DOCUMENT ME!
1813    * 
1814    * @param e
1815    *          DOCUMENT ME!
1816    */
1817   protected void buriedColour_actionPerformed()
1818   {
1819     getGroup().cs = new BuriedColourScheme();
1820     refresh();
1821   }
1822
1823   /**
1824    * DOCUMENT ME!
1825    * 
1826    * @param e
1827    *          DOCUMENT ME!
1828    */
1829   public void nucleotideMenuItem_actionPerformed()
1830   {
1831     getGroup().cs = new NucleotideColourScheme();
1832     refresh();
1833   }
1834
1835   protected void purinePyrimidineColour_actionPerformed()
1836   {
1837     getGroup().cs = new PurinePyrimidineColourScheme();
1838     refresh();
1839   }
1840
1841   /*
1842    * protected void covariationColour_actionPerformed() { getGroup().cs = new
1843    * CovariationColourScheme(sequence.getAnnotation()[0]); refresh(); }
1844    */
1845   /**
1846    * DOCUMENT ME!
1847    * 
1848    * @param e
1849    *          DOCUMENT ME!
1850    */
1851   protected void abovePIDColour_actionPerformed()
1852   {
1853     SequenceGroup sg = getGroup();
1854     if (sg.cs == null)
1855     {
1856       return;
1857     }
1858
1859     if (abovePIDColour.isSelected())
1860     {
1861       sg.cs.setConsensus(AAFrequency.calculate(
1862               sg.getSequences(ap.av.getHiddenRepSequences()),
1863               sg.getStartRes(), sg.getEndRes() + 1));
1864
1865       int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs, getGroup()
1866               .getName());
1867
1868       sg.cs.setThreshold(threshold, ap.av.isIgnoreGapsConsensus());
1869
1870       SliderPanel.showPIDSlider();
1871     }
1872     else
1873     // remove PIDColouring
1874     {
1875       sg.cs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
1876     }
1877
1878     refresh();
1879   }
1880
1881   /**
1882    * DOCUMENT ME!
1883    * 
1884    * @param e
1885    *          DOCUMENT ME!
1886    */
1887   protected void userDefinedColour_actionPerformed(ActionEvent e)
1888   {
1889     SequenceGroup sg = getGroup();
1890
1891     if (e.getSource().equals(userDefinedColour))
1892     {
1893       new UserDefinedColours(ap, sg);
1894     }
1895     else
1896     {
1897       UserColourScheme udc = UserDefinedColours
1898               .getUserColourSchemes().get(e.getActionCommand());
1899
1900       sg.cs = udc;
1901     }
1902     refresh();
1903   }
1904
1905   /**
1906    * Open a panel where the user can choose which types of sequence annotation
1907    * to show or hide.
1908    * 
1909    * @param e
1910    */
1911   protected void chooseAnnotations_actionPerformed(ActionEvent e)
1912   {
1913     // todo correct way to guard against opening a duplicate panel?
1914     new AnnotationChooser(ap);
1915   }
1916
1917   /**
1918    * DOCUMENT ME!
1919    * 
1920    * @param e
1921    *          DOCUMENT ME!
1922    */
1923   protected void PIDColour_actionPerformed()
1924   {
1925     SequenceGroup sg = getGroup();
1926     sg.cs = new PIDColourScheme();
1927     sg.cs.setConsensus(AAFrequency.calculate(
1928             sg.getSequences(ap.av.getHiddenRepSequences()),
1929             sg.getStartRes(), sg.getEndRes() + 1));
1930     refresh();
1931   }
1932
1933   /**
1934    * DOCUMENT ME!
1935    * 
1936    * @param e
1937    *          DOCUMENT ME!
1938    */
1939   protected void BLOSUM62Colour_actionPerformed()
1940   {
1941     SequenceGroup sg = getGroup();
1942
1943     sg.cs = new Blosum62ColourScheme();
1944
1945     sg.cs.setConsensus(AAFrequency.calculate(
1946             sg.getSequences(ap.av.getHiddenRepSequences()),
1947             sg.getStartRes(), sg.getEndRes() + 1));
1948
1949     refresh();
1950   }
1951
1952   /**
1953    * DOCUMENT ME!
1954    * 
1955    * @param e
1956    *          DOCUMENT ME!
1957    */
1958   protected void noColourmenuItem_actionPerformed()
1959   {
1960     getGroup().cs = null;
1961     refresh();
1962   }
1963
1964   /**
1965    * DOCUMENT ME!
1966    * 
1967    * @param e
1968    *          DOCUMENT ME!
1969    */
1970   protected void conservationMenuItem_actionPerformed()
1971   {
1972     SequenceGroup sg = getGroup();
1973     if (sg.cs == null)
1974     {
1975       return;
1976     }
1977
1978     if (conservationMenuItem.isSelected())
1979     {
1980       // JBPNote: Conservation name shouldn't be i18n translated
1981       Conservation c = new Conservation("Group", sg.getSequences(ap.av
1982               .getHiddenRepSequences()), sg.getStartRes(),
1983               sg.getEndRes() + 1);
1984
1985       c.calculate();
1986       c.verdict(false, ap.av.getConsPercGaps());
1987
1988       sg.cs.setConservation(c);
1989
1990       SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
1991       SliderPanel.showConservationSlider();
1992     }
1993     else
1994     // remove ConservationColouring
1995     {
1996       sg.cs.setConservation(null);
1997     }
1998
1999     refresh();
2000   }
2001
2002   public void annotationMenuItem_actionPerformed(ActionEvent actionEvent)
2003   {
2004     SequenceGroup sg = getGroup();
2005     if (sg == null)
2006     {
2007       return;
2008     }
2009
2010     AnnotationColourGradient acg = new AnnotationColourGradient(
2011             sequence.getAnnotation()[0], null,
2012             AnnotationColourGradient.NO_THRESHOLD);
2013
2014     acg.setPredefinedColours(true);
2015     sg.cs = acg;
2016
2017     refresh();
2018   }
2019
2020   /**
2021    * DOCUMENT ME!
2022    * 
2023    * @param e
2024    *          DOCUMENT ME!
2025    */
2026   protected void groupName_actionPerformed()
2027   {
2028
2029     SequenceGroup sg = getGroup();
2030     EditNameDialog dialog = new EditNameDialog(sg.getName(),
2031             sg.getDescription(), "       "
2032                     + MessageManager.getString("label.group_name") + " ",
2033             MessageManager.getString("label.group_description") + " ",
2034             MessageManager.getString("label.edit_group_name_description"),
2035             ap.alignFrame);
2036
2037     if (!dialog.accept)
2038     {
2039       return;
2040     }
2041
2042     sg.setName(dialog.getName());
2043     sg.setDescription(dialog.getDescription());
2044     refresh();
2045   }
2046
2047   /**
2048    * Get selection group - adding it to the alignment if necessary.
2049    * 
2050    * @return sequence group to operate on
2051    */
2052   SequenceGroup getGroup()
2053   {
2054     SequenceGroup sg = ap.av.getSelectionGroup();
2055     // this method won't add a new group if it already exists
2056     if (sg != null)
2057     {
2058       ap.av.getAlignment().addGroup(sg);
2059     }
2060
2061     return sg;
2062   }
2063
2064   /**
2065    * DOCUMENT ME!
2066    * 
2067    * @param e
2068    *          DOCUMENT ME!
2069    */
2070   void sequenceName_actionPerformed()
2071   {
2072     EditNameDialog dialog = new EditNameDialog(sequence.getName(),
2073             sequence.getDescription(),
2074             "       " + MessageManager.getString("label.sequence_name")
2075                     + " ",
2076             MessageManager.getString("label.sequence_description") + " ",
2077             MessageManager
2078                     .getString("label.edit_sequence_name_description"),
2079             ap.alignFrame);
2080
2081     if (!dialog.accept)
2082     {
2083       return;
2084     }
2085
2086     if (dialog.getName() != null)
2087     {
2088       if (dialog.getName().indexOf(" ") > -1)
2089       {
2090         JvOptionPane
2091                 .showMessageDialog(
2092                         ap,
2093                         MessageManager
2094                                 .getString("label.spaces_converted_to_backslashes"),
2095                         MessageManager
2096                                 .getString("label.no_spaces_allowed_sequence_name"),
2097                         JvOptionPane.WARNING_MESSAGE);
2098       }
2099
2100       sequence.setName(dialog.getName().replace(' ', '_'));
2101       ap.paintAlignment(false);
2102     }
2103
2104     sequence.setDescription(dialog.getDescription());
2105
2106     ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
2107             .getSequences());
2108
2109   }
2110
2111   /**
2112    * DOCUMENT ME!
2113    * 
2114    * @param e
2115    *          DOCUMENT ME!
2116    */
2117   void unGroupMenuItem_actionPerformed()
2118   {
2119     SequenceGroup sg = ap.av.getSelectionGroup();
2120     ap.av.getAlignment().deleteGroup(sg);
2121     ap.av.setSelectionGroup(null);
2122     refresh();
2123   }
2124
2125   void createGroupMenuItem_actionPerformed()
2126   {
2127     getGroup(); // implicitly creates group - note - should apply defaults / use
2128                 // standard alignment window logic for this
2129     refresh();
2130   }
2131
2132   /**
2133    * DOCUMENT ME!
2134    * 
2135    * @param e
2136    *          DOCUMENT ME!
2137    */
2138   protected void outline_actionPerformed()
2139   {
2140     SequenceGroup sg = getGroup();
2141     Color col = JColorChooser.showDialog(this,
2142             MessageManager.getString("label.select_outline_colour"),
2143             Color.BLUE);
2144
2145     if (col != null)
2146     {
2147       sg.setOutlineColour(col);
2148     }
2149
2150     refresh();
2151   }
2152
2153   /**
2154    * DOCUMENT ME!
2155    * 
2156    * @param e
2157    *          DOCUMENT ME!
2158    */
2159   public void showBoxes_actionPerformed()
2160   {
2161     getGroup().setDisplayBoxes(showBoxes.isSelected());
2162     refresh();
2163   }
2164
2165   /**
2166    * DOCUMENT ME!
2167    * 
2168    * @param e
2169    *          DOCUMENT ME!
2170    */
2171   public void showText_actionPerformed()
2172   {
2173     getGroup().setDisplayText(showText.isSelected());
2174     refresh();
2175   }
2176
2177   /**
2178    * DOCUMENT ME!
2179    * 
2180    * @param e
2181    *          DOCUMENT ME!
2182    */
2183   public void showColourText_actionPerformed()
2184   {
2185     getGroup().setColourText(showColourText.isSelected());
2186     refresh();
2187   }
2188
2189   public void showLink(String url)
2190   {
2191     try
2192     {
2193       jalview.util.BrowserLauncher.openURL(url);
2194     } catch (Exception ex)
2195     {
2196       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
2197               MessageManager.getString("label.web_browser_not_found_unix"),
2198               MessageManager.getString("label.web_browser_not_found"),
2199               JvOptionPane.WARNING_MESSAGE);
2200
2201       ex.printStackTrace();
2202     }
2203   }
2204
2205   void hideSequences(boolean representGroup)
2206   {
2207     ap.av.hideSequences(sequence, representGroup);
2208   }
2209
2210   public void copy_actionPerformed()
2211   {
2212     ap.alignFrame.copy_actionPerformed(null);
2213   }
2214
2215   public void cut_actionPerformed()
2216   {
2217     ap.alignFrame.cut_actionPerformed(null);
2218   }
2219
2220   void changeCase(ActionEvent e)
2221   {
2222     Object source = e.getSource();
2223     SequenceGroup sg = ap.av.getSelectionGroup();
2224
2225     if (sg != null)
2226     {
2227       List<int[]> startEnd = ap.av.getVisibleRegionBoundaries(
2228               sg.getStartRes(), sg.getEndRes() + 1);
2229
2230       String description;
2231       int caseChange;
2232
2233       if (source == toggle)
2234       {
2235         description = MessageManager.getString("label.toggle_case");
2236         caseChange = ChangeCaseCommand.TOGGLE_CASE;
2237       }
2238       else if (source == upperCase)
2239       {
2240         description = MessageManager.getString("label.to_upper_case");
2241         caseChange = ChangeCaseCommand.TO_UPPER;
2242       }
2243       else
2244       {
2245         description = MessageManager.getString("label.to_lower_case");
2246         caseChange = ChangeCaseCommand.TO_LOWER;
2247       }
2248
2249       ChangeCaseCommand caseCommand = new ChangeCaseCommand(description,
2250               sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
2251               startEnd, caseChange);
2252
2253       ap.alignFrame.addHistoryItem(caseCommand);
2254
2255       ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
2256               .getSequences());
2257
2258     }
2259   }
2260
2261   public void outputText_actionPerformed(ActionEvent e)
2262   {
2263     CutAndPasteTransfer cap = new CutAndPasteTransfer();
2264     cap.setForInput(null);
2265     Desktop.addInternalFrame(cap, MessageManager.formatMessage(
2266             "label.alignment_output_command",
2267             new Object[] { e.getActionCommand() }), 600, 500);
2268
2269     String[] omitHidden = null;
2270
2271     System.out.println("PROMPT USER HERE"); // TODO: decide if a prompt happens
2272     // or we simply trust the user wants
2273     // wysiwig behaviour
2274
2275     FileFormatI fileFormat = FileFormat.forName(e.getActionCommand());
2276     cap.setText(new FormatAdapter(ap).formatSequences(fileFormat, ap, true));
2277   }
2278
2279   public void sequenceFeature_actionPerformed()
2280   {
2281     SequenceGroup sg = ap.av.getSelectionGroup();
2282     if (sg == null)
2283     {
2284       return;
2285     }
2286
2287     int rsize = 0, gSize = sg.getSize();
2288     SequenceI[] rseqs, seqs = new SequenceI[gSize];
2289     SequenceFeature[] tfeatures, features = new SequenceFeature[gSize];
2290
2291     for (int i = 0; i < gSize; i++)
2292     {
2293       int start = sg.getSequenceAt(i).findPosition(sg.getStartRes());
2294       int end = sg.findEndRes(sg.getSequenceAt(i));
2295       if (start <= end)
2296       {
2297         seqs[rsize] = sg.getSequenceAt(i).getDatasetSequence();
2298         features[rsize] = new SequenceFeature(null, null, null, start, end,
2299                 "Jalview");
2300         rsize++;
2301       }
2302     }
2303     rseqs = new SequenceI[rsize];
2304     tfeatures = new SequenceFeature[rsize];
2305     System.arraycopy(seqs, 0, rseqs, 0, rsize);
2306     System.arraycopy(features, 0, tfeatures, 0, rsize);
2307     features = tfeatures;
2308     seqs = rseqs;
2309     if (ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
2310             features, true, ap))
2311     {
2312       ap.alignFrame.setShowSeqFeatures(true);
2313       ap.highlightSearchResults(null);
2314     }
2315   }
2316
2317   public void textColour_actionPerformed()
2318   {
2319     SequenceGroup sg = getGroup();
2320     if (sg != null)
2321     {
2322       new TextColourChooser().chooseColour(ap, sg);
2323     }
2324   }
2325
2326   public void colourByStructure(String pdbid)
2327   {
2328     Annotation[] anots = ap.av.getStructureSelectionManager()
2329             .colourSequenceFromStructure(sequence, pdbid);
2330
2331     AlignmentAnnotation an = new AlignmentAnnotation("Structure",
2332             "Coloured by " + pdbid, anots);
2333
2334     ap.av.getAlignment().addAnnotation(an);
2335     an.createSequenceMapping(sequence, 0, true);
2336     // an.adjustForAlignment();
2337     ap.av.getAlignment().setAnnotationIndex(an, 0);
2338
2339     ap.adjustAnnotationHeight();
2340
2341     sequence.addAlignmentAnnotation(an);
2342
2343   }
2344
2345   public void editSequence_actionPerformed(ActionEvent actionEvent)
2346   {
2347     SequenceGroup sg = ap.av.getSelectionGroup();
2348
2349     if (sg != null)
2350     {
2351       if (sequence == null)
2352       {
2353         sequence = sg.getSequenceAt(0);
2354       }
2355
2356       EditNameDialog dialog = new EditNameDialog(
2357               sequence.getSequenceAsString(sg.getStartRes(),
2358                       sg.getEndRes() + 1), null,
2359               MessageManager.getString("label.edit_sequence"), null,
2360               MessageManager.getString("label.edit_sequence"),
2361               ap.alignFrame);
2362
2363       if (dialog.accept)
2364       {
2365         EditCommand editCommand = new EditCommand(
2366                 MessageManager.getString("label.edit_sequences"),
2367                 Action.REPLACE, dialog.getName().replace(' ',
2368                         ap.av.getGapCharacter()),
2369                 sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
2370                 sg.getStartRes(), sg.getEndRes() + 1, ap.av.getAlignment());
2371
2372         ap.alignFrame.addHistoryItem(editCommand);
2373
2374         ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
2375                 .getSequences());
2376       }
2377     }
2378   }
2379
2380 }