JAL-1683 replace year/version strings with tokens in source
[jalview.git] / src / jalview / gui / PopupMenu.java
index fe0ae1c..7878f75 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -25,8 +25,11 @@ 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.AlignmentI;
 import jalview.datamodel.Annotation;
+import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.Sequence;
@@ -62,7 +65,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;
@@ -139,6 +141,8 @@ public class PopupMenu extends JPopupMenu
   JMenuItem sequenceDetails = new JMenuItem();
 
   JMenuItem sequenceSelDetails = new JMenuItem();
+  
+  JMenuItem makeReferenceSeq = new JMenuItem();
 
   JMenuItem chooseAnnotations = new JMenuItem();
 
@@ -190,11 +194,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();
 
@@ -213,6 +223,8 @@ public class PopupMenu extends JPopupMenu
 
   JMenu groupLinksMenu;
 
+  JMenuItem hideInsertions = new JMenuItem();
+
   /**
    * Creates a new PopupMenu object.
    * 
@@ -282,17 +294,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
     {
@@ -306,6 +327,12 @@ public class PopupMenu extends JPopupMenu
     if (seq != null)
     {
       sequenceMenu.setText(sequence.getName());
+      if (seq == ap.av.getAlignment().getSeqrep())
+      {
+        makeReferenceSeq.setText("Unmark representative");
+      } else {
+        makeReferenceSeq.setText("Mark as representative");
+      }
 
       if (seq.getDatasetSequence().getPDBId() != null
               && seq.getDatasetSequence().getPDBId().size() > 0)
@@ -353,17 +380,16 @@ public class PopupMenu extends JPopupMenu
         }
         // structureMenu.remove(colStructureMenu);
       }
-
       if (ap.av.getAlignment().isNucleotide() == true)
       {
         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 +399,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 +426,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();
 
@@ -425,7 +451,6 @@ public class PopupMenu extends JPopupMenu
             }
           }
         }
-
       }
 
       menuItem = new JMenuItem(
@@ -809,9 +834,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 +846,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 +881,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 +1474,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"));
@@ -1483,7 +1516,28 @@ public class PopupMenu extends JPopupMenu
         editSequence_actionPerformed(actionEvent);
       }
     });
+    makeReferenceSeq.setText(MessageManager
+            .getString("label.mark_as_representative"));
+    makeReferenceSeq.addActionListener(new ActionListener()
+    {
+      
+      @Override
+      public void actionPerformed(ActionEvent actionEvent)
+      {
+        makeReferenceSeq_actionPerformed(actionEvent);
+        
+      }
+    });
+    hideInsertions.setText(MessageManager.getString("label.hide_insertions"));
+    hideInsertions.addActionListener(new ActionListener()
+    {
 
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        hideInsertions_actionPerformed(e);
+      }
+    });
     /*
      * annotationMenuItem.setText("By Annotation");
      * annotationMenuItem.addActionListener(new ActionListener() { public void
@@ -1493,27 +1547,24 @@ public class PopupMenu extends JPopupMenu
     groupMenu.add(sequenceSelDetails);
     add(groupMenu);
     add(sequenceMenu);
-    this.add(structureMenu);
+    add(structureMenu);
+    if (sequence!=null)
+    {
+      add(hideInsertions);
+    }
     // annotations configuration panel suppressed for now
     // 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);
@@ -1522,6 +1573,7 @@ public class PopupMenu extends JPopupMenu
     groupMenu.add(jMenu1);
     sequenceMenu.add(sequenceName);
     sequenceMenu.add(sequenceDetails);
+    sequenceMenu.add(makeReferenceSeq);
     colourMenu.add(textColour);
     colourMenu.add(noColourmenuItem);
     colourMenu.add(clustalColour);
@@ -1749,12 +1801,13 @@ public class PopupMenu extends JPopupMenu
 
   /**
    * Check for any annotations on the underlying dataset sequences (for the
-   * current selection group) which are not on the alignment annotations for the
-   * sequence. If any are found, enable the option to add them to the alignment.
-   * The criteria for 'on the alignment' is finding an alignment annotation on
-   * the sequence, that matches on calcId and label. A tooltip is also
-   * constructed that displays the source (calcId) and type (label) of the
-   * annotations that can be added.
+   * current selection group) which are not 'on the alignment'.If any are found,
+   * enable the option to add them to the alignment. The criteria for 'on the
+   * alignment' is finding an alignment annotation on the alignment, matched on
+   * calcId, label and sequenceRef.
+   * 
+   * A tooltip is also constructed that displays the source (calcId) and type
+   * (label) of the annotations that can be added.
    * 
    * @param menuItem
    * @param forSequences
@@ -1781,10 +1834,11 @@ public class PopupMenu extends JPopupMenu
     /*
      * For each sequence selected in the alignment, make a list of any
      * annotations on the underlying dataset sequence which are not already on
-     * the sequence in the alignment.
+     * the alignment.
      * 
      * Build a map of { alignmentSequence, <List of annotations to add> }
      */
+    AlignmentI al = this.ap.av.getAlignment();
     final Map<SequenceI, List<AlignmentAnnotation>> candidates = new LinkedHashMap<SequenceI, List<AlignmentAnnotation>>();
     for (SequenceI seq : forSequences)
     {
@@ -1802,11 +1856,12 @@ public class PopupMenu extends JPopupMenu
       for (AlignmentAnnotation dsann : datasetAnnotations)
       {
         /*
-         * If the sequence has no annotation that matches this one, then add
-         * this one to the results list.
+         * Find matching annotations on the alignment.
          */
-        if (seq.getAlignmentAnnotations(dsann.getCalcId(), dsann.label)
-                .isEmpty())
+        final Iterable<AlignmentAnnotation> matchedAlignmentAnnotations = al
+                .findAnnotations(seq, dsann.getCalcId(),
+                        dsann.label);
+        if (!matchedAlignmentAnnotations.iterator().hasNext())
         {
           result.add(dsann);
           tipEntries.put(dsann.getCalcId(), dsann.label);
@@ -1860,7 +1915,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))
@@ -1876,18 +1930,61 @@ public class PopupMenu extends JPopupMenu
         }
         copyAnn.restrict(startRes, endRes);
 
-        // add to the sequence (sets copyAnn.datasetSequence)
-        seq.addAlignmentAnnotation(copyAnn);
+        /*
+         * Add to the sequence (sets copyAnn.datasetSequence), unless the
+         * original annotation is already on the sequence.
+         */
+        if (!seq.hasAnnotation(ann))
+        {
+          seq.addAlignmentAnnotation(copyAnn);
+        }
         // 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;
       }
     }
     refresh();
   }
 
+  protected void makeReferenceSeq_actionPerformed(ActionEvent actionEvent)
+  {
+    if (!ap.av.getAlignment().hasSeqrep())
+    {
+      // initialise the display flags so the user sees something happen
+      ap.av.setDisplayReferenceSeq(true);
+      ap.av.setColourByReferenceSeq(true);
+      ap.av.getAlignment().setSeqrep(sequence);
+    }
+    else
+    {
+      if (ap.av.getAlignment().getSeqrep() == sequence)
+      {
+        ap.av.getAlignment().setSeqrep(null);
+      }
+      else
+      {
+        ap.av.getAlignment().setSeqrep(sequence);
+      }
+    }
+    refresh();
+  }
+
+  protected void hideInsertions_actionPerformed(ActionEvent actionEvent)
+  {
+    if (sequence != null)
+    {
+      ColumnSelection cs = ap.av.getColumnSelection();
+      if (cs == null)
+      {
+        cs = new ColumnSelection();
+      }
+      cs.hideInsertionsFor(sequence);
+      ap.av.setColumnSelection(cs);
+    }
+    refresh();
+  }
   protected void sequenceSelectionDetails_actionPerformed()
   {
     createSequenceDetailsReport(ap.av.getSequenceSelection());
@@ -1918,7 +2015,9 @@ public class PopupMenu extends JPopupMenu
                       true,
                       true,
                       false,
-                      (ap.seqPanel.seqCanvas.fr != null) ? ap.seqPanel.seqCanvas.fr.minmax
+                      (ap.getSeqPanel().seqCanvas.fr != null) ? ap
+                              .getSeqPanel().seqCanvas.fr
+                              .getMinMax()
                               : null);
       contents.append("</p>");
     }
@@ -2635,7 +2734,7 @@ public class PopupMenu extends JPopupMenu
     System.arraycopy(features, 0, tfeatures, 0, rsize);
     features = tfeatures;
     seqs = rseqs;
-    if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
+    if (ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
             features, true, ap))
     {
       ap.alignFrame.setShowSeqFeatures(true);
@@ -2693,7 +2792,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());