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