JAL-2838 start on sorting
[jalview.git] / src / jalview / ext / archaeopteryx / JalviewBinding.java
index ee349b6..7ec721c 100644 (file)
@@ -16,8 +16,15 @@ import java.awt.event.MouseEvent;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+
+import javax.swing.JTabbedPane;
+import javax.swing.SwingUtilities;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
 
 import org.forester.archaeopteryx.MainFrame;
+import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
 
@@ -36,6 +43,8 @@ public final class JalviewBinding
 
   private AlignmentViewport parentAvport;
 
+  private JTabbedPane treeTabs;
+
   private final StructureSelectionManager ssm;
 
   private Map<SequenceI, PhylogenyNode> sequencesBoundToNodes;
@@ -66,12 +75,48 @@ public final class JalviewBinding
     parentAvport = jalviewAlignmentViewport;
     sequencesBoundToNodes = alignMappedToNodes;
     nodesBoundToSequences = nodesMappedToAlign;
+
     treeView = archaeopteryx.getMainPanel().getCurrentTreePanel();
+    treeTabs = archaeopteryx.getMainPanel().getTabbedPane();
     ssm = parentAvport.getStructureSelectionManager();
+
+    // archaeopteryx.getMainPanel().getControlPanel().setColorBranches(true);
+    
     ssm.addSelectionListener(this);
     treeView.addMouseListener(this);
     PaintRefresher.Register(treeView, parentAvport.getSequenceSetId());
 
+
+    treeTabs.addChangeListener(new ChangeListener()
+    {
+
+      @Override
+      public void stateChanged(ChangeEvent e)
+      {
+
+        SwingUtilities.invokeLater(new Runnable()
+        {
+
+          @Override
+          /**
+           * Resend the selection to the tree view when tabs get switched, this
+           * has to be buried in invokeLater as Forester first resets the tree
+           * view on switching tabs, without invokeLater this would get called
+           * before Forester resets which would nullify the selection.
+           */
+          public void run()
+          {
+            parentAvport.sendSelection();
+            // PaintRefresher.Refresh(treeView,
+            // parentAvport.getSequenceSetId());
+
+          }
+        });
+
+      }
+      
+    });
+
   }
 
   @Override
@@ -82,14 +127,45 @@ public final class JalviewBinding
   @Override
   public void mouseClicked(MouseEvent e)
   {
+    SwingUtilities.invokeLater(new Runnable() {
+
+      @Override
+      /**
+       * invokeLater so that this always runs after Forester's mouseClicked
+       */
+      public void run()
+      {
+        final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
+        if (node != null)
+        {
+          if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) // clear previous
+          // selection if shift
+          // IS NOT pressed
+          {
+            parentAvport.setSelectionGroup(null);
+            treeView.setFoundNodes0(null);
+
+          }
+          showNodeSelectionOnAlign(node);
+        }
+        else
+        {
+          partitionTree(e);
+        
+        
+      }
+      
+      }
+    });
+
+
   }
 
   @Override
   public void mousePressed(final MouseEvent e)
   {
-    showNodeSelectionOnAlign(e);
-  }
 
+  }
   @Override
   public void mouseReleased(MouseEvent e)
   {
@@ -133,19 +209,45 @@ public final class JalviewBinding
 
   }
 
+  /**
+   * Partially refactored from TreeCanvas
+   */
+  public void partitionTree(final MouseEvent e)
+  {
+    int x = e.getX();
+    int lineLength = treeView.getHeight();
+
+    Phylogeny tree = treeView.getPhylogeny();
+    double treeHeight = tree.calculateHeight(true);
+
+
+
+    if (treeHeight != 0)
+    {
+      int viewWidth = treeView.getWidth();
+
+      // treeView.validate();
+
+      // System.out.println("selection");
+      // System.out.println(x);
+      // System.out.println("-------------");
+      // System.out.println("width");
+      // System.out.println(viewWidth);
+
+    }
+
+
+  }
+  
+
 
   @Override
-  public void showNodeSelectionOnAlign(final MouseEvent e)
+  public void showNodeSelectionOnAlign(final PhylogenyNode node)
   {
-    final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
-    if (node != null)
+    if (treeView.getFoundNodes0() == null)
     {
-      if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) // clear previous
-                                                           // selection if shift
-                                                           // IS NOT pressed
-      {
-        parentAvport.setSelectionGroup(null);
-      }
+      treeView.setFoundNodes0(new HashSet<Long>());
+    }
 
       if (node.isInternal())
       {
@@ -156,9 +258,10 @@ public final class JalviewBinding
       {
         showMatchingSequence(node);
       }
-
+    treeView.repaint();
+    PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
     }
-  }
+
 
 
 
@@ -169,9 +272,11 @@ public final class JalviewBinding
     SequenceI matchingSequence = nodesBoundToSequences.get(nodeToMatch);
     if (matchingSequence != null)
     {
+      long nodeId = nodeToMatch.getId();
+      addOrRemoveInSet(treeView.getFoundNodes0(), nodeId);
       treeSelectionChanged(matchingSequence);
-      parentAvport.sendSelection();
-      PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
+      // parentAvport.sendSelection(); // this shouldn't be needed
+
     }
   }
 
@@ -181,19 +286,25 @@ public final class JalviewBinding
     List<PhylogenyNode> childNodes = PhylogenyMethods
             .getAllDescendants(parentNode);
 
+
     for (PhylogenyNode childNode : childNodes)
     {
+      // childNode.getBranchData().setBranchColor(new BranchColor(Color.BLUE));
 
       SequenceI matchingSequence = nodesBoundToSequences.get(childNode);
       if (matchingSequence != null)
       {
+        long nodeId = childNode.getId();
+        addOrRemoveInSet(treeView.getFoundNodes0(), nodeId);
+
         treeSelectionChanged(matchingSequence);
 
       }
+
     }
+    // parentAvport.sendSelection(); // this shouldn't be needed
+
 
-    parentAvport.sendSelection();
-    PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
   }
 
   /**
@@ -205,101 +316,124 @@ public final class JalviewBinding
   @Override
   public void treeSelectionChanged(final SequenceI sequence)
   {
-    SequenceGroup selected = parentAvport.getSelectionGroup();
-
-    if (selected == null)
+    if (!parentAvport.isClosed()) // alignment view could be closed
     {
-      selected = new SequenceGroup();
-      parentAvport.setSelectionGroup(selected);
-    }
+      SequenceGroup selected = parentAvport.getSelectionGroup();
 
-    selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
-    selected.addOrRemove(sequence, true);
+      if (selected == null)
+      {
+        selected = new SequenceGroup();
+        parentAvport.setSelectionGroup(selected);
+      }
 
-  }
+      selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
+        selected.addOrRemove(sequence, true);
+    }
 
-  public AlignmentViewport getParentAvport()
-  {
-    return parentAvport;
   }
+  public void sortByTree_actionPerformed() {
+    // parentAvport.mirrorCommand(command, undo, ssm, source);
 
-  public void setParentAvport(final AlignmentViewport parentAvport)
-  {
-    this.parentAvport = parentAvport;
+    // alignFrame
+    // .addHistoryItem(sortAlignmentIn(treeCanvas.ap));
+    
   }
-  // av.setCurrentTree(tree);
+  
+
+  /**
+   * sort the associated alignment view by the current tree.
+   * 
+   * @param e
+   */
+  // @Override
+  // public void sortByTree_actionPerformed()// modify for Aptx
+  // {
+  //
+  // // if (treeCanvas.applyToAllViews)
+  //
+  // final ArrayList<CommandI> commands = new ArrayList<>();
+  // for (AlignmentPanel ap : PaintRefresher
+  // .getAssociatedPanels(parentAvport.getSequenceSetId()))
+  // {
+  // commands.add(sortAlignmentIn(ap.av.getAlignPanel()));
+  // }
+  // av.getAlignPanel().alignFrame.addHistoryItem(new CommandI()
+  // {
   //
-  // /*
-  // * isPopupTrigger is set for mousePressed (Mac)
-  // * or mouseReleased (Windows)
-  // */
-  // if (e.isPopupTrigger())
+  // @Override
+  // public void undoCommand(AlignmentI[] views)
   // {
-  // if (highlightNode != null)
+  // for (CommandI tsort : commands)
   // {
-  // chooseSubtreeColour();
+  // tsort.undoCommand(views);
   // }
-  // return;
   // }
   //
-  // /*
-  // * defer right-click handling on Windows to
-  // * mouseClicked; note isRightMouseButton
-  // * also matches Cmd-click on Mac which should do
-  // * nothing here
-  // */
-  // if (SwingUtilities.isRightMouseButton(e))
+  // @Override
+  // public int getSize()
   // {
-  // return;
+  // return commands.size();
   // }
   //
-  // int x = e.getX();
-  // int y = e.getY();
-  //
-  // Object ob = findElement(x, y);
-  //
-  // if (ob instanceof SequenceI)
+  // @Override
+  // public String getDescription()
   // {
-  // treeSelectionChanged((Sequence) ob);
-  // PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
-  // repaint();
-  // av.sendSelection();
-  // return;
+  // return "Tree Sort (many views)";
   // }
-  // else if (!(ob instanceof SequenceNode))
-  // {
-  // // Find threshold
-  // if (tree.getMaxHeight() != 0)
-  // {
-  // threshold = (float) (x - offx)
-  // / (float) (getWidth() - labelLength - (2 * offx));
   //
-  // List<SequenceNode> groups = tree.groupNodes(threshold);
-  // setColor(tree.getTopNode(), Color.black);
-  //
-  // AlignmentPanel[] aps = getAssociatedPanels();
-  //
-  // // TODO push calls below into a single AlignViewportI method?
-  // // see also AlignViewController.deleteGroups
-  // for (int a = 0; a < aps.length; a++)
+  // @Override
+  // public void doCommand(AlignmentI[] views)
   // {
-  // aps[a].av.setSelectionGroup(null);
-  // aps[a].av.getAlignment().deleteAllGroups();
-  // aps[a].av.clearSequenceColours();
-  // if (aps[a].av.getCodingComplement() != null)
+  //
+  // for (CommandI tsort : commands)
   // {
-  // aps[a].av.getCodingComplement().setSelectionGroup(null);
-  // aps[a].av.getCodingComplement().getAlignment()
-  // .deleteAllGroups();
-  // aps[a].av.getCodingComplement().clearSequenceColours();
+  // tsort.doCommand(views);
   // }
   // }
-  // colourGroups(groups);
+  // });
+  // for (AlignmentPanel ap : PaintRefresher
+  // .getAssociatedPanels(av.getSequenceSetId()))
+  // {
+  // // ensure all the alignFrames refresh their GI after adding an undo item
+  // ap.alignFrame.updateEditMenuBar();
   // }
-  //
-  // PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
-  // repaint();
   // }
+  // else
+  // {
+  // treeCanvas.ap.alignFrame
+  // .addHistoryItem(sortAlignmentIn(treeCanvas.ap));
+  // }
+
+
+
+  /**
+   * TO BE MOVED
+   * 
+   * @param set
+   * @param objectToCheck
+   */
+  public static <E> void addOrRemoveInSet(Set<E> set, E objectToCheck)
+  {
+    if (set.contains(objectToCheck))
+    {
+      set.remove(objectToCheck);
+    }
+    else
+    {
+      set.add(objectToCheck);
+    }
+
+  }
+
+  public AlignmentViewport getParentAvport()
+  {
+    return parentAvport;
+  }
+
+  public void setParentAvport(final AlignmentViewport parentAvport)
+  {
+    this.parentAvport = parentAvport;
+  }
 }