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