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