JAL-1264 wip
[jalview.git] / src / jalview / gui / PopupMenu.java
index 6042ab8..5f6c556 100644 (file)
@@ -192,6 +192,8 @@ public class PopupMenu extends JPopupMenu
 
   JMenu hideAnnotationsMenu = new JMenu();
 
+  JMenuItem addDatasequenceAnnotations = new JMenuItem();
+
   JMenuItem sequenceFeature = new JMenuItem();
 
   JMenuItem textColour = new JMenuItem();
@@ -812,11 +814,8 @@ public class PopupMenu extends JPopupMenu
     showAnnotationsMenu.removeAll();
     hideAnnotationsMenu.removeAll();
     final List<String> all = Arrays.asList(ALL_ANNOTATIONS);
-    addAnnotationTypeToShowHide(showAnnotationsMenu,
-            all, true,
- true);
-    addAnnotationTypeToShowHide(hideAnnotationsMenu, all, true,
-            false);
+    addAnnotationTypeToShowHide(showAnnotationsMenu, all, true, true);
+    addAnnotationTypeToShowHide(hideAnnotationsMenu, all, true, false);
     showAnnotationsMenu.addSeparator();
     hideAnnotationsMenu.addSeparator();
 
@@ -866,8 +865,8 @@ public class PopupMenu extends JPopupMenu
    */
   public static void getAnnotationTypesForShowHide(
           List<List<String>> shownTypes, List<List<String>> hiddenTypes,
-          BitSet visibleGraphGroups,
-          AlignmentAnnotation[] annotations, SequenceGroup sequenceGroup)
+          BitSet visibleGraphGroups, AlignmentAnnotation[] annotations,
+          SequenceGroup sequenceGroup)
   {
     // lookup table, key = graph group, value = list of types in the group
     Map<Integer, List<String>> groupLabels = new LinkedHashMap<Integer, List<String>>();
@@ -1011,8 +1010,7 @@ public class PopupMenu extends JPopupMenu
    * @param doShow
    */
   protected void showHideAnnotation_actionPerformed(
-          Collection<String> types,
-          boolean anyType, boolean doShow)
+          Collection<String> types, boolean anyType, boolean doShow)
   {
     for (AlignmentAnnotation aa : ap.getAlignment()
             .getAlignmentAnnotation())
@@ -1519,6 +1517,19 @@ public class PopupMenu extends JPopupMenu
             .getString("label.show_annotations"));
     hideAnnotationsMenu.setText(MessageManager
             .getString("label.hide_annotations"));
+    final List<AlignmentAnnotation> referenceAnns = getDatasequenceAnnotationsNotOnAlignment(ap.av
+            .getSelectionGroup());
+    addDatasequenceAnnotations.setText(MessageManager
+            .getString("label.add_reference_annotations"));
+    addDatasequenceAnnotations.setEnabled(!referenceAnns.isEmpty());
+    addDatasequenceAnnotations.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        addReferenceAnnotations_actionPerformed(referenceAnns);
+      }
+    });
     sequenceFeature.setText(MessageManager
             .getString("label.create_sequence_feature"));
     sequenceFeature.addActionListener(new ActionListener()
@@ -1567,6 +1578,7 @@ public class PopupMenu extends JPopupMenu
     // groupMenu.add(chooseAnnotations);
     groupMenu.add(showAnnotationsMenu);
     groupMenu.add(hideAnnotationsMenu);
+    groupMenu.add(addDatasequenceAnnotations);
     groupMenu.add(editMenu);
     groupMenu.add(outputMenu);
     groupMenu.add(sequenceFeature);
@@ -1800,6 +1812,59 @@ public class PopupMenu extends JPopupMenu
             });
   }
 
+  /**
+   * Get a list of any annotations on the dataset sequences in the current
+   * selection group that are not also on the alignment.
+   * <p/>
+   * The criteria for 'on the alignment' is finding an annotation that matches
+   * on sequenceRef.datasetSequence, calcId and label.
+   * 
+   * @return
+   */
+  protected List<AlignmentAnnotation> getDatasequenceAnnotationsNotOnAlignment(
+          SequenceGroup sg)
+  {
+    List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
+    
+    for (SequenceI seq : sg.getSequences())
+    {
+      SequenceI dataset = seq.getDatasetSequence();
+      AlignmentAnnotation[] datasetAnnotations = dataset.getAnnotation();
+      if (datasetAnnotations != null)
+      {
+        for (AlignmentAnnotation dsann : datasetAnnotations)
+        {
+          /*
+           * If the alignment has no annotation that matches this one...
+           */
+          if (!ap.getAlignment()
+                  .findAnnotation(dataset, dsann.getCalcId(), dsann.label)
+                  .iterator().hasNext())
+          {
+            /*
+             * ...then add it to the result list
+             */
+            result.add(dsann);
+          }
+        }
+      }
+    }
+    return result;
+  }
+
+  /**
+   * Add any annotations on the sequence dataset to the alignment (that are not
+   * already copied to it).
+   */
+  protected void addReferenceAnnotations_actionPerformed(
+          List<AlignmentAnnotation> anns)
+  {
+    for (AlignmentAnnotation ann : anns)
+    {
+      // todo: copy, add, adjust...
+    }
+  }
+
   protected void sequenceSelectionDetails_actionPerformed()
   {
     createSequenceDetailsReport(ap.av.getSequenceSelection());