JAL-3081 JAL-3199 no auto-sort of annotations after manual reordering
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 3 Apr 2019 13:07:37 +0000 (14:07 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 3 Apr 2019 13:07:37 +0000 (14:07 +0100)
src/jalview/analysis/AnnotationSorter.java
src/jalview/gui/AlignFrame.java
src/jalview/gui/AlignmentPanel.java
src/jalview/gui/AnnotationLabels.java
src/jalview/jbgui/GAlignFrame.java

index 2f556f1..5a7e72f 100644 (file)
@@ -51,7 +51,12 @@ public class AnnotationSorter
   {
     // Text descriptions surface in the Preferences Sort by... options
     SEQUENCE_AND_LABEL("Sequence"), LABEL_AND_SEQUENCE("Label"),
-    NONE("No sort");
+    NONE("No sort"),
+
+    /**
+     * custom is set if user drags to reorder annotations
+     */
+    CUSTOM("Customised");
 
     private String description;
 
@@ -86,7 +91,7 @@ public class AnnotationSorter
   private boolean showAutocalcAbove;
 
   // working map of sequence index in alignment
-  private final Map<SequenceI, Integer> sequenceIndices = new HashMap<SequenceI, Integer>();
+  private final Map<SequenceI, Integer> sequenceIndices = new HashMap<>();
 
   /**
    * Constructor given an alignment and the location (top or bottom) of
@@ -273,7 +278,8 @@ public class AnnotationSorter
   public void sort(AlignmentAnnotation[] alignmentAnnotations,
           SequenceAnnotationOrder order)
   {
-    if (alignmentAnnotations == null)
+    if (alignmentAnnotations == null
+            || order == SequenceAnnotationOrder.CUSTOM)
     {
       return;
     }
index 0239bb4..9ee210f 100644 (file)
@@ -22,6 +22,7 @@ package jalview.gui;
 
 import jalview.analysis.AlignmentSorter;
 import jalview.analysis.AlignmentUtils;
+import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.analysis.CrossRef;
 import jalview.analysis.Dna;
 import jalview.analysis.GeneticCodeI;
@@ -145,6 +146,8 @@ import javax.swing.JMenu;
 import javax.swing.JMenuItem;
 import javax.swing.JScrollPane;
 import javax.swing.SwingUtilities;
+import javax.swing.event.MenuEvent;
+import javax.swing.event.MenuListener;
 
 /**
  * DOCUMENT ME!
@@ -362,9 +365,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       sortPairwiseMenuItem_actionPerformed(null);
     }
 
-    this.alignPanel.av
-            .setShowAutocalculatedAbove(isShowAutoCalculatedAbove());
-
     setMenusFromViewport(viewport);
     buildSortByAnnotationScoresMenu();
     calculateTree.addActionListener(new ActionListener()
@@ -481,6 +481,33 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       }
     });
 
+    /*
+     * ensure By Label/Sequence are not selected if annotations
+     * have been reordered manually to CUSTOM order
+     */
+    annotationsMenu.addMenuListener(new MenuListener()
+    {
+      @Override
+      public void menuSelected(MenuEvent e)
+      {
+        SequenceAnnotationOrder sortAnnotationsBy = viewport
+                .getSortAnnotationsBy();
+        sortAnnByLabel.setSelected(
+                sortAnnotationsBy == SequenceAnnotationOrder.LABEL_AND_SEQUENCE);
+        sortAnnBySequence.setSelected(
+                sortAnnotationsBy == SequenceAnnotationOrder.SEQUENCE_AND_LABEL);
+      }
+
+      @Override
+      public void menuDeselected(MenuEvent e)
+      {
+      }
+
+      @Override
+      public void menuCanceled(MenuEvent e)
+      {
+      }
+    });
   }
 
   /**
@@ -5410,14 +5437,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * Store selected annotation sort order for the view and repaint.
+   * Sorts annotations and repaints the alignment
    */
   @Override
-  protected void sortAnnotations_actionPerformed()
+  protected void sortAnnotations()
   {
-    this.alignPanel.av.setSortAnnotationsBy(getAnnotationSortOrder());
-    this.alignPanel.av
-            .setShowAutocalculatedAbove(isShowAutoCalculatedAbove());
+    alignPanel.sortAnnotations();
     alignPanel.paintAlignment(false, false);
   }
 
@@ -5645,6 +5670,34 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
 
   }
+
+  @Override
+  protected boolean setShowAutoCalculatedAbove(
+          boolean showAutoCalculatedAbove)
+  {
+    if (viewport.isShowAutocalculatedAbove() != showAutoCalculatedAbove)
+    {
+      viewport.setShowAutocalculatedAbove(showAutoCalculatedAbove);
+
+      /*
+       * change CUSTOM annotation ordering to NONE 
+       * so that sorting actually does something
+       */
+      if (viewport.getSortAnnotationsBy() == SequenceAnnotationOrder.CUSTOM)
+      {
+        viewport.setSortAnnotationsBy(SequenceAnnotationOrder.NONE);
+      }
+      return true;
+    }
+    return false;
+  }
+
+  @Override
+  public void setAnnotationSortOrder(
+          SequenceAnnotationOrder annotationSortOrder)
+  {
+    viewport.setSortAnnotationsBy(annotationSortOrder);
+  }
 }
 
 class PrintThread extends Thread
index 92b9a50..502ab9d 100644 (file)
@@ -804,10 +804,6 @@ public class AlignmentPanel extends GAlignmentPanel implements
   public void paintAlignment(boolean updateOverview,
           boolean updateStructures)
   {
-    final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
-            av.isShowAutocalculatedAbove());
-    sorter.sort(getAlignment().getAlignmentAnnotation(),
-            av.getSortAnnotationsBy());
     repaint();
 
     if (updateStructures)
@@ -825,6 +821,17 @@ public class AlignmentPanel extends GAlignmentPanel implements
   }
 
   /**
+   * Sorts annotations according to currently selected preference
+   */
+  void sortAnnotations()
+  {
+    final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
+            av.isShowAutocalculatedAbove());
+    sorter.sort(getAlignment().getAlignmentAnnotation(),
+            av.getSortAnnotationsBy());
+  }
+
+  /**
    * DOCUMENT ME!
    * 
    * @param g
index 6da6cc3..2f62a41 100755 (executable)
@@ -22,6 +22,7 @@ package jalview.gui;
 
 import jalview.analysis.AlignSeq;
 import jalview.analysis.AlignmentUtils;
+import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
@@ -599,6 +600,7 @@ public class AnnotationLabels extends JPanel
 
       ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
       ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
+      av.setSortAnnotationsBy(SequenceAnnotationOrder.CUSTOM);
     }
 
     resizePanel = false;
index 9f41c92..50ee6c8 100755 (executable)
@@ -200,14 +200,16 @@ public class GAlignFrame extends JInternalFrame
 
   protected JCheckBoxMenuItem applyAutoAnnotationSettings = new JCheckBoxMenuItem();
 
-  private SequenceAnnotationOrder annotationSortOrder;
-
-  private boolean showAutoCalculatedAbove = false;
-
   private Map<KeyStroke, JMenuItem> accelerators = new HashMap<>();
 
   private SplitContainerI splitFrame;
 
+  protected JCheckBoxMenuItem sortAnnBySequence;
+
+  protected JCheckBoxMenuItem sortAnnByLabel;
+
+  protected JMenu annotationsMenu;
+
   public GAlignFrame()
   {
     try
@@ -286,7 +288,7 @@ public class GAlignFrame extends JInternalFrame
 
     JMenu editMenu = new JMenu(MessageManager.getString("action.edit"));
     JMenu viewMenu = new JMenu(MessageManager.getString("action.view"));
-    JMenu annotationsMenu = new JMenu(
+    annotationsMenu = new JMenu(
             MessageManager.getString("action.annotations"));
     JMenu showMenu = new JMenu(MessageManager.getString("action.show"));
     colourMenu.setText(MessageManager.getString("action.colour"));
@@ -602,9 +604,9 @@ public class GAlignFrame extends JInternalFrame
     SequenceAnnotationOrder sortAnnotationsBy = SequenceAnnotationOrder
             .valueOf(Cache.getDefault(Preferences.SORT_ANNOTATIONS,
                     SequenceAnnotationOrder.NONE.name()));
-    final JCheckBoxMenuItem sortAnnBySequence = new JCheckBoxMenuItem(
+    sortAnnBySequence = new JCheckBoxMenuItem(
             MessageManager.getString("label.sort_annotations_by_sequence"));
-    final JCheckBoxMenuItem sortAnnByLabel = new JCheckBoxMenuItem(
+    sortAnnByLabel = new JCheckBoxMenuItem(
             MessageManager.getString("label.sort_annotations_by_label"));
 
     sortAnnBySequence.setSelected(
@@ -619,7 +621,7 @@ public class GAlignFrame extends JInternalFrame
         setAnnotationSortOrder(
                 newState ? SequenceAnnotationOrder.SEQUENCE_AND_LABEL
                         : SequenceAnnotationOrder.NONE);
-        sortAnnotations_actionPerformed();
+        sortAnnotations();
       }
     });
     sortAnnByLabel.setSelected(
@@ -634,7 +636,7 @@ public class GAlignFrame extends JInternalFrame
         setAnnotationSortOrder(
                 newState ? SequenceAnnotationOrder.LABEL_AND_SEQUENCE
                         : SequenceAnnotationOrder.NONE);
-        sortAnnotations_actionPerformed();
+        sortAnnotations();
       }
     });
     colourTextMenuItem = new JCheckBoxMenuItem(
@@ -885,14 +887,15 @@ public class GAlignFrame extends JInternalFrame
     final boolean autoFirst = Cache
             .getDefault(Preferences.SHOW_AUTOCALC_ABOVE, false);
     showAutoFirst.setSelected(autoFirst);
-    setShowAutoCalculatedAbove(autoFirst);
     showAutoFirst.addActionListener(new ActionListener()
     {
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        setShowAutoCalculatedAbove(showAutoFirst.isSelected());
-        sortAnnotations_actionPerformed();
+        if (setShowAutoCalculatedAbove(showAutoFirst.isSelected()))
+        {
+          sortAnnotations();
+        }
       }
     });
     showAutoLast.setSelected(!showAutoFirst.isSelected());
@@ -901,8 +904,10 @@ public class GAlignFrame extends JInternalFrame
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        setShowAutoCalculatedAbove(!showAutoLast.isSelected());
-        sortAnnotations_actionPerformed();
+        if (setShowAutoCalculatedAbove(!showAutoLast.isSelected()))
+        {
+          sortAnnotations();
+        }
       }
     });
 
@@ -2023,7 +2028,7 @@ public class GAlignFrame extends JInternalFrame
    * 
    * @param sortOrder
    */
-  protected void sortAnnotations_actionPerformed()
+  protected void sortAnnotations()
   {
   }
 
@@ -2620,25 +2625,22 @@ public class GAlignFrame extends JInternalFrame
 
   }
 
-  protected boolean isShowAutoCalculatedAbove()
-  {
-    return showAutoCalculatedAbove;
-  }
-
-  protected void setShowAutoCalculatedAbove(boolean showAutoCalculatedAbove)
-  {
-    this.showAutoCalculatedAbove = showAutoCalculatedAbove;
-  }
-
-  protected SequenceAnnotationOrder getAnnotationSortOrder()
+  /**
+   * Sets the value of the flag for 'show autocalculated annotations above other
+   * annotations'. Answers true if the value changed, else false.
+   * 
+   * @param showAutoCalculatedAbove
+   * @return
+   */
+  protected boolean setShowAutoCalculatedAbove(
+          boolean showAutoCalculatedAbove)
   {
-    return annotationSortOrder;
+    return false;
   }
 
-  protected void setAnnotationSortOrder(
+  public void setAnnotationSortOrder(
           SequenceAnnotationOrder annotationSortOrder)
   {
-    this.annotationSortOrder = annotationSortOrder;
   }
 
   public Map<KeyStroke, JMenuItem> getAccelerators()