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