Merge branch 'Release_2_8_0b1_Branch' into try_r20b1_merge
[jalview.git] / src / jalview / gui / PopupMenu.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.gui;
20
21 import java.util.*;
22
23 import java.awt.*;
24 import java.awt.event.*;
25
26 import javax.swing.*;
27
28 import jalview.analysis.*;
29 import jalview.commands.*;
30 import jalview.datamodel.*;
31 import jalview.io.*;
32 import jalview.schemes.*;
33 import jalview.util.GroupUrlLink;
34 import jalview.util.GroupUrlLink.UrlStringTooLongException;
35 import jalview.util.UrlLink;
36
37 /**
38  * DOCUMENT ME!
39  * 
40  * @author $author$
41  * @version $Revision: 1.118 $
42  */
43 public class PopupMenu extends JPopupMenu
44 {
45   JMenu groupMenu = new JMenu();
46
47   JMenuItem groupName = new JMenuItem();
48
49   protected JRadioButtonMenuItem clustalColour = new JRadioButtonMenuItem();
50
51   protected JRadioButtonMenuItem zappoColour = new JRadioButtonMenuItem();
52
53   protected JRadioButtonMenuItem taylorColour = new JRadioButtonMenuItem();
54
55   protected JRadioButtonMenuItem hydrophobicityColour = new JRadioButtonMenuItem();
56
57   protected JRadioButtonMenuItem helixColour = new JRadioButtonMenuItem();
58
59   protected JRadioButtonMenuItem strandColour = new JRadioButtonMenuItem();
60
61   protected JRadioButtonMenuItem turnColour = new JRadioButtonMenuItem();
62
63   protected JRadioButtonMenuItem buriedColour = new JRadioButtonMenuItem();
64
65   protected JCheckBoxMenuItem abovePIDColour = new JCheckBoxMenuItem();
66
67   protected JRadioButtonMenuItem userDefinedColour = new JRadioButtonMenuItem();
68
69   protected JRadioButtonMenuItem PIDColour = new JRadioButtonMenuItem();
70
71   protected JRadioButtonMenuItem BLOSUM62Colour = new JRadioButtonMenuItem();
72
73   protected JRadioButtonMenuItem purinePyrimidineColour = new JRadioButtonMenuItem();
74
75   // protected JRadioButtonMenuItem covariationColour = new
76   // JRadioButtonMenuItem();
77
78   JRadioButtonMenuItem noColourmenuItem = new JRadioButtonMenuItem();
79
80   protected JCheckBoxMenuItem conservationMenuItem = new JCheckBoxMenuItem();
81
82   AlignmentPanel ap;
83
84   JMenu sequenceMenu = new JMenu();
85
86   JMenuItem sequenceName = new JMenuItem();
87
88   JMenuItem sequenceDetails = new JMenuItem();
89
90   JMenuItem sequenceSelDetails = new JMenuItem();
91
92   SequenceI sequence;
93   JMenuItem createGroupMenuItem = new JMenuItem();
94   JMenuItem unGroupMenuItem = new JMenuItem();
95
96   JMenuItem outline = new JMenuItem();
97
98   JRadioButtonMenuItem nucleotideMenuItem = new JRadioButtonMenuItem();
99
100   JMenu colourMenu = new JMenu();
101
102   JCheckBoxMenuItem showBoxes = new JCheckBoxMenuItem();
103
104   JCheckBoxMenuItem showText = new JCheckBoxMenuItem();
105
106   JCheckBoxMenuItem showColourText = new JCheckBoxMenuItem();
107
108   JCheckBoxMenuItem displayNonconserved = new JCheckBoxMenuItem();
109
110   JMenu editMenu = new JMenu();
111
112   JMenuItem cut = new JMenuItem();
113
114   JMenuItem copy = new JMenuItem();
115
116   JMenuItem upperCase = new JMenuItem();
117
118   JMenuItem lowerCase = new JMenuItem();
119
120   JMenuItem toggle = new JMenuItem();
121
122   JMenu pdbMenu = new JMenu();
123
124   JMenuItem pdbFromFile = new JMenuItem();
125
126   JMenuItem enterPDB = new JMenuItem();
127
128   JMenuItem discoverPDB = new JMenuItem();
129
130   JMenu outputMenu = new JMenu();
131
132   JMenuItem sequenceFeature = new JMenuItem();
133
134   JMenuItem textColour = new JMenuItem();
135
136   JMenu jMenu1 = new JMenu();
137
138   JMenu structureMenu = new JMenu();
139
140   JMenu viewStructureMenu = new JMenu();
141
142   // JMenu colStructureMenu = new JMenu();
143   JMenuItem editSequence = new JMenuItem();
144
145   // JMenuItem annotationMenuItem = new JMenuItem();
146
147   JMenu groupLinksMenu;
148
149   /**
150    * Creates a new PopupMenu object.
151    * 
152    * @param ap
153    *          DOCUMENT ME!
154    * @param seq
155    *          DOCUMENT ME!
156    */
157   public PopupMenu(final AlignmentPanel ap, Sequence seq, Vector links)
158   {
159     this(ap, seq, links, null);
160   }
161
162   /**
163    * 
164    * @param ap
165    * @param seq
166    * @param links
167    * @param groupLinks
168    */
169   public PopupMenu(final AlignmentPanel ap, final SequenceI seq,
170           Vector links, Vector groupLinks)
171   {
172     // /////////////////////////////////////////////////////////
173     // If this is activated from the sequence panel, the user may want to
174     // edit or annotate a particular residue. Therefore display the residue menu
175     //
176     // If from the IDPanel, we must display the sequence menu
177     // ////////////////////////////////////////////////////////
178     this.ap = ap;
179     sequence = seq;
180
181     ButtonGroup colours = new ButtonGroup();
182     colours.add(noColourmenuItem);
183     colours.add(clustalColour);
184     colours.add(zappoColour);
185     colours.add(taylorColour);
186     colours.add(hydrophobicityColour);
187     colours.add(helixColour);
188     colours.add(strandColour);
189     colours.add(turnColour);
190     colours.add(buriedColour);
191     colours.add(abovePIDColour);
192     colours.add(userDefinedColour);
193     colours.add(PIDColour);
194     colours.add(BLOSUM62Colour);
195     colours.add(purinePyrimidineColour);
196     // colours.add(covariationColour);
197
198     for (int i = 0; i < jalview.io.FormatAdapter.WRITEABLE_FORMATS.length; i++)
199     {
200       JMenuItem item = new JMenuItem(
201               jalview.io.FormatAdapter.WRITEABLE_FORMATS[i]);
202
203       item.addActionListener(new java.awt.event.ActionListener()
204       {
205         public void actionPerformed(ActionEvent e)
206         {
207           outputText_actionPerformed(e);
208         }
209       });
210
211       outputMenu.add(item);
212     }
213
214     try
215     {
216       jbInit();
217     } catch (Exception e)
218     {
219       e.printStackTrace();
220     }
221
222     JMenuItem menuItem;
223     if (seq != null)
224     {
225       sequenceMenu.setText(sequence.getName());
226
227       if (seq.getDatasetSequence().getPDBId() != null
228               && seq.getDatasetSequence().getPDBId().size() > 0)
229       {
230         java.util.Enumeration e = seq.getDatasetSequence().getPDBId()
231                 .elements();
232
233         while (e.hasMoreElements())
234         {
235           final PDBEntry pdb = (PDBEntry) e.nextElement();
236
237           menuItem = new JMenuItem();
238           menuItem.setText(pdb.getId());
239           menuItem.addActionListener(new java.awt.event.ActionListener()
240           {
241             public void actionPerformed(ActionEvent e)
242             {
243               // TODO re JAL-860: optionally open dialog or provide a menu entry
244               // allowing user to open just one structure per sequence
245               new AppJmol(pdb, ap.av.collateForPDB(new PDBEntry[]
246               { pdb })[0], null, ap);
247               // new PDBViewer(pdb, seqs2, null, ap, AppletFormatAdapter.FILE);
248             }
249
250           });
251           viewStructureMenu.add(menuItem);
252
253           /*
254            * menuItem = new JMenuItem(); menuItem.setText(pdb.getId());
255            * menuItem.addActionListener(new java.awt.event.ActionListener() {
256            * public void actionPerformed(ActionEvent e) {
257            * colourByStructure(pdb.getId()); } });
258            * colStructureMenu.add(menuItem);
259            */
260         }
261       }
262       else
263       {
264         if (ap.av.getAlignment().isNucleotide() == false)
265         {
266           structureMenu.remove(viewStructureMenu);
267         }
268         // structureMenu.remove(colStructureMenu);
269       }
270
271       if (ap.av.getAlignment().isNucleotide() == true)
272       {
273         AlignmentAnnotation[] aa = ap.av.getAlignment()
274                 .getAlignmentAnnotation();
275         for (int i = 0; i < aa.length; i++)
276         {
277           if (aa[i].getRNAStruc() != null)
278           {
279             final String rnastruc = aa[i].getRNAStruc();
280             final String structureLine = aa[i].label;
281             menuItem = new JMenuItem();
282             menuItem.setText("2D RNA " + structureLine);
283             menuItem.addActionListener(new java.awt.event.ActionListener()
284             {
285               public void actionPerformed(ActionEvent e)
286               {
287                 new AppVarna(structureLine, seq, seq.getSequenceAsString(),
288                         rnastruc, seq.getName(), ap);
289               }
290             });
291             viewStructureMenu.add(menuItem);
292           }
293         }
294
295         // SequenceFeatures[] test = seq.getSequenceFeatures();
296
297         if (seq.getAnnotation() != null)
298         {
299           AlignmentAnnotation seqAnno[] = seq.getAnnotation();
300           for (int i = 0; i < seqAnno.length; i++)
301           {
302             if (seqAnno[i].getRNAStruc() != null)
303             {
304               final String rnastruc = seqAnno[i].getRNAStruc();
305
306               // TODO: make rnastrucF a bit more nice
307               menuItem = new JMenuItem();
308               menuItem.setText("2D RNA - " + seq.getName());
309               menuItem.addActionListener(new java.awt.event.ActionListener()
310               {
311                 public void actionPerformed(ActionEvent e)
312                 {
313                   // TODO: VARNA does'nt print gaps in the sequence
314                   new AppVarna(seq.getName() + " structure", seq, seq
315                           .getSequenceAsString(), rnastruc, seq.getName(),
316                           ap);
317                 }
318               });
319               viewStructureMenu.add(menuItem);
320             }
321           }
322         }
323
324       }
325
326       menuItem = new JMenuItem("Hide Sequences");
327       menuItem.addActionListener(new java.awt.event.ActionListener()
328       {
329         public void actionPerformed(ActionEvent e)
330         {
331           hideSequences(false);
332         }
333       });
334       add(menuItem);
335
336       if (ap.av.getSelectionGroup() != null
337               && ap.av.getSelectionGroup().getSize() > 1)
338       {
339         menuItem = new JMenuItem("Represent Group with " + seq.getName());
340         menuItem.addActionListener(new java.awt.event.ActionListener()
341         {
342           public void actionPerformed(ActionEvent e)
343           {
344             hideSequences(true);
345           }
346         });
347         sequenceMenu.add(menuItem);
348       }
349
350       if (ap.av.hasHiddenRows())
351       {
352         final int index = ap.av.getAlignment().findIndex(seq);
353
354         if (ap.av.adjustForHiddenSeqs(index)
355                 - ap.av.adjustForHiddenSeqs(index - 1) > 1)
356         {
357           menuItem = new JMenuItem("Reveal Sequences");
358           menuItem.addActionListener(new ActionListener()
359           {
360             public void actionPerformed(ActionEvent e)
361             {
362               ap.av.showSequence(index);
363               if (ap.overviewPanel != null)
364               {
365                 ap.overviewPanel.updateOverviewImage();
366               }
367             }
368           });
369           add(menuItem);
370         }
371       }
372     }
373     // for the case when no sequences are even visible
374     if (ap.av.hasHiddenRows())
375     {
376       {
377         menuItem = new JMenuItem("Reveal All");
378         menuItem.addActionListener(new ActionListener()
379         {
380           public void actionPerformed(ActionEvent e)
381           {
382             ap.av.showAllHiddenSeqs();
383             if (ap.overviewPanel != null)
384             {
385               ap.overviewPanel.updateOverviewImage();
386             }
387           }
388         });
389
390         add(menuItem);
391       }
392
393     }
394
395     SequenceGroup sg = ap.av.getSelectionGroup();
396     boolean isDefinedGroup = (sg!=null) ? ap.av.getAlignment().getGroups().contains(sg) : false;
397
398     if (sg != null && sg.getSize() > 0)
399     {      
400       groupName.setText("Name: " + sg.getName());
401       groupName.setText("Edit name and description of current group.");
402
403       if (sg.cs instanceof ZappoColourScheme)
404       {
405         zappoColour.setSelected(true);
406       }
407       else if (sg.cs instanceof TaylorColourScheme)
408       {
409         taylorColour.setSelected(true);
410       }
411       else if (sg.cs instanceof PIDColourScheme)
412       {
413         PIDColour.setSelected(true);
414       }
415       else if (sg.cs instanceof Blosum62ColourScheme)
416       {
417         BLOSUM62Colour.setSelected(true);
418       }
419       else if (sg.cs instanceof UserColourScheme)
420       {
421         userDefinedColour.setSelected(true);
422       }
423       else if (sg.cs instanceof HydrophobicColourScheme)
424       {
425         hydrophobicityColour.setSelected(true);
426       }
427       else if (sg.cs instanceof HelixColourScheme)
428       {
429         helixColour.setSelected(true);
430       }
431       else if (sg.cs instanceof StrandColourScheme)
432       {
433         strandColour.setSelected(true);
434       }
435       else if (sg.cs instanceof TurnColourScheme)
436       {
437         turnColour.setSelected(true);
438       }
439       else if (sg.cs instanceof BuriedColourScheme)
440       {
441         buriedColour.setSelected(true);
442       }
443       else if (sg.cs instanceof ClustalxColourScheme)
444       {
445         clustalColour.setSelected(true);
446       }
447       else if (sg.cs instanceof PurinePyrimidineColourScheme)
448       {
449         purinePyrimidineColour.setSelected(true);
450       }
451       /*
452        * else if (sg.cs instanceof CovariationColourScheme) {
453        * covariationColour.setSelected(true); }
454        */
455       else
456       {
457         noColourmenuItem.setSelected(true);
458       }
459
460       if (sg.cs != null && sg.cs.conservationApplied())
461       {
462         conservationMenuItem.setSelected(true);
463       }
464       displayNonconserved.setSelected(sg.getShowNonconserved());
465       showText.setSelected(sg.getDisplayText());
466       showColourText.setSelected(sg.getColourText());
467       showBoxes.setSelected(sg.getDisplayBoxes());
468       // add any groupURLs to the groupURL submenu and make it visible
469       if (groupLinks != null && groupLinks.size() > 0)
470       {
471         buildGroupURLMenu(sg, groupLinks);
472       }
473       // Add a 'show all structures' for the current selection
474       Hashtable<String, PDBEntry> pdbe = new Hashtable<String, PDBEntry>();
475       SequenceI sqass = null;
476       for (SequenceI sq : ap.av.getSequenceSelection())
477       {
478         Vector<PDBEntry> pes = (Vector<PDBEntry>) sq.getDatasetSequence()
479                 .getPDBId();
480         if (pes != null)
481         {
482           for (PDBEntry pe : pes)
483           {
484             pdbe.put(pe.getId(), pe);
485             if (sqass == null)
486             {
487               sqass = sq;
488             }
489           }
490         }
491       }
492       if (pdbe.size() > 0)
493       {
494         final PDBEntry[] pe = pdbe.values().toArray(
495                 new PDBEntry[pdbe.size()]);
496         final JMenuItem gpdbview;
497         if (pdbe.size() == 1)
498         {
499           structureMenu.add(gpdbview = new JMenuItem("View structure for "
500                   + sqass.getDisplayId(false)));
501         }
502         else
503         {
504           structureMenu.add(gpdbview = new JMenuItem("View all "
505                   + pdbe.size() + " structures."));
506         }
507         gpdbview.setToolTipText("Open a new Jmol view with all structures associated with the current selection and superimpose them using the alignment.");
508         gpdbview.addActionListener(new ActionListener()
509         {
510
511           @Override
512           public void actionPerformed(ActionEvent e)
513           {
514             new AppJmol(ap, pe, ap.av.collateForPDB(pe));
515           }
516         });
517       }
518     }
519     else
520     {
521       groupMenu.setVisible(false);
522       editMenu.setVisible(false);
523     }
524
525     if (!isDefinedGroup)
526     {
527       createGroupMenuItem.setVisible(true);
528       unGroupMenuItem.setVisible(false);
529       jMenu1.setText("Edit New Group");
530     } else {
531       createGroupMenuItem.setVisible(false);
532       unGroupMenuItem.setVisible(true);
533       jMenu1.setText("Edit Group");
534     }
535
536     if (seq == null)
537     {
538       sequenceMenu.setVisible(false);
539       structureMenu.setVisible(false);
540     }
541
542     if (links != null && links.size() > 0)
543     {
544
545       JMenu linkMenu = new JMenu("Link");
546       Vector linkset = new Vector();
547       for (int i = 0; i < links.size(); i++)
548       {
549         String link = links.elementAt(i).toString();
550         UrlLink urlLink = null;
551         try
552         {
553           urlLink = new UrlLink(link);
554         } catch (Exception foo)
555         {
556           jalview.bin.Cache.log.error("Exception for URLLink '" + link
557                   + "'", foo);
558           continue;
559         }
560         ;
561         if (!urlLink.isValid())
562         {
563           jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
564           continue;
565         }
566         final String label = urlLink.getLabel();
567         if (seq != null && urlLink.isDynamic())
568         {
569
570           // collect matching db-refs
571           DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(
572                   seq.getDBRef(), new String[]
573                   { urlLink.getTarget() });
574           // collect id string too
575           String id = seq.getName();
576           String descr = seq.getDescription();
577           if (descr != null && descr.length() < 1)
578           {
579             descr = null;
580           }
581
582           if (dbr != null)
583           {
584             for (int r = 0; r < dbr.length; r++)
585             {
586               if (id != null && dbr[r].getAccessionId().equals(id))
587               {
588                 // suppress duplicate link creation for the bare sequence ID
589                 // string with this link
590                 id = null;
591               }
592               // create Bare ID link for this RUL
593               String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(),
594                       true);
595               if (urls != null)
596               {
597                 for (int u = 0; u < urls.length; u += 2)
598                 {
599                   if (!linkset.contains(urls[u] + "|" + urls[u + 1]))
600                   {
601                     linkset.addElement(urls[u] + "|" + urls[u + 1]);
602                     addshowLink(linkMenu, label + "|" + urls[u],
603                             urls[u + 1]);
604                   }
605                 }
606               }
607             }
608           }
609           if (id != null)
610           {
611             // create Bare ID link for this RUL
612             String[] urls = urlLink.makeUrls(id, true);
613             if (urls != null)
614             {
615               for (int u = 0; u < urls.length; u += 2)
616               {
617                 if (!linkset.contains(urls[u] + "|" + urls[u + 1]))
618                 {
619                   linkset.addElement(urls[u] + "|" + urls[u + 1]);
620                   addshowLink(linkMenu, label, urls[u + 1]);
621                 }
622               }
623             }
624           }
625           // Create urls from description but only for URL links which are regex
626           // links
627           if (descr != null && urlLink.getRegexReplace() != null)
628           {
629             // create link for this URL from description where regex matches
630             String[] urls = urlLink.makeUrls(descr, true);
631             if (urls != null)
632             {
633               for (int u = 0; u < urls.length; u += 2)
634               {
635                 if (!linkset.contains(urls[u] + "|" + urls[u + 1]))
636                 {
637                   linkset.addElement(urls[u] + "|" + urls[u + 1]);
638                   addshowLink(linkMenu, label, urls[u + 1]);
639                 }
640               }
641             }
642           }
643         }
644         else
645         {
646           if (!linkset.contains(label + "|" + urlLink.getUrl_prefix()))
647           {
648             linkset.addElement(label + "|" + urlLink.getUrl_prefix());
649             // Add a non-dynamic link
650             addshowLink(linkMenu, label, urlLink.getUrl_prefix());
651           }
652         }
653       }
654       if (sequence != null)
655       {
656         sequenceMenu.add(linkMenu);
657       }
658       else
659       {
660         add(linkMenu);
661       }
662     }
663   }
664
665   private void buildGroupURLMenu(SequenceGroup sg, Vector groupLinks)
666   {
667
668     // TODO: usability: thread off the generation of group url content so root
669     // menu appears asap
670     // sequence only URLs
671     // ID/regex match URLs
672     groupLinksMenu = new JMenu("Group Link");
673     JMenu[] linkMenus = new JMenu[]
674     { null, new JMenu("IDS"), new JMenu("Sequences"),
675         new JMenu("IDS and Sequences") }; // three types of url that might be
676                                           // created.
677     SequenceI[] seqs = ap.av.getSelectionAsNewSequence();
678     String[][] idandseqs = GroupUrlLink.formStrings(seqs);
679     Hashtable commonDbrefs = new Hashtable();
680     for (int sq = 0; sq < seqs.length; sq++)
681     {
682
683       int start = seqs[sq].findPosition(sg.getStartRes()), end = seqs[sq]
684               .findPosition(sg.getEndRes());
685       // just collect ids from dataset sequence
686       // TODO: check if IDs collected from selecton group intersects with the
687       // current selection, too
688       SequenceI sqi = seqs[sq];
689       while (sqi.getDatasetSequence() != null)
690       {
691         sqi = sqi.getDatasetSequence();
692       }
693       DBRefEntry[] dbr = sqi.getDBRef();
694       if (dbr != null && dbr.length > 0)
695       {
696         for (int d = 0; d < dbr.length; d++)
697         {
698           String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
699           Object[] sarray = (Object[]) commonDbrefs.get(src);
700           if (sarray == null)
701           {
702             sarray = new Object[2];
703             sarray[0] = new int[]
704             { 0 };
705             sarray[1] = new String[seqs.length];
706
707             commonDbrefs.put(src, sarray);
708           }
709
710           if (((String[]) sarray[1])[sq] == null)
711           {
712             if (!dbr[d].hasMap()
713                     || (dbr[d].getMap().locateMappedRange(start, end) != null))
714             {
715               ((String[]) sarray[1])[sq] = dbr[d].getAccessionId();
716               ((int[]) sarray[0])[0]++;
717             }
718           }
719         }
720       }
721     }
722     // now create group links for all distinct ID/sequence sets.
723     boolean addMenu = false; // indicates if there are any group links to give
724                              // to user
725     for (int i = 0; i < groupLinks.size(); i++)
726     {
727       String link = groupLinks.elementAt(i).toString();
728       GroupUrlLink urlLink = null;
729       try
730       {
731         urlLink = new GroupUrlLink(link);
732       } catch (Exception foo)
733       {
734         jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
735                 + "'", foo);
736         continue;
737       }
738       ;
739       if (!urlLink.isValid())
740       {
741         jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
742         continue;
743       }
744       final String label = urlLink.getLabel();
745       boolean usingNames = false;
746       // Now see which parts of the group apply for this URL
747       String ltarget = urlLink.getTarget(); // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
748       Object[] idset = (Object[]) commonDbrefs.get(ltarget.toUpperCase());
749       String[] seqstr, ids; // input to makeUrl
750       if (idset != null)
751       {
752         int numinput = ((int[]) idset[0])[0];
753         String[] allids = ((String[]) idset[1]);
754         seqstr = new String[numinput];
755         ids = new String[numinput];
756         for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
757         {
758           if (allids[sq] != null)
759           {
760             ids[idcount] = allids[sq];
761             seqstr[idcount++] = idandseqs[1][sq];
762           }
763         }
764       }
765       else
766       {
767         // just use the id/seq set
768         seqstr = idandseqs[1];
769         ids = idandseqs[0];
770         usingNames = true;
771       }
772       // and try and make the groupURL!
773
774       Object[] urlset = null;
775       try
776       {
777         urlset = urlLink.makeUrlStubs(ids, seqstr,
778                 "FromJalview" + System.currentTimeMillis(), false);
779       } catch (UrlStringTooLongException e)
780       {
781       }
782       if (urlset != null)
783       {
784         int type = urlLink.getGroupURLType() & 3;
785         // System.out.println(urlLink.getGroupURLType()
786         // +" "+((String[])urlset[3])[0]);
787         // first two bits ofurlLink type bitfield are sequenceids and sequences
788         // TODO: FUTURE: ensure the groupURL menu structure can be generalised
789         addshowLink(linkMenus[type], label
790                 + (((type & 1) == 1) ? ("("
791                         + (usingNames ? "Names" : ltarget) + ")") : ""),
792                 urlLink, urlset);
793         addMenu = true;
794       }
795     }
796     if (addMenu)
797     {
798       groupLinksMenu = new JMenu("Group Links");
799       for (int m = 0; m < linkMenus.length; m++)
800       {
801         if (linkMenus[m] != null
802                 && linkMenus[m].getMenuComponentCount() > 0)
803         {
804           groupLinksMenu.add(linkMenus[m]);
805         }
806       }
807
808       groupMenu.add(groupLinksMenu);
809     }
810   }
811
812   /**
813    * add a show URL menu item to the given linkMenu
814    * 
815    * @param linkMenu
816    * @param label
817    *          - menu label string
818    * @param url
819    *          - url to open
820    */
821   private void addshowLink(JMenu linkMenu, String label, final String url)
822   {
823     JMenuItem item = new JMenuItem(label);
824     item.setToolTipText("open URL: " + url);
825     item.addActionListener(new java.awt.event.ActionListener()
826     {
827       public void actionPerformed(ActionEvent e)
828       {
829         new Thread(new Runnable()
830         {
831
832           public void run()
833           {
834             showLink(url);
835           }
836
837         }).start();
838       }
839     });
840
841     linkMenu.add(item);
842   }
843
844   /**
845    * add a late bound groupURL item to the given linkMenu
846    * 
847    * @param linkMenu
848    * @param label
849    *          - menu label string
850    * @param urlgenerator
851    *          GroupURLLink used to generate URL
852    * @param urlstub
853    *          Object array returned from the makeUrlStubs function.
854    */
855   private void addshowLink(JMenu linkMenu, String label,
856           final GroupUrlLink urlgenerator, final Object[] urlstub)
857   {
858     JMenuItem item = new JMenuItem(label);
859     item.setToolTipText("open URL (" + urlgenerator.getUrl_prefix()
860             + "..) (" + urlgenerator.getNumberInvolved(urlstub) + " seqs)"); // TODO:
861                                                                              // put
862                                                                              // in
863                                                                              // info
864                                                                              // about
865                                                                              // what
866                                                                              // is
867                                                                              // being
868                                                                              // sent.
869     item.addActionListener(new java.awt.event.ActionListener()
870     {
871       public void actionPerformed(ActionEvent e)
872       {
873         new Thread(new Runnable()
874         {
875
876           public void run()
877           {
878             try
879             {
880               showLink(urlgenerator.constructFrom(urlstub));
881             } catch (UrlStringTooLongException e)
882             {
883             }
884           }
885
886         }).start();
887       }
888     });
889
890     linkMenu.add(item);
891   }
892
893   /**
894    * DOCUMENT ME!
895    * 
896    * @throws Exception
897    *           DOCUMENT ME!
898    */
899   private void jbInit() throws Exception
900   {
901     groupMenu.setText("Group");
902     groupMenu.setText("Selection");
903     groupName.setText("Name");
904     groupName.addActionListener(new java.awt.event.ActionListener()
905     {
906       public void actionPerformed(ActionEvent e)
907       {
908         groupName_actionPerformed();
909       }
910     });
911     sequenceMenu.setText("Sequence");
912     sequenceName.setText("Edit Name/Description");
913     sequenceName.addActionListener(new java.awt.event.ActionListener()
914     {
915       public void actionPerformed(ActionEvent e)
916       {
917         sequenceName_actionPerformed();
918       }
919     });
920     sequenceDetails.setText("Sequence Details ...");
921     sequenceDetails.addActionListener(new java.awt.event.ActionListener()
922     {
923       public void actionPerformed(ActionEvent e)
924       {
925         sequenceDetails_actionPerformed();
926       }
927     });
928     sequenceSelDetails.setText("Sequence Details ...");
929     sequenceSelDetails
930             .addActionListener(new java.awt.event.ActionListener()
931             {
932               public void actionPerformed(ActionEvent e)
933               {
934                 sequenceSelectionDetails_actionPerformed();
935               }
936             });
937     PIDColour.setFocusPainted(false);
938     unGroupMenuItem.setText("Remove Group");
939     unGroupMenuItem.addActionListener(new java.awt.event.ActionListener()
940     {
941       public void actionPerformed(ActionEvent e)
942       {
943         unGroupMenuItem_actionPerformed();
944       }
945     });
946     createGroupMenuItem.setText("Create Group");
947     createGroupMenuItem.addActionListener(new java.awt.event.ActionListener()
948     {
949       public void actionPerformed(ActionEvent e)
950       {
951         createGroupMenuItem_actionPerformed();
952       }
953     });
954
955     outline.setText("Border colour");
956     outline.addActionListener(new java.awt.event.ActionListener()
957     {
958       public void actionPerformed(ActionEvent e)
959       {
960         outline_actionPerformed();
961       }
962     });
963     nucleotideMenuItem.setText("Nucleotide");
964     nucleotideMenuItem.addActionListener(new ActionListener()
965     {
966       public void actionPerformed(ActionEvent e)
967       {
968         nucleotideMenuItem_actionPerformed();
969       }
970     });
971     colourMenu.setText("Group Colour");
972     showBoxes.setText("Boxes");
973     showBoxes.setState(true);
974     showBoxes.addActionListener(new ActionListener()
975     {
976       public void actionPerformed(ActionEvent e)
977       {
978         showBoxes_actionPerformed();
979       }
980     });
981     showText.setText("Text");
982     showText.setState(true);
983     showText.addActionListener(new ActionListener()
984     {
985       public void actionPerformed(ActionEvent e)
986       {
987         showText_actionPerformed();
988       }
989     });
990     showColourText.setText("Colour Text");
991     showColourText.addActionListener(new ActionListener()
992     {
993       public void actionPerformed(ActionEvent e)
994       {
995         showColourText_actionPerformed();
996       }
997     });
998     displayNonconserved.setText("Show Nonconserved");
999     displayNonconserved.setState(true);
1000     displayNonconserved.addActionListener(new ActionListener()
1001     {
1002       public void actionPerformed(ActionEvent e)
1003       {
1004         showNonconserved_actionPerformed();
1005       }
1006     });
1007     editMenu.setText("Edit");
1008     cut.setText("Cut");
1009     cut.addActionListener(new ActionListener()
1010     {
1011       public void actionPerformed(ActionEvent e)
1012       {
1013         cut_actionPerformed();
1014       }
1015     });
1016     upperCase.setText("To Upper Case");
1017     upperCase.addActionListener(new ActionListener()
1018     {
1019       public void actionPerformed(ActionEvent e)
1020       {
1021         changeCase(e);
1022       }
1023     });
1024     copy.setText("Copy");
1025     copy.addActionListener(new ActionListener()
1026     {
1027       public void actionPerformed(ActionEvent e)
1028       {
1029         copy_actionPerformed();
1030       }
1031     });
1032     lowerCase.setText("To Lower Case");
1033     lowerCase.addActionListener(new ActionListener()
1034     {
1035       public void actionPerformed(ActionEvent e)
1036       {
1037         changeCase(e);
1038       }
1039     });
1040     toggle.setText("Toggle Case");
1041     toggle.addActionListener(new ActionListener()
1042     {
1043       public void actionPerformed(ActionEvent e)
1044       {
1045         changeCase(e);
1046       }
1047     });
1048     pdbMenu.setText("Associate Structure with Sequence");
1049     pdbFromFile.setText("From File");
1050     pdbFromFile.addActionListener(new ActionListener()
1051     {
1052       public void actionPerformed(ActionEvent e)
1053       {
1054         pdbFromFile_actionPerformed();
1055       }
1056     });
1057     enterPDB.setText("Enter PDB Id");
1058     enterPDB.addActionListener(new ActionListener()
1059     {
1060       public void actionPerformed(ActionEvent e)
1061       {
1062         enterPDB_actionPerformed();
1063       }
1064     });
1065     discoverPDB.setText("Discover PDB ids");
1066     discoverPDB.addActionListener(new ActionListener()
1067     {
1068       public void actionPerformed(ActionEvent e)
1069       {
1070         discoverPDB_actionPerformed();
1071       }
1072     });
1073     outputMenu.setText("Output to Textbox...");
1074     sequenceFeature.setText("Create Sequence Feature");
1075     sequenceFeature.addActionListener(new ActionListener()
1076     {
1077       public void actionPerformed(ActionEvent e)
1078       {
1079         sequenceFeature_actionPerformed();
1080       }
1081     });
1082     textColour.setText("Text Colour");
1083     textColour.addActionListener(new ActionListener()
1084     {
1085       public void actionPerformed(ActionEvent e)
1086       {
1087         textColour_actionPerformed();
1088       }
1089     });
1090     jMenu1.setText("Group");
1091     structureMenu.setText("Structure");
1092     viewStructureMenu.setText("View Structure");
1093     // colStructureMenu.setText("Colour By Structure");
1094     editSequence.setText("Edit Sequence...");
1095     editSequence.addActionListener(new ActionListener()
1096     {
1097       public void actionPerformed(ActionEvent actionEvent)
1098       {
1099         editSequence_actionPerformed(actionEvent);
1100       }
1101     });
1102
1103     /*
1104      * annotationMenuItem.setText("By Annotation");
1105      * annotationMenuItem.addActionListener(new ActionListener() { public void
1106      * actionPerformed(ActionEvent actionEvent) {
1107      * annotationMenuItem_actionPerformed(actionEvent); } });
1108      */
1109     groupMenu.add(sequenceSelDetails);
1110     add(groupMenu);
1111     add(sequenceMenu);
1112     this.add(structureMenu);
1113     groupMenu.add(editMenu);
1114     groupMenu.add(outputMenu);
1115     groupMenu.add(sequenceFeature);
1116     groupMenu.add(createGroupMenuItem);
1117     groupMenu.add(unGroupMenuItem);
1118     groupMenu.add(jMenu1);
1119     sequenceMenu.add(sequenceName);
1120     sequenceMenu.add(sequenceDetails);
1121     colourMenu.add(textColour);
1122     colourMenu.add(noColourmenuItem);
1123     colourMenu.add(clustalColour);
1124     colourMenu.add(BLOSUM62Colour);
1125     colourMenu.add(PIDColour);
1126     colourMenu.add(zappoColour);
1127     colourMenu.add(taylorColour);
1128     colourMenu.add(hydrophobicityColour);
1129     colourMenu.add(helixColour);
1130     colourMenu.add(strandColour);
1131     colourMenu.add(turnColour);
1132     colourMenu.add(buriedColour);
1133     colourMenu.add(nucleotideMenuItem);
1134     if (ap.getAlignment().isNucleotide())
1135     {
1136       colourMenu.add(purinePyrimidineColour);
1137     }
1138     // colourMenu.add(covariationColour);
1139     colourMenu.add(userDefinedColour);
1140
1141     if (jalview.gui.UserDefinedColours.getUserColourSchemes() != null)
1142     {
1143       java.util.Enumeration userColours = jalview.gui.UserDefinedColours
1144               .getUserColourSchemes().keys();
1145
1146       while (userColours.hasMoreElements())
1147       {
1148         JMenuItem item = new JMenuItem(userColours.nextElement().toString());
1149         item.addActionListener(new ActionListener()
1150         {
1151           public void actionPerformed(ActionEvent evt)
1152           {
1153             userDefinedColour_actionPerformed(evt);
1154           }
1155         });
1156         colourMenu.add(item);
1157       }
1158     }
1159
1160     colourMenu.addSeparator();
1161     colourMenu.add(abovePIDColour);
1162     colourMenu.add(conservationMenuItem);
1163     // colourMenu.add(annotationMenuItem);
1164     editMenu.add(copy);
1165     editMenu.add(cut);
1166     editMenu.add(editSequence);
1167     editMenu.add(upperCase);
1168     editMenu.add(lowerCase);
1169     editMenu.add(toggle);
1170     pdbMenu.add(pdbFromFile);
1171     pdbMenu.add(enterPDB);
1172     pdbMenu.add(discoverPDB);
1173     jMenu1.add(groupName);
1174     jMenu1.add(colourMenu);
1175     jMenu1.add(showBoxes);
1176     jMenu1.add(showText);
1177     jMenu1.add(showColourText);
1178     jMenu1.add(outline);
1179     jMenu1.add(displayNonconserved);
1180     structureMenu.add(pdbMenu);
1181     structureMenu.add(viewStructureMenu);
1182     // structureMenu.add(colStructureMenu);
1183     noColourmenuItem.setText("None");
1184     noColourmenuItem.addActionListener(new java.awt.event.ActionListener()
1185     {
1186       public void actionPerformed(ActionEvent e)
1187       {
1188         noColourmenuItem_actionPerformed();
1189       }
1190     });
1191
1192     clustalColour.setText("Clustalx colours");
1193     clustalColour.addActionListener(new java.awt.event.ActionListener()
1194     {
1195       public void actionPerformed(ActionEvent e)
1196       {
1197         clustalColour_actionPerformed();
1198       }
1199     });
1200     zappoColour.setText("Zappo");
1201     zappoColour.addActionListener(new java.awt.event.ActionListener()
1202     {
1203       public void actionPerformed(ActionEvent e)
1204       {
1205         zappoColour_actionPerformed();
1206       }
1207     });
1208     taylorColour.setText("Taylor");
1209     taylorColour.addActionListener(new java.awt.event.ActionListener()
1210     {
1211       public void actionPerformed(ActionEvent e)
1212       {
1213         taylorColour_actionPerformed();
1214       }
1215     });
1216     hydrophobicityColour.setText("Hydrophobicity");
1217     hydrophobicityColour
1218             .addActionListener(new java.awt.event.ActionListener()
1219             {
1220               public void actionPerformed(ActionEvent e)
1221               {
1222                 hydrophobicityColour_actionPerformed();
1223               }
1224             });
1225     helixColour.setText("Helix propensity");
1226     helixColour.addActionListener(new java.awt.event.ActionListener()
1227     {
1228       public void actionPerformed(ActionEvent e)
1229       {
1230         helixColour_actionPerformed();
1231       }
1232     });
1233     strandColour.setText("Strand propensity");
1234     strandColour.addActionListener(new java.awt.event.ActionListener()
1235     {
1236       public void actionPerformed(ActionEvent e)
1237       {
1238         strandColour_actionPerformed();
1239       }
1240     });
1241     turnColour.setText("Turn propensity");
1242     turnColour.addActionListener(new java.awt.event.ActionListener()
1243     {
1244       public void actionPerformed(ActionEvent e)
1245       {
1246         turnColour_actionPerformed();
1247       }
1248     });
1249     buriedColour.setText("Buried Index");
1250     buriedColour.addActionListener(new java.awt.event.ActionListener()
1251     {
1252       public void actionPerformed(ActionEvent e)
1253       {
1254         buriedColour_actionPerformed();
1255       }
1256     });
1257     abovePIDColour.setText("Above % Identity");
1258     abovePIDColour.addActionListener(new java.awt.event.ActionListener()
1259     {
1260       public void actionPerformed(ActionEvent e)
1261       {
1262         abovePIDColour_actionPerformed();
1263       }
1264     });
1265     userDefinedColour.setText("User Defined...");
1266     userDefinedColour.addActionListener(new java.awt.event.ActionListener()
1267     {
1268       public void actionPerformed(ActionEvent e)
1269       {
1270         userDefinedColour_actionPerformed(e);
1271       }
1272     });
1273     PIDColour.setText("Percentage Identity");
1274     PIDColour.addActionListener(new java.awt.event.ActionListener()
1275     {
1276       public void actionPerformed(ActionEvent e)
1277       {
1278         PIDColour_actionPerformed();
1279       }
1280     });
1281     BLOSUM62Colour.setText("BLOSUM62");
1282     BLOSUM62Colour.addActionListener(new java.awt.event.ActionListener()
1283     {
1284       public void actionPerformed(ActionEvent e)
1285       {
1286         BLOSUM62Colour_actionPerformed();
1287       }
1288     });
1289     purinePyrimidineColour.setText("Purine/Pyrimidine");
1290     purinePyrimidineColour
1291             .addActionListener(new java.awt.event.ActionListener()
1292             {
1293               public void actionPerformed(ActionEvent e)
1294               {
1295                 purinePyrimidineColour_actionPerformed();
1296               }
1297             });
1298     /*
1299      * covariationColour.addActionListener(new java.awt.event.ActionListener() {
1300      * public void actionPerformed(ActionEvent e) {
1301      * covariationColour_actionPerformed(); } });
1302      */
1303
1304     conservationMenuItem.setText("Conservation");
1305     conservationMenuItem
1306             .addActionListener(new java.awt.event.ActionListener()
1307             {
1308               public void actionPerformed(ActionEvent e)
1309               {
1310                 conservationMenuItem_actionPerformed();
1311               }
1312             });
1313   }
1314
1315   protected void sequenceSelectionDetails_actionPerformed()
1316   {
1317     createSequenceDetailsReport(ap.av.getSequenceSelection());
1318   }
1319
1320   protected void sequenceDetails_actionPerformed()
1321   {
1322     createSequenceDetailsReport(new SequenceI[]
1323     { sequence });
1324   }
1325
1326   public void createSequenceDetailsReport(SequenceI[] sequences)
1327   {
1328     CutAndPasteHtmlTransfer cap = new CutAndPasteHtmlTransfer();
1329     StringBuffer contents = new StringBuffer();
1330     for (SequenceI seq : sequences)
1331     {
1332       contents.append("<p><h2>Annotation for " + seq.getDisplayId(true)
1333               + "</h2></p><p>");
1334       new SequenceAnnotationReport(null)
1335               .createSequenceAnnotationReport(
1336                       contents,
1337                       seq,
1338                       true,
1339                       true,
1340                       false,
1341                       (ap.seqPanel.seqCanvas.fr != null) ? ap.seqPanel.seqCanvas.fr.minmax
1342                               : null);
1343       contents.append("</p>");
1344     }
1345     cap.setText("<html>" + contents.toString() + "</html>");
1346
1347     Desktop.instance.addInternalFrame(cap, "Sequence Details for "
1348             + (sequences.length == 1 ? sequences[0].getDisplayId(true)
1349                     : "Selection"), 500, 400);
1350
1351   }
1352
1353   protected void showNonconserved_actionPerformed()
1354   {
1355     getGroup().setShowNonconserved(displayNonconserved.isSelected());
1356     refresh();
1357   }
1358
1359   /**
1360    * call to refresh view after settings change
1361    */
1362   void refresh()
1363   {
1364     ap.updateAnnotation();
1365     ap.paintAlignment(true);
1366
1367     PaintRefresher.Refresh(this, ap.av.getSequenceSetId());
1368   }
1369
1370   /**
1371    * DOCUMENT ME!
1372    * 
1373    * @param e
1374    *          DOCUMENT ME!
1375    */
1376   protected void clustalColour_actionPerformed()
1377   {
1378     SequenceGroup sg = getGroup();
1379     sg.cs = new ClustalxColourScheme(sg, ap.av.getHiddenRepSequences());
1380     refresh();
1381   }
1382
1383   /**
1384    * DOCUMENT ME!
1385    * 
1386    * @param e
1387    *          DOCUMENT ME!
1388    */
1389   protected void zappoColour_actionPerformed()
1390   {
1391     getGroup().cs = new ZappoColourScheme();
1392     refresh();
1393   }
1394
1395   /**
1396    * DOCUMENT ME!
1397    * 
1398    * @param e
1399    *          DOCUMENT ME!
1400    */
1401   protected void taylorColour_actionPerformed()
1402   {
1403     getGroup().cs = new TaylorColourScheme();
1404     refresh();
1405   }
1406
1407   /**
1408    * DOCUMENT ME!
1409    * 
1410    * @param e
1411    *          DOCUMENT ME!
1412    */
1413   protected void hydrophobicityColour_actionPerformed()
1414   {
1415     getGroup().cs = new HydrophobicColourScheme();
1416     refresh();
1417   }
1418
1419   /**
1420    * DOCUMENT ME!
1421    * 
1422    * @param e
1423    *          DOCUMENT ME!
1424    */
1425   protected void helixColour_actionPerformed()
1426   {
1427     getGroup().cs = new HelixColourScheme();
1428     refresh();
1429   }
1430
1431   /**
1432    * DOCUMENT ME!
1433    * 
1434    * @param e
1435    *          DOCUMENT ME!
1436    */
1437   protected void strandColour_actionPerformed()
1438   {
1439     getGroup().cs = new StrandColourScheme();
1440     refresh();
1441   }
1442
1443   /**
1444    * DOCUMENT ME!
1445    * 
1446    * @param e
1447    *          DOCUMENT ME!
1448    */
1449   protected void turnColour_actionPerformed()
1450   {
1451     getGroup().cs = new TurnColourScheme();
1452     refresh();
1453   }
1454
1455   /**
1456    * DOCUMENT ME!
1457    * 
1458    * @param e
1459    *          DOCUMENT ME!
1460    */
1461   protected void buriedColour_actionPerformed()
1462   {
1463     getGroup().cs = new BuriedColourScheme();
1464     refresh();
1465   }
1466
1467   /**
1468    * DOCUMENT ME!
1469    * 
1470    * @param e
1471    *          DOCUMENT ME!
1472    */
1473   public void nucleotideMenuItem_actionPerformed()
1474   {
1475     getGroup().cs = new NucleotideColourScheme();
1476     refresh();
1477   }
1478
1479   protected void purinePyrimidineColour_actionPerformed()
1480   {
1481     getGroup().cs = new PurinePyrimidineColourScheme();
1482     refresh();
1483   }
1484
1485   /*
1486    * protected void covariationColour_actionPerformed() { getGroup().cs = new
1487    * CovariationColourScheme(sequence.getAnnotation()[0]); refresh(); }
1488    */
1489   /**
1490    * DOCUMENT ME!
1491    * 
1492    * @param e
1493    *          DOCUMENT ME!
1494    */
1495   protected void abovePIDColour_actionPerformed()
1496   {
1497     SequenceGroup sg = getGroup();
1498     if (sg.cs == null)
1499     {
1500       return;
1501     }
1502
1503     if (abovePIDColour.isSelected())
1504     {
1505       sg.cs.setConsensus(AAFrequency.calculate(
1506               sg.getSequences(ap.av.getHiddenRepSequences()),
1507               sg.getStartRes(), sg.getEndRes() + 1));
1508
1509       int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs, getGroup()
1510               .getName());
1511
1512       sg.cs.setThreshold(threshold, ap.av.getIgnoreGapsConsensus());
1513
1514       SliderPanel.showPIDSlider();
1515     }
1516     else
1517     // remove PIDColouring
1518     {
1519       sg.cs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
1520     }
1521
1522     refresh();
1523   }
1524
1525   /**
1526    * DOCUMENT ME!
1527    * 
1528    * @param e
1529    *          DOCUMENT ME!
1530    */
1531   protected void userDefinedColour_actionPerformed(ActionEvent e)
1532   {
1533     SequenceGroup sg = getGroup();
1534
1535     if (e.getActionCommand().equals("User Defined..."))
1536     {
1537       new UserDefinedColours(ap, sg);
1538     }
1539     else
1540     {
1541       UserColourScheme udc = (UserColourScheme) UserDefinedColours
1542               .getUserColourSchemes().get(e.getActionCommand());
1543
1544       sg.cs = udc;
1545     }
1546     refresh();
1547   }
1548
1549   /**
1550    * DOCUMENT ME!
1551    * 
1552    * @param e
1553    *          DOCUMENT ME!
1554    */
1555   protected void PIDColour_actionPerformed()
1556   {
1557     SequenceGroup sg = getGroup();
1558     sg.cs = new PIDColourScheme();
1559     sg.cs.setConsensus(AAFrequency.calculate(
1560             sg.getSequences(ap.av.getHiddenRepSequences()),
1561             sg.getStartRes(), sg.getEndRes() + 1));
1562     refresh();
1563   }
1564
1565   /**
1566    * DOCUMENT ME!
1567    * 
1568    * @param e
1569    *          DOCUMENT ME!
1570    */
1571   protected void BLOSUM62Colour_actionPerformed()
1572   {
1573     SequenceGroup sg = getGroup();
1574
1575     sg.cs = new Blosum62ColourScheme();
1576
1577     sg.cs.setConsensus(AAFrequency.calculate(
1578             sg.getSequences(ap.av.getHiddenRepSequences()),
1579             sg.getStartRes(), sg.getEndRes() + 1));
1580
1581     refresh();
1582   }
1583
1584   /**
1585    * DOCUMENT ME!
1586    * 
1587    * @param e
1588    *          DOCUMENT ME!
1589    */
1590   protected void noColourmenuItem_actionPerformed()
1591   {
1592     getGroup().cs = null;
1593     refresh();
1594   }
1595
1596   /**
1597    * DOCUMENT ME!
1598    * 
1599    * @param e
1600    *          DOCUMENT ME!
1601    */
1602   protected void conservationMenuItem_actionPerformed()
1603   {
1604     SequenceGroup sg = getGroup();
1605     if (sg.cs == null)
1606     {
1607       return;
1608     }
1609
1610     if (conservationMenuItem.isSelected())
1611     {
1612       Conservation c = new Conservation("Group",
1613               ResidueProperties.propHash, 3, sg.getSequences(ap.av
1614                       .getHiddenRepSequences()), sg.getStartRes(),
1615               sg.getEndRes() + 1);
1616
1617       c.calculate();
1618       c.verdict(false, ap.av.getConsPercGaps());
1619
1620       sg.cs.setConservation(c);
1621
1622       SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
1623       SliderPanel.showConservationSlider();
1624     }
1625     else
1626     // remove ConservationColouring
1627     {
1628       sg.cs.setConservation(null);
1629     }
1630
1631     refresh();
1632   }
1633
1634   public void annotationMenuItem_actionPerformed(ActionEvent actionEvent)
1635   {
1636     SequenceGroup sg = getGroup();
1637     if (sg == null)
1638     {
1639       return;
1640     }
1641
1642     AnnotationColourGradient acg = new AnnotationColourGradient(
1643             sequence.getAnnotation()[0], null,
1644             AnnotationColourGradient.NO_THRESHOLD);
1645
1646     acg.setPredefinedColours(true);
1647     sg.cs = acg;
1648
1649     refresh();
1650   }
1651
1652   /**
1653    * DOCUMENT ME!
1654    * 
1655    * @param e
1656    *          DOCUMENT ME!
1657    */
1658   protected void groupName_actionPerformed()
1659   {
1660
1661     SequenceGroup sg = getGroup();
1662     EditNameDialog dialog = new EditNameDialog(sg.getName(),
1663             sg.getDescription(), "       Group Name ",
1664             "Group Description ", "Edit Group Name/Description",
1665             ap.alignFrame);
1666
1667     if (!dialog.accept)
1668     {
1669       return;
1670     }
1671
1672     sg.setName(dialog.getName());
1673     sg.setDescription(dialog.getDescription());
1674     refresh();
1675   }
1676
1677   /**
1678    * Get selection group - adding it to the alignment if necessary.
1679    * 
1680    * @return sequence group to operate on
1681    */
1682   SequenceGroup getGroup()
1683   {
1684     SequenceGroup sg = ap.av.getSelectionGroup();
1685     // this method won't add a new group if it already exists
1686     if (sg != null)
1687     {
1688       ap.av.getAlignment().addGroup(sg);
1689     }
1690
1691     return sg;
1692   }
1693
1694   /**
1695    * DOCUMENT ME!
1696    * 
1697    * @param e
1698    *          DOCUMENT ME!
1699    */
1700   void sequenceName_actionPerformed()
1701   {
1702     EditNameDialog dialog = new EditNameDialog(sequence.getName(),
1703             sequence.getDescription(), "       Sequence Name ",
1704             "Sequence Description ", "Edit Sequence Name/Description",
1705             ap.alignFrame);
1706
1707     if (!dialog.accept)
1708     {
1709       return;
1710     }
1711
1712     if (dialog.getName() != null)
1713     {
1714       if (dialog.getName().indexOf(" ") > -1)
1715       {
1716         JOptionPane.showMessageDialog(ap,
1717                 "Spaces have been converted to \"_\"",
1718                 "No spaces allowed in Sequence Name",
1719                 JOptionPane.WARNING_MESSAGE);
1720       }
1721
1722       sequence.setName(dialog.getName().replace(' ', '_'));
1723       ap.paintAlignment(false);
1724     }
1725
1726     sequence.setDescription(dialog.getDescription());
1727
1728     ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
1729             .getSequences());
1730
1731   }
1732
1733   /**
1734    * DOCUMENT ME!
1735    * 
1736    * @param e
1737    *          DOCUMENT ME!
1738    */
1739   void unGroupMenuItem_actionPerformed()
1740   {
1741     SequenceGroup sg = ap.av.getSelectionGroup();
1742     ap.av.getAlignment().deleteGroup(sg);
1743     ap.av.setSelectionGroup(null);
1744     refresh();
1745   }
1746   void createGroupMenuItem_actionPerformed()
1747   {
1748     getGroup(); // implicitly creates group - note - should apply defaults / use standard alignment window logic for this
1749     refresh();
1750   }
1751
1752   /**
1753    * DOCUMENT ME!
1754    * 
1755    * @param e
1756    *          DOCUMENT ME!
1757    */
1758   protected void outline_actionPerformed()
1759   {
1760     SequenceGroup sg = getGroup();
1761     Color col = JColorChooser.showDialog(this, "Select Outline Colour",
1762             Color.BLUE);
1763
1764     if (col != null)
1765     {
1766       sg.setOutlineColour(col);
1767     }
1768
1769     refresh();
1770   }
1771
1772   /**
1773    * DOCUMENT ME!
1774    * 
1775    * @param e
1776    *          DOCUMENT ME!
1777    */
1778   public void showBoxes_actionPerformed()
1779   {
1780     getGroup().setDisplayBoxes(showBoxes.isSelected());
1781     refresh();
1782   }
1783
1784   /**
1785    * DOCUMENT ME!
1786    * 
1787    * @param e
1788    *          DOCUMENT ME!
1789    */
1790   public void showText_actionPerformed()
1791   {
1792     getGroup().setDisplayText(showText.isSelected());
1793     refresh();
1794   }
1795
1796   /**
1797    * DOCUMENT ME!
1798    * 
1799    * @param e
1800    *          DOCUMENT ME!
1801    */
1802   public void showColourText_actionPerformed()
1803   {
1804     getGroup().setColourText(showColourText.isSelected());
1805     refresh();
1806   }
1807
1808   public void showLink(String url)
1809   {
1810     try
1811     {
1812       jalview.util.BrowserLauncher.openURL(url);
1813     } catch (Exception ex)
1814     {
1815       JOptionPane
1816               .showInternalMessageDialog(
1817                       Desktop.desktop,
1818                       "Unixers: Couldn't find default web browser."
1819                               + "\nAdd the full path to your browser in Preferences.",
1820                       "Web browser not found", JOptionPane.WARNING_MESSAGE);
1821
1822       ex.printStackTrace();
1823     }
1824   }
1825
1826   void hideSequences(boolean representGroup)
1827   {
1828     SequenceGroup sg = ap.av.getSelectionGroup();
1829     if (sg == null || sg.getSize() < 1)
1830     {
1831       ap.av.hideSequence(new SequenceI[]
1832       { sequence });
1833       return;
1834     }
1835
1836     ap.av.setSelectionGroup(null);
1837
1838     if (representGroup)
1839     {
1840       ap.av.hideRepSequences(sequence, sg);
1841
1842       return;
1843     }
1844
1845     int gsize = sg.getSize();
1846     SequenceI[] hseqs;
1847
1848     hseqs = new SequenceI[gsize];
1849
1850     int index = 0;
1851     for (int i = 0; i < gsize; i++)
1852     {
1853       hseqs[index++] = sg.getSequenceAt(i);
1854     }
1855
1856     ap.av.hideSequence(hseqs);
1857     // refresh(); TODO: ? needed ?
1858     ap.av.sendSelection();
1859   }
1860
1861   public void copy_actionPerformed()
1862   {
1863     ap.alignFrame.copy_actionPerformed(null);
1864   }
1865
1866   public void cut_actionPerformed()
1867   {
1868     ap.alignFrame.cut_actionPerformed(null);
1869   }
1870
1871   void changeCase(ActionEvent e)
1872   {
1873     Object source = e.getSource();
1874     SequenceGroup sg = ap.av.getSelectionGroup();
1875
1876     if (sg != null)
1877     {
1878       int[][] startEnd = ap.av.getVisibleRegionBoundaries(sg.getStartRes(),
1879               sg.getEndRes() + 1);
1880
1881       String description;
1882       int caseChange;
1883
1884       if (source == toggle)
1885       {
1886         description = "Toggle Case";
1887         caseChange = ChangeCaseCommand.TOGGLE_CASE;
1888       }
1889       else if (source == upperCase)
1890       {
1891         description = "To Upper Case";
1892         caseChange = ChangeCaseCommand.TO_UPPER;
1893       }
1894       else
1895       {
1896         description = "To Lower Case";
1897         caseChange = ChangeCaseCommand.TO_LOWER;
1898       }
1899
1900       ChangeCaseCommand caseCommand = new ChangeCaseCommand(description,
1901               sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
1902               startEnd, caseChange);
1903
1904       ap.alignFrame.addHistoryItem(caseCommand);
1905
1906       ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
1907               .getSequences());
1908
1909     }
1910   }
1911
1912   public void outputText_actionPerformed(ActionEvent e)
1913   {
1914     CutAndPasteTransfer cap = new CutAndPasteTransfer();
1915     cap.setForInput(null);
1916     Desktop.addInternalFrame(cap,
1917             "Alignment output - " + e.getActionCommand(), 600, 500);
1918
1919     String[] omitHidden = null;
1920
1921     System.out.println("PROMPT USER HERE"); // TODO: decide if a prompt happens
1922     // or we simply trust the user wants
1923     // wysiwig behaviour
1924     SequenceGroup sg = ap.av.getSelectionGroup();
1925     ColumnSelection csel = new ColumnSelection(ap.av.getColumnSelection());
1926     omitHidden = ap.av.getViewAsString(true);
1927     Alignment oal = new Alignment(ap.av.getSequenceSelection());
1928     AlignmentAnnotation[] nala = ap.av.getAlignment()
1929             .getAlignmentAnnotation();
1930     if (nala != null)
1931     {
1932       for (int i = 0; i < nala.length; i++)
1933       {
1934         AlignmentAnnotation na = nala[i];
1935         oal.addAnnotation(na);
1936       }
1937     }
1938     cap.setText(new FormatAdapter().formatSequences(e.getActionCommand(),
1939             oal, omitHidden, csel, sg));
1940     oal = null;
1941   }
1942
1943   public void pdbFromFile_actionPerformed()
1944   {
1945     jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(
1946             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
1947     chooser.setFileView(new jalview.io.JalviewFileView());
1948     chooser.setDialogTitle("Select a PDB file for "
1949             + sequence.getDisplayId(false));
1950     chooser.setToolTipText("Load a PDB file and associate it with sequence '"
1951             + sequence.getDisplayId(false) + "'");
1952
1953     int value = chooser.showOpenDialog(null);
1954
1955     if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
1956     {
1957       String choice = chooser.getSelectedFile().getPath();
1958       jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);
1959       new AssociatePdbFileWithSeq().associatePdbWithSeq(choice,
1960               jalview.io.AppletFormatAdapter.FILE, sequence, true);
1961     }
1962
1963   }
1964
1965   public void enterPDB_actionPerformed()
1966   {
1967     String id = JOptionPane.showInternalInputDialog(Desktop.desktop,
1968             "Enter PDB Id", "Enter PDB Id", JOptionPane.QUESTION_MESSAGE);
1969
1970     if (id != null && id.length() > 0)
1971     {
1972       PDBEntry entry = new PDBEntry();
1973       entry.setId(id.toUpperCase());
1974       sequence.getDatasetSequence().addPDBId(entry);
1975     }
1976   }
1977
1978   public void discoverPDB_actionPerformed()
1979   {
1980
1981     final SequenceI[] sequences = ((ap.av.getSelectionGroup() == null) ? new SequenceI[]
1982     { sequence }
1983             : ap.av.getSequenceSelection());
1984     Thread discpdb = new Thread(new Runnable()
1985     {
1986       public void run()
1987       {
1988
1989         new jalview.ws.DBRefFetcher(sequences, ap.alignFrame)
1990                 .fetchDBRefs(false);
1991       }
1992
1993     });
1994     discpdb.start();
1995   }
1996
1997   public void sequenceFeature_actionPerformed()
1998   {
1999     SequenceGroup sg = ap.av.getSelectionGroup();
2000     if (sg == null)
2001     {
2002       return;
2003     }
2004
2005     int rsize = 0, gSize = sg.getSize();
2006     SequenceI[] rseqs, seqs = new SequenceI[gSize];
2007     SequenceFeature[] tfeatures, features = new SequenceFeature[gSize];
2008
2009     for (int i = 0; i < gSize; i++)
2010     {
2011       int start = sg.getSequenceAt(i).findPosition(sg.getStartRes());
2012       int end = sg.findEndRes(sg.getSequenceAt(i));
2013       if (start <= end)
2014       {
2015         seqs[rsize] = sg.getSequenceAt(i).getDatasetSequence();
2016         features[rsize] = new SequenceFeature(null, null, null, start, end,
2017                 "Jalview");
2018         rsize++;
2019       }
2020     }
2021     rseqs = new SequenceI[rsize];
2022     tfeatures = new SequenceFeature[rsize];
2023     System.arraycopy(seqs, 0, rseqs, 0, rsize);
2024     System.arraycopy(features, 0, tfeatures, 0, rsize);
2025     features = tfeatures;
2026     seqs = rseqs;
2027     if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
2028             features, true, ap))
2029     {
2030       ap.alignFrame.setShowSeqFeatures(true);
2031       ap.highlightSearchResults(null);
2032     }
2033   }
2034
2035   public void textColour_actionPerformed()
2036   {
2037     SequenceGroup sg = getGroup();
2038     if (sg != null)
2039     {
2040       new TextColourChooser().chooseColour(ap, sg);
2041     }
2042   }
2043
2044   public void colourByStructure(String pdbid)
2045   {
2046     Annotation[] anots = ap.av.getStructureSelectionManager()
2047             .colourSequenceFromStructure(sequence, pdbid);
2048
2049     AlignmentAnnotation an = new AlignmentAnnotation("Structure",
2050             "Coloured by " + pdbid, anots);
2051
2052     ap.av.getAlignment().addAnnotation(an);
2053     an.createSequenceMapping(sequence, 0, true);
2054     // an.adjustForAlignment();
2055     ap.av.getAlignment().setAnnotationIndex(an, 0);
2056
2057     ap.adjustAnnotationHeight();
2058
2059     sequence.addAlignmentAnnotation(an);
2060
2061   }
2062
2063   public void editSequence_actionPerformed(ActionEvent actionEvent)
2064   {
2065     SequenceGroup sg = ap.av.getSelectionGroup();
2066
2067     if (sg != null)
2068     {
2069       if (sequence == null)
2070         sequence = (Sequence) sg.getSequenceAt(0);
2071
2072       EditNameDialog dialog = new EditNameDialog(
2073               sequence.getSequenceAsString(sg.getStartRes(),
2074                       sg.getEndRes() + 1), null, "Edit Sequence ", null,
2075               "Edit Sequence", ap.alignFrame);
2076
2077       if (dialog.accept)
2078       {
2079         EditCommand editCommand = new EditCommand("Edit Sequences",
2080                 EditCommand.REPLACE, dialog.getName().replace(' ',
2081                         ap.av.getGapCharacter()),
2082                 sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
2083                 sg.getStartRes(), sg.getEndRes() + 1, ap.av.getAlignment());
2084
2085         ap.alignFrame.addHistoryItem(editCommand);
2086
2087         ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
2088                 .getSequences());
2089       }
2090     }
2091   }
2092
2093 }