updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / gui / PopupMenu.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import MCview.*;
22
23 import jalview.analysis.*;
24
25 import jalview.datamodel.*;
26
27
28 import jalview.schemes.*;
29
30 import java.awt.*;
31 import java.awt.event.*;
32
33 import javax.swing.*;
34
35 import java.util.Vector;
36 import jalview.io.FormatAdapter;
37
38
39 /**
40  * DOCUMENT ME!
41  *
42  * @author $author$
43  * @version $Revision$
44  */
45 public class PopupMenu extends JPopupMenu
46 {
47     JMenu groupMenu = new JMenu();
48     JMenuItem groupName = new JMenuItem();
49     protected JRadioButtonMenuItem clustalColour = new JRadioButtonMenuItem();
50     protected JRadioButtonMenuItem zappoColour = new JRadioButtonMenuItem();
51     protected JRadioButtonMenuItem taylorColour = new JRadioButtonMenuItem();
52     protected JRadioButtonMenuItem hydrophobicityColour = new JRadioButtonMenuItem();
53     protected JRadioButtonMenuItem helixColour = new JRadioButtonMenuItem();
54     protected JRadioButtonMenuItem strandColour = new JRadioButtonMenuItem();
55     protected JRadioButtonMenuItem turnColour = new JRadioButtonMenuItem();
56     protected JRadioButtonMenuItem buriedColour = new JRadioButtonMenuItem();
57     protected JCheckBoxMenuItem abovePIDColour = new JCheckBoxMenuItem();
58     protected JRadioButtonMenuItem userDefinedColour = new JRadioButtonMenuItem();
59     protected JRadioButtonMenuItem PIDColour = new JRadioButtonMenuItem();
60     protected JRadioButtonMenuItem BLOSUM62Colour = new JRadioButtonMenuItem();
61     JRadioButtonMenuItem noColourmenuItem = new JRadioButtonMenuItem();
62     protected JCheckBoxMenuItem conservationMenuItem = new JCheckBoxMenuItem();
63     AlignmentPanel ap;
64     JMenu sequenceMenu = new JMenu();
65     JMenuItem sequenceName = new JMenuItem();
66     Sequence sequence;
67     JMenuItem unGroupMenuItem = new JMenuItem();
68     JMenuItem outline = new JMenuItem();
69     JRadioButtonMenuItem nucleotideMenuItem = new JRadioButtonMenuItem();
70     JMenu colourMenu = new JMenu();
71     JCheckBoxMenuItem showBoxes = new JCheckBoxMenuItem();
72     JCheckBoxMenuItem showText = new JCheckBoxMenuItem();
73     JCheckBoxMenuItem showColourText = new JCheckBoxMenuItem();
74   JMenu editMenu = new JMenu();
75   JMenuItem cut = new JMenuItem();
76   JMenuItem copy = new JMenuItem();
77   JMenuItem upperCase = new JMenuItem();
78   JMenuItem lowerCase = new JMenuItem();
79   JMenuItem toggle = new JMenuItem();
80   JMenu pdbMenu = new JMenu();
81   JMenuItem pdbFromFile = new JMenuItem();
82   JMenuItem enterPDB = new JMenuItem();
83   JMenuItem discoverPDB = new JMenuItem();
84   JMenu outputMenu = new JMenu();
85
86   /**
87      * Creates a new PopupMenu object.
88      *
89      * @param ap DOCUMENT ME!
90      * @param seq DOCUMENT ME!
91      */
92     public PopupMenu(final AlignmentPanel ap, Sequence seq, Vector links)
93     {
94         ///////////////////////////////////////////////////////////
95         // If this is activated from the sequence panel, the user may want to
96         // edit or annotate a particular residue. Therefore display the residue menu
97         //
98         // If from the IDPanel, we must display the sequence menu
99         //////////////////////////////////////////////////////////
100         this.ap = ap;
101         sequence = seq;
102
103         ButtonGroup colours = new ButtonGroup();
104         colours.add(noColourmenuItem);
105         colours.add(clustalColour);
106         colours.add(zappoColour);
107         colours.add(taylorColour);
108         colours.add(hydrophobicityColour);
109         colours.add(helixColour);
110         colours.add(strandColour);
111         colours.add(turnColour);
112         colours.add(buriedColour);
113         colours.add(abovePIDColour);
114         colours.add(userDefinedColour);
115         colours.add(PIDColour);
116         colours.add(BLOSUM62Colour);
117
118         for (int i = 0; i < jalview.io.FormatAdapter.WRITEABLE_FORMATS.length; i++)
119         {
120           JMenuItem item = new JMenuItem( jalview.io.FormatAdapter.WRITEABLE_FORMATS[i] );
121
122           item.addActionListener(new java.awt.event.ActionListener()
123           {
124             public void actionPerformed(ActionEvent e)
125             {
126               outputText_actionPerformed(e);
127             }
128           });
129
130           outputMenu.add(item);
131         }
132
133
134         try
135         {
136             jbInit();
137         }
138         catch (Exception e)
139         {
140             e.printStackTrace();
141         }
142
143
144         if (seq != null)
145         {
146           int start = Math.max(sequence.getName().length()-15, 0);
147           sequenceMenu.setText(sequence.getName().substring(start));
148
149           JMenuItem menuItem;
150           if( seq.getDatasetSequence().getPDBId() != null)
151           {
152             java.util.Enumeration e = seq.getDatasetSequence().getPDBId().
153                 elements();
154
155             while (e.hasMoreElements())
156             {
157               final PDBEntry pdb = (PDBEntry) e.nextElement();
158
159               menuItem = new JMenuItem();
160               menuItem.setText("View PDB entry: " + pdb.getId());
161               menuItem.addActionListener(new java.awt.event.ActionListener()
162               {
163                 public void actionPerformed(ActionEvent e)
164                 {
165                   new PDBViewer(pdb, sequence, ap.seqPanel.seqCanvas);
166                 }
167               });
168               sequenceMenu.add(menuItem);
169             }
170           }
171
172           menuItem = new JMenuItem("Hide Sequences");
173           menuItem.addActionListener(new java.awt.event.ActionListener()
174               {
175                 public void actionPerformed(ActionEvent e)
176                 {
177                   hideSequences(false);
178                 }
179               });
180           add(menuItem);
181
182           if(ap.av.getSelectionGroup() !=null
183              && ap.av.getSelectionGroup().getSize(false)>1)
184           {
185             menuItem = new JMenuItem("Represent Group with " + seq.getName());
186             menuItem.addActionListener(new java.awt.event.ActionListener()
187             {
188               public void actionPerformed(ActionEvent e)
189               {
190                 hideSequences(true);
191               }
192             });
193             sequenceMenu.add(menuItem);
194           }
195
196           if (ap.av.hasHiddenRows)
197           {
198             final int index = ap.av.alignment.findIndex(seq);
199
200             if (ap.av.adjustForHiddenSeqs(index) -
201                 ap.av.adjustForHiddenSeqs(index - 1) > 1)
202             {
203               menuItem = new JMenuItem("Reveal Sequences");
204               menuItem.addActionListener(new ActionListener()
205               {
206                 public void actionPerformed(ActionEvent e)
207                 {
208                   ap.av.showSequence(index);
209                   if (ap.overviewPanel != null)
210                     ap.overviewPanel.updateOverviewImage();
211                 }
212               });
213               add(menuItem);
214             }
215
216             menuItem = new JMenuItem("Reveal All");
217             menuItem.addActionListener(new ActionListener()
218                 {
219                   public void actionPerformed(ActionEvent e)
220                   {
221                     ap.av.showAllHiddenSeqs();
222                     if (ap.overviewPanel != null)
223                       ap.overviewPanel.updateOverviewImage();
224                     }
225                 });
226
227             add(menuItem);
228           }
229
230
231         }
232
233
234
235         SequenceGroup sg = ap.av.getSelectionGroup();
236
237         if (sg != null)
238         {
239             groupName.setText(sg.getName());
240
241             if (sg.cs instanceof ZappoColourScheme)
242             {
243                 zappoColour.setSelected(true);
244             }
245             else if (sg.cs instanceof TaylorColourScheme)
246             {
247                 taylorColour.setSelected(true);
248             }
249             else if (sg.cs instanceof PIDColourScheme)
250             {
251                 PIDColour.setSelected(true);
252             }
253             else if (sg.cs instanceof Blosum62ColourScheme)
254             {
255                 BLOSUM62Colour.setSelected(true);
256             }
257             else if (sg.cs instanceof UserColourScheme)
258             {
259                 userDefinedColour.setSelected(true);
260             }
261             else if (sg.cs instanceof HydrophobicColourScheme)
262             {
263                 hydrophobicityColour.setSelected(true);
264             }
265             else if (sg.cs instanceof HelixColourScheme)
266             {
267                 helixColour.setSelected(true);
268             }
269             else if (sg.cs instanceof StrandColourScheme)
270             {
271                 strandColour.setSelected(true);
272             }
273             else if (sg.cs instanceof TurnColourScheme)
274             {
275                 turnColour.setSelected(true);
276             }
277             else if (sg.cs instanceof BuriedColourScheme)
278             {
279                 buriedColour.setSelected(true);
280             }
281             else if (sg.cs instanceof ClustalxColourScheme)
282             {
283                 clustalColour.setSelected(true);
284             }
285             else
286             {
287                 noColourmenuItem.setSelected(true);
288             }
289
290             if (sg.cs!=null && sg.cs.conservationApplied())
291             {
292                 conservationMenuItem.setSelected(true);
293             }
294
295             showText.setSelected(sg.getDisplayText());
296             showColourText.setSelected(sg.getColourText());
297             showBoxes.setSelected(sg.getDisplayBoxes());
298         }
299         else
300         {
301           groupMenu.setVisible(false);
302           editMenu.setVisible(false);
303         }
304
305         if (!ap.av.alignment.getGroups().contains(sg))
306         {
307             unGroupMenuItem.setVisible(false);
308         }
309
310
311         if (seq == null)
312         {
313             sequenceMenu.setVisible(false);
314             pdbMenu.setVisible(false);
315         }
316
317         if(links != null && links.size()>0)
318         {
319           JMenu linkMenu = new JMenu("Link");
320           JMenuItem item;
321           for(int i=0; i<links.size(); i++)
322           {
323             String link = links.elementAt(i).toString();
324             final String label = link.substring(0, link.indexOf("|"));
325             item = new JMenuItem(label);
326             final String url;
327
328             if (link.indexOf("$SEQUENCE_ID$") > -1)
329             {
330               String id = seq.getName();
331               if (id.indexOf("|") > -1)
332                 id = id.substring(id.lastIndexOf("|") + 1);
333
334               url = link.substring(link.indexOf("|") + 1,
335                                    link.indexOf("$SEQUENCE_ID$"))
336                   + id +
337                   link.substring(link.indexOf("$SEQUENCE_ID$") + 13);
338             }
339             else
340               url = link.substring(link.lastIndexOf("|")+1);
341
342
343             item.addActionListener(new java.awt.event.ActionListener()
344             {
345                 public void actionPerformed(ActionEvent e)
346                 {
347                     showLink(url);
348                 }
349             });
350
351             linkMenu.add(item);
352           }
353           if(sequence!=null)
354             sequenceMenu.add(linkMenu);
355           else
356             add(linkMenu);
357         }
358     }
359
360
361     /**
362      * DOCUMENT ME!
363      *
364      * @throws Exception DOCUMENT ME!
365      */
366     private void jbInit() throws Exception
367     {
368         groupMenu.setText("Group");
369         groupMenu.setText("Selection");
370         groupName.setText("Name");
371         groupName.addActionListener(new java.awt.event.ActionListener()
372             {
373                 public void actionPerformed(ActionEvent e)
374                 {
375                     groupName_actionPerformed(e);
376                 }
377             });
378         sequenceMenu.setText("Sequence");
379         sequenceName.setText("Edit Name/Description");
380         sequenceName.addActionListener(new java.awt.event.ActionListener()
381             {
382                 public void actionPerformed(ActionEvent e)
383                 {
384                     sequenceName_actionPerformed(e);
385                 }
386             });
387         PIDColour.setFocusPainted(false);
388         unGroupMenuItem.setText("Remove Group");
389         unGroupMenuItem.addActionListener(new java.awt.event.ActionListener()
390             {
391                 public void actionPerformed(ActionEvent e)
392                 {
393                     unGroupMenuItem_actionPerformed(e);
394                 }
395             });
396
397         outline.setText("Border colour");
398         outline.addActionListener(new java.awt.event.ActionListener()
399             {
400                 public void actionPerformed(ActionEvent e)
401                 {
402                     outline_actionPerformed(e);
403                 }
404             });
405         nucleotideMenuItem.setText("Nucleotide");
406         nucleotideMenuItem.addActionListener(new ActionListener()
407             {
408                 public void actionPerformed(ActionEvent e)
409                 {
410                     nucleotideMenuItem_actionPerformed(e);
411                 }
412             });
413         colourMenu.setText("Group Colour");
414         showBoxes.setText("Boxes");
415         showBoxes.setState(true);
416         showBoxes.addActionListener(new ActionListener()
417             {
418                 public void actionPerformed(ActionEvent e)
419                 {
420                     showBoxes_actionPerformed(e);
421                 }
422             });
423         showText.setText("Text");
424         showText.setState(true);
425         showText.addActionListener(new ActionListener()
426             {
427                 public void actionPerformed(ActionEvent e)
428                 {
429                     showText_actionPerformed(e);
430                 }
431             });
432         showColourText.setText("Colour Text");
433         showColourText.addActionListener(new ActionListener()
434             {
435                 public void actionPerformed(ActionEvent e)
436                 {
437                     showColourText_actionPerformed(e);
438                 }
439             });
440     editMenu.setText("Edit");
441     cut.setText("Cut");
442     cut.addActionListener(new ActionListener()
443     {
444       public void actionPerformed(ActionEvent e)
445       {
446         cut_actionPerformed(e);
447       }
448     });
449     upperCase.setText("To Upper Case");
450     upperCase.addActionListener(new ActionListener()
451     {
452       public void actionPerformed(ActionEvent e)
453       {
454         upperCase_actionPerformed(e);
455       }
456     });
457     copy.setText("Copy");
458     copy.addActionListener(new ActionListener()
459     {
460       public void actionPerformed(ActionEvent e)
461       {
462         copy_actionPerformed(e);
463       }
464     });
465     lowerCase.setText("To Lower Case");
466     lowerCase.addActionListener(new ActionListener()
467     {
468       public void actionPerformed(ActionEvent e)
469       {
470         lowerCase_actionPerformed(e);
471       }
472     });
473     toggle.setText("Toggle Case");
474     toggle.addActionListener(new ActionListener()
475     {
476       public void actionPerformed(ActionEvent e)
477       {
478         toggle_actionPerformed(e);
479       }
480     });
481     pdbMenu.setText("Associate Structure with Sequence");
482     pdbFromFile.setText("From File");
483     pdbFromFile.addActionListener(new ActionListener()
484     {
485       public void actionPerformed(ActionEvent e)
486       {
487         pdbFromFile_actionPerformed(e);
488       }
489     });
490     enterPDB.setText("Enter PDB Id");
491     enterPDB.addActionListener(new ActionListener()
492     {
493       public void actionPerformed(ActionEvent e)
494       {
495         enterPDB_actionPerformed(e);
496       }
497     });
498     discoverPDB.setText("Discover PDB ids");
499     discoverPDB.addActionListener(new ActionListener()
500     {
501       public void actionPerformed(ActionEvent e)
502       {
503         discoverPDB_actionPerformed(e);
504       }
505     });
506     outputMenu.setText("Output to Textbox...");
507     add(groupMenu);
508
509     add(sequenceMenu);
510     groupMenu.add(editMenu);
511     groupMenu.add(outputMenu);
512     groupMenu.addSeparator();
513     groupMenu.add(groupName);
514     groupMenu.add(unGroupMenuItem);
515         groupMenu.add(colourMenu);
516     groupMenu.add(showBoxes);
517         groupMenu.add(showText);
518         groupMenu.add(showColourText);
519     groupMenu.add(outline);
520         sequenceMenu.add(sequenceName);
521     sequenceMenu.add(pdbMenu);
522     colourMenu.add(noColourmenuItem);
523         colourMenu.add(clustalColour);
524         colourMenu.add(BLOSUM62Colour);
525         colourMenu.add(PIDColour);
526         colourMenu.add(zappoColour);
527         colourMenu.add(taylorColour);
528         colourMenu.add(hydrophobicityColour);
529         colourMenu.add(helixColour);
530         colourMenu.add(strandColour);
531         colourMenu.add(turnColour);
532         colourMenu.add(buriedColour);
533         colourMenu.add(nucleotideMenuItem);
534         colourMenu.add(userDefinedColour);
535
536         if(jalview.gui.UserDefinedColours.getUserColourSchemes()!=null)
537         {
538           java.util.Enumeration userColours = jalview.gui.UserDefinedColours.
539               getUserColourSchemes().keys();
540
541           while (userColours.hasMoreElements())
542           {
543             JMenuItem item = new JMenuItem(userColours.
544                 nextElement().toString());
545             item.addActionListener(new ActionListener()
546             {
547               public void actionPerformed(ActionEvent evt)
548               {
549                 userDefinedColour_actionPerformed(evt);
550               }
551             });
552             colourMenu.add(item);
553           }
554         }
555
556
557         colourMenu.addSeparator();
558         colourMenu.add(abovePIDColour);
559         colourMenu.add(conservationMenuItem);
560     editMenu.add(copy);
561     editMenu.add(cut);
562     editMenu.add(upperCase);
563     editMenu.add(lowerCase);
564     editMenu.add(toggle);
565     pdbMenu.add(pdbFromFile);
566     pdbMenu.add(enterPDB);
567     pdbMenu.add(discoverPDB);
568     noColourmenuItem.setText("None");
569         noColourmenuItem.addActionListener(new java.awt.event.ActionListener()
570             {
571                 public void actionPerformed(ActionEvent e)
572                 {
573                     noColourmenuItem_actionPerformed(e);
574                 }
575             });
576
577         clustalColour.setText("Clustalx colours");
578         clustalColour.addActionListener(new java.awt.event.ActionListener()
579             {
580                 public void actionPerformed(ActionEvent e)
581                 {
582                     clustalColour_actionPerformed(e);
583                 }
584             });
585         zappoColour.setText("Zappo");
586         zappoColour.addActionListener(new java.awt.event.ActionListener()
587             {
588                 public void actionPerformed(ActionEvent e)
589                 {
590                     zappoColour_actionPerformed(e);
591                 }
592             });
593         taylorColour.setText("Taylor");
594         taylorColour.addActionListener(new java.awt.event.ActionListener()
595             {
596                 public void actionPerformed(ActionEvent e)
597                 {
598                     taylorColour_actionPerformed(e);
599                 }
600             });
601         hydrophobicityColour.setText("Hydrophobicity");
602         hydrophobicityColour.addActionListener(new java.awt.event.ActionListener()
603             {
604                 public void actionPerformed(ActionEvent e)
605                 {
606                     hydrophobicityColour_actionPerformed(e);
607                 }
608             });
609         helixColour.setText("Helix propensity");
610         helixColour.addActionListener(new java.awt.event.ActionListener()
611             {
612                 public void actionPerformed(ActionEvent e)
613                 {
614                     helixColour_actionPerformed(e);
615                 }
616             });
617         strandColour.setText("Strand propensity");
618         strandColour.addActionListener(new java.awt.event.ActionListener()
619             {
620                 public void actionPerformed(ActionEvent e)
621                 {
622                     strandColour_actionPerformed(e);
623                 }
624             });
625         turnColour.setText("Turn propensity");
626         turnColour.addActionListener(new java.awt.event.ActionListener()
627             {
628                 public void actionPerformed(ActionEvent e)
629                 {
630                     turnColour_actionPerformed(e);
631                 }
632             });
633         buriedColour.setText("Buried Index");
634         buriedColour.addActionListener(new java.awt.event.ActionListener()
635             {
636                 public void actionPerformed(ActionEvent e)
637                 {
638                     buriedColour_actionPerformed(e);
639                 }
640             });
641         abovePIDColour.setText("Above % Identity");
642         abovePIDColour.addActionListener(new java.awt.event.ActionListener()
643             {
644                 public void actionPerformed(ActionEvent e)
645                 {
646                     abovePIDColour_actionPerformed(e);
647                 }
648             });
649         userDefinedColour.setText("User Defined...");
650         userDefinedColour.addActionListener(new java.awt.event.ActionListener()
651             {
652                 public void actionPerformed(ActionEvent e)
653                 {
654                     userDefinedColour_actionPerformed(e);
655                 }
656             });
657         PIDColour.setText("Percentage Identity");
658         PIDColour.addActionListener(new java.awt.event.ActionListener()
659             {
660                 public void actionPerformed(ActionEvent e)
661                 {
662                     PIDColour_actionPerformed(e);
663                 }
664             });
665         BLOSUM62Colour.setText("BLOSUM62");
666         BLOSUM62Colour.addActionListener(new java.awt.event.ActionListener()
667             {
668                 public void actionPerformed(ActionEvent e)
669                 {
670                     BLOSUM62Colour_actionPerformed(e);
671                 }
672             });
673         conservationMenuItem.setText("Conservation");
674         conservationMenuItem.addActionListener(new java.awt.event.ActionListener()
675             {
676                 public void actionPerformed(ActionEvent e)
677                 {
678                     conservationMenuItem_actionPerformed(e);
679                 }
680             });
681     }
682
683     /**
684      * DOCUMENT ME!
685      */
686     void refresh()
687     {
688         if (ap.overviewPanel != null)
689           ap.overviewPanel.updateOverviewImage();
690
691         ap.seqPanel.repaint();
692     }
693
694     /**
695      * DOCUMENT ME!
696      *
697      * @param e DOCUMENT ME!
698      */
699     protected void clustalColour_actionPerformed(ActionEvent e)
700     {
701         SequenceGroup sg = getGroup();
702         sg.cs = new ClustalxColourScheme(sg.getSequences(true),
703                 ap.av.alignment.getWidth());
704         refresh();
705     }
706
707     /**
708      * DOCUMENT ME!
709      *
710      * @param e DOCUMENT ME!
711      */
712     protected void zappoColour_actionPerformed(ActionEvent e)
713     {
714         getGroup().cs = new ZappoColourScheme();
715         refresh();
716     }
717
718     /**
719      * DOCUMENT ME!
720      *
721      * @param e DOCUMENT ME!
722      */
723     protected void taylorColour_actionPerformed(ActionEvent e)
724     {
725         getGroup().cs = new TaylorColourScheme();
726         refresh();
727     }
728
729     /**
730      * DOCUMENT ME!
731      *
732      * @param e DOCUMENT ME!
733      */
734     protected void hydrophobicityColour_actionPerformed(ActionEvent e)
735     {
736         getGroup().cs = new HydrophobicColourScheme();
737         refresh();
738     }
739
740     /**
741      * DOCUMENT ME!
742      *
743      * @param e DOCUMENT ME!
744      */
745     protected void helixColour_actionPerformed(ActionEvent e)
746     {
747         getGroup().cs = new HelixColourScheme();
748         refresh();
749     }
750
751     /**
752      * DOCUMENT ME!
753      *
754      * @param e DOCUMENT ME!
755      */
756     protected void strandColour_actionPerformed(ActionEvent e)
757     {
758         getGroup().cs = new StrandColourScheme();
759         refresh();
760     }
761
762     /**
763      * DOCUMENT ME!
764      *
765      * @param e DOCUMENT ME!
766      */
767     protected void turnColour_actionPerformed(ActionEvent e)
768     {
769         getGroup().cs = new TurnColourScheme();
770         refresh();
771     }
772
773     /**
774      * DOCUMENT ME!
775      *
776      * @param e DOCUMENT ME!
777      */
778     protected void buriedColour_actionPerformed(ActionEvent e)
779     {
780         getGroup().cs = new BuriedColourScheme();
781         refresh();
782     }
783
784     /**
785      * DOCUMENT ME!
786      *
787      * @param e DOCUMENT ME!
788      */
789     public void nucleotideMenuItem_actionPerformed(ActionEvent e)
790     {
791         getGroup().cs = new NucleotideColourScheme();
792         refresh();
793     }
794
795     /**
796      * DOCUMENT ME!
797      *
798      * @param e DOCUMENT ME!
799      */
800     protected void abovePIDColour_actionPerformed(ActionEvent e)
801     {
802         SequenceGroup sg = getGroup();
803         if(sg.cs==null)
804           return;
805
806         if (abovePIDColour.isSelected())
807         {
808             sg.cs.setConsensus(AAFrequency.calculate(
809                     sg.getSequences(true), 0,
810                     ap.av.alignment.getWidth()));
811
812             int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs,
813                     getGroup().getName());
814
815            sg.cs.setThreshold(threshold, ap.av.getIgnoreGapsConsensus());
816
817             SliderPanel.showPIDSlider();
818         }
819         else // remove PIDColouring
820         {
821             sg.cs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
822         }
823
824         refresh();
825     }
826
827     /**
828      * DOCUMENT ME!
829      *
830      * @param e DOCUMENT ME!
831      */
832     protected void userDefinedColour_actionPerformed(ActionEvent e)
833     {
834        SequenceGroup sg = getGroup();
835
836        if (e.getActionCommand().equals("User Defined..."))
837          new UserDefinedColours(ap, sg);
838        else
839        {
840          UserColourScheme udc = (UserColourScheme) UserDefinedColours.
841              getUserColourSchemes().get(e.getActionCommand());
842
843          sg.cs = udc;
844        }
845      }
846     /**
847      * DOCUMENT ME!
848      *
849      * @param e DOCUMENT ME!
850      */
851     protected void PIDColour_actionPerformed(ActionEvent e)
852     {
853         SequenceGroup sg = getGroup();
854         sg.cs = new PIDColourScheme();
855         sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(true), 0,
856                 ap.av.alignment.getWidth()));
857         refresh();
858     }
859
860     /**
861      * DOCUMENT ME!
862      *
863      * @param e DOCUMENT ME!
864      */
865     protected void BLOSUM62Colour_actionPerformed(ActionEvent e)
866     {
867         SequenceGroup sg = getGroup();
868
869         sg.cs = new Blosum62ColourScheme();
870
871         sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(true), 0,
872                 ap.av.alignment.getWidth()));
873
874         refresh();
875     }
876
877     /**
878      * DOCUMENT ME!
879      *
880      * @param e DOCUMENT ME!
881      */
882     protected void noColourmenuItem_actionPerformed(ActionEvent e)
883     {
884         getGroup().cs = null;
885         refresh();
886     }
887
888     /**
889      * DOCUMENT ME!
890      *
891      * @param e DOCUMENT ME!
892      */
893     protected void conservationMenuItem_actionPerformed(ActionEvent e)
894     {
895         SequenceGroup sg = getGroup();
896         if(sg.cs==null)
897           return;
898
899         if (conservationMenuItem.isSelected())
900         {
901             Conservation c = new Conservation("Group",
902                     ResidueProperties.propHash, 3,
903                     sg.getSequences(true), 0,
904                     ap.av.alignment.getWidth());
905
906             c.calculate();
907             c.verdict(false, ap.av.ConsPercGaps);
908
909             sg.cs.setConservation(c);
910
911             SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
912             SliderPanel.showConservationSlider();
913         }
914         else // remove ConservationColouring
915         {
916             sg.cs.setConservation(null);
917         }
918
919         refresh();
920     }
921
922     /**
923      * DOCUMENT ME!
924      *
925      * @param e DOCUMENT ME!
926      */
927     protected void groupName_actionPerformed(ActionEvent e)
928     {
929         SequenceGroup sg = getGroup();
930         String reply = JOptionPane.showInternalInputDialog(Desktop.desktop,
931                 "Enter new group name", "Edit group name",
932                 JOptionPane.QUESTION_MESSAGE);
933
934         if (reply == null)
935         {
936             return;
937         }
938
939         sg.setName(reply);
940         groupName.setText(reply);
941     }
942
943     /**
944      * DOCUMENT ME!
945      *
946      * @param e DOCUMENT ME!
947      */
948     protected void analyze_actionPerformed(ActionEvent e)
949     {
950         CutAndPasteTransfer cap = new CutAndPasteTransfer();
951         JInternalFrame frame = new JInternalFrame();
952         frame.setContentPane(cap);
953         Desktop.addInternalFrame(frame, "Analyze this - ", 400, 300);
954
955         SequenceGroup sg = getGroup();
956         StringBuffer sb = new StringBuffer();
957
958         for (int i = 0; i < sg.getSize(false); i++)
959         {
960             Sequence tmp = (Sequence) sg.getSequences(false).elementAt(i);
961             sb.append(tmp.getSequence(sg.getStartRes(), sg.getEndRes() + 1));
962             sb.append("\n");
963         }
964
965         sb.append("Something amazing will happen soon");
966         cap.setText(sb.toString());
967     }
968
969     /**
970      * DOCUMENT ME!
971      *
972      * @return DOCUMENT ME!
973      */
974     SequenceGroup getGroup()
975     {
976         SequenceGroup sg = ap.av.getSelectionGroup();
977       // this method won't add a new group if it already exists
978         if(sg!=null)
979           ap.av.alignment.addGroup(sg);
980
981         return sg;
982     }
983
984     /**
985      * DOCUMENT ME!
986      *
987      * @param e DOCUMENT ME!
988      */
989     void sequenceName_actionPerformed(ActionEvent e)
990     {
991       JLabel idlabel = new JLabel(  "       Sequence Name ");
992       JLabel desclabel = new JLabel("Sequence Description ");
993       idlabel.setFont(new Font("Courier", Font.PLAIN, 12));
994       desclabel.setFont(new Font("Courier", Font.PLAIN, 12));
995       JTextField id = new JTextField(sequence.getName(), 40);
996       JTextField description = new JTextField(sequence.getDescription(), 40);
997       JPanel panel = new JPanel(new BorderLayout());
998       JPanel panel2 = new JPanel(new BorderLayout());
999       panel2.add(idlabel, BorderLayout.WEST);
1000       panel2.add(id, BorderLayout.CENTER);
1001       panel.add(panel2, BorderLayout.NORTH);
1002       panel2 = new JPanel(new BorderLayout());
1003       panel2.add(desclabel, BorderLayout.WEST);
1004       panel2.add(description, BorderLayout.CENTER);
1005       panel.add(panel2, BorderLayout.SOUTH);
1006
1007
1008        int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
1009           panel, "Edit Sequence Name/Description",
1010           JOptionPane.OK_CANCEL_OPTION );
1011
1012
1013         if (reply != JOptionPane.OK_OPTION )
1014         {
1015             return;
1016         }
1017
1018         String s = id.getText();
1019
1020         if (s != null)
1021         {
1022             if (s.indexOf(" ") > -1)
1023             {
1024                 JOptionPane.showMessageDialog(ap,
1025                     "Spaces have been converted to \"_\"",
1026                     "No spaces allowed in Sequence Name",
1027                     JOptionPane.WARNING_MESSAGE);
1028             }
1029
1030             s = s.replace(' ', '_');
1031             sequence.getDatasetSequence().setName(s);
1032             sequence.setName(s);
1033             ap.repaint();
1034         }
1035
1036         sequence.getDatasetSequence().setDescription(description.getText());
1037         sequence.setDescription(description.getText());
1038     }
1039
1040     /**
1041      * DOCUMENT ME!
1042      *
1043      * @param e DOCUMENT ME!
1044      */
1045     void unGroupMenuItem_actionPerformed(ActionEvent e)
1046     {
1047         SequenceGroup sg = ap.av.getSelectionGroup();
1048         ap.av.alignment.deleteGroup(sg);
1049         ap.av.setSelectionGroup(null);
1050         refresh();
1051     }
1052
1053
1054     /**
1055      * DOCUMENT ME!
1056      *
1057      * @param e DOCUMENT ME!
1058      */
1059     protected void outline_actionPerformed(ActionEvent e)
1060     {
1061         SequenceGroup sg = getGroup();
1062         Color col = JColorChooser.showDialog(this, "Select Outline Colour",
1063                 Color.BLUE);
1064
1065         if (col != null)
1066         {
1067             sg.setOutlineColour(col);
1068         }
1069
1070         refresh();
1071     }
1072
1073     /**
1074      * DOCUMENT ME!
1075      *
1076      * @param e DOCUMENT ME!
1077      */
1078     public void showBoxes_actionPerformed(ActionEvent e)
1079     {
1080         getGroup().setDisplayBoxes(showBoxes.isSelected());
1081         refresh();
1082     }
1083
1084     /**
1085      * DOCUMENT ME!
1086      *
1087      * @param e DOCUMENT ME!
1088      */
1089     public void showText_actionPerformed(ActionEvent e)
1090     {
1091         getGroup().setDisplayText(showText.isSelected());
1092         refresh();
1093     }
1094
1095     /**
1096      * DOCUMENT ME!
1097      *
1098      * @param e DOCUMENT ME!
1099      */
1100     public void showColourText_actionPerformed(ActionEvent e)
1101     {
1102         getGroup().setColourText(showColourText.isSelected());
1103         refresh();
1104     }
1105
1106     public void showLink(String url)
1107     {
1108       try
1109       {
1110         jalview.util.BrowserLauncher.openURL(url);
1111       }
1112       catch (Exception ex)
1113       {
1114         JOptionPane.showInternalMessageDialog(Desktop.desktop,
1115             "Unixers: Couldn't find default web browser."
1116            +"\nAdd the full path to your browser in Preferences.",
1117            "Web browser not found", JOptionPane.WARNING_MESSAGE );
1118
1119
1120         ex.printStackTrace();
1121       }
1122     }
1123
1124     void hideSequences(boolean representGroup)
1125     {
1126       SequenceGroup sg = ap.av.getSelectionGroup();
1127       if(sg==null || sg.getSize(false)<1)
1128       {
1129         ap.av.hideSequence(sequence);
1130         return;
1131       }
1132
1133         int index = 0;
1134         while(index < sg.getSize(false))
1135         {
1136           if(representGroup && sg.getSequenceAt(index)!=sequence)
1137           {
1138             sequence.addHiddenSequence(sg.getSequenceAt(index));
1139             ap.av.hideSequence(sg.getSequenceAt(index));
1140           }
1141           else if(!representGroup)
1142           {
1143             ap.av.hideSequence(sg.getSequenceAt(index));
1144           }
1145           index ++;
1146         }
1147
1148         ap.av.setSelectionGroup(null);
1149     }
1150
1151   public void copy_actionPerformed(ActionEvent e)
1152   {
1153     ap.alignFrame.copy_actionPerformed(null);
1154   }
1155
1156   public void cut_actionPerformed(ActionEvent e)
1157   {
1158     ap.alignFrame.cut_actionPerformed(null);
1159   }
1160
1161   public void upperCase_actionPerformed(ActionEvent e)
1162   {
1163     changeCase(e.getSource());
1164   }
1165
1166   public void lowerCase_actionPerformed(ActionEvent e)
1167   {
1168     changeCase(e.getSource());
1169   }
1170
1171   public void toggle_actionPerformed(ActionEvent e)
1172   {
1173     changeCase(e.getSource());
1174   }
1175
1176   void changeCase(Object source)
1177   {
1178     SequenceGroup sg = ap.av.getSelectionGroup();
1179     if (sg != null)
1180     {
1181       for (int g = 0; g < sg.getSize(true); g++)
1182       {
1183         int start = sg.getStartRes();
1184         int end = sg.getEndRes() + 1;
1185
1186         do
1187         {
1188           if (ap.av.hasHiddenColumns)
1189           {
1190             end = ap.av.colSel.getHiddenBoundaryRight(start);
1191             if (start == end)
1192               end = sg.getEndRes() + 1;
1193             if (end > sg.getEndRes())
1194               end = sg.getEndRes() + 1;
1195           }
1196
1197           if (source == toggle)
1198             ( (SequenceI) sg.getSequences(true).elementAt(g))
1199                 .toggleCase(start, end);
1200           else
1201             ( (SequenceI) sg.getSequences(true).elementAt(g))
1202                 .changeCase(source == upperCase, start, end);
1203
1204           if (ap.av.hasHiddenColumns)
1205           {
1206             start = ap.av.colSel.adjustForHiddenColumns(end);
1207             start = ap.av.colSel.getHiddenBoundaryLeft(start) + 1;
1208           }
1209
1210         }
1211         while (end < sg.getEndRes());
1212       }
1213       ap.repaint();
1214     }
1215   }
1216
1217   public void outputText_actionPerformed(ActionEvent e)
1218   {
1219     CutAndPasteTransfer cap = new CutAndPasteTransfer();
1220     Desktop.addInternalFrame(cap,
1221                              "Alignment output - " + e.getActionCommand(), 600,
1222                              500);
1223
1224     String [] omitHidden = null;
1225
1226     if(ap.av.hasHiddenColumns)
1227     {
1228       System.out.println("PROMPT USER HERE");
1229       omitHidden = ap.av.getViewAsString(true);
1230     }
1231
1232     cap.setText(new FormatAdapter().formatSequences(
1233         e.getActionCommand(),
1234         ap.av.getSelectionAsNewSequence(),
1235         omitHidden));
1236   }
1237
1238
1239   public void pdbFromFile_actionPerformed(ActionEvent e)
1240   {
1241      jalview.io.JalviewFileChooser chooser
1242          = new jalview.io.JalviewFileChooser(jalview.bin.Cache.
1243          getProperty(
1244              "LAST_DIRECTORY"));
1245      chooser.setFileView(new jalview.io.JalviewFileView());
1246      chooser.setDialogTitle("Select a PDB file");
1247      chooser.setToolTipText("Load a PDB file");
1248
1249      int value = chooser.showOpenDialog(null);
1250
1251      if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
1252      {
1253        PDBEntry entry = new PDBEntry();
1254        String choice = chooser.getSelectedFile().getPath();
1255        jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);
1256        try
1257        {
1258          MCview.PDBfile pdbfile = new MCview.PDBfile(choice,
1259                                        jalview.io.AppletFormatAdapter.FILE);
1260
1261          if (pdbfile.id == null)
1262          {
1263            String reply = JOptionPane.showInternalInputDialog(
1264                Desktop.desktop,
1265                "Couldn't find a PDB id in the file supplied."
1266                + "Please enter an Id to identify this structure.",
1267                "No PDB Id in File", JOptionPane.QUESTION_MESSAGE);
1268            if (reply == null)
1269              return;
1270
1271            entry.setId(reply);
1272          }
1273          else
1274            entry.setId(pdbfile.id);
1275        }
1276        catch (java.io.IOException ex)
1277        {
1278          ex.printStackTrace();
1279        }
1280
1281        entry.setFile(choice);
1282        sequence.getDatasetSequence().addPDBId(entry);
1283      }
1284
1285   }
1286
1287   public void enterPDB_actionPerformed(ActionEvent e)
1288   {
1289     String id = JOptionPane.showInternalInputDialog(Desktop.desktop,
1290         "Enter PDB Id", "Enter PDB Id", JOptionPane.QUESTION_MESSAGE);
1291
1292     if (id != null && id.length() > 0)
1293     {
1294       PDBEntry entry = new PDBEntry();
1295       entry.setId(id);
1296       sequence.getDatasetSequence()
1297           .addPDBId(entry);
1298     }
1299   }
1300
1301   public void discoverPDB_actionPerformed(ActionEvent e)
1302   {
1303     new jalview.io.DBRefFetcher(
1304              ap.av.getAlignment(), ap.alignFrame).fetchDBRefs(false);
1305   }
1306 }