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