merge commit
[jalview.git] / src / jalview / gui / PopupMenu.java
index ad7726d..9976471 100644 (file)
@@ -25,6 +25,7 @@ import jalview.analysis.AlignmentAnnotationUtils;
 import jalview.analysis.Conservation;
 import jalview.commands.ChangeCaseCommand;
 import jalview.commands.EditCommand;
+import jalview.commands.EditCommand.Action;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.DBRefEntry;
@@ -62,7 +63,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -190,11 +190,17 @@ public class PopupMenu extends JPopupMenu
 
   JMenu outputMenu = new JMenu();
 
-  JMenu showAnnotationsMenu = new JMenu();
+  JMenu seqShowAnnotationsMenu = new JMenu();
 
-  JMenu hideAnnotationsMenu = new JMenu();
+  JMenu seqHideAnnotationsMenu = new JMenu();
 
-  JMenuItem addReferenceAnnotations = new JMenuItem();
+  JMenuItem seqAddReferenceAnnotations = new JMenuItem();
+
+  JMenu groupShowAnnotationsMenu = new JMenu();
+
+  JMenu groupHideAnnotationsMenu = new JMenu();
+
+  JMenuItem groupAddReferenceAnnotations = new JMenuItem();
 
   JMenuItem sequenceFeature = new JMenuItem();
 
@@ -282,17 +288,26 @@ public class PopupMenu extends JPopupMenu
 
     /*
      * Build menus for annotation types that may be shown or hidden, and for
-     * 'reference annotations' that may be added to the alignment. The scope is
-     * annotations for the current selection group (if there is one), else the
-     * current sequence (if there is one), else not applicable (e.g. for popup
-     * menu within the sequence).
+     * 'reference annotations' that may be added to the alignment. First for the
+     * currently selected sequence (if there is one):
      */
-    final List<SequenceI> sequenceScope = getSequenceScope(seq);
-    if (!sequenceScope.isEmpty())
-    {
-      buildAnnotationTypesMenus(sequenceScope);
-      configureReferenceAnnotationsMenu(addReferenceAnnotations, sequenceScope);
-    }
+    final List<SequenceI> selectedSequence = (seq == null ? Collections
+            .<SequenceI> emptyList() : Arrays.asList(seq));
+    buildAnnotationTypesMenus(seqShowAnnotationsMenu,
+            seqHideAnnotationsMenu, selectedSequence);
+    configureReferenceAnnotationsMenu(seqAddReferenceAnnotations,
+            selectedSequence);
+
+    /*
+     * And repeat for the current selection group (if there is one):
+     */
+    final List<SequenceI> selectedGroup = (ap.av.getSelectionGroup() == null ? Collections
+            .<SequenceI> emptyList() : ap.av.getSelectionGroup()
+            .getSequences());
+    buildAnnotationTypesMenus(groupShowAnnotationsMenu,
+            groupHideAnnotationsMenu, selectedGroup);
+    configureReferenceAnnotationsMenu(groupAddReferenceAnnotations,
+            selectedGroup);
 
     try
     {
@@ -358,12 +373,12 @@ public class PopupMenu extends JPopupMenu
       {
         AlignmentAnnotation[] aa = ap.av.getAlignment()
                 .getAlignmentAnnotation();
-        for (int i = 0; i < aa.length; i++)
+        for (int i = 0; aa != null && i < aa.length; i++)
         {
-          if (aa[i].getRNAStruc() != null)
+          if (aa[i].isValidStruc() && aa[i].sequenceRef == null)
           {
             final String rnastruc = aa[i].getRNAStruc();
-            final String structureLine = aa[i].label;
+            final String structureLine = aa[i].label + " (alignment)";
             menuItem = new JMenuItem();
             menuItem.setText(MessageManager.formatMessage(
                     "label.2d_rna_structure_line", new String[]
@@ -373,15 +388,15 @@ public class PopupMenu extends JPopupMenu
               @Override
               public void actionPerformed(ActionEvent e)
               {
-                // System.out.println("1:"+structureLine);
-                System.out.println("1:sname" + seq.getName());
-                System.out.println("2:seq" + seq);
-
-                // System.out.println("3:"+seq.getSequenceAsString());
-                System.out.println("3:strucseq" + rnastruc);
-                // System.out.println("4:struc"+seq.getRNA());
-                System.out.println("5:name" + seq.getName());
-                System.out.println("6:ap" + ap);
+                // // System.out.println("1:"+structureLine);
+                // System.out.println("1:sname" + seq.getName());
+                // System.out.println("2:seq" + seq);
+                //
+                // // System.out.println("3:"+seq.getSequenceAsString());
+                // System.out.println("3:strucseq" + rnastruc);
+                // // System.out.println("4:struc"+seq.getRNA());
+                // System.out.println("5:name" + seq.getName());
+                // System.out.println("6:ap" + ap);
                 new AppVarna(structureLine, seq, seq.getSequenceAsString(),
                         rnastruc, seq.getName(), ap);
                 // new AppVarna(seq.getName(),seq,rnastruc,seq.getRNA(),
@@ -400,7 +415,7 @@ public class PopupMenu extends JPopupMenu
           AlignmentAnnotation seqAnno[] = seq.getAnnotation();
           for (int i = 0; i < seqAnno.length; i++)
           {
-            if (seqAnno[i].getRNAStruc() != null)
+            if (seqAnno[i].isValidStruc())
             {
               final String rnastruc = seqAnno[i].getRNAStruc();
 
@@ -809,9 +824,10 @@ public class PopupMenu extends JPopupMenu
   }
 
   /**
-   * Add annotation types to a 'Show annotations' or 'Hide annotations' menu.
+   * Add annotation types to 'Show annotations' and/or 'Hide annotations' menus.
    * "All" is added first, followed by a separator. Then add any annotation
-   * types associated with the current selection.
+   * types associated with the current selection. Separate menus are built for
+   * the selected sequence group (if any), and the selected sequence.
    * <p>
    * Some annotation rows are always rendered together - these can be identified
    * by a common graphGroup property > -1. Only one of each group will be marked
@@ -820,29 +836,32 @@ public class PopupMenu extends JPopupMenu
    * <p>
    * IUPredWS (Long), IUPredWS (Short)
    * 
-   * @param forSequences
+   * @param seq
    */
-  protected void buildAnnotationTypesMenus(List<SequenceI> forSequences)
+  protected void buildAnnotationTypesMenus(JMenu showMenu, JMenu hideMenu,
+          List<SequenceI> forSequences)
   {
-    showAnnotationsMenu.removeAll();
-    hideAnnotationsMenu.removeAll();
+    showMenu.removeAll();
+    hideMenu.removeAll();
+
     final List<String> all = Arrays.asList(ALL_ANNOTATIONS);
-    addAnnotationTypeToShowHide(showAnnotationsMenu, forSequences, "", all,
-            true, true);
-    addAnnotationTypeToShowHide(hideAnnotationsMenu, forSequences, "", all,
-            true, false);
-    showAnnotationsMenu.addSeparator();
-    hideAnnotationsMenu.addSeparator();
+    addAnnotationTypeToShowHide(showMenu, forSequences, "", all, true, true);
+    addAnnotationTypeToShowHide(hideMenu, forSequences, "", all, true,
+            false);
+    showMenu.addSeparator();
+    hideMenu.addSeparator();
 
     final AlignmentAnnotation[] annotations = ap.getAlignment()
             .getAlignmentAnnotation();
 
     /*
      * Find shown/hidden annotations types, distinguished by source (calcId),
-     * and grouped by graphGroup.
+     * and grouped by graphGroup. Using LinkedHashMap means we will retrieve in
+     * the insertion order, which is the order of the annotations on the
+     * alignment.
      */
-    Map<String, List<List<String>>> shownTypes = new HashMap<String, List<List<String>>>();
-    Map<String, List<List<String>>> hiddenTypes = new HashMap<String, List<List<String>>>();
+    Map<String, List<List<String>>> shownTypes = new LinkedHashMap<String, List<List<String>>>();
+    Map<String, List<List<String>>> hiddenTypes = new LinkedHashMap<String, List<List<String>>>();
     AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes,
             hiddenTypes,
             AlignmentAnnotationUtils.asList(annotations),
@@ -852,23 +871,23 @@ public class PopupMenu extends JPopupMenu
     {
       for (List<String> type : hiddenTypes.get(calcId))
       {
-        addAnnotationTypeToShowHide(showAnnotationsMenu, forSequences,
+        addAnnotationTypeToShowHide(showMenu, forSequences,
                 calcId, type, false, true);
       }
     }
     // grey out 'show annotations' if none are hidden
-    showAnnotationsMenu.setEnabled(!hiddenTypes.isEmpty());
+    showMenu.setEnabled(!hiddenTypes.isEmpty());
 
     for (String calcId : shownTypes.keySet())
     {
       for (List<String> type : shownTypes.get(calcId))
       {
-        addAnnotationTypeToShowHide(hideAnnotationsMenu, forSequences,
+        addAnnotationTypeToShowHide(hideMenu, forSequences,
                 calcId, type, false, false);
       }
     }
     // grey out 'hide annotations' if none are shown
-    hideAnnotationsMenu.setEnabled(!shownTypes.isEmpty());
+    hideMenu.setEnabled(!shownTypes.isEmpty());
   }
 
   /**
@@ -1445,9 +1464,13 @@ public class PopupMenu extends JPopupMenu
     });
     outputMenu.setText(MessageManager.getString("label.out_to_textbox")
             + "...");
-    showAnnotationsMenu.setText(MessageManager
+    seqShowAnnotationsMenu.setText(MessageManager
             .getString("label.show_annotations"));
-    hideAnnotationsMenu.setText(MessageManager
+    seqHideAnnotationsMenu.setText(MessageManager
+            .getString("label.hide_annotations"));
+    groupShowAnnotationsMenu.setText(MessageManager
+            .getString("label.show_annotations"));
+    groupHideAnnotationsMenu.setText(MessageManager
             .getString("label.hide_annotations"));
     sequenceFeature.setText(MessageManager
             .getString("label.create_sequence_feature"));
@@ -1498,22 +1521,15 @@ public class PopupMenu extends JPopupMenu
     // groupMenu.add(chooseAnnotations);
 
     /*
-     * Add show/hide annotations to either Selection menu (if a selection group
-     * in force), else to the Sequence menu.
+     * Add show/hide annotations to the Sequence menu, and to the Selection menu
+     * (if a selection group is in force).
      */
-    SequenceGroup sg = this.ap.av.getSelectionGroup();
-    if (sg != null && sg.getSize() > 0)
-    {
-      groupMenu.add(showAnnotationsMenu);
-      groupMenu.add(hideAnnotationsMenu);
-      groupMenu.add(addReferenceAnnotations);
-    }
-    else
-    {
-      sequenceMenu.add(showAnnotationsMenu);
-      sequenceMenu.add(hideAnnotationsMenu);
-      sequenceMenu.add(addReferenceAnnotations);
-    }
+    sequenceMenu.add(seqShowAnnotationsMenu);
+    sequenceMenu.add(seqHideAnnotationsMenu);
+    sequenceMenu.add(seqAddReferenceAnnotations);
+    groupMenu.add(groupShowAnnotationsMenu);
+    groupMenu.add(groupHideAnnotationsMenu);
+    groupMenu.add(groupAddReferenceAnnotations);
     groupMenu.add(editMenu);
     groupMenu.add(outputMenu);
     groupMenu.add(sequenceFeature);
@@ -1860,7 +1876,6 @@ public class PopupMenu extends JPopupMenu
      * Add annotations at the top of the annotation, in the same order as their
      * related sequences.
      */
-    int insertPosition = 0;
     for (SequenceI seq : candidates.keySet())
     {
       for (AlignmentAnnotation ann : candidates.get(seq))
@@ -1881,7 +1896,7 @@ public class PopupMenu extends JPopupMenu
         // adjust for gaps
         copyAnn.adjustForAlignment();
         // add to the alignment and set visible
-        this.ap.getAlignment().addAnnotation(copyAnn, insertPosition++);
+        this.ap.getAlignment().addAnnotation(copyAnn);
         copyAnn.visible = true;
       }
     }
@@ -2552,7 +2567,8 @@ public class PopupMenu extends JPopupMenu
       String choice = chooser.getSelectedFile().getPath();
       jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);
       new AssociatePdbFileWithSeq().associatePdbWithSeq(choice,
-              jalview.io.AppletFormatAdapter.FILE, sequence, true);
+              jalview.io.AppletFormatAdapter.FILE, sequence, true,
+              Desktop.instance);
     }
 
   }
@@ -2692,7 +2708,7 @@ public class PopupMenu extends JPopupMenu
       {
         EditCommand editCommand = new EditCommand(
                 MessageManager.getString("label.edit_sequences"),
-                EditCommand.REPLACE, dialog.getName().replace(' ',
+                Action.REPLACE, dialog.getName().replace(' ',
                         ap.av.getGapCharacter()),
                 sg.getSequencesAsArray(ap.av.getHiddenRepSequences()),
                 sg.getStartRes(), sg.getEndRes() + 1, ap.av.getAlignment());