(JAL-969) removed TODOs
[jalview.git] / src / jalview / appletgui / APopupMenu.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.appletgui;
19
20 import java.util.*;
21
22 import java.awt.*;
23 import java.awt.event.*;
24
25 import jalview.analysis.*;
26 import jalview.commands.*;
27 import jalview.datamodel.*;
28 import jalview.schemes.*;
29 import jalview.util.UrlLink;
30 import jalview.io.AppletFormatAdapter;
31
32 public class APopupMenu extends java.awt.PopupMenu implements
33         ActionListener, ItemListener
34 {
35   Menu groupMenu = new Menu();
36
37   MenuItem editGroupName = new MenuItem();
38
39   protected MenuItem clustalColour = new MenuItem();
40
41   protected MenuItem zappoColour = new MenuItem();
42
43   protected MenuItem taylorColour = new MenuItem();
44
45   protected MenuItem hydrophobicityColour = new MenuItem();
46
47   protected MenuItem helixColour = new MenuItem();
48
49   protected MenuItem strandColour = new MenuItem();
50
51   protected MenuItem turnColour = new MenuItem();
52
53   protected MenuItem buriedColour = new MenuItem();
54
55   protected CheckboxMenuItem abovePIDColour = new CheckboxMenuItem();
56
57   protected MenuItem userDefinedColour = new MenuItem();
58
59   protected MenuItem PIDColour = new MenuItem();
60
61   protected MenuItem BLOSUM62Colour = new MenuItem();
62
63   MenuItem noColourmenuItem = new MenuItem();
64
65   protected CheckboxMenuItem conservationMenuItem = new CheckboxMenuItem();
66
67   final AlignmentPanel ap;
68
69   MenuItem unGroupMenuItem = new MenuItem();
70
71   MenuItem nucleotideMenuItem = new MenuItem();
72
73   Menu colourMenu = new Menu();
74
75   CheckboxMenuItem showBoxes = new CheckboxMenuItem();
76
77   CheckboxMenuItem showText = new CheckboxMenuItem();
78
79   CheckboxMenuItem showColourText = new CheckboxMenuItem();
80
81   CheckboxMenuItem displayNonconserved = new CheckboxMenuItem();
82
83   Menu editMenu = new Menu("Edit");
84
85   MenuItem copy = new MenuItem("Copy (Jalview Only)");
86
87   MenuItem cut = new MenuItem("Cut (Jalview Only)");
88
89   MenuItem toUpper = new MenuItem("To Upper Case");
90
91   MenuItem toLower = new MenuItem("To Lower Case");
92
93   MenuItem toggleCase = new MenuItem("Toggle Case");
94
95   Menu outputmenu = new Menu();
96
97   Menu seqMenu = new Menu();
98
99   MenuItem pdb = new MenuItem();
100
101   MenuItem hideSeqs = new MenuItem();
102
103   MenuItem repGroup = new MenuItem();
104
105   MenuItem sequenceName = new MenuItem("Edit Name/Description");
106
107   MenuItem sequenceFeature = new MenuItem("Create Sequence Feature");
108
109   MenuItem editSequence = new MenuItem("Edit Sequence");
110
111   Sequence seq;
112
113   MenuItem revealAll = new MenuItem();
114
115   MenuItem revealSeq = new MenuItem();
116   /**
117    * index of sequence to be revealed
118    */
119   int revealSeq_index=-1;
120   Menu menu1 = new Menu();
121
122   public APopupMenu(AlignmentPanel apanel, final Sequence seq, Vector links)
123   {
124     // /////////////////////////////////////////////////////////
125     // If this is activated from the sequence panel, the user may want to
126     // edit or annotate a particular residue. Therefore display the residue menu
127     //
128     // If from the IDPanel, we must display the sequence menu
129     // ////////////////////////////////////////////////////////
130
131     this.ap = apanel;
132     this.seq = seq;
133
134     try
135     {
136       jbInit();
137     } catch (Exception e)
138     {
139       e.printStackTrace();
140     }
141
142     for (int i = 0; i < jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS.length; i++)
143     {
144       MenuItem item = new MenuItem(
145               jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS[i]);
146
147       item.addActionListener(this);
148       outputmenu.add(item);
149     }
150
151     SequenceGroup sg = ap.av.getSelectionGroup();
152
153     if (sg != null && sg.getSize() > 0)
154     {
155       editGroupName.setLabel("Name: "+sg.getName());
156       showText.setState(sg.getDisplayText());
157       showColourText.setState(sg.getColourText());
158       showBoxes.setState(sg.getDisplayBoxes());
159       displayNonconserved.setState(sg.getShowNonconserved());
160       if (!ap.av.getAlignment().getGroups().contains(sg))
161       {
162         groupMenu.remove(unGroupMenuItem);
163       }
164
165     }
166     else
167     {
168       remove(hideSeqs);
169       remove(groupMenu);
170     }
171
172     if (links != null && links.size() > 0)
173     {
174       Menu linkMenu = new Menu("Link");
175       String link;
176       for (int i = 0; i < links.size(); i++)
177       {
178         link = links.elementAt(i).toString();
179         UrlLink urlLink = new UrlLink(link);
180         if (!urlLink.isValid())
181         {
182           System.err.println(urlLink.getInvalidMessage());
183           continue;
184         }
185         final String target = urlLink.getTarget(); // link.substring(0,
186         // link.indexOf("|"));
187         final String label = urlLink.getLabel();
188         if (seq!=null && urlLink.isDynamic())
189         {
190
191           // collect matching db-refs
192           DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(
193                   seq.getDBRef(), new String[]
194                   { target });
195           // collect id string too
196           String id = seq.getName();
197           String descr = seq.getDescription();
198           if (descr != null && descr.length() < 1)
199           {
200             descr = null;
201           }
202           if (dbr != null)
203           {
204             for (int r = 0; r < dbr.length; r++)
205             {
206               if (id != null && dbr[r].getAccessionId().equals(id))
207               {
208                 // suppress duplicate link creation for the bare sequence ID
209                 // string with this link
210                 id = null;
211               }
212               // create Bare ID link for this RUL
213               String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(),
214                       true);
215               if (urls != null)
216               {
217                 for (int u = 0; u < urls.length; u += 2)
218                 {
219                   addshowLink(linkMenu, label + "|" + urls[u], urls[u + 1]);
220                 }
221               }
222             }
223           }
224           if (id != null)
225           {
226             // create Bare ID link for this RUL
227             String[] urls = urlLink.makeUrls(id, true);
228             if (urls != null)
229             {
230               for (int u = 0; u < urls.length; u += 2)
231               {
232                 addshowLink(linkMenu, label, urls[u + 1]);
233               }
234             }
235             // addshowLink(linkMenu, target, url_pref + id + url_suff);
236           }
237           // Now construct URLs from description but only try to do it for regex
238           // URL links
239           if (descr != null && urlLink.getRegexReplace() != null)
240           {
241             // create link for this URL from description only if regex matches
242             String[] urls = urlLink.makeUrls(descr, true);
243             if (urls != null)
244             {
245               for (int u = 0; u < urls.length; u += 2)
246               {
247                 addshowLink(linkMenu, label, urls[u + 1]);
248               }
249             }
250           }
251         }
252         else
253         {
254           addshowLink(linkMenu, target, urlLink.getUrl_prefix()); // link.substring(link.lastIndexOf("|")+1));
255         }
256         /*
257          * final String url;
258          * 
259          * if (link.indexOf("$SEQUENCE_ID$") > -1) { // Substitute SEQUENCE_ID
260          * string and any matching database reference accessions String url_pref
261          * = link.substring(link.indexOf("|") + 1,
262          * link.indexOf("$SEQUENCE_ID$"));
263          * 
264          * String url_suff = link.substring(link.indexOf("$SEQUENCE_ID$") + 13);
265          * // collect matching db-refs DBRefEntry[] dbr =
266          * jalview.util.DBRefUtils.selectRefs(seq.getDBRef(), new
267          * String[]{target}); // collect id string too String id =
268          * seq.getName(); if (id.indexOf("|") > -1) { id =
269          * id.substring(id.lastIndexOf("|") + 1); } if (dbr!=null) { for (int
270          * r=0;r<dbr.length; r++) { if (dbr[r].getAccessionId().equals(id)) { //
271          * suppress duplicate link creation for the bare sequence ID string with
272          * this link id = null; } addshowLink(linkMenu,
273          * dbr[r].getSource()+"|"+dbr[r].getAccessionId(), target,
274          * url_pref+dbr[r].getAccessionId()+url_suff); } } if (id!=null) { //
275          * create Bare ID link for this RUL addshowLink(linkMenu, target,
276          * url_pref + id + url_suff); } } else { addshowLink(linkMenu, target,
277          * link.substring(link.lastIndexOf("|")+1)); }
278          */
279       }
280       if (linkMenu.getItemCount() > 0)
281       {
282         if (seq != null)
283         {
284           seqMenu.add(linkMenu);
285         }
286         else
287         {
288           add(linkMenu);
289         }
290       }
291     }
292     // TODO: add group link menu entry here
293     if (seq != null)
294     {
295       seqMenu.setLabel(seq.getName());
296       repGroup.setLabel("Represent Group with " + seq.getName());
297     }
298     else
299     {
300       remove(seqMenu);
301     }
302
303     if (!ap.av.hasHiddenRows())
304     {
305       remove(revealAll);
306       remove(revealSeq);
307     } else {
308       final int index = ap.av.getAlignment().findIndex(seq);
309
310       if (ap.av.adjustForHiddenSeqs(index)
311               - ap.av.adjustForHiddenSeqs(index - 1) > 1)
312       {
313         revealSeq_index=index;
314       } else {
315         remove(revealSeq);
316       }
317     }
318   }
319
320   /**
321    * add a show URL menu item to the given linkMenu
322    * 
323    * @param linkMenu
324    * @param target
325    *          - menu label string
326    * @param url
327    *          - url to open
328    */
329   private void addshowLink(Menu linkMenu, final String target,
330           final String url)
331   {
332     addshowLink(linkMenu, target, target, url);
333   }
334
335   /**
336    * add a show URL menu item to the given linkMenu
337    * 
338    * @param linkMenu
339    * @param target
340    *          - URL target window
341    * @param label
342    *          - menu label string
343    * @param url
344    *          - url to open
345    */
346   private void addshowLink(Menu linkMenu, final String target,
347           final String label, final String url)
348   {
349     MenuItem item = new MenuItem(label);
350     item.addActionListener(new java.awt.event.ActionListener()
351     {
352       public void actionPerformed(ActionEvent e)
353       {
354         ap.alignFrame.showURL(url, target);
355       }
356     });
357     linkMenu.add(item);
358   }
359
360   public void itemStateChanged(ItemEvent evt)
361   {
362     if (evt.getSource() == abovePIDColour)
363     {
364       abovePIDColour_itemStateChanged();
365     }
366     else if (evt.getSource() == showColourText)
367     {
368       showColourText_itemStateChanged();
369     }
370     else if (evt.getSource() == showText)
371     {
372       showText_itemStateChanged();
373     }
374     else if (evt.getSource() == showBoxes)
375     {
376       showBoxes_itemStateChanged();
377     }
378     else if (evt.getSource() == displayNonconserved)
379     {
380       this.showNonconserved_itemStateChanged();
381     }
382   }
383
384   public void actionPerformed(ActionEvent evt)
385   {
386     Object source = evt.getSource();
387     if (source == clustalColour)
388     {
389       clustalColour_actionPerformed();
390     }
391     else if (source == zappoColour)
392     {
393       zappoColour_actionPerformed();
394     }
395     else if (source == taylorColour)
396     {
397       taylorColour_actionPerformed();
398     }
399     else if (source == hydrophobicityColour)
400     {
401       hydrophobicityColour_actionPerformed();
402     }
403     else if (source == helixColour)
404     {
405       helixColour_actionPerformed();
406     }
407     else if (source == strandColour)
408     {
409       strandColour_actionPerformed();
410     }
411     else if (source == turnColour)
412     {
413       turnColour_actionPerformed();
414     }
415     else if (source == buriedColour)
416     {
417       buriedColour_actionPerformed();
418     }
419     else if (source == nucleotideMenuItem)
420     {
421       nucleotideMenuItem_actionPerformed();
422     }
423
424     else if (source == userDefinedColour)
425     {
426       userDefinedColour_actionPerformed();
427     }
428     else if (source == PIDColour)
429     {
430       PIDColour_actionPerformed();
431     }
432     else if (source == BLOSUM62Colour)
433     {
434       BLOSUM62Colour_actionPerformed();
435     }
436     else if (source == noColourmenuItem)
437     {
438       noColourmenuItem_actionPerformed();
439     }
440     else if (source == conservationMenuItem)
441     {
442       conservationMenuItem_itemStateChanged();
443     }
444     else if (source == unGroupMenuItem)
445     {
446       unGroupMenuItem_actionPerformed();
447     }
448
449     else if (source == sequenceName)
450     {
451       editName();
452     }
453     else if (source == pdb)
454     {
455       addPDB();
456     }
457     else if (source == hideSeqs)
458     {
459       hideSequences(false);
460     }
461     else if (source == repGroup)
462     {
463       hideSequences(true);
464     }
465     else if (source == revealSeq)
466     {
467       ap.av.showSequence(revealSeq_index);
468     }
469     else if (source == revealAll)
470     {
471       ap.av.showAllHiddenSeqs();
472     }
473
474     else if (source == editGroupName)
475     {
476       EditNameDialog dialog = new EditNameDialog(getGroup().getName(),
477               getGroup().getDescription(), "       Group Name",
478               "Group Description", ap.alignFrame,
479               "Edit Group Name / Description", 500, 100, true);
480
481       if (dialog.accept)
482       {
483         getGroup().setName(dialog.getName().replace(' ', '_'));
484         getGroup().setDescription(dialog.getDescription());
485       }
486
487     }
488     else if (source == copy)
489     {
490       ap.alignFrame.copy_actionPerformed();
491     }
492     else if (source == cut)
493     {
494       ap.alignFrame.cut_actionPerformed();
495     }
496     else if (source == editSequence)
497     {
498       SequenceGroup sg = ap.av.getSelectionGroup();
499
500       if (sg != null)
501       {
502         if (seq == null)
503           seq = (Sequence) sg.getSequenceAt(0);
504
505         EditNameDialog dialog = new EditNameDialog(seq.getSequenceAsString(
506                 sg.getStartRes(), sg.getEndRes() + 1), null,
507                 "Edit Sequence ", null,
508
509                 ap.alignFrame, "Edit Sequence", 500, 100, true);
510
511         if (dialog.accept)
512         {
513           EditCommand editCommand = new EditCommand("Edit Sequences",
514                   EditCommand.REPLACE, dialog.getName().replace(' ',
515                           ap.av.getGapCharacter()),
516                   sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
517                   sg.getStartRes(), sg.getEndRes() + 1, ap.av.getAlignment());
518
519           ap.alignFrame.addHistoryItem(editCommand);
520
521           ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
522                   .getSequences());
523         }
524       }
525     }
526     else if (source == toUpper || source == toLower || source == toggleCase)
527     {
528       SequenceGroup sg = ap.av.getSelectionGroup();
529       Vector regions = new Vector();
530       if (sg != null)
531       {
532         int[][] startEnd = ap.av.getVisibleRegionBoundaries(sg.getStartRes(),
533                 sg.getEndRes() + 1);
534
535         String description;
536         int caseChange;
537
538         if (source == toggleCase)
539         {
540           description = "Toggle Case";
541           caseChange = ChangeCaseCommand.TOGGLE_CASE;
542         }
543         else if (source == toUpper)
544         {
545           description = "To Upper Case";
546           caseChange = ChangeCaseCommand.TO_UPPER;
547         }
548         else
549         {
550           description = "To Lower Case";
551           caseChange = ChangeCaseCommand.TO_LOWER;
552         }
553
554         ChangeCaseCommand caseCommand = new ChangeCaseCommand(description,
555                 sg.getSequencesAsArray(ap.av.getHiddenRepSequences()), startEnd,
556                 caseChange);
557
558         ap.alignFrame.addHistoryItem(caseCommand);
559
560         ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
561                 .getSequences());
562
563       }
564     }
565     else if (source == sequenceFeature)
566     {
567       SequenceGroup sg = ap.av.getSelectionGroup();
568       if (sg == null)
569       {
570         return;
571       }
572
573       int rsize = 0, gSize = sg.getSize();
574       SequenceI[] rseqs, seqs = new SequenceI[gSize];
575       SequenceFeature[] tfeatures, features = new SequenceFeature[gSize];
576
577       for (int i = 0; i < gSize; i++)
578       {
579         int start = sg.getSequenceAt(i).findPosition(sg.getStartRes());
580         int end = sg.findEndRes(sg.getSequenceAt(i));
581         if (start <= end)
582         {
583           seqs[rsize] = sg.getSequenceAt(i);
584           features[rsize] = new SequenceFeature(null, null, null, start,
585                   end, "Jalview");
586           rsize++;
587         }
588       }
589       rseqs = new SequenceI[rsize];
590       tfeatures = new SequenceFeature[rsize];
591       System.arraycopy(seqs, 0, rseqs, 0, rsize);
592       System.arraycopy(features, 0, tfeatures, 0, rsize);
593       features = tfeatures;
594       seqs = rseqs;
595
596       if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
597               features, true, ap))
598       {
599         ap.alignFrame.sequenceFeatures.setState(true);
600         ap.av.showSequenceFeatures(true);
601         ap.highlightSearchResults(null);
602       }
603     }
604     else
605     {
606       outputText(evt);
607     }
608
609   }
610
611   void outputText(ActionEvent e)
612   {
613     CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);
614
615     Frame frame = new Frame();
616     frame.add(cap);
617     jalview.bin.JalviewLite.addFrame(frame,
618             "Selection output - " + e.getActionCommand(), 600, 500);
619     // JBPNote: getSelectionAsNewSequence behaviour has changed - this method now returns a full copy of sequence data
620     // TODO consider using getSequenceSelection instead here
621
622     cap.setText(new jalview.io.AppletFormatAdapter().formatSequences(
623             e.getActionCommand(),
624             new Alignment(ap.av.getSelectionAsNewSequence()),
625             ap.av.showJVSuffix));
626
627   }
628
629   void editName()
630   {
631     EditNameDialog dialog = new EditNameDialog(seq.getName(),
632             seq.getDescription(), "       Sequence Name",
633             "Sequence Description", ap.alignFrame,
634             "Edit Sequence Name / Description", 500, 100, true);
635
636     if (dialog.accept)
637     {
638       seq.setName(dialog.getName());
639       seq.setDescription(dialog.getDescription());
640       ap.paintAlignment(false);
641     }
642   }
643
644   void addPDB()
645   {
646     if (seq.getPDBId() != null)
647     {
648       PDBEntry entry = (PDBEntry) seq.getPDBId().firstElement();
649
650       if (ap.av.applet.jmolAvailable)
651         new jalview.appletgui.AppletJmol(entry, new Sequence[]
652         { seq }, null, ap, AppletFormatAdapter.URL);
653       else
654         new MCview.AppletPDBViewer(entry, new Sequence[]
655         { seq }, null, ap, AppletFormatAdapter.URL);
656
657     }
658     else
659     {
660       CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);
661       cap.setText("Paste your PDB file here.");
662       cap.setPDBImport(seq);
663       Frame frame = new Frame();
664       frame.add(cap);
665       jalview.bin.JalviewLite.addFrame(frame, "Paste PDB file for sequence "+seq.getName(), 400, 300);
666     }
667   }
668
669   private void jbInit() throws Exception
670   {
671     groupMenu.setLabel("Group");
672     groupMenu.setLabel("Selection");
673     sequenceFeature.addActionListener(this);
674
675     editGroupName.addActionListener(this);
676     unGroupMenuItem.setLabel("Remove Group");
677     unGroupMenuItem.addActionListener(this);
678
679     nucleotideMenuItem.setLabel("Nucleotide");
680     nucleotideMenuItem.addActionListener(this);
681     conservationMenuItem.addItemListener(this);
682     abovePIDColour.addItemListener(this);
683     colourMenu.setLabel("Group Colour");
684     showBoxes.setLabel("Boxes");
685     showBoxes.setState(true);
686     showBoxes.addItemListener(this);
687     sequenceName.addActionListener(this);
688     displayNonconserved.setLabel("Show Nonconserved");
689     displayNonconserved.setState(false);
690     displayNonconserved.addItemListener(this);
691     showText.setLabel("Text");
692     showText.addItemListener(this);
693     showColourText.setLabel("Colour Text");
694     showColourText.addItemListener(this);
695     outputmenu.setLabel("Output to Textbox...");
696     seqMenu.setLabel("Sequence");
697     pdb.setLabel("View PDB Structure");
698     hideSeqs.setLabel("Hide Sequences");
699     repGroup.setLabel("Represent Group with");
700     revealAll.setLabel("Reveal All");
701     revealSeq.setLabel("Reveal Sequences");
702     menu1.setLabel("Group");
703     add(groupMenu);
704     this.add(seqMenu);
705     this.add(hideSeqs);
706     this.add(revealSeq);
707     this.add(revealAll);
708     groupMenu.add(editGroupName);
709     groupMenu.add(editMenu);
710     groupMenu.add(outputmenu);
711     groupMenu.add(sequenceFeature);
712     groupMenu.add(menu1);
713
714     colourMenu.add(noColourmenuItem);
715     colourMenu.add(clustalColour);
716     colourMenu.add(BLOSUM62Colour);
717     colourMenu.add(PIDColour);
718     colourMenu.add(zappoColour);
719     colourMenu.add(taylorColour);
720     colourMenu.add(hydrophobicityColour);
721     colourMenu.add(helixColour);
722     colourMenu.add(strandColour);
723     colourMenu.add(turnColour);
724     colourMenu.add(buriedColour);
725     colourMenu.add(nucleotideMenuItem);
726     colourMenu.add(userDefinedColour);
727     colourMenu.addSeparator();
728     colourMenu.add(abovePIDColour);
729     colourMenu.add(conservationMenuItem);
730
731     noColourmenuItem.setLabel("None");
732     noColourmenuItem.addActionListener(this);
733
734     clustalColour.setLabel("Clustalx colours");
735     clustalColour.addActionListener(this);
736     zappoColour.setLabel("Zappo");
737     zappoColour.addActionListener(this);
738     taylorColour.setLabel("Taylor");
739     taylorColour.addActionListener(this);
740     hydrophobicityColour.setLabel("Hydrophobicity");
741     hydrophobicityColour.addActionListener(this);
742     helixColour.setLabel("Helix propensity");
743     helixColour.addActionListener(this);
744     strandColour.setLabel("Strand propensity");
745     strandColour.addActionListener(this);
746     turnColour.setLabel("Turn propensity");
747     turnColour.addActionListener(this);
748     buriedColour.setLabel("Buried Index");
749     buriedColour.addActionListener(this);
750     abovePIDColour.setLabel("Above % Identity");
751
752     userDefinedColour.setLabel("User Defined");
753     userDefinedColour.addActionListener(this);
754     PIDColour.setLabel("Percentage Identity");
755     PIDColour.addActionListener(this);
756     BLOSUM62Colour.setLabel("BLOSUM62");
757     BLOSUM62Colour.addActionListener(this);
758     conservationMenuItem.setLabel("Conservation");
759
760     editMenu.add(copy);
761     copy.addActionListener(this);
762     editMenu.add(cut);
763     cut.addActionListener(this);
764
765     editMenu.add(editSequence);
766     editSequence.addActionListener(this);
767
768     editMenu.add(toUpper);
769     toUpper.addActionListener(this);
770     editMenu.add(toLower);
771     toLower.addActionListener(this);
772     editMenu.add(toggleCase);
773     seqMenu.add(sequenceName);
774     if (!ap.av.applet.useXtrnalSviewer)
775     {
776       seqMenu.add(pdb);
777     }
778     seqMenu.add(repGroup);
779     menu1.add(unGroupMenuItem);
780     menu1.add(colourMenu);
781     menu1.add(showBoxes);
782     menu1.add(showText);
783     menu1.add(showColourText);
784     menu1.add(displayNonconserved);
785     toggleCase.addActionListener(this);
786     pdb.addActionListener(this);
787     hideSeqs.addActionListener(this);
788     repGroup.addActionListener(this);
789     revealAll.addActionListener(this);
790     revealSeq.addActionListener(this);
791   }
792
793   void refresh()
794   {
795     ap.paintAlignment(true);
796   }
797
798   protected void clustalColour_actionPerformed()
799   {
800     SequenceGroup sg = getGroup();
801     sg.cs = new ClustalxColourScheme(
802             sg.getSequences(ap.av.getHiddenRepSequences()),
803             ap.av.getAlignment().getWidth());
804     refresh();
805   }
806
807   protected void zappoColour_actionPerformed()
808   {
809     getGroup().cs = new ZappoColourScheme();
810     refresh();
811   }
812
813   protected void taylorColour_actionPerformed()
814   {
815     getGroup().cs = new TaylorColourScheme();
816     refresh();
817   }
818
819   protected void hydrophobicityColour_actionPerformed()
820   {
821     getGroup().cs = new HydrophobicColourScheme();
822     refresh();
823   }
824
825   protected void helixColour_actionPerformed()
826   {
827     getGroup().cs = new HelixColourScheme();
828     refresh();
829   }
830
831   protected void strandColour_actionPerformed()
832   {
833     getGroup().cs = new StrandColourScheme();
834     refresh();
835   }
836
837   protected void turnColour_actionPerformed()
838   {
839     getGroup().cs = new TurnColourScheme();
840     refresh();
841   }
842
843   protected void buriedColour_actionPerformed()
844   {
845     getGroup().cs = new BuriedColourScheme();
846     refresh();
847   }
848
849   public void nucleotideMenuItem_actionPerformed()
850   {
851     getGroup().cs = new NucleotideColourScheme();
852     refresh();
853   }
854
855   protected void abovePIDColour_itemStateChanged()
856   {
857     SequenceGroup sg = getGroup();
858     if (sg.cs == null)
859     {
860       return;
861     }
862
863     if (abovePIDColour.getState())
864     {
865       sg.cs.setConsensus(AAFrequency.calculate(
866               sg.getSequences(ap.av.getHiddenRepSequences()), 0,
867               ap.av.getAlignment().getWidth()));
868       int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs, getGroup()
869               .getName());
870
871       sg.cs.setThreshold(threshold, ap.av.getIgnoreGapsConsensus());
872
873       SliderPanel.showPIDSlider();
874
875     }
876     else
877     // remove PIDColouring
878     {
879       sg.cs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
880     }
881
882     refresh();
883
884   }
885
886   protected void userDefinedColour_actionPerformed()
887   {
888     new UserDefinedColours(ap, getGroup());
889   }
890
891   protected void PIDColour_actionPerformed()
892   {
893     SequenceGroup sg = getGroup();
894     sg.cs = new PIDColourScheme();
895     sg.cs.setConsensus(AAFrequency.calculate(
896             sg.getSequences(ap.av.getHiddenRepSequences()), 0,
897             ap.av.getAlignment().getWidth()));
898     refresh();
899   }
900
901   protected void BLOSUM62Colour_actionPerformed()
902   {
903     SequenceGroup sg = getGroup();
904
905     sg.cs = new Blosum62ColourScheme();
906
907     sg.cs.setConsensus(AAFrequency.calculate(
908             sg.getSequences(ap.av.getHiddenRepSequences()), 0,
909             ap.av.getAlignment().getWidth()));
910
911     refresh();
912   }
913
914   protected void noColourmenuItem_actionPerformed()
915   {
916     getGroup().cs = null;
917     refresh();
918   }
919
920   protected void conservationMenuItem_itemStateChanged()
921   {
922     SequenceGroup sg = getGroup();
923     if (sg.cs == null)
924     {
925       return;
926     }
927
928     if (conservationMenuItem.getState())
929     {
930
931       sg.cs.setConservation(Conservation.calculateConservation("Group",
932               ResidueProperties.propHash, 3,
933               sg.getSequences(ap.av.getHiddenRepSequences()), 0,
934               ap.av.getAlignment().getWidth(),
935               false, ap.av.getConsPercGaps(),false));
936       SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
937       SliderPanel.showConservationSlider();
938     }
939     else
940     // remove ConservationColouring
941     {
942       sg.cs.setConservation(null);
943     }
944
945     refresh();
946   }
947
948   SequenceGroup getGroup()
949   {
950     SequenceGroup sg = ap.av.getSelectionGroup();
951
952     // this method won't add a new group if it already exists
953     if (sg != null)
954     {
955       ap.av.getAlignment().addGroup(sg);
956     }
957
958     return sg;
959   }
960
961   void unGroupMenuItem_actionPerformed()
962   {
963     SequenceGroup sg = ap.av.getSelectionGroup();
964     ap.av.getAlignment().deleteGroup(sg);
965     ap.av.setSelectionGroup(null);
966     ap.paintAlignment(true);
967   }
968
969   public void showColourText_itemStateChanged()
970   {
971     getGroup().setColourText(showColourText.getState());
972     refresh();
973   }
974
975   public void showText_itemStateChanged()
976   {
977     getGroup().setDisplayText(showText.getState());
978     refresh();
979   }
980
981   public void showNonconserved_itemStateChanged()
982   {
983     getGroup().setShowNonconserved(this.displayNonconserved.getState());
984     refresh();
985   }
986
987   public void showBoxes_itemStateChanged()
988   {
989     getGroup().setDisplayBoxes(showBoxes.getState());
990     refresh();
991   }
992
993   void hideSequences(boolean representGroup)
994   {
995     SequenceGroup sg = ap.av.getSelectionGroup();
996     if (sg == null || sg.getSize() < 1)
997     {
998       ap.av.hideSequence(new SequenceI[]
999       { seq });
1000       return;
1001     }
1002
1003     ap.av.setSelectionGroup(null);
1004
1005     if (representGroup)
1006     {
1007       ap.av.hideRepSequences(seq, sg);
1008
1009       return;
1010     }
1011
1012     int gsize = sg.getSize();
1013     SequenceI[] hseqs;
1014
1015     hseqs = new SequenceI[gsize];
1016
1017     int index = 0;
1018     for (int i = 0; i < gsize; i++)
1019     {
1020       hseqs[index++] = sg.getSequenceAt(i);
1021     }
1022
1023     ap.av.hideSequence(hseqs);
1024     ap.av.sendSelection();
1025   }
1026
1027 }