JAL-2794 removed several redundancies in the binding code
authorkjvdheide <kjvanderheide@dundee.ac.uk>
Wed, 22 Nov 2017 18:01:13 +0000 (18:01 +0000)
committerkjvdheide <kjvanderheide@dundee.ac.uk>
Wed, 22 Nov 2017 18:01:13 +0000 (18:01 +0000)
src/jalview/ext/archaeopteryx/JalviewBinding.java

index 53078cf..d2072cc 100644 (file)
@@ -16,6 +16,7 @@ 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;
@@ -23,6 +24,7 @@ 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;
 
@@ -78,7 +80,7 @@ public final class JalviewBinding
     treeTabs = archaeopteryx.getMainPanel().getTabbedPane();
     ssm = parentAvport.getStructureSelectionManager();
 
-   // archaeopteryx.getMainPanel().getControlPanel().setColorBranches(true);
+    // archaeopteryx.getMainPanel().getControlPanel().setColorBranches(true);
     
     ssm.addSelectionListener(this);
     treeView.addMouseListener(this);
@@ -125,27 +127,44 @@ 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)
   {
-    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);
 
-      }
-      showNodeSelectionOnAlign(node);
-    }
-    else
-    {
-      partitionTree();
-    }
   }
   @Override
   public void mouseReleased(MouseEvent e)
@@ -190,8 +209,31 @@ public final class JalviewBinding
 
   }
 
-  public void partitionTree()
+  /**
+   * Partially refactored from TreeCanvas
+   */
+  public void partitionTree(final MouseEvent e)
   {
+    int x = e.getX();
+
+    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);
+
+    }
+
 
   }
 
@@ -199,6 +241,11 @@ public final class JalviewBinding
   @Override
   public void showNodeSelectionOnAlign(final PhylogenyNode node)
   {
+    if (treeView.getFoundNodes0() == null)
+    {
+      treeView.setFoundNodes0(new HashSet<Long>());
+    }
+
       if (node.isInternal())
       {
         showMatchingChildSequences(node);
@@ -208,7 +255,8 @@ public final class JalviewBinding
       {
         showMatchingSequence(node);
       }
-
+    treeView.repaint();
+    PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
     }
 
 
@@ -221,9 +269,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()); // redundant?
+      // parentAvport.sendSelection(); // this shouldn't be needed
+
     }
   }
 
@@ -233,6 +283,7 @@ public final class JalviewBinding
     List<PhylogenyNode> childNodes = PhylogenyMethods
             .getAllDescendants(parentNode);
 
+
     for (PhylogenyNode childNode : childNodes)
     {
       // childNode.getBranchData().setBranchColor(new BranchColor(Color.BLUE));
@@ -240,12 +291,17 @@ public final class JalviewBinding
       SequenceI matchingSequence = nodesBoundToSequences.get(childNode);
       if (matchingSequence != null)
       {
+        long nodeId = childNode.getId();
+        addOrRemoveInSet(treeView.getFoundNodes0(), nodeId);
+
         treeSelectionChanged(matchingSequence);
 
       }
+
     }
-    parentAvport.sendSelection();
-    PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId()); // redundant?
+    // parentAvport.sendSelection(); // this shouldn't be needed
+
+
   }
 
   /**
@@ -257,16 +313,38 @@ 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();
+
+      if (selected == null)
+      {
+        selected = new SequenceGroup();
+        parentAvport.setSelectionGroup(selected);
+      }
+
+      selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
+        selected.addOrRemove(sequence, true);
     }
 
-    selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
-    selected.addOrRemove(sequence, true);
+  }
+
+  /**
+   * TO BE MOVED
+   * 
+   * @param set
+   * @param objectToCheck
+   */
+  public <E> void addOrRemoveInSet(Set<E> set, E objectToCheck)
+  {
+    if (set.contains(objectToCheck))
+    {
+      set.remove(objectToCheck);
+    }
+    else
+    {
+      set.add(objectToCheck);
+    }
 
   }