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