TreeUi: misc tests and fixes
authorKirill Kalishev <kirill.kalishev@jetbrains.com>
Thu, 8 Oct 2009 15:11:15 +0000 (19:11 +0400)
committerKirill Kalishev <kirill.kalishev@jetbrains.com>
Thu, 8 Oct 2009 15:11:15 +0000 (19:11 +0400)
platform/platform-api/src/com/intellij/ide/util/treeView/AbstractTreeUi.java
platform/platform-api/src/com/intellij/ide/util/treeView/UpdaterTreeState.java
platform/platform-api/src/com/intellij/ui/treeStructure/filtered/FilteringTreeBuilder.java

index 3d9876bfc65d2819345d2be186bd7fe5d7da97ab..52c2ebf233083226a70b1076e16d5e64d9a48fda 100644 (file)
@@ -643,8 +643,6 @@ public class AbstractTreeUi {
   }
 
   final void updateSubtreeNow(TreeUpdatePass pass, boolean canSmartExpand) {
-    System.out.println("AbstractTreeUi.updateSubtreeNow pass=" + pass);
-
     maybeSetBusyAndScheduleWaiterForReady(true);
 
     initRootNodeNowIfNeeded(pass);
@@ -1813,6 +1811,8 @@ public class AbstractTreeUi {
   }
 
   private boolean isValidForSelectionAdjusting(TreeNode node) {
+    if (!myTree.isRootVisible() && getRootNode() == node) return false;
+
     if (isLoadingNode(node)) return true;
 
     final Object elementInTree = getElementFor(node);
@@ -2376,7 +2376,14 @@ public class AbstractTreeUi {
           if (!addToSelection) {
             myTree.clearSelection();
           }
-          addNext(elementsToSelect, 0, onDone, originalRows, deferred, scrollToVisible, canSmartExpand);
+          addNext(elementsToSelect, 0, new Runnable() {
+            public void run() {
+              if (getTree().isSelectionEmpty()) {
+                restoreSelection(currentElements);
+              }
+              runDone(onDone);
+            }
+          }, originalRows, deferred, scrollToVisible, canSmartExpand);
         }
         else {
           addToDeferred(elementsToSelect, onDone);
@@ -2385,6 +2392,16 @@ public class AbstractTreeUi {
     });
   }
 
+  private void restoreSelection(Set<Object> selection) {
+    for (Object each : selection) {
+      DefaultMutableTreeNode node = getNodeForElement(each, false);
+      if (node != null && isValidForSelectionAdjusting(node)) {
+        addSelectionPath(getPathFor(node), false, null);
+      }
+    }
+  }
+
+
   private void addToDeferred(final Object[] elementsToSelect, final Runnable onDone) {
     myDeferredSelections.clear();
     myDeferredSelections.add(new Runnable() {
index 79305738c32d748fed4efd40e94cc1ef6f374573..4441ed412ff0679bad89a459e4fbc1b15913daad 100644 (file)
@@ -130,8 +130,6 @@ public class UpdaterTreeState {
 
     myProcessingNow = true;
 
-    System.out.println("UpdaterTreeState.restore actionNode=" + actionNode);
-
     final Object[] toSelect = getToSelect();
     final Object[] toExpand = getToExpand();
 
@@ -144,10 +142,8 @@ public class UpdaterTreeState {
 
     final Set<Object> originallySelected = myUi.getSelectedElements();
 
-    System.out.println("UpdaterTreeState.restore toSelect=" + Arrays.asList(toSelect) + " now selected=" + originallySelected);
     myUi._select(toSelect, new Runnable() {
       public void run() {
-        System.out.println("UpdaterTreeState.run finished selecting=" + Arrays.asList(toSelect) + " now selected=" + myUi.getSelectedElements() + " adjuested=" + adjusted);
         processUnsuccessfulSelections(toSelect, new Function<Object, Object>() {
           public Object fun(final Object o) {
             if (myUi.getTree().isRootVisible() || !myUi.getTreeStructure().getRootElement().equals(o)) {
@@ -186,11 +182,9 @@ public class UpdaterTreeState {
           if (readyElement.equals(myUi.getTreeStructure().getParentElement(eachToSelect))) {
             List<Object> children = myUi.getLoadedChildrenFor(readyElement);
             if (!children.contains(eachToSelect)) {
-              System.out.println(" removed from toSelect node=" + eachToSelect);
               toSelect.remove();
               if (!myToSelect.containsKey(readyElement) && !myUi.getSelectedElements().contains(eachToSelect)) {
                 addAdjustedSelection(eachToSelect, Condition.FALSE);
-                System.out.println(" addToAdjusted node=" + eachToSelect);
               }
             }
           }
@@ -328,7 +322,6 @@ public class UpdaterTreeState {
   }
 
   public void addAdjustedSelection(final Object element, Condition isExpired) {
-    System.out.println("UpdaterTreeState.addAdjustedSelection " + element);
     myAdjustedSelection.put(element, isExpired);
   }
 
index 87404a69257e4f4e077d95909249ebfdadf1b69d..ca9f2331a1b09942922ac2f6b21da87d3dd6e2e1 100644 (file)
@@ -6,16 +6,17 @@ import com.intellij.ide.util.treeView.NodeDescriptor;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.ActionCallback;
 import com.intellij.openapi.util.Disposer;
-import com.intellij.openapi.util.Ref;
 import com.intellij.ui.speedSearch.ElementFilter;
-import com.intellij.ui.treeStructure.*;
+import com.intellij.ui.treeStructure.PatchedDefaultMutableTreeNode;
+import com.intellij.ui.treeStructure.SimpleTree;
+import com.intellij.ui.treeStructure.Tree;
 import com.intellij.util.ui.update.MergingUpdateQueue;
 import com.intellij.util.ui.update.Update;
 import org.jetbrains.annotations.Nullable;
 
+import javax.swing.*;
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.DefaultTreeModel;
-import javax.swing.*;
 import java.util.Comparator;
 
 public class FilteringTreeBuilder extends AbstractTreeBuilder {
@@ -105,18 +106,22 @@ public class FilteringTreeBuilder extends AbstractTreeBuilder {
   private ActionCallback refilterNow(final Object preferredSelection, final boolean adjustSelection) {
     final ActionCallback result = new ActionCallback();
 
-    ((FilteringTreeStructure)getTreeStructure()).refilter();
+    getFilteredStructure().refilter();
     updateFromRoot();
 
     getReady(this).doWhenDone(new Runnable() {
       public void run() {
-        System.out.println("ready=" + getUi().isReady() + " preferred=" + preferredSelection);
+        Object toSelect = preferredSelection != null ? preferredSelection : myLastSuccessfulSelect;
+
+        if (adjustSelection && toSelect != null) {
+          final FilteringTreeStructure.Node nodeToSelect = getFilteredStructure().getVisibleNodeFor(toSelect);
 
-        if (adjustSelection && preferredSelection != null) {
-          FilteringTreeStructure.Node node = ((FilteringTreeStructure)getTreeStructure()).getVisibleNodeFor(preferredSelection);
-          if (node != null) {
-            select(node, new Runnable() {
+          if (nodeToSelect != null) {
+            select(nodeToSelect, new Runnable() {
               public void run() {
+                if (getSelectedElements().contains(nodeToSelect)) {
+                  myLastSuccessfulSelect = getOriginalNode(nodeToSelect);
+                }
                 result.setDone();
               }
             });
@@ -130,64 +135,8 @@ public class FilteringTreeBuilder extends AbstractTreeBuilder {
     return result;
   }
 
-  private void select(boolean adjustSelection, final Ref<Object> toSelect) {
-    if (adjustSelection) {
-      System.out.println("FilteringTreeBuilder.select toSelect=" + toSelect.get());
-
-      boolean wasSelected = false;
-      if (toSelect.get() != null && isSelectable(toSelect.get()) && isSimpleTree()) {
-        wasSelected = ((SimpleTree)myTree).select(this, new SimpleNodeVisitor() {
-          public boolean accept(SimpleNode simpleNode) {
-            if (simpleNode instanceof FilteringTreeStructure.Node) {
-              FilteringTreeStructure.Node node = (FilteringTreeStructure.Node)simpleNode;
-              return node.getDelegate().equals(toSelect.get());
-            }
-            else {
-              return false;
-            }
-          }
-        }, true);
-      }
-
-      if (!wasSelected && isSimpleTree()) {
-        ((SimpleTree)myTree).select(this, new SimpleNodeVisitor() {
-          public boolean accept(SimpleNode simpleNode) {
-            if (simpleNode instanceof FilteringTreeStructure.Node) {
-
-              final boolean isRoot = getTreeStructure().getRootElement() == simpleNode;
-              if (isRoot && !myTree.isRootVisible()) return false;
-
-              FilteringTreeStructure.Node node = (FilteringTreeStructure.Node)simpleNode;
-              if (isSelectable(node.getDelegate())) {
-                return true;
-              }
-            }
-            else {
-              return false;
-            }
-            return false;
-          }
-        }, true);
-      }
-
-      if (!wasSelected && myLastSuccessfulSelect != null && isSimpleTree()) {
-        wasSelected = ((SimpleTree)myTree).select(this, new SimpleNodeVisitor() {
-          public boolean accept(SimpleNode simpleNode) {
-            if (simpleNode instanceof FilteringTreeStructure.Node) {
-              Object object = ((FilteringTreeStructure.Node)simpleNode).getDelegate();
-              return myLastSuccessfulSelect.equals(object);
-            }
-            return false;
-          }
-        }, true);
-        if (wasSelected) {
-          myLastSuccessfulSelect = getSelected();
-        }
-      }
-      else if (wasSelected) {
-        myLastSuccessfulSelect = getSelected();
-      }
-    }
+  private FilteringTreeStructure getFilteredStructure() {
+    return ((FilteringTreeStructure)getTreeStructure());
   }
 
   //todo kirillk
@@ -207,7 +156,7 @@ public class FilteringTreeBuilder extends AbstractTreeBuilder {
   }
 
   public FilteringTreeStructure.Node getVisibleNodeFor(Object nodeObject) {
-    return ((FilteringTreeStructure)getTreeStructure()).getVisibleNodeFor(nodeObject);
+    return getFilteredStructure().getVisibleNodeFor(nodeObject);
   }
 
   public Object getOriginalNode(Object node) {