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