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