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