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