mearge with develop
[jalview.git] / src / jalview / gui / PopupMenu.java
index ad7726d..2845122 100644 (file)
@@ -62,7 +62,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 +189,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 +287,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
     {
@@ -809,9 +823,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 +835,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 +870,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 +1463,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 +1520,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);
@@ -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);
     }
 
   }