summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/dialogs/browserCache/KeepingExpandedExpander.java
blob: 7e967009e8c38eab3a161c70a2195c4527ed947d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.idea.svn.dialogs.browserCache;

import com.intellij.util.NotNullFunction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.svn.dialogs.RepositoryBrowserComponent;
import org.jetbrains.idea.svn.dialogs.RepositoryTreeNode;

import javax.swing.tree.TreeNode;
import java.util.*;

public class KeepingExpandedExpander implements Expander {
  private final Expander mySelectionExpander;
  private final RepositoryBrowserComponent myBrowser;

  private Map<String, ChildrenData> myThirdLevel;
  private Collection<TreeNode> myExpanded;

  public KeepingExpandedExpander(final RepositoryBrowserComponent browser, final Expander selectionInstaller) {
    myBrowser = browser;
    mySelectionExpander = selectionInstaller;
  }

  public KeepingExpandedExpander(final RepositoryBrowserComponent browser) {
    this(browser, new KeepingSelectionExpander(browser));
  }

  public void onBeforeRefresh(final RepositoryTreeNode node) {
    mySelectionExpander.onBeforeRefresh(node);
    
    myThirdLevel = new HashMap<String, ChildrenData>();
    myExpanded = new ArrayList<TreeNode>();

    final Enumeration<TreeNode> children = node.children();
    while (children.hasMoreElements()) {
      final TreeNode treeNode = children.nextElement();
      if (treeNode instanceof RepositoryTreeNode) {
        final RepositoryTreeNode repositoryNode = (RepositoryTreeNode) treeNode;
        final List<TreeNode> thirdLevelChildren = repositoryNode.getAllAlreadyLoadedChildren();

        final String nodeName = repositoryNode.getSVNDirEntry().getName();

        if (! thirdLevelChildren.isEmpty()) {
          final boolean selfExpanded = myBrowser.isExpanded(repositoryNode);
          myThirdLevel.put(nodeName, new ChildrenData(selfExpanded, thirdLevelChildren, repositoryNode.getChildrenLoadState()));
          if (selfExpanded) {
            myExpanded.addAll(myBrowser.getExpandedSubTree(repositoryNode));
          }
        }
      }
    }
  }

  public void onAfterRefresh(final RepositoryTreeNode node) {
    final Enumeration<TreeNode> children = node.children();
    while (children.hasMoreElements()) {
      final RepositoryTreeNode treeNode = (RepositoryTreeNode) children.nextElement();
      final String name = treeNode.getSVNDirEntry().getName();

      final ChildrenData thirdLevelLoaded = myThirdLevel.get(name);
      if (thirdLevelLoaded != null) {
        treeNode.setAlienChildren(thirdLevelLoaded.getChildren(), thirdLevelLoaded.getChildrenState());
        if (thirdLevelLoaded.isExpanded()) {
          myBrowser.expandNode(treeNode);
        }
      }
    }

    for (TreeNode expandedNode : myExpanded) {
      myBrowser.expandNode(expandedNode);
    }

    mySelectionExpander.onAfterRefresh(node);
  }

  public static class Factory implements NotNullFunction<RepositoryBrowserComponent, Expander> {
    @NotNull
    public Expander fun(final RepositoryBrowserComponent repositoryBrowserComponent) {
      return new KeepingExpandedExpander(repositoryBrowserComponent);
    }
  }

  private static class ChildrenData {
    private final boolean myExpanded;
    private final List<TreeNode> myChildren;
    private final NodeLoadState myChildrenState;

    private ChildrenData(final boolean expanded, final List<TreeNode> children, final NodeLoadState childrenState) {
      myExpanded = expanded;
      myChildren = children;
      myChildrenState = childrenState;
    }

    public List<TreeNode> getChildren() {
      return myChildren;
    }

    public NodeLoadState getChildrenState() {
      return myChildrenState;
    }

    public boolean isExpanded() {
      return myExpanded;
    }
  }
}