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