JAL-2282 Remove Cache dependency from applet
[jalview.git] / src / jalview / appletgui / APopupMenu.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.appletgui;
22
23 import jalview.analysis.AAFrequency;
24 import jalview.analysis.AlignmentAnnotationUtils;
25 import jalview.analysis.AlignmentUtils;
26 import jalview.analysis.Conservation;
27 import jalview.commands.ChangeCaseCommand;
28 import jalview.commands.EditCommand;
29 import jalview.commands.EditCommand.Action;
30 import jalview.datamodel.AlignmentAnnotation;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.PDBEntry;
33 import jalview.datamodel.SequenceFeature;
34 import jalview.datamodel.SequenceGroup;
35 import jalview.datamodel.SequenceI;
36 import jalview.io.AppletFormatAdapter;
37 import jalview.io.SequenceAnnotationReport;
38 import jalview.schemes.Blosum62ColourScheme;
39 import jalview.schemes.BuriedColourScheme;
40 import jalview.schemes.ClustalxColourScheme;
41 import jalview.schemes.HelixColourScheme;
42 import jalview.schemes.HydrophobicColourScheme;
43 import jalview.schemes.NucleotideColourScheme;
44 import jalview.schemes.PIDColourScheme;
45 import jalview.schemes.StrandColourScheme;
46 import jalview.schemes.TaylorColourScheme;
47 import jalview.schemes.TurnColourScheme;
48 import jalview.schemes.ZappoColourScheme;
49 import jalview.util.MessageManager;
50 import jalview.util.UrlLink;
51
52 import java.awt.CheckboxMenuItem;
53 import java.awt.Frame;
54 import java.awt.Menu;
55 import java.awt.MenuItem;
56 import java.awt.event.ActionEvent;
57 import java.awt.event.ActionListener;
58 import java.awt.event.ItemEvent;
59 import java.awt.event.ItemListener;
60 import java.util.Arrays;
61 import java.util.Collection;
62 import java.util.Collections;
63 import java.util.LinkedHashMap;
64 import java.util.List;
65 import java.util.Map;
66 import java.util.TreeMap;
67 import java.util.Vector;
68
69 public class APopupMenu extends java.awt.PopupMenu implements
70         ActionListener, ItemListener
71 {
72   Menu groupMenu = new Menu();
73
74   MenuItem editGroupName = new MenuItem();
75
76   protected MenuItem clustalColour = new MenuItem();
77
78   protected MenuItem zappoColour = new MenuItem();
79
80   protected MenuItem taylorColour = new MenuItem();
81
82   protected MenuItem hydrophobicityColour = new MenuItem();
83
84   protected MenuItem helixColour = new MenuItem();
85
86   protected MenuItem strandColour = new MenuItem();
87
88   protected MenuItem turnColour = new MenuItem();
89
90   protected MenuItem buriedColour = new MenuItem();
91
92   protected CheckboxMenuItem abovePIDColour = new CheckboxMenuItem();
93
94   protected MenuItem userDefinedColour = new MenuItem();
95
96   protected MenuItem PIDColour = new MenuItem();
97
98   protected MenuItem BLOSUM62Colour = new MenuItem();
99
100   MenuItem noColourmenuItem = new MenuItem();
101
102   protected CheckboxMenuItem conservationMenuItem = new CheckboxMenuItem();
103
104   final AlignmentPanel ap;
105
106   MenuItem unGroupMenuItem = new MenuItem();
107
108   MenuItem createGroupMenuItem = new MenuItem();
109
110   MenuItem nucleotideMenuItem = new MenuItem();
111
112   Menu colourMenu = new Menu();
113
114   CheckboxMenuItem showBoxes = new CheckboxMenuItem();
115
116   CheckboxMenuItem showText = new CheckboxMenuItem();
117
118   CheckboxMenuItem showColourText = new CheckboxMenuItem();
119
120   CheckboxMenuItem displayNonconserved = new CheckboxMenuItem();
121
122   Menu seqShowAnnotationsMenu = new Menu(
123           MessageManager.getString("label.show_annotations"));
124
125   Menu seqHideAnnotationsMenu = new Menu(
126           MessageManager.getString("label.hide_annotations"));
127
128   MenuItem seqAddReferenceAnnotations = new MenuItem(
129           MessageManager.getString("label.add_reference_annotations"));
130
131   Menu groupShowAnnotationsMenu = new Menu(
132           MessageManager.getString("label.show_annotations"));
133
134   Menu groupHideAnnotationsMenu = new Menu(
135           MessageManager.getString("label.hide_annotations"));
136
137   MenuItem groupAddReferenceAnnotations = new MenuItem(
138           MessageManager.getString("label.add_reference_annotations"));
139
140   Menu editMenu = new Menu(MessageManager.getString("action.edit"));
141
142   MenuItem copy = new MenuItem(MessageManager.getString("action.copy"));
143
144   MenuItem cut = new MenuItem(MessageManager.getString("action.cut"));
145
146   MenuItem toUpper = new MenuItem(
147           MessageManager.getString("label.to_upper_case"));
148
149   MenuItem toLower = new MenuItem(
150           MessageManager.getString("label.to_lower_case"));
151
152   MenuItem toggleCase = new MenuItem(
153           MessageManager.getString("label.toggle_case"));
154
155   Menu outputmenu = new Menu();
156
157   Menu seqMenu = new Menu();
158
159   MenuItem pdb = new MenuItem();
160
161   MenuItem hideSeqs = new MenuItem();
162
163   MenuItem repGroup = new MenuItem();
164
165   MenuItem sequenceName = new MenuItem(
166           MessageManager.getString("label.edit_name_description"));
167
168   MenuItem sequenceFeature = new MenuItem(
169           MessageManager.getString("label.create_sequence_feature"));
170
171   MenuItem editSequence = new MenuItem(
172           MessageManager.getString("label.edit_sequence"));
173
174   MenuItem sequenceDetails = new MenuItem(
175           MessageManager.getString("label.sequence_details"));
176
177   MenuItem selSeqDetails = new MenuItem(
178           MessageManager.getString("label.sequence_details"));
179
180   MenuItem makeReferenceSeq = new MenuItem();
181
182   SequenceI seq;
183
184   MenuItem revealAll = new MenuItem();
185
186   MenuItem revealSeq = new MenuItem();
187
188   /**
189    * index of sequence to be revealed
190    */
191   int revealSeq_index = -1;
192
193   Menu menu1 = new Menu();
194
195   public APopupMenu(AlignmentPanel apanel, final SequenceI seq,
196           Vector<String> links)
197   {
198     // /////////////////////////////////////////////////////////
199     // If this is activated from the sequence panel, the user may want to
200     // edit or annotate a particular residue. Therefore display the residue menu
201     //
202     // If from the IDPanel, we must display the sequence menu
203     // ////////////////////////////////////////////////////////
204
205     this.ap = apanel;
206     this.seq = seq;
207
208     try
209     {
210       jbInit();
211     } catch (Exception e)
212     {
213       e.printStackTrace();
214     }
215
216     for (int i = 0; i < jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS.length; i++)
217     {
218       MenuItem item = new MenuItem(
219               jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS[i]);
220
221       item.addActionListener(this);
222       outputmenu.add(item);
223     }
224
225     buildAnnotationSubmenus();
226
227     SequenceGroup sg = ap.av.getSelectionGroup();
228     if (sg != null && sg.getSize() > 0)
229     {
230       editGroupName.setLabel(MessageManager.formatMessage(
231               "label.name_param", new Object[] { sg.getName() }));
232       showText.setState(sg.getDisplayText());
233       showColourText.setState(sg.getColourText());
234       showBoxes.setState(sg.getDisplayBoxes());
235       displayNonconserved.setState(sg.getShowNonconserved());
236       if (!ap.av.getAlignment().getGroups().contains(sg))
237       {
238         menu1.setLabel(MessageManager.getString("action.edit_new_group"));
239         groupMenu.remove(unGroupMenuItem);
240       }
241       else
242       {
243         menu1.setLabel(MessageManager.getString("action.edit_group"));
244         groupMenu.remove(createGroupMenuItem);
245       }
246
247     }
248     else
249     {
250       remove(hideSeqs);
251       remove(groupMenu);
252     }
253
254     if (links != null && links.size() > 0)
255     {
256       addFeatureLinks(seq, links);
257     }
258
259     // TODO: add group link menu entry here
260     if (seq != null)
261     {
262       seqMenu.setLabel(seq.getName());
263       if (seq == ap.av.getAlignment().getSeqrep())
264       {
265         makeReferenceSeq.setLabel(MessageManager
266                 .getString("action.unmark_as_reference"));// Unmark
267                                                           // representative");
268       }
269       else
270       {
271         makeReferenceSeq.setLabel(MessageManager
272                 .getString("action.set_as_reference")); // );
273       }
274       repGroup.setLabel(MessageManager.formatMessage(
275               "label.represent_group_with", new Object[] { seq.getName() }));
276     }
277     else
278     {
279       remove(seqMenu);
280     }
281
282     if (!ap.av.hasHiddenRows())
283     {
284       remove(revealAll);
285       remove(revealSeq);
286     }
287     else
288     {
289       final int index = ap.av.getAlignment().findIndex(seq);
290
291       if (ap.av.adjustForHiddenSeqs(index)
292               - ap.av.adjustForHiddenSeqs(index - 1) > 1)
293       {
294         revealSeq_index = index;
295       }
296       else
297       {
298         remove(revealSeq);
299       }
300     }
301   }
302
303   /**
304    * Adds a 'Link' menu item with a sub-menu item for each hyperlink provided.
305    * 
306    * @param seq
307    * @param links
308    */
309   void addFeatureLinks(final SequenceI seq, List<String> links)
310   {
311     Menu linkMenu = new Menu(MessageManager.getString("action.link"));
312     Map<String, List<String>> linkset = new LinkedHashMap<String, List<String>>();
313
314     for (String link : links)
315     {
316       UrlLink urlLink = null;
317       try
318       {
319         urlLink = new UrlLink(link);
320       } catch (Exception foo)
321       {
322         System.err.println("Exception for URLLink '" + link + "': "
323                 + foo.getMessage());
324         continue;
325       }
326
327       if (!urlLink.isValid())
328       {
329         System.err.println(urlLink.getInvalidMessage());
330         continue;
331       }
332
333       urlLink.createLinksFromSeq(seq, linkset);
334     }
335
336     addshowLinks(linkMenu, linkset.values());
337
338     // disable link menu if there are no valid entries
339     if (linkMenu.getItemCount() > 0)
340     {
341       linkMenu.setEnabled(true);
342     }
343     else
344     {
345       linkMenu.setEnabled(false);
346     }
347
348     if (seq != null)
349     {
350       seqMenu.add(linkMenu);
351     }
352     else
353     {
354       add(linkMenu);
355     }
356
357   }
358
359   private void addshowLinks(Menu linkMenu, Collection<List<String>> linkset)
360   {
361     for (List<String> linkstrset : linkset)
362     {
363       // split linkstr into label and url
364       addshowLink(linkMenu, linkstrset.get(1), linkstrset.get(3));
365     }
366   }
367
368   /**
369    * Build menus for annotation types that may be shown or hidden, and for
370    * 'reference annotations' that may be added to the alignment.
371    */
372   private void buildAnnotationSubmenus()
373   {
374     /*
375      * First for the currently selected sequence (if there is one):
376      */
377     final List<SequenceI> selectedSequence = (seq == null ? Collections
378             .<SequenceI> emptyList() : Arrays.asList(seq));
379     buildAnnotationTypesMenus(seqShowAnnotationsMenu,
380             seqHideAnnotationsMenu, selectedSequence);
381     configureReferenceAnnotationsMenu(seqAddReferenceAnnotations,
382             selectedSequence);
383
384     /*
385      * and repeat for the current selection group (if there is one):
386      */
387     final List<SequenceI> selectedGroup = (ap.av.getSelectionGroup() == null ? Collections
388             .<SequenceI> emptyList() : ap.av.getSelectionGroup()
389             .getSequences());
390     buildAnnotationTypesMenus(groupShowAnnotationsMenu,
391             groupHideAnnotationsMenu, selectedGroup);
392     configureReferenceAnnotationsMenu(groupAddReferenceAnnotations,
393             selectedGroup);
394   }
395
396   /**
397    * Determine whether or not to enable 'add reference annotations' menu item.
398    * It is enable if there are any annotations, on any of the selected
399    * sequences, which are not yet on the alignment (visible or not).
400    * 
401    * @param menu
402    * @param forSequences
403    */
404   private void configureReferenceAnnotationsMenu(MenuItem menuItem,
405           List<SequenceI> forSequences)
406   {
407     menuItem.setEnabled(false);
408
409     /*
410      * Temporary store to hold distinct calcId / type pairs for the tooltip.
411      * Using TreeMap means calcIds are shown in alphabetical order.
412      */
413     Map<String, String> tipEntries = new TreeMap<String, String>();
414     final Map<SequenceI, List<AlignmentAnnotation>> candidates = new LinkedHashMap<SequenceI, List<AlignmentAnnotation>>();
415     AlignmentI al = this.ap.av.getAlignment();
416     AlignmentUtils.findAddableReferenceAnnotations(forSequences,
417             tipEntries, candidates, al);
418     if (!candidates.isEmpty())
419     {
420       StringBuilder tooltip = new StringBuilder(64);
421       tooltip.append(MessageManager.getString("label.add_annotations_for"));
422
423       /*
424        * Found annotations that could be added. Enable the menu item, and
425        * configure its action.
426        */
427       menuItem.setEnabled(true);
428
429       menuItem.addActionListener(new ActionListener()
430       {
431         @Override
432         public void actionPerformed(ActionEvent e)
433         {
434           addReferenceAnnotations_actionPerformed(candidates);
435         }
436       });
437     }
438   }
439
440   /**
441    * Add annotations to the sequences and to the alignment.
442    * 
443    * @param candidates
444    *          a map whose keys are sequences on the alignment, and values a list
445    *          of annotations to add to each sequence
446    */
447   protected void addReferenceAnnotations_actionPerformed(
448           Map<SequenceI, List<AlignmentAnnotation>> candidates)
449   {
450     final SequenceGroup selectionGroup = this.ap.av.getSelectionGroup();
451     final AlignmentI alignment = this.ap.getAlignment();
452     AlignmentUtils.addReferenceAnnotations(candidates, alignment,
453             selectionGroup);
454     refresh();
455   }
456
457   /**
458    * add a show URL menu item to the given linkMenu
459    * 
460    * @param linkMenu
461    * @param target
462    *          - menu label string
463    * @param url
464    *          - url to open
465    */
466   private void addshowLink(Menu linkMenu, final String target,
467           final String url)
468   {
469     addshowLink(linkMenu, target, target, url);
470   }
471
472   /**
473    * add a show URL menu item to the given linkMenu
474    * 
475    * @param linkMenu
476    * @param target
477    *          - URL target window
478    * @param label
479    *          - menu label string
480    * @param url
481    *          - url to open
482    */
483   private void addshowLink(Menu linkMenu, final String target,
484           final String label, final String url)
485   {
486     MenuItem item = new MenuItem(label);
487     item.addActionListener(new java.awt.event.ActionListener()
488     {
489       @Override
490       public void actionPerformed(ActionEvent e)
491       {
492         ap.alignFrame.showURL(url, target);
493       }
494     });
495     linkMenu.add(item);
496   }
497
498   @Override
499   public void itemStateChanged(ItemEvent evt)
500   {
501     if (evt.getSource() == abovePIDColour)
502     {
503       abovePIDColour_itemStateChanged();
504     }
505     else if (evt.getSource() == showColourText)
506     {
507       showColourText_itemStateChanged();
508     }
509     else if (evt.getSource() == showText)
510     {
511       showText_itemStateChanged();
512     }
513     else if (evt.getSource() == showBoxes)
514     {
515       showBoxes_itemStateChanged();
516     }
517     else if (evt.getSource() == displayNonconserved)
518     {
519       this.showNonconserved_itemStateChanged();
520     }
521   }
522
523   @Override
524   public void actionPerformed(ActionEvent evt)
525   {
526     Object source = evt.getSource();
527     if (source == clustalColour)
528     {
529       clustalColour_actionPerformed();
530     }
531     else if (source == zappoColour)
532     {
533       zappoColour_actionPerformed();
534     }
535     else if (source == taylorColour)
536     {
537       taylorColour_actionPerformed();
538     }
539     else if (source == hydrophobicityColour)
540     {
541       hydrophobicityColour_actionPerformed();
542     }
543     else if (source == helixColour)
544     {
545       helixColour_actionPerformed();
546     }
547     else if (source == strandColour)
548     {
549       strandColour_actionPerformed();
550     }
551     else if (source == turnColour)
552     {
553       turnColour_actionPerformed();
554     }
555     else if (source == buriedColour)
556     {
557       buriedColour_actionPerformed();
558     }
559     else if (source == nucleotideMenuItem)
560     {
561       nucleotideMenuItem_actionPerformed();
562     }
563
564     else if (source == userDefinedColour)
565     {
566       userDefinedColour_actionPerformed();
567     }
568     else if (source == PIDColour)
569     {
570       PIDColour_actionPerformed();
571     }
572     else if (source == BLOSUM62Colour)
573     {
574       BLOSUM62Colour_actionPerformed();
575     }
576     else if (source == noColourmenuItem)
577     {
578       noColourmenuItem_actionPerformed();
579     }
580     else if (source == conservationMenuItem)
581     {
582       conservationMenuItem_itemStateChanged();
583     }
584     else if (source == unGroupMenuItem)
585     {
586       unGroupMenuItem_actionPerformed();
587     }
588
589     else if (source == createGroupMenuItem)
590     {
591       createGroupMenuItem_actionPerformed();
592     }
593
594     else if (source == sequenceName)
595     {
596       editName();
597     }
598     else if (source == makeReferenceSeq)
599     {
600       makeReferenceSeq_actionPerformed();
601     }
602     else if (source == sequenceDetails)
603     {
604       showSequenceDetails();
605     }
606     else if (source == selSeqDetails)
607     {
608       showSequenceSelectionDetails();
609     }
610     else if (source == pdb)
611     {
612       addPDB();
613     }
614     else if (source == hideSeqs)
615     {
616       hideSequences(false);
617     }
618     else if (source == repGroup)
619     {
620       hideSequences(true);
621     }
622     else if (source == revealSeq)
623     {
624       ap.av.showSequence(revealSeq_index);
625     }
626     else if (source == revealAll)
627     {
628       ap.av.showAllHiddenSeqs();
629     }
630
631     else if (source == editGroupName)
632     {
633       EditNameDialog dialog = new EditNameDialog(getGroup().getName(),
634               getGroup().getDescription(), "       Group Name",
635               "Group Description", ap.alignFrame,
636               "Edit Group Name / Description", 500, 100, true);
637
638       if (dialog.accept)
639       {
640         getGroup().setName(dialog.getName().replace(' ', '_'));
641         getGroup().setDescription(dialog.getDescription());
642       }
643
644     }
645     else if (source == copy)
646     {
647       ap.alignFrame.copy_actionPerformed();
648     }
649     else if (source == cut)
650     {
651       ap.alignFrame.cut_actionPerformed();
652     }
653     else if (source == editSequence)
654     {
655       SequenceGroup sg = ap.av.getSelectionGroup();
656
657       if (sg != null)
658       {
659         if (seq == null)
660         {
661           seq = sg.getSequenceAt(0);
662         }
663
664         EditNameDialog dialog = new EditNameDialog(seq.getSequenceAsString(
665                 sg.getStartRes(), sg.getEndRes() + 1), null,
666                 "Edit Sequence ", null,
667
668                 ap.alignFrame, "Edit Sequence", 500, 100, true);
669
670         if (dialog.accept)
671         {
672           EditCommand editCommand = new EditCommand(
673                   MessageManager.getString("label.edit_sequences"),
674                   Action.REPLACE, dialog.getName().replace(' ',
675                           ap.av.getGapCharacter()),
676                   sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
677                   sg.getStartRes(), sg.getEndRes() + 1,
678                   ap.av.getAlignment());
679
680           ap.alignFrame.addHistoryItem(editCommand);
681
682           ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
683                   .getSequences());
684         }
685       }
686     }
687     else if (source == toUpper || source == toLower || source == toggleCase)
688     {
689       SequenceGroup sg = ap.av.getSelectionGroup();
690       if (sg != null)
691       {
692         List<int[]> startEnd = ap.av.getVisibleRegionBoundaries(
693                 sg.getStartRes(), sg.getEndRes() + 1);
694
695         String description;
696         int caseChange;
697
698         if (source == toggleCase)
699         {
700           description = "Toggle Case";
701           caseChange = ChangeCaseCommand.TOGGLE_CASE;
702         }
703         else if (source == toUpper)
704         {
705           description = "To Upper Case";
706           caseChange = ChangeCaseCommand.TO_UPPER;
707         }
708         else
709         {
710           description = "To Lower Case";
711           caseChange = ChangeCaseCommand.TO_LOWER;
712         }
713
714         ChangeCaseCommand caseCommand = new ChangeCaseCommand(description,
715                 sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
716                 startEnd, caseChange);
717
718         ap.alignFrame.addHistoryItem(caseCommand);
719
720         ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
721                 .getSequences());
722
723       }
724     }
725     else if (source == sequenceFeature)
726     {
727       SequenceGroup sg = ap.av.getSelectionGroup();
728       if (sg == null)
729       {
730         return;
731       }
732
733       int rsize = 0, gSize = sg.getSize();
734       SequenceI[] rseqs, seqs = new SequenceI[gSize];
735       SequenceFeature[] tfeatures, features = new SequenceFeature[gSize];
736
737       for (int i = 0; i < gSize; i++)
738       {
739         int start = sg.getSequenceAt(i).findPosition(sg.getStartRes());
740         int end = sg.findEndRes(sg.getSequenceAt(i));
741         if (start <= end)
742         {
743           seqs[rsize] = sg.getSequenceAt(i);
744           features[rsize] = new SequenceFeature(null, null, null, start,
745                   end, "Jalview");
746           rsize++;
747         }
748       }
749       rseqs = new SequenceI[rsize];
750       tfeatures = new SequenceFeature[rsize];
751       System.arraycopy(seqs, 0, rseqs, 0, rsize);
752       System.arraycopy(features, 0, tfeatures, 0, rsize);
753       features = tfeatures;
754       seqs = rseqs;
755
756       if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
757               features, true, ap))
758       {
759         ap.alignFrame.sequenceFeatures.setState(true);
760         ap.av.setShowSequenceFeatures(true);
761         ;
762         ap.highlightSearchResults(null);
763       }
764     }
765     else
766     {
767       outputText(evt);
768     }
769
770   }
771
772   void outputText(ActionEvent e)
773   {
774     CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);
775
776     Frame frame = new Frame();
777     frame.add(cap);
778     jalview.bin.JalviewLite.addFrame(frame, MessageManager.formatMessage(
779             "label.selection_output_command",
780             new Object[] { e.getActionCommand() }), 600, 500);
781     // JBPNote: getSelectionAsNewSequence behaviour has changed - this method
782     // now returns a full copy of sequence data
783     // TODO consider using getSequenceSelection instead here
784
785     cap.setText(new jalview.io.AppletFormatAdapter().formatSequences(
786             e.getActionCommand(), ap.av.getShowJVSuffix(), ap, true));
787
788   }
789
790   protected void showSequenceSelectionDetails()
791   {
792     createSequenceDetailsReport(ap.av.getSequenceSelection());
793   }
794
795   protected void showSequenceDetails()
796   {
797     createSequenceDetailsReport(new SequenceI[] { seq });
798   }
799
800   public void createSequenceDetailsReport(SequenceI[] sequences)
801   {
802
803     CutAndPasteTransfer cap = new CutAndPasteTransfer(false, ap.alignFrame);
804
805     StringBuffer contents = new StringBuffer();
806     for (SequenceI seq : sequences)
807     {
808       contents.append(MessageManager.formatMessage(
809               "label.annotation_for_displayid",
810               new Object[] { seq.getDisplayId(true) }));
811       new SequenceAnnotationReport(null).createSequenceAnnotationReport(
812               contents,
813               seq,
814               true,
815               true,
816               false,
817               (ap.seqPanel.seqCanvas.fr != null) ? ap.seqPanel.seqCanvas.fr
818                       .getMinMax() : null);
819       contents.append("</p>");
820     }
821     Frame frame = new Frame();
822     frame.add(cap);
823     jalview.bin.JalviewLite.addFrame(frame, "Sequence Details for "
824             + (sequences.length == 1 ? sequences[0].getDisplayId(true)
825                     : "Selection"), 600, 500);
826     cap.setText(MessageManager.formatMessage("label.html_content",
827             new Object[] { contents.toString() }));
828   }
829
830   void editName()
831   {
832     EditNameDialog dialog = new EditNameDialog(seq.getName(),
833             seq.getDescription(), "       Sequence Name",
834             "Sequence Description", ap.alignFrame,
835             "Edit Sequence Name / Description", 500, 100, true);
836
837     if (dialog.accept)
838     {
839       seq.setName(dialog.getName());
840       seq.setDescription(dialog.getDescription());
841       ap.paintAlignment(false);
842     }
843   }
844
845   void addPDB()
846   {
847     if (seq.getAllPDBEntries() != null)
848     {
849       PDBEntry entry = seq.getAllPDBEntries().firstElement();
850
851       if (ap.av.applet.jmolAvailable)
852       {
853         new jalview.appletgui.AppletJmol(entry, new SequenceI[] { seq },
854                 null, ap, AppletFormatAdapter.URL);
855       }
856       else
857       {
858         new MCview.AppletPDBViewer(entry, new SequenceI[] { seq }, null,
859                 ap, AppletFormatAdapter.URL);
860       }
861
862     }
863     else
864     {
865       CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);
866       cap.setText(MessageManager.getString("label.paste_pdb_file"));
867       cap.setPDBImport(seq);
868       Frame frame = new Frame();
869       frame.add(cap);
870       jalview.bin.JalviewLite.addFrame(frame, MessageManager.formatMessage(
871               "label.paste_pdb_file_for_sequence",
872               new Object[] { seq.getName() }), 400, 300);
873     }
874   }
875
876   private void jbInit() throws Exception
877   {
878     groupMenu.setLabel(MessageManager.getString("label.selection"));
879     sequenceFeature.addActionListener(this);
880
881     editGroupName.addActionListener(this);
882     unGroupMenuItem.setLabel(MessageManager
883             .getString("action.remove_group"));
884     unGroupMenuItem.addActionListener(this);
885
886     createGroupMenuItem.setLabel(MessageManager
887             .getString("action.create_group"));
888     createGroupMenuItem.addActionListener(this);
889
890     nucleotideMenuItem.setLabel(MessageManager
891             .getString("label.nucleotide"));
892     nucleotideMenuItem.addActionListener(this);
893     conservationMenuItem.addItemListener(this);
894     abovePIDColour.addItemListener(this);
895     colourMenu.setLabel(MessageManager.getString("label.group_colour"));
896     showBoxes.setLabel(MessageManager.getString("action.boxes"));
897     showBoxes.setState(true);
898     showBoxes.addItemListener(this);
899     sequenceName.addActionListener(this);
900     sequenceDetails.addActionListener(this);
901     selSeqDetails.addActionListener(this);
902     displayNonconserved.setLabel(MessageManager
903             .getString("label.show_non_conversed"));
904     displayNonconserved.setState(false);
905     displayNonconserved.addItemListener(this);
906     showText.setLabel(MessageManager.getString("action.text"));
907     showText.addItemListener(this);
908     showColourText.setLabel(MessageManager.getString("label.colour_text"));
909     showColourText.addItemListener(this);
910     outputmenu.setLabel(MessageManager.getString("label.out_to_textbox"));
911     seqMenu.setLabel(MessageManager.getString("label.sequence"));
912     pdb.setLabel(MessageManager.getString("label.view_pdb_structure"));
913     hideSeqs.setLabel(MessageManager.getString("action.hide_sequences"));
914     repGroup.setLabel(MessageManager.formatMessage(
915             "label.represent_group_with", new Object[] { "" }));
916     revealAll.setLabel(MessageManager.getString("action.reveal_all"));
917     revealSeq.setLabel(MessageManager.getString("action.reveal_sequences"));
918     menu1.setLabel(MessageManager.getString("label.group:"));
919     add(groupMenu);
920     this.add(seqMenu);
921     this.add(hideSeqs);
922     this.add(revealSeq);
923     this.add(revealAll);
924     // groupMenu.add(selSeqDetails);
925     groupMenu.add(groupShowAnnotationsMenu);
926     groupMenu.add(groupHideAnnotationsMenu);
927     groupMenu.add(groupAddReferenceAnnotations);
928     groupMenu.add(editMenu);
929     groupMenu.add(outputmenu);
930     groupMenu.add(sequenceFeature);
931     groupMenu.add(createGroupMenuItem);
932     groupMenu.add(unGroupMenuItem);
933     groupMenu.add(menu1);
934
935     colourMenu.add(noColourmenuItem);
936     colourMenu.add(clustalColour);
937     colourMenu.add(BLOSUM62Colour);
938     colourMenu.add(PIDColour);
939     colourMenu.add(zappoColour);
940     colourMenu.add(taylorColour);
941     colourMenu.add(hydrophobicityColour);
942     colourMenu.add(helixColour);
943     colourMenu.add(strandColour);
944     colourMenu.add(turnColour);
945     colourMenu.add(buriedColour);
946     colourMenu.add(nucleotideMenuItem);
947     colourMenu.add(userDefinedColour);
948     colourMenu.addSeparator();
949     colourMenu.add(abovePIDColour);
950     colourMenu.add(conservationMenuItem);
951
952     noColourmenuItem.setLabel(MessageManager.getString("label.none"));
953     noColourmenuItem.addActionListener(this);
954
955     clustalColour.setLabel(MessageManager
956             .getString("label.clustalx_colours"));
957     clustalColour.addActionListener(this);
958     zappoColour.setLabel(MessageManager.getString("label.zappo"));
959     zappoColour.addActionListener(this);
960     taylorColour.setLabel(MessageManager.getString("label.taylor"));
961     taylorColour.addActionListener(this);
962     hydrophobicityColour.setLabel(MessageManager
963             .getString("label.hydrophobicity"));
964     hydrophobicityColour.addActionListener(this);
965     helixColour
966             .setLabel(MessageManager.getString("label.helix_propensity"));
967     helixColour.addActionListener(this);
968     strandColour.setLabel(MessageManager
969             .getString("label.strand_propensity"));
970     strandColour.addActionListener(this);
971     turnColour.setLabel(MessageManager.getString("label.turn_propensity"));
972     turnColour.addActionListener(this);
973     buriedColour.setLabel(MessageManager.getString("label.buried_index"));
974     buriedColour.addActionListener(this);
975     abovePIDColour.setLabel(MessageManager
976             .getString("label.above_identity_percentage"));
977
978     userDefinedColour.setLabel(MessageManager
979             .getString("action.user_defined"));
980     userDefinedColour.addActionListener(this);
981     PIDColour.setLabel(MessageManager
982             .getString("label.percentage_identity"));
983     PIDColour.addActionListener(this);
984     BLOSUM62Colour.setLabel("BLOSUM62");
985     BLOSUM62Colour.addActionListener(this);
986     conservationMenuItem.setLabel(MessageManager
987             .getString("label.conservation"));
988
989     editMenu.add(copy);
990     copy.addActionListener(this);
991     editMenu.add(cut);
992     cut.addActionListener(this);
993
994     editMenu.add(editSequence);
995     editSequence.addActionListener(this);
996
997     editMenu.add(toUpper);
998     toUpper.addActionListener(this);
999     editMenu.add(toLower);
1000     toLower.addActionListener(this);
1001     editMenu.add(toggleCase);
1002     seqMenu.add(seqShowAnnotationsMenu);
1003     seqMenu.add(seqHideAnnotationsMenu);
1004     seqMenu.add(seqAddReferenceAnnotations);
1005     seqMenu.add(sequenceName);
1006     seqMenu.add(makeReferenceSeq);
1007     // seqMenu.add(sequenceDetails);
1008
1009     if (!ap.av.applet.useXtrnalSviewer)
1010     {
1011       seqMenu.add(pdb);
1012     }
1013     seqMenu.add(repGroup);
1014     menu1.add(editGroupName);
1015     menu1.add(colourMenu);
1016     menu1.add(showBoxes);
1017     menu1.add(showText);
1018     menu1.add(showColourText);
1019     menu1.add(displayNonconserved);
1020     toggleCase.addActionListener(this);
1021     pdb.addActionListener(this);
1022     hideSeqs.addActionListener(this);
1023     repGroup.addActionListener(this);
1024     revealAll.addActionListener(this);
1025     revealSeq.addActionListener(this);
1026     makeReferenceSeq.addActionListener(this);
1027   }
1028
1029   void refresh()
1030   {
1031     ap.paintAlignment(true);
1032   }
1033
1034   protected void clustalColour_actionPerformed()
1035   {
1036     SequenceGroup sg = getGroup();
1037     sg.cs = new ClustalxColourScheme(sg, ap.av.getHiddenRepSequences());
1038     refresh();
1039   }
1040
1041   protected void zappoColour_actionPerformed()
1042   {
1043     getGroup().cs = new ZappoColourScheme();
1044     refresh();
1045   }
1046
1047   protected void taylorColour_actionPerformed()
1048   {
1049     getGroup().cs = new TaylorColourScheme();
1050     refresh();
1051   }
1052
1053   protected void hydrophobicityColour_actionPerformed()
1054   {
1055     getGroup().cs = new HydrophobicColourScheme();
1056     refresh();
1057   }
1058
1059   protected void helixColour_actionPerformed()
1060   {
1061     getGroup().cs = new HelixColourScheme();
1062     refresh();
1063   }
1064
1065   protected void strandColour_actionPerformed()
1066   {
1067     getGroup().cs = new StrandColourScheme();
1068     refresh();
1069   }
1070
1071   protected void turnColour_actionPerformed()
1072   {
1073     getGroup().cs = new TurnColourScheme();
1074     refresh();
1075   }
1076
1077   protected void buriedColour_actionPerformed()
1078   {
1079     getGroup().cs = new BuriedColourScheme();
1080     refresh();
1081   }
1082
1083   public void nucleotideMenuItem_actionPerformed()
1084   {
1085     getGroup().cs = new NucleotideColourScheme();
1086     refresh();
1087   }
1088
1089   protected void abovePIDColour_itemStateChanged()
1090   {
1091     SequenceGroup sg = getGroup();
1092     if (sg.cs == null)
1093     {
1094       return;
1095     }
1096
1097     if (abovePIDColour.getState())
1098     {
1099       sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av
1100               .getHiddenRepSequences()), 0, ap.av.getAlignment().getWidth()));
1101       int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs, getGroup()
1102               .getName());
1103
1104       sg.cs.setThreshold(threshold, ap.av.isIgnoreGapsConsensus());
1105
1106       SliderPanel.showPIDSlider();
1107
1108     }
1109     else
1110     // remove PIDColouring
1111     {
1112       sg.cs.setThreshold(0, ap.av.isIgnoreGapsConsensus());
1113     }
1114
1115     refresh();
1116
1117   }
1118
1119   protected void userDefinedColour_actionPerformed()
1120   {
1121     new UserDefinedColours(ap, getGroup());
1122   }
1123
1124   protected void PIDColour_actionPerformed()
1125   {
1126     SequenceGroup sg = getGroup();
1127     sg.cs = new PIDColourScheme();
1128     sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av
1129             .getHiddenRepSequences()), 0, ap.av.getAlignment().getWidth()));
1130     refresh();
1131   }
1132
1133   protected void BLOSUM62Colour_actionPerformed()
1134   {
1135     SequenceGroup sg = getGroup();
1136
1137     sg.cs = new Blosum62ColourScheme();
1138
1139     sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av
1140             .getHiddenRepSequences()), 0, ap.av.getAlignment().getWidth()));
1141
1142     refresh();
1143   }
1144
1145   protected void noColourmenuItem_actionPerformed()
1146   {
1147     getGroup().cs = null;
1148     refresh();
1149   }
1150
1151   protected void conservationMenuItem_itemStateChanged()
1152   {
1153     SequenceGroup sg = getGroup();
1154     if (sg.cs == null)
1155     {
1156       return;
1157     }
1158
1159     if (conservationMenuItem.getState())
1160     {
1161       sg.cs.setConservation(Conservation.calculateConservation("Group", sg
1162               .getSequences(ap.av.getHiddenRepSequences()), 0, ap.av
1163               .getAlignment().getWidth(), false, ap.av.getConsPercGaps(),
1164               false));
1165       SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
1166       SliderPanel.showConservationSlider();
1167     }
1168     else
1169     // remove ConservationColouring
1170     {
1171       sg.cs.setConservation(null);
1172     }
1173
1174     refresh();
1175   }
1176
1177   SequenceGroup getGroup()
1178   {
1179     SequenceGroup sg = ap.av.getSelectionGroup();
1180
1181     // this method won't add a new group if it already exists
1182     if (sg != null)
1183     {
1184       ap.av.getAlignment().addGroup(sg);
1185     }
1186
1187     return sg;
1188   }
1189
1190   void unGroupMenuItem_actionPerformed()
1191   {
1192     SequenceGroup sg = ap.av.getSelectionGroup();
1193     ap.av.getAlignment().deleteGroup(sg);
1194     ap.av.setSelectionGroup(null);
1195     ap.paintAlignment(true);
1196   }
1197
1198   void createGroupMenuItem_actionPerformed()
1199   {
1200     getGroup(); // implicitly create group
1201     refresh();
1202   }
1203
1204   public void showColourText_itemStateChanged()
1205   {
1206     getGroup().setColourText(showColourText.getState());
1207     refresh();
1208   }
1209
1210   public void showText_itemStateChanged()
1211   {
1212     getGroup().setDisplayText(showText.getState());
1213     refresh();
1214   }
1215
1216   public void makeReferenceSeq_actionPerformed()
1217   {
1218     if (!ap.av.getAlignment().hasSeqrep())
1219     {
1220       // initialise the display flags so the user sees something happen
1221       ap.av.setDisplayReferenceSeq(true);
1222       ap.av.setColourByReferenceSeq(true);
1223       ap.av.getAlignment().setSeqrep(seq);
1224     }
1225     else
1226     {
1227       if (ap.av.getAlignment().getSeqrep() == seq)
1228       {
1229         ap.av.getAlignment().setSeqrep(null);
1230       }
1231       else
1232       {
1233         ap.av.getAlignment().setSeqrep(seq);
1234       }
1235     }
1236     refresh();
1237   }
1238
1239   public void showNonconserved_itemStateChanged()
1240   {
1241     getGroup().setShowNonconserved(this.displayNonconserved.getState());
1242     refresh();
1243   }
1244
1245   public void showBoxes_itemStateChanged()
1246   {
1247     getGroup().setDisplayBoxes(showBoxes.getState());
1248     refresh();
1249   }
1250
1251   void hideSequences(boolean representGroup)
1252   {
1253     ap.av.hideSequences(seq, representGroup);
1254   }
1255
1256   /**
1257    * Add annotation types to 'Show annotations' and/or 'Hide annotations' menus.
1258    * "All" is added first, followed by a separator. Then add any annotation
1259    * types associated with the current selection. Separate menus are built for
1260    * the selected sequence group (if any), and the selected sequence.
1261    * <p>
1262    * Some annotation rows are always rendered together - these can be identified
1263    * by a common graphGroup property > -1. Only one of each group will be marked
1264    * as visible (to avoid duplication of the display). For such groups we add a
1265    * composite type name, e.g.
1266    * <p>
1267    * IUPredWS (Long), IUPredWS (Short)
1268    * 
1269    * @param seq
1270    */
1271   protected void buildAnnotationTypesMenus(Menu showMenu, Menu hideMenu,
1272           List<SequenceI> forSequences)
1273   {
1274     showMenu.removeAll();
1275     hideMenu.removeAll();
1276
1277     final List<String> all = Arrays.asList(new String[] { MessageManager
1278             .getString("label.all") });
1279     addAnnotationTypeToShowHide(showMenu, forSequences, "", all, true, true);
1280     addAnnotationTypeToShowHide(hideMenu, forSequences, "", all, true,
1281             false);
1282     showMenu.addSeparator();
1283     hideMenu.addSeparator();
1284
1285     final AlignmentAnnotation[] annotations = ap.getAlignment()
1286             .getAlignmentAnnotation();
1287
1288     /*
1289      * Find shown/hidden annotations types, distinguished by source (calcId),
1290      * and grouped by graphGroup. Using LinkedHashMap means we will retrieve in
1291      * the insertion order, which is the order of the annotations on the
1292      * alignment.
1293      */
1294     Map<String, List<List<String>>> shownTypes = new LinkedHashMap<String, List<List<String>>>();
1295     Map<String, List<List<String>>> hiddenTypes = new LinkedHashMap<String, List<List<String>>>();
1296     AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes, hiddenTypes,
1297             AlignmentAnnotationUtils.asList(annotations), forSequences);
1298
1299     for (String calcId : hiddenTypes.keySet())
1300     {
1301       for (List<String> type : hiddenTypes.get(calcId))
1302       {
1303         addAnnotationTypeToShowHide(showMenu, forSequences, calcId, type,
1304                 false, true);
1305       }
1306     }
1307     // grey out 'show annotations' if none are hidden
1308     showMenu.setEnabled(!hiddenTypes.isEmpty());
1309
1310     for (String calcId : shownTypes.keySet())
1311     {
1312       for (List<String> type : shownTypes.get(calcId))
1313       {
1314         addAnnotationTypeToShowHide(hideMenu, forSequences, calcId, type,
1315                 false, false);
1316       }
1317     }
1318     // grey out 'hide annotations' if none are shown
1319     hideMenu.setEnabled(!shownTypes.isEmpty());
1320   }
1321
1322   /**
1323    * Add one annotation type to the 'Show Annotations' or 'Hide Annotations'
1324    * menus.
1325    * 
1326    * @param showOrHideMenu
1327    *          the menu to add to
1328    * @param forSequences
1329    *          the sequences whose annotations may be shown or hidden
1330    * @param calcId
1331    * @param types
1332    *          the label to add
1333    * @param allTypes
1334    *          if true this is a special label meaning 'All'
1335    * @param actionIsShow
1336    *          if true, the select menu item action is to show the annotation
1337    *          type, else hide
1338    */
1339   protected void addAnnotationTypeToShowHide(Menu showOrHideMenu,
1340           final List<SequenceI> forSequences, String calcId,
1341           final List<String> types, final boolean allTypes,
1342           final boolean actionIsShow)
1343   {
1344     String label = types.toString(); // [a, b, c]
1345     label = label.substring(1, label.length() - 1);
1346     final MenuItem item = new MenuItem(label);
1347     item.addActionListener(new java.awt.event.ActionListener()
1348     {
1349       @Override
1350       public void actionPerformed(ActionEvent e)
1351       {
1352         AlignmentUtils.showOrHideSequenceAnnotations(ap.getAlignment(),
1353                 types, forSequences, allTypes, actionIsShow);
1354         refresh();
1355       }
1356     });
1357     showOrHideMenu.add(item);
1358   }
1359
1360 }