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