(JAL-969) refactored alignmentViewport base class to own package and pulled up most...
[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         // TODO test change case behaves identically in JvLite
533         int[][] startEnd = ap.av.getVisibleRegionBoundaries(sg.getStartRes(),
534                 sg.getEndRes() + 1);
535
536         String description;
537         int caseChange;
538
539         if (source == toggleCase)
540         {
541           description = "Toggle Case";
542           caseChange = ChangeCaseCommand.TOGGLE_CASE;
543         }
544         else if (source == toUpper)
545         {
546           description = "To Upper Case";
547           caseChange = ChangeCaseCommand.TO_UPPER;
548         }
549         else
550         {
551           description = "To Lower Case";
552           caseChange = ChangeCaseCommand.TO_LOWER;
553         }
554
555         ChangeCaseCommand caseCommand = new ChangeCaseCommand(description,
556                 sg.getSequencesAsArray(ap.av.getHiddenRepSequences()), startEnd,
557                 caseChange);
558
559         ap.alignFrame.addHistoryItem(caseCommand);
560
561         ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
562                 .getSequences());
563
564       }
565     }
566     else if (source == sequenceFeature)
567     {
568       SequenceGroup sg = ap.av.getSelectionGroup();
569       if (sg == null)
570       {
571         return;
572       }
573
574       int rsize = 0, gSize = sg.getSize();
575       SequenceI[] rseqs, seqs = new SequenceI[gSize];
576       SequenceFeature[] tfeatures, features = new SequenceFeature[gSize];
577
578       for (int i = 0; i < gSize; i++)
579       {
580         int start = sg.getSequenceAt(i).findPosition(sg.getStartRes());
581         int end = sg.findEndRes(sg.getSequenceAt(i));
582         if (start <= end)
583         {
584           seqs[rsize] = sg.getSequenceAt(i);
585           features[rsize] = new SequenceFeature(null, null, null, start,
586                   end, "Jalview");
587           rsize++;
588         }
589       }
590       rseqs = new SequenceI[rsize];
591       tfeatures = new SequenceFeature[rsize];
592       System.arraycopy(seqs, 0, rseqs, 0, rsize);
593       System.arraycopy(features, 0, tfeatures, 0, rsize);
594       features = tfeatures;
595       seqs = rseqs;
596
597       if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
598               features, true, ap))
599       {
600         ap.alignFrame.sequenceFeatures.setState(true);
601         ap.av.showSequenceFeatures(true);
602         ap.highlightSearchResults(null);
603       }
604     }
605     else
606     {
607       outputText(evt);
608     }
609
610   }
611
612   void outputText(ActionEvent e)
613   {
614     CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);
615
616     Frame frame = new Frame();
617     frame.add(cap);
618     jalview.bin.JalviewLite.addFrame(frame,
619             "Selection output - " + e.getActionCommand(), 600, 500);
620     // JBPNote: getSelectionAsNewSequence behaviour has changed - this method now returns a full copy of sequence data
621     // TODO consider using getSequenceSelection instead here
622
623     cap.setText(new jalview.io.AppletFormatAdapter().formatSequences(
624             e.getActionCommand(),
625             new Alignment(ap.av.getSelectionAsNewSequence()),
626             ap.av.showJVSuffix));
627
628   }
629
630   void editName()
631   {
632     EditNameDialog dialog = new EditNameDialog(seq.getName(),
633             seq.getDescription(), "       Sequence Name",
634             "Sequence Description", ap.alignFrame,
635             "Edit Sequence Name / Description", 500, 100, true);
636
637     if (dialog.accept)
638     {
639       seq.setName(dialog.getName());
640       seq.setDescription(dialog.getDescription());
641       ap.paintAlignment(false);
642     }
643   }
644
645   void addPDB()
646   {
647     if (seq.getPDBId() != null)
648     {
649       PDBEntry entry = (PDBEntry) seq.getPDBId().firstElement();
650
651       if (ap.av.applet.jmolAvailable)
652         new jalview.appletgui.AppletJmol(entry, new Sequence[]
653         { seq }, null, ap, AppletFormatAdapter.URL);
654       else
655         new MCview.AppletPDBViewer(entry, new Sequence[]
656         { seq }, null, ap, AppletFormatAdapter.URL);
657
658     }
659     else
660     {
661       CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);
662       cap.setText("Paste your PDB file here.");
663       cap.setPDBImport(seq);
664       Frame frame = new Frame();
665       frame.add(cap);
666       jalview.bin.JalviewLite.addFrame(frame, "Paste PDB file for sequence "+seq.getName(), 400, 300);
667     }
668   }
669
670   private void jbInit() throws Exception
671   {
672     groupMenu.setLabel("Group");
673     groupMenu.setLabel("Selection");
674     sequenceFeature.addActionListener(this);
675
676     editGroupName.addActionListener(this);
677     unGroupMenuItem.setLabel("Remove Group");
678     unGroupMenuItem.addActionListener(this);
679
680     nucleotideMenuItem.setLabel("Nucleotide");
681     nucleotideMenuItem.addActionListener(this);
682     conservationMenuItem.addItemListener(this);
683     abovePIDColour.addItemListener(this);
684     colourMenu.setLabel("Group Colour");
685     showBoxes.setLabel("Boxes");
686     showBoxes.setState(true);
687     showBoxes.addItemListener(this);
688     sequenceName.addActionListener(this);
689     displayNonconserved.setLabel("Show Nonconserved");
690     displayNonconserved.setState(false);
691     displayNonconserved.addItemListener(this);
692     showText.setLabel("Text");
693     showText.addItemListener(this);
694     showColourText.setLabel("Colour Text");
695     showColourText.addItemListener(this);
696     outputmenu.setLabel("Output to Textbox...");
697     seqMenu.setLabel("Sequence");
698     pdb.setLabel("View PDB Structure");
699     hideSeqs.setLabel("Hide Sequences");
700     repGroup.setLabel("Represent Group with");
701     revealAll.setLabel("Reveal All");
702     revealSeq.setLabel("Reveal Sequences");
703     menu1.setLabel("Group");
704     add(groupMenu);
705     this.add(seqMenu);
706     this.add(hideSeqs);
707     this.add(revealSeq);
708     this.add(revealAll);
709     groupMenu.add(editGroupName);
710     groupMenu.add(editMenu);
711     groupMenu.add(outputmenu);
712     groupMenu.add(sequenceFeature);
713     groupMenu.add(menu1);
714
715     colourMenu.add(noColourmenuItem);
716     colourMenu.add(clustalColour);
717     colourMenu.add(BLOSUM62Colour);
718     colourMenu.add(PIDColour);
719     colourMenu.add(zappoColour);
720     colourMenu.add(taylorColour);
721     colourMenu.add(hydrophobicityColour);
722     colourMenu.add(helixColour);
723     colourMenu.add(strandColour);
724     colourMenu.add(turnColour);
725     colourMenu.add(buriedColour);
726     colourMenu.add(nucleotideMenuItem);
727     colourMenu.add(userDefinedColour);
728     colourMenu.addSeparator();
729     colourMenu.add(abovePIDColour);
730     colourMenu.add(conservationMenuItem);
731
732     noColourmenuItem.setLabel("None");
733     noColourmenuItem.addActionListener(this);
734
735     clustalColour.setLabel("Clustalx colours");
736     clustalColour.addActionListener(this);
737     zappoColour.setLabel("Zappo");
738     zappoColour.addActionListener(this);
739     taylorColour.setLabel("Taylor");
740     taylorColour.addActionListener(this);
741     hydrophobicityColour.setLabel("Hydrophobicity");
742     hydrophobicityColour.addActionListener(this);
743     helixColour.setLabel("Helix propensity");
744     helixColour.addActionListener(this);
745     strandColour.setLabel("Strand propensity");
746     strandColour.addActionListener(this);
747     turnColour.setLabel("Turn propensity");
748     turnColour.addActionListener(this);
749     buriedColour.setLabel("Buried Index");
750     buriedColour.addActionListener(this);
751     abovePIDColour.setLabel("Above % Identity");
752
753     userDefinedColour.setLabel("User Defined");
754     userDefinedColour.addActionListener(this);
755     PIDColour.setLabel("Percentage Identity");
756     PIDColour.addActionListener(this);
757     BLOSUM62Colour.setLabel("BLOSUM62");
758     BLOSUM62Colour.addActionListener(this);
759     conservationMenuItem.setLabel("Conservation");
760
761     editMenu.add(copy);
762     copy.addActionListener(this);
763     editMenu.add(cut);
764     cut.addActionListener(this);
765
766     editMenu.add(editSequence);
767     editSequence.addActionListener(this);
768
769     editMenu.add(toUpper);
770     toUpper.addActionListener(this);
771     editMenu.add(toLower);
772     toLower.addActionListener(this);
773     editMenu.add(toggleCase);
774     seqMenu.add(sequenceName);
775     if (!ap.av.applet.useXtrnalSviewer)
776     {
777       seqMenu.add(pdb);
778     }
779     seqMenu.add(repGroup);
780     menu1.add(unGroupMenuItem);
781     menu1.add(colourMenu);
782     menu1.add(showBoxes);
783     menu1.add(showText);
784     menu1.add(showColourText);
785     menu1.add(displayNonconserved);
786     toggleCase.addActionListener(this);
787     pdb.addActionListener(this);
788     hideSeqs.addActionListener(this);
789     repGroup.addActionListener(this);
790     revealAll.addActionListener(this);
791     revealSeq.addActionListener(this);
792   }
793
794   void refresh()
795   {
796     ap.paintAlignment(true);
797   }
798
799   protected void clustalColour_actionPerformed()
800   {
801     SequenceGroup sg = getGroup();
802     sg.cs = new ClustalxColourScheme(
803             sg.getSequences(ap.av.getHiddenRepSequences()),
804             ap.av.getAlignment().getWidth());
805     refresh();
806   }
807
808   protected void zappoColour_actionPerformed()
809   {
810     getGroup().cs = new ZappoColourScheme();
811     refresh();
812   }
813
814   protected void taylorColour_actionPerformed()
815   {
816     getGroup().cs = new TaylorColourScheme();
817     refresh();
818   }
819
820   protected void hydrophobicityColour_actionPerformed()
821   {
822     getGroup().cs = new HydrophobicColourScheme();
823     refresh();
824   }
825
826   protected void helixColour_actionPerformed()
827   {
828     getGroup().cs = new HelixColourScheme();
829     refresh();
830   }
831
832   protected void strandColour_actionPerformed()
833   {
834     getGroup().cs = new StrandColourScheme();
835     refresh();
836   }
837
838   protected void turnColour_actionPerformed()
839   {
840     getGroup().cs = new TurnColourScheme();
841     refresh();
842   }
843
844   protected void buriedColour_actionPerformed()
845   {
846     getGroup().cs = new BuriedColourScheme();
847     refresh();
848   }
849
850   public void nucleotideMenuItem_actionPerformed()
851   {
852     getGroup().cs = new NucleotideColourScheme();
853     refresh();
854   }
855
856   protected void abovePIDColour_itemStateChanged()
857   {
858     SequenceGroup sg = getGroup();
859     if (sg.cs == null)
860     {
861       return;
862     }
863
864     if (abovePIDColour.getState())
865     {
866       sg.cs.setConsensus(AAFrequency.calculate(
867               sg.getSequences(ap.av.getHiddenRepSequences()), 0,
868               ap.av.getAlignment().getWidth()));
869       int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs, getGroup()
870               .getName());
871
872       sg.cs.setThreshold(threshold, ap.av.getIgnoreGapsConsensus());
873
874       SliderPanel.showPIDSlider();
875
876     }
877     else
878     // remove PIDColouring
879     {
880       sg.cs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
881     }
882
883     refresh();
884
885   }
886
887   protected void userDefinedColour_actionPerformed()
888   {
889     new UserDefinedColours(ap, getGroup());
890   }
891
892   protected void PIDColour_actionPerformed()
893   {
894     SequenceGroup sg = getGroup();
895     sg.cs = new PIDColourScheme();
896     sg.cs.setConsensus(AAFrequency.calculate(
897             sg.getSequences(ap.av.getHiddenRepSequences()), 0,
898             ap.av.getAlignment().getWidth()));
899     refresh();
900   }
901
902   protected void BLOSUM62Colour_actionPerformed()
903   {
904     SequenceGroup sg = getGroup();
905
906     sg.cs = new Blosum62ColourScheme();
907
908     sg.cs.setConsensus(AAFrequency.calculate(
909             sg.getSequences(ap.av.getHiddenRepSequences()), 0,
910             ap.av.getAlignment().getWidth()));
911
912     refresh();
913   }
914
915   protected void noColourmenuItem_actionPerformed()
916   {
917     getGroup().cs = null;
918     refresh();
919   }
920
921   protected void conservationMenuItem_itemStateChanged()
922   {
923     SequenceGroup sg = getGroup();
924     if (sg.cs == null)
925     {
926       return;
927     }
928
929     if (conservationMenuItem.getState())
930     {
931
932       sg.cs.setConservation(Conservation.calculateConservation("Group",
933               ResidueProperties.propHash, 3,
934               sg.getSequences(ap.av.getHiddenRepSequences()), 0,
935               ap.av.getAlignment().getWidth(),
936               false, ap.av.getConsPercGaps(),false));
937       SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
938       SliderPanel.showConservationSlider();
939     }
940     else
941     // remove ConservationColouring
942     {
943       sg.cs.setConservation(null);
944     }
945
946     refresh();
947   }
948
949   SequenceGroup getGroup()
950   {
951     SequenceGroup sg = ap.av.getSelectionGroup();
952
953     // this method won't add a new group if it already exists
954     if (sg != null)
955     {
956       ap.av.getAlignment().addGroup(sg);
957     }
958
959     return sg;
960   }
961
962   void unGroupMenuItem_actionPerformed()
963   {
964     SequenceGroup sg = ap.av.getSelectionGroup();
965     ap.av.getAlignment().deleteGroup(sg);
966     ap.av.setSelectionGroup(null);
967     ap.paintAlignment(true);
968   }
969
970   public void showColourText_itemStateChanged()
971   {
972     getGroup().setColourText(showColourText.getState());
973     refresh();
974   }
975
976   public void showText_itemStateChanged()
977   {
978     getGroup().setDisplayText(showText.getState());
979     refresh();
980   }
981
982   public void showNonconserved_itemStateChanged()
983   {
984     getGroup().setShowNonconserved(this.displayNonconserved.getState());
985     refresh();
986   }
987
988   public void showBoxes_itemStateChanged()
989   {
990     getGroup().setDisplayBoxes(showBoxes.getState());
991     refresh();
992   }
993
994   void hideSequences(boolean representGroup)
995   {
996     SequenceGroup sg = ap.av.getSelectionGroup();
997     if (sg == null || sg.getSize() < 1)
998     {
999       ap.av.hideSequence(new SequenceI[]
1000       { seq });
1001       return;
1002     }
1003
1004     ap.av.setSelectionGroup(null);
1005
1006     if (representGroup)
1007     {
1008       ap.av.hideRepSequences(seq, sg);
1009
1010       return;
1011     }
1012
1013     int gsize = sg.getSize();
1014     SequenceI[] hseqs;
1015
1016     hseqs = new SequenceI[gsize];
1017
1018     int index = 0;
1019     for (int i = 0; i < gsize; i++)
1020     {
1021       hseqs[index++] = sg.getSequenceAt(i);
1022     }
1023
1024     ap.av.hideSequence(hseqs);
1025     ap.av.sendSelection();
1026   }
1027
1028 }