summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/branchConfig/NewRootBunch.java
blob: 6f7f4185a815c8397fbdaed0d3c692dc464eafa5 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * 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.branchConfig;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressManagerQueue;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vcs.CalledInBackground;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.SvnVcs;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;

import java.util.*;

// synch is here
public class NewRootBunch {
  private final static Logger LOG = Logger.getInstance("#org.jetbrains.idea.svn.branchConfig.NewRootBunch");
  private final Object myLock = new Object();
  private final Project myProject;
  private final ProgressManagerQueue myBranchesLoader;
  private final Map<VirtualFile, InfoStorage<SvnBranchConfigurationNew>> myMap;

  public NewRootBunch(final Project project, ProgressManagerQueue branchesLoader) {
    myProject = project;
    myBranchesLoader = branchesLoader;
    myMap = new HashMap<VirtualFile, InfoStorage<SvnBranchConfigurationNew>>();
  }

  public void updateForRoot(@NotNull final VirtualFile root,
                            @NotNull final InfoStorage<SvnBranchConfigurationNew> config,
                            boolean reload) {
    synchronized (myLock) {
      final SvnBranchConfigurationNew previous;
      boolean override;
      final InfoStorage<SvnBranchConfigurationNew> existing = myMap.get(root);

      if (existing == null) {
        previous = null;
        override = true;
        myMap.put(root, config);
      }
      else {
        previous = existing.getValue();
        override = existing.accept(config);
      }

      if (reload && override) {
        myBranchesLoader.run(new Runnable() {
          @Override
          public void run() {
            reloadBranches(root, previous, config.getValue());
          }
        });
      }
    }
  }

  public void updateBranches(@NotNull final VirtualFile root, @NotNull final String branchesParent,
                             @NotNull final InfoStorage<List<SvnBranchItem>> items) {
    synchronized (myLock) {
      final InfoStorage<SvnBranchConfigurationNew> existing = myMap.get(root);
      if (existing == null) {
        LOG.info("cannot update branches, branches parent not found: " + branchesParent);
      } else {
        existing.getValue().updateBranch(branchesParent, items);
      }
    }
  }

  @NotNull
  public SvnBranchConfigurationNew getConfig(@NotNull final VirtualFile root) {
    synchronized (myLock) {
      final InfoStorage<SvnBranchConfigurationNew> value = myMap.get(root);
      final SvnBranchConfigurationNew result;
      if (value == null) {
        result = new SvnBranchConfigurationNew();
        myMap.put(root, new InfoStorage<SvnBranchConfigurationNew>(result, InfoReliability.empty));
        myBranchesLoader.run(new DefaultBranchConfigInitializer(myProject, this, root));
      }
      else {
        result = value.getValue();
      }
      return result;
    }
  }

  public void reloadBranchesAsync(@NotNull final VirtualFile root,
                                  @NotNull final String branchLocation,
                                  @NotNull final InfoReliability reliability) {
    ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
      @Override
      public void run() {
        reloadBranches(root, branchLocation, reliability, true);
      }
    });
  }

  public void reloadBranches(@NotNull VirtualFile root, @Nullable SvnBranchConfigurationNew prev, @NotNull SvnBranchConfigurationNew next) {
    final Set<String> oldUrls = (prev == null) ? Collections.<String>emptySet() : new HashSet<String>(prev.getBranchUrls());
    final SvnVcs vcs = SvnVcs.getInstance(myProject);
    if (!vcs.isVcsBackgroundOperationsAllowed(root)) return;

    for (String newBranchUrl : next.getBranchUrls()) {
      // check if cancel had been put
      if (!vcs.isVcsBackgroundOperationsAllowed(root)) return;
      if (!oldUrls.contains(newBranchUrl)) {
        reloadBranches(root, newBranchUrl, InfoReliability.defaultValues, true);
      }
    }
  }

  public void reloadBranches(@NotNull VirtualFile root,
                             @NotNull String branchLocation,
                             @NotNull InfoReliability reliability,
                             boolean passive) {
    new BranchesLoader(myProject, this, branchLocation, reliability, root, passive).run();
  }

  @Nullable
  @CalledInBackground
  public SVNURL getWorkingBranchWithReload(final SVNURL svnurl, final VirtualFile root) {
    final Ref<SVNURL> result = new Ref<SVNURL>();
    try {
      final SvnBranchConfigurationNew configuration = myMap.get(root).getValue();
      final String group = configuration.getGroupToLoadToReachUrl(svnurl);

      if (group != null) {
        reloadBranches(root, group, InfoReliability.setByUser, true);
      }
      result.set(myMap.get(root).getValue().getWorkingBranch(svnurl));
    }
    catch (SVNException e) {
      //
    }
    return result.get();
  }

  public Map<VirtualFile, SvnBranchConfigurationNew> getMapCopy() {
    synchronized (myLock) {
      final Map<VirtualFile, SvnBranchConfigurationNew> result = new HashMap<VirtualFile, SvnBranchConfigurationNew>();
      for (VirtualFile vf : myMap.keySet()) {
        result.put(vf, myMap.get(vf).getValue());
      }
      return result;
    }
  }
}